SteamWar/BauSystem
Archiviert
13
0

Script Turing Completeness #152

Manuell gemergt
YoyoNow hat 14 Commits von ScriptBranches nach master 2021-01-09 20:40:17 +01:00 zusammengeführt
Nur Änderungen aus Commit a8e6edefb8 werden angezeigt - Alle Commits anzeigen

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,49 +112,34 @@ 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":
}
if (command.toLowerCase().startsWith("sleep")) {
ScriptListener.sleepCommand(this, generateArgumentArray("sleep", command)); ScriptListener.sleepCommand(this, generateArgumentArray("sleep", command));
lastCommandWasSleep = true;
return; return;
} case "exit":
lastCommandWasSleep = false;
if (command.toLowerCase().startsWith("exit")) {
return; return;
} case "jump":
if (command.toLowerCase().startsWith("jump")) {
int jumpIndex = ScriptListener.jumpCommand(this, generateArgumentArray("jump", command)); int jumpIndex = ScriptListener.jumpCommand(this, generateArgumentArray("jump", command));
if (jumpIndex != -1) { if (jumpIndex != -1) {
executionPoints += 2;
index = jumpIndex; index = jumpIndex;
} else { } else {
player.sendMessage(BauSystem.PREFIX + "§cUnbekannter Jump Punkt: " + command); player.sendMessage(BauSystem.PREFIX + "§cUnbekannter Jump Punkt: " + command);
} }
continue; continue;
} case "info":
if (command.toLowerCase().startsWith("info")) {
executionPoints -= 1;
ScriptListener.infoCommand(this, generateArgumentArray("info", command)); ScriptListener.infoCommand(this, generateArgumentArray("info", command));
continue; continue;
} case "var":
if (command.toLowerCase().startsWith("var")) {
executionPoints -= 1;
ScriptListener.variableCommand(this, generateArgumentArray("var", command)); ScriptListener.variableCommand(this, generateArgumentArray("var", command));
continue; continue;
} case "if":
if (command.toLowerCase().startsWith("if")) { int ifJumpIndex = ScriptListener.ifCommand(this, generateArgumentArray("if", command));
int jumpIndex = ScriptListener.ifCommand(this, generateArgumentArray("if", command)); if (ifJumpIndex != -1) {
if (jumpIndex != -1) { index = ifJumpIndex;
executionPoints += 2;
index = jumpIndex;
} }
continue; continue;
} }
@ -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,22 +247,18 @@ 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<>();
public void setValue(String key, String value) {
variables.put(key, parseValue(value));
} }
public void add(String key, int value) { private static void add(ScriptExecutor scriptExecutor, String key, int value) {
if (!isVariable(key)) { if (!isVariable(scriptExecutor, key)) {
variables.put(key, 0); scriptExecutor.variables.put(key, 0);
} }
variables.put(key, variables.get(key) + value); scriptExecutor.variables.put(key, scriptExecutor.variables.get(key) + value);
} }
public int getValue(String key) { private static int getValue(ScriptExecutor scriptExecutor, String key) {
switch (key) { switch (key) {
case "trace": case "trace":
return RecordManager.getStatus().isTracing() ? 1 : 0; return RecordManager.getStatus().isTracing() ? 1 : 0;
@ -293,10 +269,10 @@ public class ScriptListener implements Listener {
case "fire": case "fire":
return CommandFire.getInstance().isOn() ? 1 : 0; return CommandFire.getInstance().isOn() ? 1 : 0;
} }
return variables.getOrDefault(key, 0); return scriptExecutor.variables.getOrDefault(key, 0);
} }
public boolean isVariable(String key) { private static boolean isVariable(ScriptExecutor scriptExecutor, String key) {
switch (key) { switch (key) {
case "trace": case "trace":
case "tnt": case "tnt":
@ -304,11 +280,11 @@ public class ScriptListener implements Listener {
case "fire": case "fire":
return true; return true;
default: default:
return variables.containsKey(key); return scriptExecutor.variables.containsKey(key);
} }
} }
public int parseValue(String value) { private static int parseValue(String value) {
if (value.equalsIgnoreCase("true") || value.equalsIgnoreCase("yes")) { if (value.equalsIgnoreCase("true") || value.equalsIgnoreCase("yes")) {
return 1; return 1;
} else if (value.equalsIgnoreCase("false") || value.equalsIgnoreCase("no")) { } else if (value.equalsIgnoreCase("false") || value.equalsIgnoreCase("no")) {
@ -322,5 +298,3 @@ public class ScriptListener implements Listener {
} }
} }
}