From 54b34bbfaec374d318b58e7f624fa5edb6962fca Mon Sep 17 00:00:00 2001 From: yoyosource Date: Wed, 28 Dec 2022 16:09:05 +0100 Subject: [PATCH] Update script structure Signed-off-by: yoyosource --- .../features/region/TNTListener.java | 2 +- .../features/script/CustomScript.java | 323 ------------------ .../features/script/CustomScriptManager.java | 162 +++------ .../features/script/ScriptSyntaxSender.java | 1 + .../features/script/custom/MenuScript.java | 29 ++ .../features/script/custom/Script.java | 23 ++ .../custom/command/CommandListener.java | 45 +++ .../script/custom/command/CustomCommand.java | 87 +++++ .../custom/command/InventoryCommand.java | 51 +++ .../script/custom/command/MenuCommand.java | 99 ++++++ .../script/custom/event/CustomEvent.java | 34 ++ .../event/EventListener.java} | 52 +-- .../script/{ => custom/event}/EventType.java | 2 +- .../script/custom/event/InventoryEvent.java | 47 +++ .../script/custom/event/MenuEvent.java | 76 +++++ .../features/script/custom/hotkey/Hotkey.java | 29 ++ .../script/custom/hotkey/HotkeyListener.java | 57 ++++ .../script/{ => custom/hotkey}/Hotkeys.java | 2 +- .../script/custom/hotkey/InventoryHotkey.java | 51 +++ .../script/custom/hotkey/MenuHotkey.java | 80 +++++ 20 files changed, 783 insertions(+), 469 deletions(-) delete mode 100644 BauSystem_Main/src/de/steamwar/bausystem/features/script/CustomScript.java create mode 100644 BauSystem_Main/src/de/steamwar/bausystem/features/script/custom/MenuScript.java create mode 100644 BauSystem_Main/src/de/steamwar/bausystem/features/script/custom/Script.java create mode 100644 BauSystem_Main/src/de/steamwar/bausystem/features/script/custom/command/CommandListener.java create mode 100644 BauSystem_Main/src/de/steamwar/bausystem/features/script/custom/command/CustomCommand.java create mode 100644 BauSystem_Main/src/de/steamwar/bausystem/features/script/custom/command/InventoryCommand.java create mode 100644 BauSystem_Main/src/de/steamwar/bausystem/features/script/custom/command/MenuCommand.java create mode 100644 BauSystem_Main/src/de/steamwar/bausystem/features/script/custom/event/CustomEvent.java rename BauSystem_Main/src/de/steamwar/bausystem/features/script/{ScriptEventListener.java => custom/event/EventListener.java} (63%) rename BauSystem_Main/src/de/steamwar/bausystem/features/script/{ => custom/event}/EventType.java (98%) create mode 100644 BauSystem_Main/src/de/steamwar/bausystem/features/script/custom/event/InventoryEvent.java create mode 100644 BauSystem_Main/src/de/steamwar/bausystem/features/script/custom/event/MenuEvent.java create mode 100644 BauSystem_Main/src/de/steamwar/bausystem/features/script/custom/hotkey/Hotkey.java create mode 100644 BauSystem_Main/src/de/steamwar/bausystem/features/script/custom/hotkey/HotkeyListener.java rename BauSystem_Main/src/de/steamwar/bausystem/features/script/{ => custom/hotkey}/Hotkeys.java (98%) create mode 100644 BauSystem_Main/src/de/steamwar/bausystem/features/script/custom/hotkey/InventoryHotkey.java create mode 100644 BauSystem_Main/src/de/steamwar/bausystem/features/script/custom/hotkey/MenuHotkey.java diff --git a/BauSystem_Main/src/de/steamwar/bausystem/features/region/TNTListener.java b/BauSystem_Main/src/de/steamwar/bausystem/features/region/TNTListener.java index 92164545..6bb5b15b 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/features/region/TNTListener.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/features/region/TNTListener.java @@ -20,7 +20,7 @@ package de.steamwar.bausystem.features.region; import de.steamwar.bausystem.features.script.CustomScriptManager; -import de.steamwar.bausystem.features.script.EventType; +import de.steamwar.bausystem.features.script.custom.event.EventType; import de.steamwar.bausystem.region.Region; import de.steamwar.bausystem.region.RegionUtils; import de.steamwar.bausystem.region.flags.Flag; diff --git a/BauSystem_Main/src/de/steamwar/bausystem/features/script/CustomScript.java b/BauSystem_Main/src/de/steamwar/bausystem/features/script/CustomScript.java deleted file mode 100644 index 136dea6b..00000000 --- a/BauSystem_Main/src/de/steamwar/bausystem/features/script/CustomScript.java +++ /dev/null @@ -1,323 +0,0 @@ -/* - * This file is a part of the SteamWar software. - * - * Copyright (C) 2021 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 de.steamwar.bausystem.BauSystem; -import de.steamwar.bausystem.features.script.variables.Value; -import de.steamwar.inventory.SWItem; -import lombok.RequiredArgsConstructor; -import lombok.experimental.UtilityClass; -import org.bukkit.Material; -import org.bukkit.entity.Player; -import org.bukkit.event.Event; -import org.bukkit.event.player.PlayerCommandPreprocessEvent; -import org.bukkit.inventory.meta.BookMeta; -import yapion.hierarchy.types.YAPIONArray; -import yapion.hierarchy.types.YAPIONMap; - -import java.util.HashMap; -import java.util.List; -import java.util.Map; -import java.util.function.Consumer; - -@UtilityClass -public class CustomScript { - - public interface Script { - } - - public interface MenuScript { - void toYAPION(YAPIONMap yapionMap); - 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, null); - 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, null); - 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(Player p, Map variables, Consumer echoConsumer); - - } - - @RequiredArgsConstructor - public static class InventoryEvent implements CustomEvent { - public final BookMeta bookMeta; - public final String[] args; - - @Override - public String eventName() { - return args[0]; - } - - @Override - public boolean execute(Player p, Map variables, Consumer echoConsumer) { - new ScriptExecutor(bookMeta, p, variables, echoConsumer); - return true; - } - } - - @RequiredArgsConstructor - public static class MenuEvent implements CustomEvent, MenuScript { - public final List pages; - public final String[] args; - - @Override - public String eventName() { - return args[0]; - } - - @Override - public boolean execute(Player p, Map variables, Consumer echoConsumer) { - new ScriptExecutor(pages, p, variables, echoConsumer); - 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_EVENT_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 CustomCommand extends Script { - String[] command(); - - default Map check(String[] args, String[] command) { - if (args.length < command.length) { - return null; - } - - if (!args[0].equals(command[0])) { - return null; - } - - Map arguments = new HashMap<>(); - if (!check(arguments, args, command, 0, 0)) { - return null; - } - return arguments; - } - - default boolean check(Map arguments, String[] args, String[] command, int argsIndex, int commandIndex) { - if (command.length <= commandIndex) { - for (int i = argsIndex; i < args.length; i++) { - if (!(args[i].startsWith("(") && args[i].endsWith(")"))) { - return false; - } - } - return true; - } - if (args.length <= argsIndex) return true; - - String currentArg = args[argsIndex]; - String currentCommand = command[commandIndex]; - - if (currentArg.startsWith("<") && currentArg.endsWith(">")) { - arguments.put(trim(currentArg, 1), new Value.StringValue(currentCommand)); - return check(arguments, args, command, argsIndex + 1, commandIndex + 1); - } else if (currentArg.startsWith("(<") && currentArg.endsWith(">)")) { - arguments.put(trim(currentArg, 2), new Value.StringValue(currentCommand)); - return check(arguments, args, command, argsIndex + 1, commandIndex + 1); - } else if (currentArg.startsWith("(") && currentArg.endsWith(")")) { - if (!trim(currentArg, 1).equals(currentCommand)) { - arguments.put(trim(currentArg, 1), new Value.BooleanValue(false)); - return check(arguments, args, command, argsIndex + 1, commandIndex); - } else { - arguments.put(trim(currentArg, 1), new Value.BooleanValue(true)); - return check(arguments, args, command, argsIndex + 1, commandIndex + 1); - } - } else { - if (!currentArg.equals(currentCommand)) return false; - return check(arguments, args, command, argsIndex + 1, commandIndex + 1); - } - } - - default String trim(String s, int count) { - return s.substring(count, s.length() - count); - } - - boolean execute(String[] command, PlayerCommandPreprocessEvent e); - } - - @RequiredArgsConstructor - public static class InventoryCommand implements CustomCommand { - public final BookMeta bookMeta; - public final String[] args; - - @Override - public String[] command() { - return args; - } - - public boolean execute(String[] command, PlayerCommandPreprocessEvent e) { - Map arguments = check(args, command); - if (arguments == null) { - return false; - } - - e.setCancelled(true); - new ScriptExecutor(bookMeta, e.getPlayer(), arguments, null); - return true; - } - } - - @RequiredArgsConstructor - public static class MenuCommand implements CustomCommand, MenuScript { - public final List pages; - public final String[] args; - - @Override - public String[] command() { - return args; - } - - public boolean execute(String[] command, PlayerCommandPreprocessEvent e) { - Map arguments = check(args, command); - if (arguments == null) { - return false; - } - - e.setCancelled(true); - new ScriptExecutor(pages, e.getPlayer(), arguments, null); - return true; - } - - public boolean equals(MenuCommand menuCommand) { - if (menuCommand.args.length != args.length) { - return false; - } - if (!args[0].equals(menuCommand.args[0])) { - return false; - } - for (int i = 0; i < args.length; i++) { - if (i == 0) continue; - String s1 = args[i]; - String s2 = menuCommand.args[i]; - if (!s1.equals(s2)) { - return false; - } - if (!(s1.startsWith("<") && s1.endsWith(">") && s2.startsWith("<") && s2.endsWith(">"))) { - return false; - } - if (!(s1.startsWith("(<") && s1.endsWith(">)") && s2.startsWith("(<") && s2.endsWith(">)"))) { - return false; - } - } - return true; - } - - @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) { - SWItem swItem = new SWItem(Material.WRITABLE_BOOK, BauSystem.MESSAGE.parse("SCRIPT_COMMAND_ITEM_NAME", p, String.join(" ", args))); - BookMeta bookMeta = (BookMeta) swItem.getItemMeta(); - bookMeta.setPages(pages.toArray(new String[0])); - swItem.setItemMeta(bookMeta); - return swItem; - } - } -} 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 914e5cff..0c4e50af 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/features/script/CustomScriptManager.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/features/script/CustomScriptManager.java @@ -21,6 +21,19 @@ package de.steamwar.bausystem.features.script; import de.steamwar.bausystem.BauSystem; import de.steamwar.bausystem.SWUtils; +import de.steamwar.bausystem.features.script.custom.MenuScript; +import de.steamwar.bausystem.features.script.custom.Script; +import de.steamwar.bausystem.features.script.custom.command.CustomCommand; +import de.steamwar.bausystem.features.script.custom.command.InventoryCommand; +import de.steamwar.bausystem.features.script.custom.command.MenuCommand; +import de.steamwar.bausystem.features.script.custom.event.CustomEvent; +import de.steamwar.bausystem.features.script.custom.event.EventType; +import de.steamwar.bausystem.features.script.custom.event.InventoryEvent; +import de.steamwar.bausystem.features.script.custom.event.MenuEvent; +import de.steamwar.bausystem.features.script.custom.hotkey.Hotkey; +import de.steamwar.bausystem.features.script.custom.hotkey.Hotkeys; +import de.steamwar.bausystem.features.script.custom.hotkey.InventoryHotkey; +import de.steamwar.bausystem.features.script.custom.hotkey.MenuHotkey; import de.steamwar.bausystem.features.script.variables.Value; import de.steamwar.bausystem.utils.FlatteningWrapper; import de.steamwar.inventory.SWItem; @@ -54,11 +67,11 @@ import java.util.stream.Collectors; @Linked public class CustomScriptManager implements Listener { - private final Map> playerMap = Collections.synchronizedMap(new HashMap<>()); // new ConcurrentHashMap<>(); + public final Map> playerMap = Collections.synchronizedMap(new HashMap<>()); // new ConcurrentHashMap<>(); private void updateInventory(Player p) { playerMap.computeIfPresent(p, (player, customCommands) -> { - customCommands.removeIf(script -> !(script instanceof CustomScript.MenuScript)); + customCommands.removeIf(script -> !(script instanceof MenuScript)); return customCommands; }); for (ItemStack item : p.getInventory().getContents()) { @@ -75,11 +88,11 @@ public class CustomScriptManager implements Listener { } String s = bookMeta.getPage(1).split("\n")[0]; if (s.startsWith("#!CMD /")) { - playerMap.computeIfAbsent(p, player -> new ArrayList<>()).add(new CustomScript.InventoryCommand(bookMeta, s.substring(6).split(" "))); + playerMap.computeIfAbsent(p, player -> new ArrayList<>()).add(new 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(" "))); + playerMap.computeIfAbsent(p, player -> new ArrayList<>()).add(new 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(" "))); + playerMap.computeIfAbsent(p, player -> new ArrayList<>()).add(new InventoryHotkey(bookMeta, s.substring(9).split(" "))); } } } @@ -113,19 +126,19 @@ public class CustomScriptManager implements Listener { yapionObject.getYAPIONMapOrSetDefault("commands", new YAPIONMap()).forEach((key, value) -> { String[] command = ((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.MenuCommand(pages, command)); + playerMap.computeIfAbsent(p, player -> new ArrayList<>()).add(new MenuCommand(pages, command)); }); yapionObject.getYAPIONMapOrSetDefault("events", new YAPIONMap()).forEach((key, value) -> { String[] event = ((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.MenuEvent(pages, event)); + playerMap.computeIfAbsent(p, player -> new ArrayList<>()).add(new 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)); + playerMap.computeIfAbsent(p, player -> new ArrayList<>()).add(new MenuHotkey(pages, hotkey)); }); } @@ -141,20 +154,20 @@ public class CustomScriptManager implements Listener { 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); + playerMap.get(p).stream().filter(MenuCommand.class::isInstance).map(MenuCommand.class::cast).forEach(menuCommand -> { + menuCommand.save(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); + playerMap.get(p).stream().filter(MenuEvent.class::isInstance).map(MenuEvent.class::cast).forEach(menuCommand -> { + menuCommand.save(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); + playerMap.get(p).stream().filter(MenuHotkey.class::isInstance).map(MenuHotkey.class::cast).forEach(menuCommand -> { + menuCommand.save(hotkeysMap); }); return yapionObject; } @@ -180,13 +193,13 @@ public class CustomScriptManager implements Listener { } public void openCommandsMenu(Player p) { - List> menuCommands = new ArrayList<>(); - playerMap.getOrDefault(p, new ArrayList<>()).stream().filter(CustomScript.MenuScript.class::isInstance).map(CustomScript.MenuScript.class::cast).forEach(menuItem -> { + List> menuCommands = new ArrayList<>(); + playerMap.getOrDefault(p, new ArrayList<>()).stream().filter(MenuScript.class::isInstance).map(MenuScript.class::cast).forEach(menuItem -> { SWItem swItem = menuItem.toItem(p); ItemStack itemStack = swItem.getItemStack(); - if (menuItem instanceof CustomScript.MenuHotkey) { + if (menuItem instanceof MenuHotkey) { itemStack.setType(Material.CHAIN_COMMAND_BLOCK); - } else if (menuItem instanceof CustomScript.MenuEvent) { + } else if (menuItem instanceof MenuEvent) { itemStack.setType(Material.REPEATING_COMMAND_BLOCK); } else { itemStack.setType(Material.COMMAND_BLOCK); @@ -201,7 +214,7 @@ public class CustomScriptManager implements Listener { double percentage = ((int) ((length / 655336.0) * 1000)) / 10.0; String menuName = BauSystem.MESSAGE.parse("SCRIPT_MENU_GUI_NAME", p, percentage > 99 ? "§c" : (percentage >= 75 ? "§6" : "§a"), percentage); - SWListInv menuCommandSWListInv = new SWListInv<>(p, menuName, false, menuCommands, (clickType, menuCommand) -> { + SWListInv menuCommandSWListInv = new SWListInv<>(p, menuName, false, menuCommands, (clickType, menuCommand) -> { if (!clickType.isShiftClick()) { playerMap.get(p).removeIf(customCommand -> customCommand == menuCommand); } @@ -227,19 +240,19 @@ public class CustomScriptManager implements Listener { } String s = bookMeta.getPage(1).split("\n")[0]; if (s.startsWith("#!CMD /")) { - CustomScript.MenuCommand menuCommand = new CustomScript.MenuCommand(bookMeta.getPages(), s.substring(6).split(" ")); - for (CustomScript.Script script : playerMap.computeIfAbsent(p, player -> new ArrayList<>())) { - if (!(script instanceof CustomScript.MenuCommand)) { + MenuCommand menuCommand = new MenuCommand(bookMeta.getPages(), s.substring(6).split(" ")); + for (Script script : playerMap.computeIfAbsent(p, player -> new ArrayList<>())) { + if (!(script instanceof MenuCommand)) { continue; } - if (((CustomScript.MenuCommand) script).equals(menuCommand)) { + if (((MenuCommand) script).equals(menuCommand)) { BauSystem.MESSAGE.sendPrefixless("SCRIPT_MENU_GUI_DUPLICATE_COMMAND", p, String.join(" ", menuCommand.command())); return; } } scriptBookLimit(p, menuCommand); } else if (s.startsWith("#!EVENT ")) { - CustomScript.MenuEvent menuEvent = new CustomScript.MenuEvent(bookMeta.getPages(), s.substring(8).split(" ")); + MenuEvent menuEvent = new MenuEvent(bookMeta.getPages(), s.substring(8).split(" ")); try { EventType.valueOf(menuEvent.eventName()); } catch (Exception e) { @@ -248,13 +261,13 @@ public class CustomScriptManager implements Listener { } scriptBookLimit(p, menuEvent); } else if (s.startsWith("#!HOTKEY ")) { - scriptBookLimit(p, new CustomScript.MenuHotkey(bookMeta.getPages(), s.substring(9).split(" "))); + scriptBookLimit(p, new MenuHotkey(bookMeta.getPages(), s.substring(9).split(" "))); } })); menuCommandSWListInv.open(); } - private void scriptBookLimit(Player p, CustomScript.Script menuScript) { + private void scriptBookLimit(Player p, Script menuScript) { playerMap.computeIfAbsent(p, player -> new ArrayList<>()).add(menuScript); if (!save(p)) { playerMap.get(p).removeIf(script -> script == menuScript); @@ -268,14 +281,25 @@ public class CustomScriptManager implements Listener { openCommandsMenu(p); } + public boolean callCommand(Player p, PlayerCommandPreprocessEvent e, String message) { + List customCommands = playerMap.getOrDefault(p, new ArrayList<>()).stream().filter(CustomCommand.class::isInstance).map(CustomCommand.class::cast).collect(Collectors.toList()); + String[] command = message.split(" "); + for (CustomCommand customCommand : customCommands) { + if (customCommand.execute(command, e)) { + return true; + } + } + return false; + } + public boolean callScoreboard(Player p, Map variables, Consumer echoConsumer) { Map valueMap = new HashMap<>(); for (Map.Entry entry : variables.entrySet()) { valueMap.put(entry.getKey(), new Value.StringValue(entry.getValue())); } - List customEvents = playerMap.getOrDefault(p, new ArrayList<>()).stream().filter(CustomScript.CustomEvent.class::isInstance).map(CustomScript.CustomEvent.class::cast).collect(Collectors.toList()); + List customEvents = playerMap.getOrDefault(p, new ArrayList<>()).stream().filter(CustomEvent.class::isInstance).map(CustomEvent.class::cast).collect(Collectors.toList()); boolean hasScript = false; - for (CustomScript.CustomEvent customEvent : customEvents) { + for (CustomEvent customEvent : customEvents) { if (customEvent.eventName().equals(EventType.Scoreboard.name())) { customEvent.execute(p, valueMap, echoConsumer); hasScript = true; @@ -288,8 +312,8 @@ public class CustomScriptManager implements Listener { if (eventType == null) return; if (!eventType.getEventType().equals(e.getClass())) return; - List customEvents = playerMap.getOrDefault(p, new ArrayList<>()).stream().filter(CustomScript.CustomEvent.class::isInstance).map(CustomScript.CustomEvent.class::cast).collect(Collectors.toList()); - for (CustomScript.CustomEvent customEvent : customEvents) { + List customEvents = playerMap.getOrDefault(p, new ArrayList<>()).stream().filter(CustomEvent.class::isInstance).map(CustomEvent.class::cast).collect(Collectors.toList()); + for (CustomEvent customEvent : customEvents) { if (customEvent.eventName().equals(eventType.name())) { Map variables = eventType.getEventValues().apply(e); if (variables == null) { @@ -308,85 +332,11 @@ public class CustomScriptManager implements Listener { } 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) { + List hotkeys = playerMap.getOrDefault(p, new ArrayList<>()).stream().filter(Hotkey.class::isInstance).map(Hotkey.class::cast).collect(Collectors.toList()); + for (Hotkey hotkey : hotkeys) { if (Hotkeys.isSame(modifiers, pressedHotkey, hotkey.hotkey())) { hotkey.execute(p, pressed); } } } - - @EventHandler - public void onPlayerCommandPreprocess(PlayerCommandPreprocessEvent e) { - if (e.getMessage().startsWith("/script:")) { - e.setMessage("/" + e.getMessage().substring(8)); - return; - } - - List customCommands = playerMap.getOrDefault(e.getPlayer(), new ArrayList<>()).stream().filter(CustomScript.CustomCommand.class::isInstance).map(CustomScript.CustomCommand.class::cast).collect(Collectors.toList()); - String[] command = e.getMessage().split(" "); - for (CustomScript.CustomCommand customCommand : customCommands) { - if (customCommand.execute(command, e)) { - return; - } - } - } - - /* - { - final Class packetPlayOutCommands = Reflection.getClass("{nms}.PacketPlayOutCommands"); - final Reflection.FieldAccessor playerInfoData = Reflection.getField(packetPlayOutCommands, RootCommandNode.class, 0); - - ProtocolAPI.setOutgoingHandler(packetPlayOutCommands, (player, packet) -> { - PacketPlayOutCommands commands = (PacketPlayOutCommands) packet; - RootCommandNode rootCommandNode = playerInfoData.get(commands); - if (!playerMap.containsKey(player)) { - load(player); - } - // rootCommandNode.addChild(new LiteralCommandNode("Hello", null, t -> true, null, null, false)); - List customCommands; - synchronized (playerMap) { - customCommands = playerMap.getOrDefault(player, new ArrayList<>()).stream().filter(CustomScript.CustomCommand.class::isInstance).map(CustomScript.CustomCommand.class::cast).collect(Collectors.toList()); - } - for (CustomScript.CustomCommand customCommand : customCommands) { - System.out.println("ADDING: " + customCommand); - String[] args = customCommand.command(); - - List> commandNodes = new ArrayList<>(); - for (int i = args.length - 1; i >= 0; i--) { - if (i == 0) { - CommandNode current = new LiteralCommandNode(args[i].substring(1), null, currentPlayer -> currentPlayer.getUniqueId().equals(player.getUniqueId()), null, null, false); - commandNodes.forEach(current::addChild); - rootCommandNode.addChild(current); - } else { - String arg = args[i]; - if (arg.startsWith("<") && arg.endsWith(">")) { - CommandNode current = new ArgumentCommandNode(arg.substring(1, arg.length() - 1), StringArgumentType.word(), null, currentPlayer -> currentPlayer.getUniqueId().equals(player.getUniqueId()), null, null, false, (commandContext, suggestionsBuilder) -> suggestionsBuilder.buildFuture()); - commandNodes.forEach(current::addChild); - commandNodes.clear(); - commandNodes.add(current); - } else if (arg.startsWith("(<") && arg.endsWith(">)")) { - // CommandNode current = new ArgumentCommandNode(arg, StringArgumentType.word(), null, currentPlayer -> currentPlayer.getUniqueId().equals(player.getUniqueId()), null, null, false, (commandContext, suggestionsBuilder) -> suggestionsBuilder.buildFuture()); - // commandNodes.forEach(current::addChild); - // commandNodes.add(current); - CommandNode current = new LiteralCommandNode(arg, null, currentPlayer -> currentPlayer.getUniqueId().equals(player.getUniqueId()), null, null, false); - commandNodes.forEach(current::addChild); - commandNodes.add(current); - } else if (arg.startsWith("(") && arg.endsWith(")")) { - CommandNode current = new LiteralCommandNode(arg.substring(1, arg.length() - 1), null, currentPlayer -> currentPlayer.getUniqueId().equals(player.getUniqueId()), null, null, false); - commandNodes.forEach(current::addChild); - commandNodes.add(current); - } else { - CommandNode current = new LiteralCommandNode(arg, null, currentPlayer -> currentPlayer.getUniqueId().equals(player.getUniqueId()), null, null, false); - commandNodes.forEach(current::addChild); - commandNodes.clear(); - commandNodes.add(current); - } - } - } - } - return packet; - }); - } - */ } diff --git a/BauSystem_Main/src/de/steamwar/bausystem/features/script/ScriptSyntaxSender.java b/BauSystem_Main/src/de/steamwar/bausystem/features/script/ScriptSyntaxSender.java index 4c73b143..8c9763c3 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/features/script/ScriptSyntaxSender.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/features/script/ScriptSyntaxSender.java @@ -20,6 +20,7 @@ package de.steamwar.bausystem.features.script; import de.steamwar.bausystem.BauSystem; +import de.steamwar.bausystem.features.script.custom.event.EventType; import de.steamwar.bausystem.features.script.expression.Expression; import de.steamwar.linkage.Linked; import lombok.SneakyThrows; diff --git a/BauSystem_Main/src/de/steamwar/bausystem/features/script/custom/MenuScript.java b/BauSystem_Main/src/de/steamwar/bausystem/features/script/custom/MenuScript.java new file mode 100644 index 00000000..49befa41 --- /dev/null +++ b/BauSystem_Main/src/de/steamwar/bausystem/features/script/custom/MenuScript.java @@ -0,0 +1,29 @@ +/* + * 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.custom; + +import de.steamwar.inventory.SWItem; +import org.bukkit.entity.Player; +import yapion.hierarchy.types.YAPIONMap; + +public interface MenuScript { + void save(YAPIONMap yapionMap); + SWItem toItem(Player p); +} diff --git a/BauSystem_Main/src/de/steamwar/bausystem/features/script/custom/Script.java b/BauSystem_Main/src/de/steamwar/bausystem/features/script/custom/Script.java new file mode 100644 index 00000000..17693f8a --- /dev/null +++ b/BauSystem_Main/src/de/steamwar/bausystem/features/script/custom/Script.java @@ -0,0 +1,23 @@ +/* + * 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.custom; + +public interface Script { +} diff --git a/BauSystem_Main/src/de/steamwar/bausystem/features/script/custom/command/CommandListener.java b/BauSystem_Main/src/de/steamwar/bausystem/features/script/custom/command/CommandListener.java new file mode 100644 index 00000000..4c2e7ddf --- /dev/null +++ b/BauSystem_Main/src/de/steamwar/bausystem/features/script/custom/command/CommandListener.java @@ -0,0 +1,45 @@ +/* + * 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.custom.command; + +import de.steamwar.bausystem.features.script.CustomScriptManager; +import de.steamwar.linkage.Linked; +import de.steamwar.linkage.LinkedInstance; +import org.bukkit.event.EventHandler; +import org.bukkit.event.Listener; +import org.bukkit.event.player.PlayerCommandPreprocessEvent; + + +@Linked +public class CommandListener implements Listener { + + @LinkedInstance + public CustomScriptManager manager; + + @EventHandler + public void onPlayerCommandPreprocess(PlayerCommandPreprocessEvent e) { + if (e.getMessage().startsWith("/script:")) { + e.setMessage("/" + e.getMessage().substring(8)); + return; + } + + manager.callCommand(e.getPlayer(), e, e.getMessage()); + } +} diff --git a/BauSystem_Main/src/de/steamwar/bausystem/features/script/custom/command/CustomCommand.java b/BauSystem_Main/src/de/steamwar/bausystem/features/script/custom/command/CustomCommand.java new file mode 100644 index 00000000..b0a7d386 --- /dev/null +++ b/BauSystem_Main/src/de/steamwar/bausystem/features/script/custom/command/CustomCommand.java @@ -0,0 +1,87 @@ +/* + * 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.custom.command; + +import de.steamwar.bausystem.features.script.custom.Script; +import de.steamwar.bausystem.features.script.variables.Value; +import org.bukkit.event.player.PlayerCommandPreprocessEvent; + +import java.util.HashMap; +import java.util.Map; + +public interface CustomCommand extends Script { + String[] command(); + + default Map check(String[] args, String[] command) { + if (args.length < command.length) { + return null; + } + + if (!args[0].equals(command[0])) { + return null; + } + + Map arguments = new HashMap<>(); + if (!check(arguments, args, command, 0, 0)) { + return null; + } + return arguments; + } + + default boolean check(Map arguments, String[] args, String[] command, int argsIndex, int commandIndex) { + if (command.length <= commandIndex) { + for (int i = argsIndex; i < args.length; i++) { + if (!(args[i].startsWith("(") && args[i].endsWith(")"))) { + return false; + } + } + return true; + } + if (args.length <= argsIndex) return true; + + String currentArg = args[argsIndex]; + String currentCommand = command[commandIndex]; + + if (currentArg.startsWith("<") && currentArg.endsWith(">")) { + arguments.put(trim(currentArg, 1), new Value.StringValue(currentCommand)); + return check(arguments, args, command, argsIndex + 1, commandIndex + 1); + } else if (currentArg.startsWith("(<") && currentArg.endsWith(">)")) { + arguments.put(trim(currentArg, 2), new Value.StringValue(currentCommand)); + return check(arguments, args, command, argsIndex + 1, commandIndex + 1); + } else if (currentArg.startsWith("(") && currentArg.endsWith(")")) { + if (!trim(currentArg, 1).equals(currentCommand)) { + arguments.put(trim(currentArg, 1), new Value.BooleanValue(false)); + return check(arguments, args, command, argsIndex + 1, commandIndex); + } else { + arguments.put(trim(currentArg, 1), new Value.BooleanValue(true)); + return check(arguments, args, command, argsIndex + 1, commandIndex + 1); + } + } else { + if (!currentArg.equals(currentCommand)) return false; + return check(arguments, args, command, argsIndex + 1, commandIndex + 1); + } + } + + default String trim(String s, int count) { + return s.substring(count, s.length() - count); + } + + boolean execute(String[] command, PlayerCommandPreprocessEvent e); +} diff --git a/BauSystem_Main/src/de/steamwar/bausystem/features/script/custom/command/InventoryCommand.java b/BauSystem_Main/src/de/steamwar/bausystem/features/script/custom/command/InventoryCommand.java new file mode 100644 index 00000000..34cfc738 --- /dev/null +++ b/BauSystem_Main/src/de/steamwar/bausystem/features/script/custom/command/InventoryCommand.java @@ -0,0 +1,51 @@ +/* + * 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.custom.command; + +import de.steamwar.bausystem.features.script.ScriptExecutor; +import de.steamwar.bausystem.features.script.variables.Value; +import lombok.RequiredArgsConstructor; +import org.bukkit.event.player.PlayerCommandPreprocessEvent; +import org.bukkit.inventory.meta.BookMeta; + +import java.util.Map; + +@RequiredArgsConstructor +public +class InventoryCommand implements CustomCommand { + public final BookMeta bookMeta; + public final String[] args; + + @Override + public String[] command() { + return args; + } + + public boolean execute(String[] command, PlayerCommandPreprocessEvent e) { + Map arguments = check(args, command); + if (arguments == null) { + return false; + } + + e.setCancelled(true); + new ScriptExecutor(bookMeta, e.getPlayer(), arguments, null); + return true; + } +} diff --git a/BauSystem_Main/src/de/steamwar/bausystem/features/script/custom/command/MenuCommand.java b/BauSystem_Main/src/de/steamwar/bausystem/features/script/custom/command/MenuCommand.java new file mode 100644 index 00000000..b5fbd637 --- /dev/null +++ b/BauSystem_Main/src/de/steamwar/bausystem/features/script/custom/command/MenuCommand.java @@ -0,0 +1,99 @@ +/* + * 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.custom.command; + +import de.steamwar.bausystem.BauSystem; +import de.steamwar.bausystem.features.script.ScriptExecutor; +import de.steamwar.bausystem.features.script.custom.MenuScript; +import de.steamwar.bausystem.features.script.variables.Value; +import de.steamwar.inventory.SWItem; +import lombok.RequiredArgsConstructor; +import org.bukkit.Material; +import org.bukkit.entity.Player; +import org.bukkit.event.player.PlayerCommandPreprocessEvent; +import org.bukkit.inventory.meta.BookMeta; +import yapion.hierarchy.types.YAPIONArray; +import yapion.hierarchy.types.YAPIONMap; + +import java.util.List; +import java.util.Map; + +@RequiredArgsConstructor +public +class MenuCommand implements CustomCommand, MenuScript { + public final List pages; + public final String[] args; + + @Override + public String[] command() { + return args; + } + + public boolean execute(String[] command, PlayerCommandPreprocessEvent e) { + Map arguments = check(args, command); + if (arguments == null) { + return false; + } + + e.setCancelled(true); + new ScriptExecutor(pages, e.getPlayer(), arguments, null); + return true; + } + + public boolean equals(CustomScript.MenuCommand menuCommand) { + if (menuCommand.args.length != args.length) { + return false; + } + if (!args[0].equals(menuCommand.args[0])) { + return false; + } + for (int i = 0; i < args.length; i++) { + if (i == 0) continue; + String s1 = args[i]; + String s2 = menuCommand.args[i]; + if (!s1.equals(s2)) { + return false; + } + if (!(s1.startsWith("<") && s1.endsWith(">") && s2.startsWith("<") && s2.endsWith(">"))) { + return false; + } + if (!(s1.startsWith("(<") && s1.endsWith(">)") && s2.startsWith("(<") && s2.endsWith(">)"))) { + return false; + } + } + return true; + } + + @Override + public void save(YAPIONMap yapionMap) { + YAPIONArray yapionArray = new YAPIONArray(); + pages.forEach(yapionArray::add); + yapionMap.put(String.join(" ", args), yapionArray); + } + + @Override + public SWItem toItem(Player p) { + SWItem swItem = new SWItem(Material.WRITABLE_BOOK, BauSystem.MESSAGE.parse("SCRIPT_COMMAND_ITEM_NAME", p, String.join(" ", args))); + BookMeta bookMeta = (BookMeta) swItem.getItemMeta(); + bookMeta.setPages(pages.toArray(new String[0])); + swItem.setItemMeta(bookMeta); + return swItem; + } +} diff --git a/BauSystem_Main/src/de/steamwar/bausystem/features/script/custom/event/CustomEvent.java b/BauSystem_Main/src/de/steamwar/bausystem/features/script/custom/event/CustomEvent.java new file mode 100644 index 00000000..ec4bebd7 --- /dev/null +++ b/BauSystem_Main/src/de/steamwar/bausystem/features/script/custom/event/CustomEvent.java @@ -0,0 +1,34 @@ +/* + * 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.custom.event; + +import de.steamwar.bausystem.features.script.custom.Script; +import de.steamwar.bausystem.features.script.variables.Value; +import org.bukkit.entity.Player; + +import java.util.Map; +import java.util.function.Consumer; + +public interface CustomEvent extends Script { + + String eventName(); + boolean execute(Player p, Map variables, Consumer echoConsumer); + +} diff --git a/BauSystem_Main/src/de/steamwar/bausystem/features/script/ScriptEventListener.java b/BauSystem_Main/src/de/steamwar/bausystem/features/script/custom/event/EventListener.java similarity index 63% rename from BauSystem_Main/src/de/steamwar/bausystem/features/script/ScriptEventListener.java rename to BauSystem_Main/src/de/steamwar/bausystem/features/script/custom/event/EventListener.java index ec9c596a..f8913e28 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/features/script/ScriptEventListener.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/features/script/custom/event/EventListener.java @@ -17,9 +17,10 @@ * along with this program. If not, see . */ -package de.steamwar.bausystem.features.script; +package de.steamwar.bausystem.features.script.custom.event; import de.steamwar.bausystem.BauSystem; +import de.steamwar.bausystem.features.script.CustomScriptManager; import de.steamwar.bausystem.region.Region; import de.steamwar.bausystem.region.utils.RegionExtensionType; import de.steamwar.bausystem.region.utils.RegionType; @@ -37,7 +38,6 @@ 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; @@ -45,14 +45,10 @@ import java.util.Map; import java.util.Set; @Linked -public class ScriptEventListener implements Listener, PluginMessageListener { - - { - Bukkit.getServer().getMessenger().registerIncomingPluginChannel(BauSystem.getInstance(), "sw:hotkeys", this); - } +public class EventListener implements Listener { @LinkedInstance - public CustomScriptManager customScriptManager; + public CustomScriptManager manager; private static final Map LAST_FS = new HashMap<>(); @@ -64,18 +60,18 @@ public class ScriptEventListener implements Listener, PluginMessageListener { @EventHandler public void onPlayerJoin(PlayerJoinEvent event) { - customScriptManager.callEvent(EventType.SelfJoin, event.getPlayer(), event); + manager.callEvent(EventType.SelfJoin, event.getPlayer(), event); } @EventHandler public void onPlayerQuit(PlayerQuitEvent event) { - customScriptManager.callEvent(EventType.SelfLeave, event.getPlayer(), event); + manager.callEvent(EventType.SelfLeave, event.getPlayer(), event); } @EventHandler public void onPlayerSwapHandItems(PlayerSwapHandItemsEvent event) { if (LAST_FS.containsKey(event.getPlayer())) { - customScriptManager.callEvent(EventType.FF, event.getPlayer(), event); + manager.callEvent(EventType.FF, event.getPlayer(), event); } else { LAST_FS.put(event.getPlayer(), System.currentTimeMillis()); } @@ -83,12 +79,12 @@ public class ScriptEventListener implements Listener, PluginMessageListener { @EventHandler public void onBlockPlace(BlockPlaceEvent event) { - customScriptManager.callEvent(EventType.PlaceBlock, event.getPlayer(), event); + manager.callEvent(EventType.PlaceBlock, event.getPlayer(), event); } @EventHandler public void onBlockBreak(BlockBreakEvent event) { - customScriptManager.callEvent(EventType.BreakBlock, event.getPlayer(), event); + manager.callEvent(EventType.BreakBlock, event.getPlayer(), event); } private Set ignore = new HashSet<>(); @@ -99,10 +95,10 @@ public class ScriptEventListener implements Listener, PluginMessageListener { return; } if (event.getAction() == Action.RIGHT_CLICK_AIR || event.getAction() == Action.RIGHT_CLICK_BLOCK) { - customScriptManager.callEvent(EventType.RightClick, event.getPlayer(), event); + manager.callEvent(EventType.RightClick, event.getPlayer(), event); } if (event.getAction() == Action.LEFT_CLICK_AIR || event.getAction() == Action.LEFT_CLICK_BLOCK) { - customScriptManager.callEvent(EventType.LeftClick, event.getPlayer(), event); + manager.callEvent(EventType.LeftClick, event.getPlayer(), event); } } @@ -115,7 +111,7 @@ public class ScriptEventListener implements Listener, PluginMessageListener { for (Player player : Bukkit.getOnlinePlayers()) { if (tntRegion.inRegion(player.getLocation(), RegionType.NORMAL, RegionExtensionType.NORMAL)) { - customScriptManager.callEvent(EventType.TNTSpawn, player, event); + manager.callEvent(EventType.TNTSpawn, player, event); } } } @@ -129,7 +125,7 @@ public class ScriptEventListener implements Listener, PluginMessageListener { for (Player player : Bukkit.getOnlinePlayers()) { if (tntRegion.inRegion(player.getLocation(), RegionType.NORMAL, RegionExtensionType.NORMAL)) { - customScriptManager.callEvent(EventType.TNTExplode, player, event); + manager.callEvent(EventType.TNTExplode, player, event); } } } @@ -137,31 +133,13 @@ public class ScriptEventListener implements Listener, PluginMessageListener { @EventHandler public void onPlayerDropItem(PlayerDropItemEvent event) { ignore.add(event.getPlayer()); - customScriptManager.callEvent(EventType.DropItem, event.getPlayer(), event); + manager.callEvent(EventType.DropItem, event.getPlayer(), event); } @EventHandler public void onEntityDeath(EntityDeathEvent event) { for (Player player : Bukkit.getOnlinePlayers()) { - 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 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; - 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); + manager.callEvent(EventType.EntityDeath, player, event); } } } diff --git a/BauSystem_Main/src/de/steamwar/bausystem/features/script/EventType.java b/BauSystem_Main/src/de/steamwar/bausystem/features/script/custom/event/EventType.java similarity index 98% rename from BauSystem_Main/src/de/steamwar/bausystem/features/script/EventType.java rename to BauSystem_Main/src/de/steamwar/bausystem/features/script/custom/event/EventType.java index 728e60e0..3a6d471d 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/features/script/EventType.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/features/script/custom/event/EventType.java @@ -17,7 +17,7 @@ * along with this program. If not, see . */ -package de.steamwar.bausystem.features.script; +package de.steamwar.bausystem.features.script.custom.event; import de.steamwar.bausystem.features.script.variables.Value; import lombok.Getter; diff --git a/BauSystem_Main/src/de/steamwar/bausystem/features/script/custom/event/InventoryEvent.java b/BauSystem_Main/src/de/steamwar/bausystem/features/script/custom/event/InventoryEvent.java new file mode 100644 index 00000000..d323dbc8 --- /dev/null +++ b/BauSystem_Main/src/de/steamwar/bausystem/features/script/custom/event/InventoryEvent.java @@ -0,0 +1,47 @@ +/* + * 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.custom.event; + +import de.steamwar.bausystem.features.script.ScriptExecutor; +import de.steamwar.bausystem.features.script.variables.Value; +import lombok.RequiredArgsConstructor; +import org.bukkit.entity.Player; +import org.bukkit.inventory.meta.BookMeta; + +import java.util.Map; +import java.util.function.Consumer; + +@RequiredArgsConstructor +public +class InventoryEvent implements CustomEvent { + public final BookMeta bookMeta; + public final String[] args; + + @Override + public String eventName() { + return args[0]; + } + + @Override + public boolean execute(Player p, Map variables, Consumer echoConsumer) { + new ScriptExecutor(bookMeta, p, variables, echoConsumer); + return true; + } +} diff --git a/BauSystem_Main/src/de/steamwar/bausystem/features/script/custom/event/MenuEvent.java b/BauSystem_Main/src/de/steamwar/bausystem/features/script/custom/event/MenuEvent.java new file mode 100644 index 00000000..96a7d6d2 --- /dev/null +++ b/BauSystem_Main/src/de/steamwar/bausystem/features/script/custom/event/MenuEvent.java @@ -0,0 +1,76 @@ +/* + * 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.custom.event; + +import de.steamwar.bausystem.BauSystem; +import de.steamwar.bausystem.features.script.ScriptExecutor; +import de.steamwar.bausystem.features.script.custom.MenuScript; +import de.steamwar.bausystem.features.script.variables.Value; +import de.steamwar.inventory.SWItem; +import lombok.RequiredArgsConstructor; +import org.bukkit.Material; +import org.bukkit.entity.Player; +import org.bukkit.inventory.meta.BookMeta; +import yapion.hierarchy.types.YAPIONArray; +import yapion.hierarchy.types.YAPIONMap; + +import java.util.List; +import java.util.Map; +import java.util.function.Consumer; + +@RequiredArgsConstructor +public +class MenuEvent implements CustomEvent, MenuScript { + public final List pages; + public final String[] args; + + @Override + public String eventName() { + return args[0]; + } + + @Override + public boolean execute(Player p, Map variables, Consumer echoConsumer) { + new ScriptExecutor(pages, p, variables, echoConsumer); + return false; + } + + @Override + public void save(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_EVENT_ITEM_NAME", p, args[0], st.toString())); + BookMeta bookMeta = (BookMeta) swItem.getItemMeta(); + bookMeta.setPages(pages.toArray(new String[0])); + swItem.setItemMeta(bookMeta); + return swItem; + } +} diff --git a/BauSystem_Main/src/de/steamwar/bausystem/features/script/custom/hotkey/Hotkey.java b/BauSystem_Main/src/de/steamwar/bausystem/features/script/custom/hotkey/Hotkey.java new file mode 100644 index 00000000..2c9bd6c8 --- /dev/null +++ b/BauSystem_Main/src/de/steamwar/bausystem/features/script/custom/hotkey/Hotkey.java @@ -0,0 +1,29 @@ +/* + * 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.custom.hotkey; + +import de.steamwar.bausystem.features.script.custom.Script; +import org.bukkit.entity.Player; + +public interface Hotkey extends Script { + + String hotkey(); + boolean execute(Player p, boolean pressed); +} diff --git a/BauSystem_Main/src/de/steamwar/bausystem/features/script/custom/hotkey/HotkeyListener.java b/BauSystem_Main/src/de/steamwar/bausystem/features/script/custom/hotkey/HotkeyListener.java new file mode 100644 index 00000000..5f8adadd --- /dev/null +++ b/BauSystem_Main/src/de/steamwar/bausystem/features/script/custom/hotkey/HotkeyListener.java @@ -0,0 +1,57 @@ +/* + * 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.custom.hotkey; + +import de.steamwar.bausystem.BauSystem; +import de.steamwar.bausystem.features.script.CustomScriptManager; +import de.steamwar.linkage.Linked; +import de.steamwar.linkage.LinkedInstance; +import org.bukkit.Bukkit; +import org.bukkit.entity.Player; +import org.bukkit.plugin.messaging.PluginMessageListener; + +@Linked +public class HotkeyListener implements PluginMessageListener { + + { + Bukkit.getServer().getMessenger().registerIncomingPluginChannel(BauSystem.getInstance(), "sw:hotkeys", this); + } + + @LinkedInstance + public CustomScriptManager manager; + + @Override + public void onPluginMessageReceived(String channel, Player player, byte[] message) { + if (!channel.equals("sw:hotkeys")) return; + 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; + 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)); + manager.callHotkey(mods, ((char) key), player, action == 1); + } else { + // player.sendMessage("Hotkey: " + (char) key + " " + action); + manager.callHotkey(0, ((char) key), player, action == 1); + } + } +} diff --git a/BauSystem_Main/src/de/steamwar/bausystem/features/script/Hotkeys.java b/BauSystem_Main/src/de/steamwar/bausystem/features/script/custom/hotkey/Hotkeys.java similarity index 98% rename from BauSystem_Main/src/de/steamwar/bausystem/features/script/Hotkeys.java rename to BauSystem_Main/src/de/steamwar/bausystem/features/script/custom/hotkey/Hotkeys.java index c4c5193c..056afaea 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/features/script/Hotkeys.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/features/script/custom/hotkey/Hotkeys.java @@ -17,7 +17,7 @@ * along with this program. If not, see . */ -package de.steamwar.bausystem.features.script; +package de.steamwar.bausystem.features.script.custom.hotkey; import java.util.HashSet; import java.util.Set; diff --git a/BauSystem_Main/src/de/steamwar/bausystem/features/script/custom/hotkey/InventoryHotkey.java b/BauSystem_Main/src/de/steamwar/bausystem/features/script/custom/hotkey/InventoryHotkey.java new file mode 100644 index 00000000..f467e460 --- /dev/null +++ b/BauSystem_Main/src/de/steamwar/bausystem/features/script/custom/hotkey/InventoryHotkey.java @@ -0,0 +1,51 @@ +/* + * 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.custom.hotkey; + +import de.steamwar.bausystem.features.script.ScriptExecutor; +import de.steamwar.bausystem.features.script.variables.Value; +import lombok.RequiredArgsConstructor; +import org.bukkit.entity.Player; +import org.bukkit.inventory.meta.BookMeta; + +import java.util.HashMap; +import java.util.Map; + +@RequiredArgsConstructor +public +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, null); + return false; + } +} diff --git a/BauSystem_Main/src/de/steamwar/bausystem/features/script/custom/hotkey/MenuHotkey.java b/BauSystem_Main/src/de/steamwar/bausystem/features/script/custom/hotkey/MenuHotkey.java new file mode 100644 index 00000000..0407f621 --- /dev/null +++ b/BauSystem_Main/src/de/steamwar/bausystem/features/script/custom/hotkey/MenuHotkey.java @@ -0,0 +1,80 @@ +/* + * 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.custom.hotkey; + +import de.steamwar.bausystem.BauSystem; +import de.steamwar.bausystem.features.script.ScriptExecutor; +import de.steamwar.bausystem.features.script.custom.MenuScript; +import de.steamwar.bausystem.features.script.variables.Value; +import de.steamwar.inventory.SWItem; +import lombok.RequiredArgsConstructor; +import org.bukkit.Material; +import org.bukkit.entity.Player; +import org.bukkit.inventory.meta.BookMeta; +import yapion.hierarchy.types.YAPIONArray; +import yapion.hierarchy.types.YAPIONMap; + +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +@RequiredArgsConstructor +public +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, null); + return false; + } + + @Override + public void save(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; + } +}