From 1b22f9b895d3f72385c2569eec4efdb42ed08980 Mon Sep 17 00:00:00 2001 From: yoyosource Date: Mon, 20 Sep 2021 22:01:37 +0200 Subject: [PATCH] Implement new logic for CustomScript Signed-off-by: yoyosource --- .../features/script/CustomScript.java | 27 ++- .../features/script/CustomScriptListener.java | 206 +++++------------- .../features/script/ScriptCommand.java | 2 +- 3 files changed, 68 insertions(+), 167 deletions(-) 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 6c9eb5f7..f67c89e1 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/features/script/CustomScript.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/features/script/CustomScript.java @@ -26,7 +26,6 @@ import lombok.experimental.UtilityClass; import org.bukkit.Material; import org.bukkit.event.player.PlayerCommandPreprocessEvent; import org.bukkit.event.player.PlayerEvent; -import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.meta.BookMeta; import yapion.hierarchy.types.YAPIONArray; import yapion.hierarchy.types.YAPIONMap; @@ -43,7 +42,7 @@ public class CustomScript { public interface MenuScript { void toYAPION(YAPIONMap yapionMap); - ItemStack toItem(); + SWItem toItem(); } public interface CustomEvent extends Script { @@ -53,8 +52,8 @@ public class CustomScript { @RequiredArgsConstructor public class InventoryEvent implements CustomEvent { - private final BookMeta bookMeta; - private final String eventName; + public final BookMeta bookMeta; + public final String eventName; @Override public String eventName() { @@ -70,8 +69,8 @@ public class CustomScript { @RequiredArgsConstructor public class MenuEvent implements CustomEvent, MenuScript { - private final List pages; - private final String eventName; + public final List pages; + public final String eventName; @Override public String eventName() { @@ -92,12 +91,12 @@ public class CustomScript { } @Override - public ItemStack toItem() { + public SWItem toItem() { SWItem swItem = new SWItem(Material.WRITABLE_BOOK, "§7Event§8: §e" + eventName); BookMeta bookMeta = (BookMeta) swItem.getItemMeta(); bookMeta.setPages(pages.toArray(new String[0])); swItem.setItemMeta(bookMeta); - return swItem.getItemStack(); + return swItem; } } @@ -161,8 +160,8 @@ public class CustomScript { @RequiredArgsConstructor public class InventoryCommand implements CustomCommand { - private final BookMeta bookMeta; - private final String[] args; + public final BookMeta bookMeta; + public final String[] args; public boolean execute(String[] command, PlayerCommandPreprocessEvent e) { Map arguments = check(args, command); @@ -178,8 +177,8 @@ public class CustomScript { @RequiredArgsConstructor public class MenuCommand implements CustomCommand, MenuScript { - private final List pages; - private final String[] args; + public final List pages; + public final String[] args; public boolean execute(String[] command, PlayerCommandPreprocessEvent e) { Map arguments = check(args, command); @@ -200,12 +199,12 @@ public class CustomScript { } @Override - public ItemStack toItem() { + public SWItem toItem() { SWItem swItem = new SWItem(Material.WRITABLE_BOOK, "§8/§e" + String.join(" ", args)); BookMeta bookMeta = (BookMeta) swItem.getItemMeta(); bookMeta.setPages(pages.toArray(new String[0])); swItem.setItemMeta(bookMeta); - return swItem.getItemStack(); + return swItem; } } } diff --git a/BauSystem_Main/src/de/steamwar/bausystem/features/script/CustomScriptListener.java b/BauSystem_Main/src/de/steamwar/bausystem/features/script/CustomScriptListener.java index 82333c5a..eb6e0e52 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/features/script/CustomScriptListener.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/features/script/CustomScriptListener.java @@ -20,15 +20,12 @@ package de.steamwar.bausystem.features.script; import de.steamwar.bausystem.SWUtils; -import de.steamwar.bausystem.features.script.variables.Value; import de.steamwar.bausystem.linkage.LinkageType; import de.steamwar.bausystem.linkage.Linked; import de.steamwar.core.VersionedCallable; import de.steamwar.inventory.SWItem; import de.steamwar.inventory.SWListInv; import de.steamwar.sql.UserConfig; -import lombok.AccessLevel; -import lombok.RequiredArgsConstructor; import org.bukkit.Material; import org.bukkit.entity.Player; import org.bukkit.event.EventHandler; @@ -53,117 +50,11 @@ import java.util.stream.Collectors; @Linked(LinkageType.LISTENER) public class CustomScriptListener implements Listener { - private interface CustomCommand { - 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(access = AccessLevel.PRIVATE) - private static class InventoryCommand implements CustomCommand { - private final BookMeta bookMeta; - private final String[] 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); - return true; - } - } - - @RequiredArgsConstructor(access = AccessLevel.PRIVATE) - private static class MenuCommand implements CustomCommand { - private final List pages; - private final String[] 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); - return true; - } - - public void toYAPION(YAPIONMap yapionMap) { - YAPIONArray yapionArray = new YAPIONArray(); - pages.forEach(yapionArray::add); - yapionMap.put(String.join(" ", args), yapionArray); - } - - public ItemStack toItem() { - SWItem swItem = new SWItem(Material.WRITABLE_BOOK, String.join(" ", args)); - BookMeta bookMeta = (BookMeta) swItem.getItemMeta(); - bookMeta.setPages(pages.toArray(new String[0])); - swItem.setItemMeta(bookMeta); - return swItem.getItemStack(); - } - } - - private Map> playerMap = new HashMap<>(); + private Map> playerMap = new HashMap<>(); private void updateInventory(Player p) { playerMap.computeIfPresent(p, (player, customCommands) -> { - customCommands.removeIf(InventoryCommand.class::isInstance); + customCommands.removeIf(CustomScript.InventoryCommand.class::isInstance); return customCommands; }); for (ItemStack item : p.getInventory().getContents()) { @@ -179,10 +70,11 @@ public class CustomScriptListener implements Listener { continue; } String s = bookMeta.getPage(1).split("\n")[0]; - if (!s.startsWith("#!CMD /")) { - continue; + if (s.startsWith("#!CMD /")) { + playerMap.computeIfAbsent(p, player -> new ArrayList<>()).add(new CustomScript.InventoryCommand(bookMeta, s.substring(6).split(" "))); + } else if (s.startsWith("#!EVENT ")) { + // Event System } - playerMap.computeIfAbsent(p, player -> new ArrayList<>()).add(new InventoryCommand(bookMeta, s.substring(6).split(" "))); } } @@ -210,7 +102,13 @@ public class CustomScriptListener 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 MenuCommand(pages, command)); + playerMap.computeIfAbsent(p, player -> new ArrayList<>()).add(new CustomScript.MenuCommand(pages, command)); + }); + + yapionObject.getYAPIONMapOrSetDefault("events", new YAPIONMap()).forEach((key, value) -> { + String eventName = ((YAPIONValue) key).get(); + 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, eventName)); }); } @@ -223,10 +121,15 @@ public class CustomScriptListener implements Listener { private YAPIONObject output(Player p) { if (!playerMap.containsKey(p)) return new YAPIONObject(); YAPIONObject yapionObject = new YAPIONObject(); - YAPIONMap yapionMap = new YAPIONMap(); - yapionObject.add("commands", yapionMap); - playerMap.get(p).stream().filter(MenuCommand.class::isInstance).map(MenuCommand.class::cast).forEach(menuCommand -> { - menuCommand.toYAPION(yapionMap); + 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(commandsMap); }); return yapionObject; } @@ -258,13 +161,9 @@ public class CustomScriptListener implements Listener { return; } - List inventoryCommands = playerMap.get(e.getPlayer()); - if (inventoryCommands == null) { - 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 (CustomCommand customCommand : inventoryCommands) { + for (CustomScript.CustomCommand customCommand : customCommands) { if (customCommand.execute(command, e)) { return; } @@ -276,13 +175,14 @@ public class CustomScriptListener implements Listener { } public void openCommandsMenu(Player p) { - List> menuCommands = new ArrayList<>(); - playerMap.getOrDefault(p, new ArrayList<>()).stream().filter(MenuCommand.class::isInstance).map(MenuCommand.class::cast).forEach(menuCommand -> { - String command = "§e" + String.join(" ", menuCommand.args); + List> menuCommands = new ArrayList<>(); + playerMap.getOrDefault(p, new ArrayList<>()).stream().filter(CustomScript.MenuScript.class::isInstance).map(CustomScript.MenuScript.class::cast).forEach(menuItem -> { + SWItem swItem = menuItem.toItem(); + swItem.setLore(Arrays.asList("§7Klicke zum rausnehmen", "§7Middle Klicke zum kopieren")); - menuCommands.add(new SWListInv.SWListEntry<>(new SWItem(Material.BOOK, command, Arrays.asList("§7Klicke zum rausnehmen", "§7Middle Klicke zum kopieren"), false, clickType -> { - }), menuCommand)); + menuCommands.add(new SWListInv.SWListEntry<>(swItem, menuItem)); }); + int length = (int) output(p).toYAPION(new LengthOutput()).getLength(); StringBuilder menuName = new StringBuilder(); menuName.append("§eScript Commands "); @@ -295,11 +195,12 @@ public class CustomScriptListener implements Listener { menuName.append("§a"); } menuName.append(percentage).append("§7%"); - SWListInv menuCommandSWListInv = new SWListInv<>(p, menuName.toString(), false, menuCommands, (clickType, menuCommand) -> { + + SWListInv menuCommandSWListInv = new SWListInv<>(p, menuName.toString(), false, menuCommands, (clickType, menuCommand) -> { if (!clickType.isCreativeAction()) { playerMap.get(p).removeIf(customCommand -> customCommand == menuCommand); } - SWUtils.giveItemToPlayer(p, menuCommand.toItem()); + SWUtils.giveItemToPlayer(p, menuCommand.toItem().getItemStack()); p.closeInventory(); save(p); }); @@ -320,30 +221,31 @@ public class CustomScriptListener implements Listener { return; } String s = bookMeta.getPage(1).split("\n")[0]; - if (!s.startsWith("#!CMD /")) { - return; - } - MenuCommand menuCommand = new MenuCommand(bookMeta.getPages(), s.substring(6).split(" ")); - for (CustomCommand customCommand : playerMap.computeIfAbsent(p, player -> new ArrayList<>())) { - if (!(customCommand instanceof MenuCommand)) { - continue; + 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)) { + continue; + } + if (Arrays.equals(((CustomScript.MenuCommand) script).args, menuCommand.args)) { + p.sendMessage("§cCommand '" + (String.join(" ", menuCommand.args)) + "' bereits definiert"); + return; + } } - if (Arrays.equals(((MenuCommand) customCommand).args, menuCommand.args)) { - p.sendMessage("§cCommand '" + (String.join(" ", menuCommand.args)) + "' bereits definiert"); + playerMap.computeIfAbsent(p, player -> new ArrayList<>()).add(menuCommand); + if (!save(p)) { + playerMap.get(p).removeIf(customCommand -> customCommand == menuCommand); + p.closeInventory(); + SWUtils.giveItemToPlayer(p, p.getItemOnCursor()); + save(p); + p.sendMessage("§cScript-Buch Limit erreicht"); return; } + p.setItemOnCursor(null); + openCommandsMenu(p); + } else if (s.startsWith("#!EVENT ")) { + // Event stuff } - playerMap.computeIfAbsent(p, player -> new ArrayList<>()).add(menuCommand); - if (!save(p)) { - playerMap.get(p).removeIf(customCommand -> customCommand == menuCommand); - p.closeInventory(); - SWUtils.giveItemToPlayer(p, p.getItemOnCursor()); - save(p); - p.sendMessage("§cScript-Buch Limit erreicht"); - return; - } - p.setItemOnCursor(null); - openCommandsMenu(p); })); menuCommandSWListInv.open(); } 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 156be531..5a394436 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/features/script/ScriptCommand.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/features/script/ScriptCommand.java @@ -43,7 +43,7 @@ public class ScriptCommand extends SWCommand { } swItems.add(new SWListInv.SWListEntry<>(new SWItem(Material.BOOK, "§eCustom Commands", Arrays.asList("§7Schreibe§8: §7#!CMD 'COMMAND'", "§7an den Anfang eines Script Buches um", "§7ein Custom Command zu nutzen. Der", "§7Befehl startet immer mit / und kann dann so", "§7aufgebaut sein wie du willst. Alles was in Spitzen", "§7Klammern steht (<>) wird als Parameter und somit", "§7als Variable gewertet.", "§7Parameter, welche in runden Klammern", "§7stehen sind Optional. Einfache", "§7Texte als Parameter bekommen", "§7eine gleichnamige Variable mit", "§7true/false als Wert je nachdem", "§7ob dieser angegeben wurde oder nicht"), false, clickType -> { }), null)); - swItems.add(new SWListInv.SWListEntry<>(new SWItem(Material.GRAY_STAINED_GLASS_PANE, "§7", new ArrayList<>(), false, clickType -> { + swItems.add(new SWListInv.SWListEntry<>(new SWItem(Material.BOOK, "§eCustom Events", Arrays.asList("§7Schreibe§8: §7#!EVENT 'EventName'", "§7an den Anfang eines Script Buches um", "§7ein Custom Event zu nutzen.", "§7Nutzbare Events sind:", "§8-§7 FF"), false, clickType -> { }), null)); swItems.add(new SWListInv.SWListEntry<>(new SWItem(Material.BOOK, "§eOther", Arrays.asList("§7Kommentare fangen mit §e#§7 an.", "§7Jump_Points fangen mit §e.§7 an.", "§7Eine Variablen Namen in '<>'", "§7eingeschlossen wird ersetzt, bis zu zwei mal.", "§7Eine Variable in '<>' kann mit 'const.'", "§7oder 'local.' oder 'global.' prefixed werden", "§7für den genauen type wo nach geguckt werden soll"), false, clickType -> { }), null));