From 9279f885cbba4ce533dabd8c020160dc19f81f13 Mon Sep 17 00:00:00 2001 From: yoyosource Date: Fri, 27 Aug 2021 23:39:02 +0200 Subject: [PATCH] Update CustomCommandListener Fix ScriptExecutor Signed-off-by: yoyosource --- .../script/CustomCommandListener.java | 51 +++++++++++++++---- .../features/script/ScriptCommand.java | 2 +- .../features/script/ScriptExecutor.java | 14 ++++- .../script/command/arithmetic/Add.java | 4 +- .../script/command/arithmetic/Div.java | 4 +- .../script/command/arithmetic/Mul.java | 4 +- .../script/command/arithmetic/Sub.java | 4 +- .../features/script/command/variable/Var.java | 12 +---- 8 files changed, 64 insertions(+), 31 deletions(-) diff --git a/BauSystem_Main/src/de/steamwar/bausystem/features/script/CustomCommandListener.java b/BauSystem_Main/src/de/steamwar/bausystem/features/script/CustomCommandListener.java index 69702fe4..4945a789 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/features/script/CustomCommandListener.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/features/script/CustomCommandListener.java @@ -55,7 +55,7 @@ public class CustomCommandListener implements Listener { private interface CustomCommand { default Map check(String[] args, String[] command) { - if (args.length != command.length) { + if (args.length < command.length) { return null; } @@ -64,19 +64,50 @@ public class CustomCommandListener implements Listener { } Map arguments = new HashMap<>(); - for (int i = 1; i < args.length; i++) { - String current = args[i]; - if (current.startsWith("<") && current.endsWith(">")) { - arguments.put(current.substring(1, current.length() - 1), new Value.StringValue(command[i])); - } else { - if (!current.equals(command[i])) { - return null; - } - } + 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/ScriptCommand.java b/BauSystem_Main/src/de/steamwar/bausystem/features/script/ScriptCommand.java index d685d468..f2828e72 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/features/script/ScriptCommand.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/features/script/ScriptCommand.java @@ -41,7 +41,7 @@ public class ScriptCommand extends SWCommand { swItems.add(new SWListInv.SWListEntry<>(new SWItem(Material.GRAY_STAINED_GLASS_PANE, "§7", new ArrayList<>(), false, clickType -> { }), null)); } - 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."), false, clickType -> { + 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 -> { }), null)); diff --git a/BauSystem_Main/src/de/steamwar/bausystem/features/script/ScriptExecutor.java b/BauSystem_Main/src/de/steamwar/bausystem/features/script/ScriptExecutor.java index 939fae0f..46529054 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/features/script/ScriptExecutor.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/features/script/ScriptExecutor.java @@ -164,10 +164,22 @@ public final class ScriptExecutor { } return s.split(" "); } + + public Value parse(String varValue) { + try { + return new Value.LongValue(Long.parseLong(varValue)); + } catch (NumberFormatException e) { + if (varValue.equalsIgnoreCase("true") || varValue.equalsIgnoreCase("false")) { + return new Value.BooleanValue(varValue.equalsIgnoreCase("true")); + } else { + return new Value.StringValue(varValue); + } + } + } public Value getOrItselfValue(String variable) { if (!isVariable(variable)) { - return new Value.StringValue(variable); + return parse(variable); } if (Constants.isConstant(variable)) { diff --git a/BauSystem_Main/src/de/steamwar/bausystem/features/script/command/arithmetic/Add.java b/BauSystem_Main/src/de/steamwar/bausystem/features/script/command/arithmetic/Add.java index b8103f8f..47b4e2d3 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/features/script/command/arithmetic/Add.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/features/script/command/arithmetic/Add.java @@ -14,8 +14,8 @@ public class Add implements SpecialCommand { @Override public String[] description() { return new String[]{ - "§eadd §8<§7Variable§8> §8<§7Variable§8>", - "§eadd §8<§7Variable§8> §8<§7Variable§8> §8<§7Variable§8>", + "§eadd §8<§7Variable§8> §8<§7Variable§8|§7Wert§8>", + "§eadd §8<§7Variable§8> §8<§7Variable§8§8|§7Wert> §8<§7Variable§8|§7Wert§8>", "", "Addition zwischen den letzten beiden Variablen und schreibt es in die erste." }; diff --git a/BauSystem_Main/src/de/steamwar/bausystem/features/script/command/arithmetic/Div.java b/BauSystem_Main/src/de/steamwar/bausystem/features/script/command/arithmetic/Div.java index 8049b627..17e26444 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/features/script/command/arithmetic/Div.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/features/script/command/arithmetic/Div.java @@ -14,8 +14,8 @@ public class Div implements SpecialCommand { @Override public String[] description() { return new String[]{ - "§ediv §8<§7Variable§8> §8<§7Variable§8>", - "§ediv §8<§7Variable§8> §8<§7Variable§8> §8<§7Variable§8>", + "§ediv §8<§7Variable§8> §8<§7Variable§8|§7Wert§8>", + "§ediv §8<§7Variable§8> §8<§7Variable§8|§7Wert§8> §8<§7Variable§8|§7Wert§8>", "", "Division zwischen den letzten beiden Variablen und schreibt es in die erste." }; diff --git a/BauSystem_Main/src/de/steamwar/bausystem/features/script/command/arithmetic/Mul.java b/BauSystem_Main/src/de/steamwar/bausystem/features/script/command/arithmetic/Mul.java index 59bd9072..8844a282 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/features/script/command/arithmetic/Mul.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/features/script/command/arithmetic/Mul.java @@ -14,8 +14,8 @@ public class Mul implements SpecialCommand { @Override public String[] description() { return new String[]{ - "§emul §8<§7Variable§8> §8<§7Variable§8>", - "§emul §8<§7Variable§8> §8<§7Variable§8> §8<§7Variable§8>", + "§emul §8<§7Variable§8> §8<§7Variable§8|§7Wert§8>", + "§emul §8<§7Variable§8> §8<§7Variable§8|§7Wert§8> §8<§7Variable§8|§7Wert§8>", "", "Multiplikation zwischen den letzten beiden Variablen und schreibt es in die erste." }; diff --git a/BauSystem_Main/src/de/steamwar/bausystem/features/script/command/arithmetic/Sub.java b/BauSystem_Main/src/de/steamwar/bausystem/features/script/command/arithmetic/Sub.java index 06d4345f..036de18b 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/features/script/command/arithmetic/Sub.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/features/script/command/arithmetic/Sub.java @@ -14,8 +14,8 @@ public class Sub implements SpecialCommand { @Override public String[] description() { return new String[]{ - "§esub §8<§7Variable§8> §8<§7Variable§8>", - "§esub §8<§7Variable§8> §8<§7Variable§8> §8<§7Variable§8>", + "§esub §8<§7Variable§8> §8<§7Variable§8|§7Wert§8>", + "§esub §8<§7Variable§8> §8<§7Variable§8|§7Wert§8> §8<§7Variable§8|§7Wert§8>", "", "Subtraktion zwischen den letzten beiden Variablen und schreibt es in die erste." }; diff --git a/BauSystem_Main/src/de/steamwar/bausystem/features/script/command/variable/Var.java b/BauSystem_Main/src/de/steamwar/bausystem/features/script/command/variable/Var.java index 5204940c..e8dc2cb5 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/features/script/command/variable/Var.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/features/script/command/variable/Var.java @@ -48,17 +48,7 @@ public class Var implements SpecialCommand { } varValue.append(command[i]); } - try { - long value = Long.parseLong(varValue.toString()); - scriptExecutor.getLocalVariables().putValue(varName, new Value.LongValue(value)); - } catch (NumberFormatException e) { - String s = varValue.toString(); - if (s.equalsIgnoreCase("true") || s.equalsIgnoreCase("false")) { - scriptExecutor.getLocalVariables().putValue(varName, new Value.BooleanValue(s.equalsIgnoreCase("true"))); - } else { - scriptExecutor.getLocalVariables().putValue(varName, new Value.StringValue(s)); - } - } + scriptExecutor.getLocalVariables().putValue(varName, scriptExecutor.parse(varValue.toString())); return true; } }