forked from unimatrix/web
Connect native travel and War Marshal entry points to DuneMenus
This commit is contained in:
parent
b3100d0187
commit
c8fcf9d2e5
3 changed files with 114 additions and 0 deletions
|
|
@ -19,6 +19,7 @@ public final class DuneMenusPlugin extends JavaPlugin implements TabExecutor {
|
|||
"spice", "war", "mentat", "lore", "politics", "house", "equipment", "bank", "atm", "withdraw");
|
||||
private static final String PACK_CONTROL = "dunerealms:pack_control";
|
||||
private Backend backend;
|
||||
private NativeMenuRoutes nativeRoutes;
|
||||
private final Map<Player, String> loadedPacks = new HashMap<>();
|
||||
private final Map<UUID, Menu> openMenus = new HashMap<>();
|
||||
|
||||
|
|
@ -53,6 +54,11 @@ public final class DuneMenusPlugin extends JavaPlugin implements TabExecutor {
|
|||
getServer().getPluginManager().registerEvents(new ChatPrompt(this), this);
|
||||
getCommand("dm").setExecutor(this);
|
||||
getCommand("dm").setTabCompleter(this);
|
||||
getServer().getScheduler().runTask(this, () -> nativeRoutes = new NativeMenuRoutes(this));
|
||||
}
|
||||
|
||||
@Override public void onDisable() {
|
||||
if (nativeRoutes != null) nativeRoutes.close();
|
||||
}
|
||||
|
||||
Backend backend() { return backend; }
|
||||
|
|
|
|||
|
|
@ -2,16 +2,24 @@ package com.dunerealms.menus;
|
|||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.util.UUID;
|
||||
import org.bukkit.NamespacedKey;
|
||||
import org.bukkit.event.EventHandler;
|
||||
import org.bukkit.event.Listener;
|
||||
import org.bukkit.event.inventory.InventoryClickEvent;
|
||||
import org.bukkit.event.inventory.InventoryCloseEvent;
|
||||
import org.bukkit.event.inventory.InventoryDragEvent;
|
||||
import org.bukkit.event.player.PlayerChangedWorldEvent;
|
||||
import org.bukkit.event.player.PlayerInteractEntityEvent;
|
||||
import org.bukkit.event.player.PlayerQuitEvent;
|
||||
import org.bukkit.inventory.EquipmentSlot;
|
||||
import org.bukkit.persistence.PersistentDataType;
|
||||
|
||||
final class MenuListener implements Listener {
|
||||
private static final NamespacedKey QUEST_CHARACTER = new NamespacedKey("dunerealms", "quest_character");
|
||||
private static final Set<String> WAR_MARSHAL_WORLDS =
|
||||
Set.of("arrakis", "caladan", "giedi_prime", "guild_lobby", "kaitain", "sigma_draconis_iv");
|
||||
private final Map<UUID, Long> last = new HashMap<>();
|
||||
private final DuneMenusPlugin plugin;
|
||||
|
||||
|
|
@ -38,6 +46,18 @@ final class MenuListener implements Listener {
|
|||
plugin.create("hub", event.getPlayer(), null).open();
|
||||
}
|
||||
|
||||
@EventHandler(priority = org.bukkit.event.EventPriority.HIGH, ignoreCancelled = true)
|
||||
public void openWarMarshal(PlayerInteractEntityEvent event) {
|
||||
var player = event.getPlayer();
|
||||
if (!(event.getRightClicked() instanceof org.bukkit.entity.LivingEntity)
|
||||
|| !WAR_MARSHAL_WORLDS.contains(player.getWorld().getName())
|
||||
|| !"war_marshal".equals(event.getRightClicked().getPersistentDataContainer().get(
|
||||
QUEST_CHARACTER, PersistentDataType.STRING))
|
||||
|| !player.hasPermission("dunemenus.use")) return;
|
||||
event.setCancelled(true);
|
||||
if (event.getHand() == EquipmentSlot.HAND) plugin.create("war", player, null).open();
|
||||
}
|
||||
|
||||
@EventHandler
|
||||
public void onClick(InventoryClickEvent e) {
|
||||
var top = e.getView().getTopInventory();
|
||||
|
|
|
|||
|
|
@ -0,0 +1,67 @@
|
|||
package com.dunerealms.menus;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
import org.bukkit.command.CommandExecutor;
|
||||
import org.bukkit.command.PluginCommand;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
/** Routes menu-only calls made by the existing server commands and NPCs. */
|
||||
final class NativeMenuRoutes implements AutoCloseable {
|
||||
private static final List<String> COMMANDS = List.of("dunecore:dune", "duneess:duneess", "duneess:exchange");
|
||||
private final List<Binding> bindings = new ArrayList<>();
|
||||
|
||||
private record Binding(PluginCommand command, CommandExecutor original, CommandExecutor route) {}
|
||||
|
||||
NativeMenuRoutes(DuneMenusPlugin plugin) {
|
||||
for (String name : COMMANDS) {
|
||||
PluginCommand command = plugin.getServer().getPluginCommand(name);
|
||||
if (command == null || !command.getPlugin().isEnabled()) continue;
|
||||
CommandExecutor original = command.getExecutor();
|
||||
CommandExecutor route = (sender, invoked, label, args) -> {
|
||||
String menu = menuFor(name, args);
|
||||
if (menu != null && sender instanceof Player player && player.hasPermission("dunemenus.use")) {
|
||||
plugin.create(menu, player, null).open();
|
||||
return true;
|
||||
}
|
||||
return original.onCommand(sender, invoked, label, args);
|
||||
};
|
||||
bindings.add(new Binding(command, original, route));
|
||||
command.setExecutor(route);
|
||||
}
|
||||
}
|
||||
|
||||
static String menuFor(String command, String[] args) {
|
||||
if (args.length > 1) return null;
|
||||
if (command.equals("duneess:exchange")) return args.length == 0 ? "exchange" : null;
|
||||
String action = args.length == 0 ? "" : args[0].toLowerCase(Locale.ROOT);
|
||||
if (command.equals("dunecore:dune")) {
|
||||
return switch (action) {
|
||||
case "", "menu", "open", "gui" -> "hub";
|
||||
case "choose", "faction", "house" -> "house";
|
||||
case "quests", "quest" -> "missions";
|
||||
case "exchange" -> "exchange";
|
||||
case "politics" -> "politics";
|
||||
case "travel" -> "travel";
|
||||
case "bank" -> "bank";
|
||||
default -> null;
|
||||
};
|
||||
}
|
||||
if (command.equals("duneess:duneess")) {
|
||||
return switch (action) {
|
||||
case "travel" -> "travel";
|
||||
case "exchange" -> "exchange";
|
||||
default -> null;
|
||||
};
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override public void close() {
|
||||
for (var binding : bindings) {
|
||||
if (binding.command().getExecutor() == binding.route()) binding.command().setExecutor(binding.original());
|
||||
}
|
||||
bindings.clear();
|
||||
}
|
||||
}
|
||||
Loading…
Reference in a new issue

Staff