web/DuneMenus/src/main/java/com/dunerealms/menus/NativeMenuRoutes.java

104 lines
5 KiB
Java

package com.dunerealms.menus;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Set;
import org.bukkit.NamespacedKey;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.PluginCommand;
import org.bukkit.entity.Player;
import org.bukkit.entity.Villager;
import org.bukkit.persistence.PersistentDataType;
/** Routes menu-only calls made by the existing server commands and NPCs. */
final class NativeMenuRoutes implements AutoCloseable {
private static final NamespacedKey GUILD_NAVIGATOR = new NamespacedKey("dunerealms", "guild");
private static final List<String> COMMANDS = List.of("dunecore:dune", "duneess:duneess", "duneess:exchange", "duneess:dune");
private static final Map<String, String> DUNE_SERVICES = Map.ofEntries(
Map.entry("menu", "dm"), Map.entry("faction", "house"), Map.entry("house", "house"),
Map.entry("start", "start"), Map.entry("mentat", "mentat"), Map.entry("exchange", "exchange"),
Map.entry("spice", "spice"), Map.entry("sell", "sellall"), Map.entry("sellall", "sellall"),
Map.entry("clan", "clan"), Map.entry("company", "company"), Map.entry("office", "politics"),
Map.entry("scoreboard", "scoreboard"), Map.entry("guildtravel", "guildtravel"),
Map.entry("warmuster", "warmuster"), Map.entry("rosterkey", "rosterkey"));
private static final Set<String> CLAN_MEMBERSHIP = Set.of("invite", "accept", "join", "leave");
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) -> {
if (name.equals("duneess:dune")) {
String target = duneService(args);
if (target != null) {
PluginCommand destination = plugin.getCommand(target);
if (destination != null)
return destination.execute(sender, "dune " + args[0], Arrays.copyOfRange(args, 1, args.length));
}
}
String menu = menuFor(name, args);
if (menu != null && sender instanceof Player player && player.hasPermission("dunemenus.use")) {
if (name.equals("duneess:duneess") && menu.equals("travel") && player.isSneaking()
&& player.getNearbyEntities(5, 3, 5).stream().anyMatch(entity -> entity instanceof Villager
&& entity.getPersistentDataContainer().has(GUILD_NAVIGATOR, PersistentDataType.BYTE))) {
plugin.openNativeQuestContact(player, "guild_navigator");
} else {
plugin.openMenu(player, menu);
}
return true;
}
return original.onCommand(sender, invoked, label, args);
};
bindings.add(new Binding(command, original, route));
command.setExecutor(route);
}
}
static String duneService(String[] args) {
if (args.length == 0) return null;
String action = args[0].toLowerCase(Locale.ROOT);
if (action.equals("clan") && args.length > 1 && CLAN_MEMBERSHIP.contains(args[1].toLowerCase(Locale.ROOT)))
return null;
return DUNE_SERVICES.get(action);
}
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();
}
}