From 766a3aef51c61b67dcbe5af7988e91804b2c8678 Mon Sep 17 00:00:00 2001 From: yoyosource Date: Fri, 23 Dec 2022 21:08:16 +0100 Subject: [PATCH] Add hotkeys --- BauSystem_Main/src/BauSystem.properties | 1 + BauSystem_Main/src/BauSystem_de.properties | 1 + .../features/script/CustomScript.java | 68 +++++++++++++++++++ .../features/script/CustomScriptManager.java | 31 ++++++++- .../features/script/ScriptEventListener.java | 18 ++++- 5 files changed, 117 insertions(+), 2 deletions(-) diff --git a/BauSystem_Main/src/BauSystem.properties b/BauSystem_Main/src/BauSystem.properties index 72b0a128..e84b646d 100644 --- a/BauSystem_Main/src/BauSystem.properties +++ b/BauSystem_Main/src/BauSystem.properties @@ -349,6 +349,7 @@ SCRIPT_COMMAND_SLEEP_ERROR = §cThe given number needs to be greater than zero. SCRIPT_GUI_ITEM_NAME = §eScript Help ## CustomScript +SCRIPT_HOTKEY_ITEM_NAME = §7Hotkey§8: §e{0} §8-§7 {1} SCRIPT_EVENT_ITEM_NAME = §7Event§8: §e{0} §8-§7 {1} SCRIPT_COMMAND_ITEM_NAME = §7Command§8: §e{0} diff --git a/BauSystem_Main/src/BauSystem_de.properties b/BauSystem_Main/src/BauSystem_de.properties index 555ba5f3..4cb5bb02 100644 --- a/BauSystem_Main/src/BauSystem_de.properties +++ b/BauSystem_Main/src/BauSystem_de.properties @@ -343,6 +343,7 @@ SCRIPT_COMMAND_SLEEP_ERROR = §cDie Zeit muss eine Zahl großer 0 sein. SCRIPT_GUI_ITEM_NAME = §eScript Hilfe ## CustomScript +SCRIPT_HOTKEY_ITEM_NAME = §7Hotkey§8: §e{0} §8-§7 {1} SCRIPT_EVENT_ITEM_NAME = §7Event§8: §e{0} §8-§7 {1} SCRIPT_COMMAND_ITEM_NAME = §7Command§8: §e{0} diff --git a/BauSystem_Main/src/de/steamwar/bausystem/features/script/CustomScript.java b/BauSystem_Main/src/de/steamwar/bausystem/features/script/CustomScript.java index 0645b61b..35298a09 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/features/script/CustomScript.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/features/script/CustomScript.java @@ -47,9 +47,77 @@ public class CustomScript { SWItem toItem(Player p); } + public interface Hotkey extends Script { + String hotkey(); + boolean execute(Player p, boolean pressed); + } + + @RequiredArgsConstructor + public static class InventoryHotkey implements Hotkey { + public final BookMeta bookMeta; + public final String[] args; + + @Override + public String hotkey() { + return args[0]; + } + + @Override + public boolean execute(Player p, boolean pressed) { + Map variables = new HashMap<>(); + variables.put("pressed", new Value.BooleanValue(pressed)); + variables.put("released", new Value.BooleanValue(!pressed)); + new ScriptExecutor(bookMeta, p, variables); + return false; + } + } + + @RequiredArgsConstructor + public static class MenuHotkey implements Hotkey, MenuScript { + public final List pages; + public final String[] args; + + @Override + public String hotkey() { + return args[0]; + } + + @Override + public boolean execute(Player p, boolean pressed) { + Map variables = new HashMap<>(); + variables.put("pressed", new Value.BooleanValue(pressed)); + variables.put("released", new Value.BooleanValue(!pressed)); + new ScriptExecutor(pages, p, variables); + return false; + } + + @Override + public void toYAPION(YAPIONMap yapionMap) { + YAPIONArray yapionArray = new YAPIONArray(); + pages.forEach(yapionArray::add); + yapionMap.put(String.join(" ", args), yapionArray); + } + + @Override + public SWItem toItem(Player p) { + StringBuilder st = new StringBuilder(); + for (int i = 1; i < args.length; i++) { + if (i != 1) st.append(" "); + st.append(args[i]); + } + + SWItem swItem = new SWItem(Material.WRITABLE_BOOK, BauSystem.MESSAGE.parse("SCRIPT_HOTKEY_ITEM_NAME", p, args[0], st.toString())); + BookMeta bookMeta = (BookMeta) swItem.getItemMeta(); + bookMeta.setPages(pages.toArray(new String[0])); + swItem.setItemMeta(bookMeta); + return swItem; + } + } + public interface CustomEvent extends Script { String eventName(); boolean execute(Event e, Player p, Map variables); + } @RequiredArgsConstructor diff --git a/BauSystem_Main/src/de/steamwar/bausystem/features/script/CustomScriptManager.java b/BauSystem_Main/src/de/steamwar/bausystem/features/script/CustomScriptManager.java index a8bebb34..1499214e 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/features/script/CustomScriptManager.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/features/script/CustomScriptManager.java @@ -77,6 +77,8 @@ public class CustomScriptManager implements Listener { playerMap.computeIfAbsent(p, player -> new ArrayList<>()).add(new CustomScript.InventoryCommand(bookMeta, s.substring(6).split(" "))); } else if (s.startsWith("#!EVENT ")) { playerMap.computeIfAbsent(p, player -> new ArrayList<>()).add(new CustomScript.InventoryEvent(bookMeta, s.substring(8).split(" "))); + } else if (s.startsWith("#!HOTKEY ")) { + playerMap.computeIfAbsent(p, player -> new ArrayList<>()).add(new CustomScript.InventoryHotkey(bookMeta, s.substring(9).split(" "))); } } } @@ -118,6 +120,12 @@ public class CustomScriptManager implements Listener { List pages = ((YAPIONArray) value).stream().map(YAPIONValue.class::cast).map(YAPIONValue::get).map(String.class::cast).collect(Collectors.toList()); playerMap.computeIfAbsent(p, player -> new ArrayList<>()).add(new CustomScript.MenuEvent(pages, event)); }); + + yapionObject.getYAPIONMapOrSetDefault("hotkeys", new YAPIONMap()).forEach((key, value) -> { + String[] hotkey = ((YAPIONValue) key).get().split(" "); + List pages = ((YAPIONArray) value).stream().map(YAPIONValue.class::cast).map(YAPIONValue::get).map(String.class::cast).collect(Collectors.toList()); + playerMap.computeIfAbsent(p, player -> new ArrayList<>()).add(new CustomScript.MenuHotkey(pages, hotkey)); + }); } @EventHandler @@ -129,16 +137,24 @@ public class CustomScriptManager implements Listener { private YAPIONObject output(Player p) { if (!playerMap.containsKey(p)) return new YAPIONObject(); YAPIONObject yapionObject = new YAPIONObject(); + YAPIONMap commandsMap = new YAPIONMap(); yapionObject.add("commands", commandsMap); playerMap.get(p).stream().filter(CustomScript.MenuCommand.class::isInstance).map(CustomScript.MenuCommand.class::cast).forEach(menuCommand -> { menuCommand.toYAPION(commandsMap); }); + YAPIONMap eventsMap = new YAPIONMap(); yapionObject.add("events", eventsMap); playerMap.get(p).stream().filter(CustomScript.MenuEvent.class::isInstance).map(CustomScript.MenuEvent.class::cast).forEach(menuCommand -> { menuCommand.toYAPION(eventsMap); }); + + YAPIONMap hotkeysMap = new YAPIONMap(); + yapionObject.add("hotkeys", hotkeysMap); + playerMap.get(p).stream().filter(CustomScript.MenuHotkey.class::isInstance).map(CustomScript.MenuHotkey.class::cast).forEach(menuCommand -> { + menuCommand.toYAPION(hotkeysMap); + }); return yapionObject; } @@ -167,7 +183,9 @@ public class CustomScriptManager implements Listener { playerMap.getOrDefault(p, new ArrayList<>()).stream().filter(CustomScript.MenuScript.class::isInstance).map(CustomScript.MenuScript.class::cast).forEach(menuItem -> { SWItem swItem = menuItem.toItem(p); ItemStack itemStack = swItem.getItemStack(); - if (menuItem instanceof CustomScript.MenuEvent) { + if (menuItem instanceof CustomScript.MenuHotkey) { + itemStack.setType(Material.CHAIN_COMMAND_BLOCK); + } else if (menuItem instanceof CustomScript.MenuEvent) { itemStack.setType(Material.REPEATING_COMMAND_BLOCK); } else { itemStack.setType(Material.COMMAND_BLOCK); @@ -228,6 +246,8 @@ public class CustomScriptManager implements Listener { return; } scriptBookLimit(p, menuEvent); + } else if (s.startsWith("#!HOTKEY ")) { + scriptBookLimit(p, new CustomScript.MenuHotkey(bookMeta.getPages(), s.substring(9).split(" "))); } })); menuCommandSWListInv.open(); @@ -270,6 +290,15 @@ public class CustomScriptManager implements Listener { } } + public void callHotkey(String pressedHotkey, Player p, boolean pressed) { + List hotkeys = playerMap.getOrDefault(p, new ArrayList<>()).stream().filter(CustomScript.Hotkey.class::isInstance).map(CustomScript.Hotkey.class::cast).collect(Collectors.toList()); + for (CustomScript.Hotkey hotkey : hotkeys) { + if (hotkey.hotkey().equals(pressedHotkey)) { + hotkey.execute(p, pressed); + } + } + } + @EventHandler public void onPlayerCommandPreprocess(PlayerCommandPreprocessEvent e) { if (e.getMessage().startsWith("/script:")) { diff --git a/BauSystem_Main/src/de/steamwar/bausystem/features/script/ScriptEventListener.java b/BauSystem_Main/src/de/steamwar/bausystem/features/script/ScriptEventListener.java index 57e39d70..5cbd5f93 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/features/script/ScriptEventListener.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/features/script/ScriptEventListener.java @@ -37,6 +37,7 @@ import org.bukkit.event.entity.EntityDeathEvent; import org.bukkit.event.entity.EntityExplodeEvent; import org.bukkit.event.entity.EntitySpawnEvent; import org.bukkit.event.player.*; +import org.bukkit.plugin.messaging.PluginMessageListener; import java.util.HashMap; import java.util.HashSet; @@ -44,7 +45,11 @@ import java.util.Map; import java.util.Set; @Linked -public class ScriptEventListener implements Listener { +public class ScriptEventListener implements Listener, PluginMessageListener { + + { + Bukkit.getServer().getMessenger().registerIncomingPluginChannel(BauSystem.getInstance(), "sw:hotkeys", this); + } @LinkedInstance public CustomScriptManager customScriptManager; @@ -141,4 +146,15 @@ public class ScriptEventListener implements Listener { customScriptManager.callEvent(EventType.EntityDeath, player, event); } } + + @Override + public void onPluginMessageReceived(String channel, Player player, byte[] message) { + if (!channel.equals("sw:hotkeys")) return; + if (message.length != 5) return; + int key = (message[0] & 0xFF) << 24 | (message[1] & 0xFF) << 16 | (message[2] & 0xFF) << 8 | (message[3] & 0xFF); + int action = message[4] & 0xFF; + if (action == 2) return; + if (!(key >= 'A' && key <= 'Z' || key >= '0' && key <= '9')) return; + customScriptManager.callHotkey(((char) key) + "", player, action == 1); + } }