Update CustomCommandListener
Fix ScriptExecutor Signed-off-by: yoyosource <yoyosource@nidido.de>
Dieser Commit ist enthalten in:
Ursprung
deae5095b3
Commit
9279f885cb
@ -55,7 +55,7 @@ public class CustomCommandListener implements Listener {
|
|||||||
|
|
||||||
private interface CustomCommand {
|
private interface CustomCommand {
|
||||||
default Map<String, Value> check(String[] args, String[] command) {
|
default Map<String, Value> check(String[] args, String[] command) {
|
||||||
if (args.length != command.length) {
|
if (args.length < command.length) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -64,19 +64,50 @@ public class CustomCommandListener implements Listener {
|
|||||||
}
|
}
|
||||||
|
|
||||||
Map<String, Value> arguments = new HashMap<>();
|
Map<String, Value> arguments = new HashMap<>();
|
||||||
for (int i = 1; i < args.length; i++) {
|
if (!check(arguments, args, command, 0, 0)) {
|
||||||
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;
|
return null;
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
|
||||||
return arguments;
|
return arguments;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
default boolean check(Map<String, Value> 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);
|
boolean execute(String[] command, PlayerCommandPreprocessEvent e);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -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 -> {
|
swItems.add(new SWListInv.SWListEntry<>(new SWItem(Material.GRAY_STAINED_GLASS_PANE, "§7", new ArrayList<>(), false, clickType -> {
|
||||||
}), null));
|
}), 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));
|
}), 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.GRAY_STAINED_GLASS_PANE, "§7", new ArrayList<>(), false, clickType -> {
|
||||||
}), null));
|
}), null));
|
||||||
|
@ -165,9 +165,21 @@ public final class ScriptExecutor {
|
|||||||
return s.split(" ");
|
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) {
|
public Value getOrItselfValue(String variable) {
|
||||||
if (!isVariable(variable)) {
|
if (!isVariable(variable)) {
|
||||||
return new Value.StringValue(variable);
|
return parse(variable);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (Constants.isConstant(variable)) {
|
if (Constants.isConstant(variable)) {
|
||||||
|
@ -14,8 +14,8 @@ public class Add implements SpecialCommand {
|
|||||||
@Override
|
@Override
|
||||||
public String[] description() {
|
public String[] description() {
|
||||||
return new String[]{
|
return new String[]{
|
||||||
"§eadd §8<§7Variable§8> §8<§7Variable§8>",
|
"§eadd §8<§7Variable§8> §8<§7Variable§8|§7Wert§8>",
|
||||||
"§eadd §8<§7Variable§8> §8<§7Variable§8> §8<§7Variable§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."
|
"Addition zwischen den letzten beiden Variablen und schreibt es in die erste."
|
||||||
};
|
};
|
||||||
|
@ -14,8 +14,8 @@ public class Div implements SpecialCommand {
|
|||||||
@Override
|
@Override
|
||||||
public String[] description() {
|
public String[] description() {
|
||||||
return new String[]{
|
return new String[]{
|
||||||
"§ediv §8<§7Variable§8> §8<§7Variable§8>",
|
"§ediv §8<§7Variable§8> §8<§7Variable§8|§7Wert§8>",
|
||||||
"§ediv §8<§7Variable§8> §8<§7Variable§8> §8<§7Variable§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."
|
"Division zwischen den letzten beiden Variablen und schreibt es in die erste."
|
||||||
};
|
};
|
||||||
|
@ -14,8 +14,8 @@ public class Mul implements SpecialCommand {
|
|||||||
@Override
|
@Override
|
||||||
public String[] description() {
|
public String[] description() {
|
||||||
return new String[]{
|
return new String[]{
|
||||||
"§emul §8<§7Variable§8> §8<§7Variable§8>",
|
"§emul §8<§7Variable§8> §8<§7Variable§8|§7Wert§8>",
|
||||||
"§emul §8<§7Variable§8> §8<§7Variable§8> §8<§7Variable§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."
|
"Multiplikation zwischen den letzten beiden Variablen und schreibt es in die erste."
|
||||||
};
|
};
|
||||||
|
@ -14,8 +14,8 @@ public class Sub implements SpecialCommand {
|
|||||||
@Override
|
@Override
|
||||||
public String[] description() {
|
public String[] description() {
|
||||||
return new String[]{
|
return new String[]{
|
||||||
"§esub §8<§7Variable§8> §8<§7Variable§8>",
|
"§esub §8<§7Variable§8> §8<§7Variable§8|§7Wert§8>",
|
||||||
"§esub §8<§7Variable§8> §8<§7Variable§8> §8<§7Variable§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."
|
"Subtraktion zwischen den letzten beiden Variablen und schreibt es in die erste."
|
||||||
};
|
};
|
||||||
|
@ -48,17 +48,7 @@ public class Var implements SpecialCommand {
|
|||||||
}
|
}
|
||||||
varValue.append(command[i]);
|
varValue.append(command[i]);
|
||||||
}
|
}
|
||||||
try {
|
scriptExecutor.getLocalVariables().putValue(varName, scriptExecutor.parse(varValue.toString()));
|
||||||
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));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
In neuem Issue referenzieren
Einen Benutzer sperren