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 List<String> commands = new ArrayList<>();
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 executionPoints = 0;
public ScriptExecutor(BookMeta bookMeta, Player player) {
this.player = player;
@ -114,49 +112,34 @@ public class ScriptListener implements Listener {
while (index < commands.size()) {
String command = commands.get(index++);
executionPoints++;
if (executionPoints > 200) {
player.sendMessage(BauSystem.PREFIX + "§cBitte füge ein sleep in dein Script ein.");
return;
String firstArg = command;
if (command.contains(" ")) {
firstArg = command.substring(0, command.indexOf(' '));
}
if (executionPoints < 0) {
executionPoints = 0;
}
if (command.toLowerCase().startsWith("sleep")) {
switch (firstArg.toLowerCase()) {
case "sleep":
ScriptListener.sleepCommand(this, generateArgumentArray("sleep", command));
lastCommandWasSleep = true;
return;
}
lastCommandWasSleep = false;
if (command.toLowerCase().startsWith("exit")) {
case "exit":
return;
}
if (command.toLowerCase().startsWith("jump")) {
case "jump":
int jumpIndex = ScriptListener.jumpCommand(this, generateArgumentArray("jump", command));
if (jumpIndex != -1) {
executionPoints += 2;
index = jumpIndex;
} else {
player.sendMessage(BauSystem.PREFIX + "§cUnbekannter Jump Punkt: " + command);
}
continue;
}
if (command.toLowerCase().startsWith("info")) {
executionPoints -= 1;
case "info":
ScriptListener.infoCommand(this, generateArgumentArray("info", command));
continue;
}
if (command.toLowerCase().startsWith("var")) {
executionPoints -= 1;
case "var":
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;
case "if":
int ifJumpIndex = ScriptListener.ifCommand(this, generateArgumentArray("if", command));
if (ifJumpIndex != -1) {
index = ifJumpIndex;
}
continue;
}
@ -191,9 +174,6 @@ public class ScriptListener implements Listener {
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);
}
@ -207,8 +187,8 @@ public class ScriptListener implements Listener {
private static void infoCommand(ScriptExecutor scriptExecutor, String[] args) {
for (int i = 0; i < args.length; i++) {
if (args[i].startsWith("$") && scriptExecutor.variableHolder.isVariable(args[i].substring(1))) {
args[i] = scriptExecutor.variableHolder.getValue(args[i].substring(1)) + "";
if (args[i].startsWith("$") && isVariable(scriptExecutor, args[i].substring(1))) {
args[i] = getValue(scriptExecutor, args[i].substring(1)) + "";
}
}
scriptExecutor.player.sendMessage("§eInfo§8» §7" + ChatColor.translateAlternateColorCodes('&', String.join(" ", args)));
@ -227,15 +207,15 @@ public class ScriptListener implements Listener {
case "inc":
case "increment":
case "++":
scriptExecutor.variableHolder.add(args[0], 1);
add(scriptExecutor, args[0], 1);
return;
case "dec":
case "decrement":
case "--":
scriptExecutor.variableHolder.add(args[0], -1);
add(scriptExecutor, args[0], -1);
return;
}
scriptExecutor.variableHolder.setValue(args[0], args[1]);
setValue(scriptExecutor, args[0], args[1]);
}
private static int ifCommand(ScriptExecutor scriptExecutor, String[] args) {
@ -249,15 +229,15 @@ public class ScriptListener implements Listener {
int firstValue;
int secondValue;
if (scriptExecutor.variableHolder.isVariable(args[0])) {
firstValue = scriptExecutor.variableHolder.getValue(args[0]);
if (isVariable(scriptExecutor, args[0])) {
firstValue = getValue(scriptExecutor, args[0]);
} else {
firstValue = scriptExecutor.variableHolder.parseValue(args[0]);
firstValue = parseValue(args[0]);
}
if (scriptExecutor.variableHolder.isVariable(args[1])) {
secondValue = scriptExecutor.variableHolder.getValue(args[1]);
if (isVariable(scriptExecutor, args[1])) {
secondValue = getValue(scriptExecutor, args[1]);
} else {
secondValue = scriptExecutor.variableHolder.parseValue(args[1]);
secondValue = parseValue(args[1]);
}
if (firstValue == secondValue) {
@ -267,22 +247,18 @@ public class ScriptListener implements Listener {
}
}
private static class VariableHolder {
private Map<String, Integer> variables = new HashMap<>();
public void setValue(String key, String value) {
variables.put(key, parseValue(value));
private static void setValue(ScriptExecutor scriptExecutor, String key, String value) {
scriptExecutor.variables.put(key, parseValue(value));
}
public void add(String key, int value) {
if (!isVariable(key)) {
variables.put(key, 0);
private static void add(ScriptExecutor scriptExecutor, String key, int value) {
if (!isVariable(scriptExecutor, key)) {
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) {
case "trace":
return RecordManager.getStatus().isTracing() ? 1 : 0;
@ -293,10 +269,10 @@ public class ScriptListener implements Listener {
case "fire":
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) {
case "trace":
case "tnt":
@ -304,11 +280,11 @@ public class ScriptListener implements Listener {
case "fire":
return true;
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")) {
return 1;
} else if (value.equalsIgnoreCase("false") || value.equalsIgnoreCase("no")) {
@ -321,6 +297,4 @@ public class ScriptListener implements Listener {
}
}
}
}