SteamWar/BauSystem
Archiviert
13
0

Simplify ScriptListener

Dieser Commit ist enthalten in:
jojo 2020-12-29 20:28:57 +01:00
Ursprung 5022da13a8
Commit a8e6edefb8

Datei anzeigen

@ -83,11 +83,9 @@ public class ScriptListener implements Listener {
private final Player player; private final Player player;
private final List<String> commands = new ArrayList<>(); private final List<String> commands = new ArrayList<>();
private final Map<String, Integer> jumpPoints = new HashMap<>(); private final Map<String, Integer> jumpPoints = new HashMap<>();
private final VariableHolder variableHolder = new VariableHolder(); private Map<String, Integer> variables = new HashMap<>();
private boolean lastCommandWasSleep = false;
private int index = 0; private int index = 0;
private int executionPoints = 0;
public ScriptExecutor(BookMeta bookMeta, Player player) { public ScriptExecutor(BookMeta bookMeta, Player player) {
this.player = player; this.player = player;
@ -114,51 +112,36 @@ public class ScriptListener implements Listener {
while (index < commands.size()) { while (index < commands.size()) {
String command = commands.get(index++); String command = commands.get(index++);
executionPoints++; String firstArg = command;
if (executionPoints > 200) { if (command.contains(" ")) {
player.sendMessage(BauSystem.PREFIX + "§cBitte füge ein sleep in dein Script ein."); firstArg = command.substring(0, command.indexOf(' '));
return;
} }
if (executionPoints < 0) { switch (firstArg.toLowerCase()) {
executionPoints = 0; case "sleep":
} ScriptListener.sleepCommand(this, generateArgumentArray("sleep", command));
return;
if (command.toLowerCase().startsWith("sleep")) { case "exit":
ScriptListener.sleepCommand(this, generateArgumentArray("sleep", command)); return;
lastCommandWasSleep = true; case "jump":
return; int jumpIndex = ScriptListener.jumpCommand(this, generateArgumentArray("jump", command));
} if (jumpIndex != -1) {
lastCommandWasSleep = false; index = jumpIndex;
if (command.toLowerCase().startsWith("exit")) { } else {
return; player.sendMessage(BauSystem.PREFIX + "§cUnbekannter Jump Punkt: " + command);
} }
if (command.toLowerCase().startsWith("jump")) { continue;
int jumpIndex = ScriptListener.jumpCommand(this, generateArgumentArray("jump", command)); case "info":
if (jumpIndex != -1) { ScriptListener.infoCommand(this, generateArgumentArray("info", command));
executionPoints += 2; continue;
index = jumpIndex; case "var":
} else { ScriptListener.variableCommand(this, generateArgumentArray("var", command));
player.sendMessage(BauSystem.PREFIX + "§cUnbekannter Jump Punkt: " + command); continue;
} case "if":
continue; int ifJumpIndex = ScriptListener.ifCommand(this, generateArgumentArray("if", command));
} if (ifJumpIndex != -1) {
if (command.toLowerCase().startsWith("info")) { index = ifJumpIndex;
executionPoints -= 1; }
ScriptListener.infoCommand(this, generateArgumentArray("info", command)); continue;
continue;
}
if (command.toLowerCase().startsWith("var")) {
executionPoints -= 1;
ScriptListener.variableCommand(this, generateArgumentArray("var", command));
continue;
}
if (command.toLowerCase().startsWith("if")) {
int jumpIndex = ScriptListener.ifCommand(this, generateArgumentArray("if", command));
if (jumpIndex != -1) {
executionPoints += 2;
index = jumpIndex;
}
continue;
} }
PlayerCommandPreprocessEvent preprocessEvent = new PlayerCommandPreprocessEvent(player, "/" + command); PlayerCommandPreprocessEvent preprocessEvent = new PlayerCommandPreprocessEvent(player, "/" + command);
@ -191,9 +174,6 @@ public class ScriptListener implements Listener {
scriptExecutor.player.sendMessage(BauSystem.PREFIX + "§cDie Zeit darf nur aus Zahlen bestehen."); scriptExecutor.player.sendMessage(BauSystem.PREFIX + "§cDie Zeit darf nur aus Zahlen bestehen.");
} }
} }
if (!scriptExecutor.lastCommandWasSleep) {
scriptExecutor.executionPoints -= sleepTime - 1;
}
Bukkit.getScheduler().runTaskLater(BauSystem.getPlugin(), scriptExecutor::resume, sleepTime); Bukkit.getScheduler().runTaskLater(BauSystem.getPlugin(), scriptExecutor::resume, sleepTime);
} }
@ -207,8 +187,8 @@ public class ScriptListener implements Listener {
private static void infoCommand(ScriptExecutor scriptExecutor, String[] args) { private static void infoCommand(ScriptExecutor scriptExecutor, String[] args) {
for (int i = 0; i < args.length; i++) { for (int i = 0; i < args.length; i++) {
if (args[i].startsWith("$") && scriptExecutor.variableHolder.isVariable(args[i].substring(1))) { if (args[i].startsWith("$") && isVariable(scriptExecutor, args[i].substring(1))) {
args[i] = scriptExecutor.variableHolder.getValue(args[i].substring(1)) + ""; args[i] = getValue(scriptExecutor, args[i].substring(1)) + "";
} }
} }
scriptExecutor.player.sendMessage("§eInfo§8» §7" + ChatColor.translateAlternateColorCodes('&', String.join(" ", args))); scriptExecutor.player.sendMessage("§eInfo§8» §7" + ChatColor.translateAlternateColorCodes('&', String.join(" ", args)));
@ -227,15 +207,15 @@ public class ScriptListener implements Listener {
case "inc": case "inc":
case "increment": case "increment":
case "++": case "++":
scriptExecutor.variableHolder.add(args[0], 1); add(scriptExecutor, args[0], 1);
return; return;
case "dec": case "dec":
case "decrement": case "decrement":
case "--": case "--":
scriptExecutor.variableHolder.add(args[0], -1); add(scriptExecutor, args[0], -1);
return; return;
} }
scriptExecutor.variableHolder.setValue(args[0], args[1]); setValue(scriptExecutor, args[0], args[1]);
} }
private static int ifCommand(ScriptExecutor scriptExecutor, String[] args) { private static int ifCommand(ScriptExecutor scriptExecutor, String[] args) {
@ -249,15 +229,15 @@ public class ScriptListener implements Listener {
int firstValue; int firstValue;
int secondValue; int secondValue;
if (scriptExecutor.variableHolder.isVariable(args[0])) { if (isVariable(scriptExecutor, args[0])) {
firstValue = scriptExecutor.variableHolder.getValue(args[0]); firstValue = getValue(scriptExecutor, args[0]);
} else { } else {
firstValue = scriptExecutor.variableHolder.parseValue(args[0]); firstValue = parseValue(args[0]);
} }
if (scriptExecutor.variableHolder.isVariable(args[1])) { if (isVariable(scriptExecutor, args[1])) {
secondValue = scriptExecutor.variableHolder.getValue(args[1]); secondValue = getValue(scriptExecutor, args[1]);
} else { } else {
secondValue = scriptExecutor.variableHolder.parseValue(args[1]); secondValue = parseValue(args[1]);
} }
if (firstValue == secondValue) { if (firstValue == secondValue) {
@ -267,60 +247,54 @@ public class ScriptListener implements Listener {
} }
} }
private static class VariableHolder { private static void setValue(ScriptExecutor scriptExecutor, String key, String value) {
scriptExecutor.variables.put(key, parseValue(value));
}
private Map<String, Integer> variables = new HashMap<>(); private static void add(ScriptExecutor scriptExecutor, String key, int value) {
if (!isVariable(scriptExecutor, key)) {
public void setValue(String key, String value) { scriptExecutor.variables.put(key, 0);
variables.put(key, parseValue(value));
} }
scriptExecutor.variables.put(key, scriptExecutor.variables.get(key) + value);
}
public void add(String key, int value) { private static int getValue(ScriptExecutor scriptExecutor, String key) {
if (!isVariable(key)) { switch (key) {
variables.put(key, 0); case "trace":
} return RecordManager.getStatus().isTracing() ? 1 : 0;
variables.put(key, variables.get(key) + value); case "tnt":
return CommandTNT.getInstance().isOn() ? 1 : 0;
case "freeze":
return CommandFreeze.getInstance().isOn() ? 1 : 0;
case "fire":
return CommandFire.getInstance().isOn() ? 1 : 0;
} }
return scriptExecutor.variables.getOrDefault(key, 0);
}
public int getValue(String key) { private static boolean isVariable(ScriptExecutor scriptExecutor, String key) {
switch (key) { switch (key) {
case "trace": case "trace":
return RecordManager.getStatus().isTracing() ? 1 : 0; case "tnt":
case "tnt": case "freeze":
return CommandTNT.getInstance().isOn() ? 1 : 0; case "fire":
case "freeze": return true;
return CommandFreeze.getInstance().isOn() ? 1 : 0; default:
case "fire": return scriptExecutor.variables.containsKey(key);
return CommandFire.getInstance().isOn() ? 1 : 0;
}
return variables.getOrDefault(key, 0);
} }
}
public boolean isVariable(String key) { private static int parseValue(String value) {
switch (key) { if (value.equalsIgnoreCase("true") || value.equalsIgnoreCase("yes")) {
case "trace": return 1;
case "tnt": } else if (value.equalsIgnoreCase("false") || value.equalsIgnoreCase("no")) {
case "freeze": return 0;
case "fire":
return true;
default:
return variables.containsKey(key);
}
} }
try {
public int parseValue(String value) { return Integer.parseInt(value);
if (value.equalsIgnoreCase("true") || value.equalsIgnoreCase("yes")) { } catch (NumberFormatException e) {
return 1; return 0;
} else if (value.equalsIgnoreCase("false") || value.equalsIgnoreCase("no")) {
return 0;
}
try {
return Integer.parseInt(value);
} catch (NumberFormatException e) {
return 0;
}
} }
} }
} }