From c2eb369163588392c88c5559b8d831ebc8a2c197 Mon Sep 17 00:00:00 2001 From: yoyosource Date: Fri, 23 Dec 2022 22:10:58 +0100 Subject: [PATCH] Add Hotkeys --- BauSystem_Main/src/BauSystem.properties | 7 + BauSystem_Main/src/BauSystem_de.properties | 7 + .../features/script/CustomScriptManager.java | 4 +- .../bausystem/features/script/Hotkeys.java | 135 ++++++++++++++++++ .../features/script/ScriptCommand.java | 6 +- .../features/script/ScriptEventListener.java | 13 +- 6 files changed, 165 insertions(+), 7 deletions(-) create mode 100644 BauSystem_Main/src/de/steamwar/bausystem/features/script/Hotkeys.java diff --git a/BauSystem_Main/src/BauSystem.properties b/BauSystem_Main/src/BauSystem.properties index e84b646d..139a4a1f 100644 --- a/BauSystem_Main/src/BauSystem.properties +++ b/BauSystem_Main/src/BauSystem.properties @@ -372,6 +372,13 @@ SCRIPT_COMMAND_HELP_MENU = §8/§escript menu §8- §7Opens the ScriptGUI for cu SCRIPT_GUI_NAME = Script Elements SCRIPT_GUI_COMMAND_CHAT = §eScript Command§8: §e{0} +SCRIPT_GUI_CUSTOM_HOTKEYS = §eCustom Hotkeys +SCRIPT_GUI_CUSTOM_HOTKEYS_COMMANDS_LORE_1 = §7Write§8: §7#!HOTKEY 'Char' +SCRIPT_GUI_CUSTOM_HOTKEYS_COMMANDS_LORE_2 = §7at the beginning of a Script Book to use a custom hotkey. The 'Char' can be any char between '§eA§7' and '§eZ§7' as well as '§e0§7' and '§e9§7'. While executing two variables are available: §epressed§7, §ereleased§7. +SCRIPT_GUI_CUSTOM_HOTKEYS_COMMANDS_LORE_3 = §7You can add modifiers like "SHIFT", "CTRL", "ALT" or "META" to the hotkey. §7Example: §e#!HOTKEY SHIFT+A +SCRIPT_GUI_CUSTOM_HOTKEYS_COMMANDS_LORE_4 = §7 +SCRIPT_GUI_CUSTOM_HOTKEYS_COMMANDS_LORE_5 = §cThis can only be used in conjunction with the Fabric-Mod: §eAdvancedScripts §7found on §ehttps://steamwar.de/downloads + SCRIPT_GUI_CUSTOM_COMMANDS = §eCustom Commands SCRIPT_GUI_CUSTOM_COMMANDS_LORE_1 = §7Write§8: §7#!CMD 'COMMAND' SCRIPT_GUI_CUSTOM_COMMANDS_LORE_2 = §7at the beginning of a Script Book to use a custom command. The command always starts with / and can be structured as you wish. Everything in pointy Brackets '<>' will be counted as a Parameter and therefore as a variable. Parameters in round brackets '()' are optional. Simple texts as parameters get a variable with the same name with the values true/false, depending on whether the value was given or not diff --git a/BauSystem_Main/src/BauSystem_de.properties b/BauSystem_Main/src/BauSystem_de.properties index 4cb5bb02..2858a884 100644 --- a/BauSystem_Main/src/BauSystem_de.properties +++ b/BauSystem_Main/src/BauSystem_de.properties @@ -366,6 +366,13 @@ SCRIPT_COMMAND_HELP_MENU = §8/§escript menu §8- §7Öffnet die ScriptMenuGUI SCRIPT_GUI_NAME = Script Elements SCRIPT_GUI_COMMAND_CHAT = §eScript Command§8: §e{0} +SCRIPT_GUI_CUSTOM_HOTKEYS = §eCustom Hotkeys +SCRIPT_GUI_CUSTOM_HOTKEYS_COMMANDS_LORE_1 = §7Schreibe§8: §7#!HOTKEY 'Char' +SCRIPT_GUI_CUSTOM_HOTKEYS_COMMANDS_LORE_2 = §7am Anfang eines Skript Buches, um einen benutzerdefinierten Hotkey zu verwenden. Das 'Char' kann ein beliebiges Zeichen zwischen '§eA§7' und '§eZ§7' sowie '§e0§7' und '§e9§7' sein. Während der Ausführung sind zwei Variablen verfügbar: §epressed§7, §ereleased§7 +SCRIPT_GUI_CUSTOM_HOTKEYS_COMMANDS_LORE_3 = §7Sie können dem Hotkey Modifikatoren wie "SHIFT", "CTRL", "ALT" oder "META" hinzufügen. §7Beispiel: §e#!HOTKEY SHIFT+A +SCRIPT_GUI_CUSTOM_HOTKEYS_COMMANDS_LORE_4 = §7 +SCRIPT_GUI_CUSTOM_HOTKEYS_COMMANDS_LORE_5 = §cDies kann nur in Verbindung mit den Fabric-Mod: §eAdvancedScripts §7runterladbar unter §ehttps://steamwar.de/downloads §cverwendet werden. + SCRIPT_GUI_CUSTOM_COMMANDS = §eCustom Commands SCRIPT_GUI_CUSTOM_COMMANDS_LORE_1 = §7Schreibe§8: §7#!CMD 'COMMAND' SCRIPT_GUI_CUSTOM_COMMANDS_LORE_2 = §7an den Anfang eines Script Buches um ein Custom Command zu nutzen. Der Befehl startet immer mit / und kann dann so aufgebaut sein wie du willst. Alles was in Spitzen Klammern steht '<>' wird als Parameter und somit als Variable gewertet. Parameter, welche in runden Klammern stehen sind Optional. Einfache Texte als Parameter bekommen eine gleichnamige Variable mit true/false als Wert je nachdem ob dieser angegeben wurde oder nicht 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 1499214e..a19a5219 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/features/script/CustomScriptManager.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/features/script/CustomScriptManager.java @@ -290,10 +290,10 @@ public class CustomScriptManager implements Listener { } } - public void callHotkey(String pressedHotkey, Player p, boolean pressed) { + public void callHotkey(int modifiers, char 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)) { + if (Hotkeys.isSame(modifiers, pressedHotkey, hotkey.hotkey())) { hotkey.execute(p, pressed); } } diff --git a/BauSystem_Main/src/de/steamwar/bausystem/features/script/Hotkeys.java b/BauSystem_Main/src/de/steamwar/bausystem/features/script/Hotkeys.java new file mode 100644 index 00000000..c4c5193c --- /dev/null +++ b/BauSystem_Main/src/de/steamwar/bausystem/features/script/Hotkeys.java @@ -0,0 +1,135 @@ +/* + * This file is a part of the SteamWar software. + * + * Copyright (C) 2022 SteamWar.de-Serverteam + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +package de.steamwar.bausystem.features.script; + +import java.util.HashSet; +import java.util.Set; + +public interface Hotkeys { + + public static boolean isSame(int mods, char key, String checkAgainst) { + Set modifiers = Modifier.get(mods); + Key k = Key.fromString(key + ""); + if (k == null) { + return false; + } + + try { + String[] split = checkAgainst.split("\\+"); + Set checkModifiers = new HashSet<>(); + Key checkKey = null; + for (String s : split) { + Modifier m = Modifier.fromString(s); + if (m != null) { + checkModifiers.add(m); + } else { + checkKey = Key.fromString(s); + } + } + return checkKey == k && checkModifiers.equals(modifiers); + } catch (Exception e) { + return false; + } + } + + enum Modifier { + SHIFT, + CTRL, + ALT, + META, + ; + + public static Set get(int mods) { + Set modifiers = new HashSet<>(); + for (Modifier modifier : values()) { + if ((mods & (1 << modifier.ordinal())) != 0) { + modifiers.add(modifier); + } + } + return modifiers; + } + + public static Modifier fromString(String string) { + try { + return valueOf(string.toUpperCase()); + } catch (IllegalArgumentException e) { + return null; + } + } + } + + enum Key { + // A-Z + A, + B, + C, + D, + E, + F, + G, + H, + I, + J, + K, + L, + M, + N, + O, + P, + Q, + R, + S, + T, + U, + V, + W, + X, + Y, + Z, + + // 0-9 + NUM_0, + NUM_1, + NUM_2, + NUM_3, + NUM_4, + NUM_5, + NUM_6, + NUM_7, + NUM_8, + NUM_9, + ; + + public static Key fromString(String key) { + key = key.toUpperCase(); + try { + return Key.valueOf(key); + } catch (IllegalArgumentException e) { + // ignore + } + try { + return Key.valueOf("NUM_" + key); + } catch (IllegalArgumentException e) { + // ignore + } + return null; + } + } +} diff --git a/BauSystem_Main/src/de/steamwar/bausystem/features/script/ScriptCommand.java b/BauSystem_Main/src/de/steamwar/bausystem/features/script/ScriptCommand.java index 13cc741c..d6f6e3d6 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/features/script/ScriptCommand.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/features/script/ScriptCommand.java @@ -36,11 +36,13 @@ public class ScriptCommand extends SWCommand { @Register(description = "SCRIPT_COMMAND_HELP") public void menuCommand(Player p) { List> swItems = new ArrayList<>(); - addEmptyItems(swItems, 3); + addEmptyItems(swItems, 2); + swItems.add(new SWListInv.SWListEntry<>(createItem(p, Material.BOOK, "SCRIPT_GUI_CUSTOM_HOTKEYS", loreBuilder(p, "SCRIPT_GUI_CUSTOM_HOTKEYS_COMMANDS_LORE_1", "SCRIPT_GUI_CUSTOM_HOTKEYS_COMMANDS_LORE_2", "SCRIPT_GUI_CUSTOM_HOTKEYS_COMMANDS_LORE_3", "SCRIPT_GUI_CUSTOM_HOTKEYS_COMMANDS_LORE_4", "SCRIPT_GUI_CUSTOM_HOTKEYS_COMMANDS_LORE_5")), null)); swItems.add(new SWListInv.SWListEntry<>(createItem(p, Material.BOOK, "SCRIPT_GUI_CUSTOM_COMMANDS", loreBuilder(p, "SCRIPT_GUI_CUSTOM_COMMANDS_LORE_1", "SCRIPT_GUI_CUSTOM_COMMANDS_LORE_2")), null)); swItems.add(new SWListInv.SWListEntry<>(createItem(p, Material.BOOK, "SCRIPT_GUI_CUSTOM_EVENTS", loreBuilder(p, "SCRIPT_GUI_CUSTOM_EVENTS_LORE_1", "SCRIPT_GUI_CUSTOM_EVENTS_LORE_2", "SCRIPT_GUI_CUSTOM_EVENTS_LORE_3", "SCRIPT_GUI_CUSTOM_EVENTS_LORE_4", "SCRIPT_GUI_CUSTOM_EVENTS_LORE_5", "SCRIPT_GUI_CUSTOM_EVENTS_LORE_6", "SCRIPT_GUI_CUSTOM_EVENTS_LORE_7", "SCRIPT_GUI_CUSTOM_EVENTS_LORE_8", "SCRIPT_GUI_CUSTOM_EVENTS_LORE_9", "SCRIPT_GUI_CUSTOM_EVENTS_LORE_10", "SCRIPT_GUI_CUSTOM_EVENTS_LORE_11", "SCRIPT_GUI_CUSTOM_EVENTS_LORE_12", "SCRIPT_GUI_CUSTOM_EVENTS_LORE_13", "SCRIPT_GUI_CUSTOM_EVENTS_LORE_14", "SCRIPT_GUI_CUSTOM_EVENTS_LORE_15", "SCRIPT_GUI_CUSTOM_EVENTS_LORE_16", "SCRIPT_GUI_CUSTOM_EVENTS_LORE_17", "SCRIPT_GUI_CUSTOM_EVENTS_LORE_STAR_1")), null)); + addEmptyItems(swItems, 1); swItems.add(new SWListInv.SWListEntry<>(createItem(p, Material.BOOK, "SCRIPT_GUI_OTHER", loreBuilder(p, "SCRIPT_GUI_OTHER_LORE_1", "SCRIPT_GUI_OTHER_LORE_2", "SCRIPT_GUI_OTHER_LORE_3", "SCRIPT_GUI_OTHER_LORE_4", "SCRIPT_GUI_OTHER_LORE_5", "SCRIPT_GUI_OTHER_LORE_6", "SCRIPT_GUI_OTHER_LORE_7", "SCRIPT_GUI_OTHER_LORE_8", "SCRIPT_GUI_OTHER_LORE_9", "SCRIPT_GUI_OTHER_LORE_10", "SCRIPT_GUI_OTHER_LORE_11", "SCRIPT_GUI_OTHER_LORE_12", "SCRIPT_GUI_OTHER_LORE_13", "SCRIPT_GUI_OTHER_LORE_14", "SCRIPT_GUI_OTHER_LORE_15", "SCRIPT_GUI_OTHER_LORE_16", "SCRIPT_GUI_OTHER_LORE_17", "SCRIPT_GUI_OTHER_LORE_18")), null)); - addEmptyItems(swItems, 3); + addEmptyItems(swItems, 2); addCustomScriptsItems(swItems, p); addEmptyItems(swItems, 16); swItems.add(new SWListInv.SWListEntry<>(createItem(p, Material.BOOK, "SCRIPT_GUI_CUSTOM_VARIABLES", new ArrayList<>()), null)); 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 5cbd5f93..ec9c596a 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/features/script/ScriptEventListener.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/features/script/ScriptEventListener.java @@ -150,11 +150,18 @@ public class ScriptEventListener implements Listener, PluginMessageListener { @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); + if (message.length < 5) return; int action = message[4] & 0xFF; if (action == 2) return; + int key = (message[0] & 0xFF) << 24 | (message[1] & 0xFF) << 16 | (message[2] & 0xFF) << 8 | (message[3] & 0xFF); if (!(key >= 'A' && key <= 'Z' || key >= '0' && key <= '9')) return; - customScriptManager.callHotkey(((char) key) + "", player, action == 1); + if (message.length >= 9) { + int mods = (message[5] & 0xFF) << 24 | (message[6] & 0xFF) << 16 | (message[7] & 0xFF) << 8 | (message[8] & 0xFF); + // player.sendMessage("Hotkey: " + (char) key + " " + action + " " + Long.toBinaryString(mods)); + customScriptManager.callHotkey(mods, ((char) key), player, action == 1); + } else { + // player.sendMessage("Hotkey: " + (char) key + " " + action); + customScriptManager.callHotkey(0, ((char) key), player, action == 1); + } } }