Simplify ScriptListener
Dieser Commit ist enthalten in:
Ursprung
5022da13a8
Commit
a8e6edefb8
@ -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,51 +112,36 @@ 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")) {
|
||||
ScriptListener.sleepCommand(this, generateArgumentArray("sleep", command));
|
||||
lastCommandWasSleep = true;
|
||||
return;
|
||||
}
|
||||
lastCommandWasSleep = false;
|
||||
if (command.toLowerCase().startsWith("exit")) {
|
||||
return;
|
||||
}
|
||||
if (command.toLowerCase().startsWith("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;
|
||||
ScriptListener.infoCommand(this, generateArgumentArray("info", command));
|
||||
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;
|
||||
switch (firstArg.toLowerCase()) {
|
||||
case "sleep":
|
||||
ScriptListener.sleepCommand(this, generateArgumentArray("sleep", command));
|
||||
return;
|
||||
case "exit":
|
||||
return;
|
||||
case "jump":
|
||||
int jumpIndex = ScriptListener.jumpCommand(this, generateArgumentArray("jump", command));
|
||||
if (jumpIndex != -1) {
|
||||
index = jumpIndex;
|
||||
} else {
|
||||
player.sendMessage(BauSystem.PREFIX + "§cUnbekannter Jump Punkt: " + command);
|
||||
}
|
||||
continue;
|
||||
case "info":
|
||||
ScriptListener.infoCommand(this, generateArgumentArray("info", command));
|
||||
continue;
|
||||
case "var":
|
||||
ScriptListener.variableCommand(this, generateArgumentArray("var", command));
|
||||
continue;
|
||||
case "if":
|
||||
int ifJumpIndex = ScriptListener.ifCommand(this, generateArgumentArray("if", command));
|
||||
if (ifJumpIndex != -1) {
|
||||
index = ifJumpIndex;
|
||||
}
|
||||
continue;
|
||||
}
|
||||
|
||||
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.");
|
||||
}
|
||||
}
|
||||
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,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<>();
|
||||
|
||||
public void setValue(String key, String value) {
|
||||
variables.put(key, parseValue(value));
|
||||
private static void add(ScriptExecutor scriptExecutor, String key, int value) {
|
||||
if (!isVariable(scriptExecutor, key)) {
|
||||
scriptExecutor.variables.put(key, 0);
|
||||
}
|
||||
scriptExecutor.variables.put(key, scriptExecutor.variables.get(key) + value);
|
||||
}
|
||||
|
||||
public void add(String key, int value) {
|
||||
if (!isVariable(key)) {
|
||||
variables.put(key, 0);
|
||||
}
|
||||
variables.put(key, variables.get(key) + value);
|
||||
private static int getValue(ScriptExecutor scriptExecutor, String key) {
|
||||
switch (key) {
|
||||
case "trace":
|
||||
return RecordManager.getStatus().isTracing() ? 1 : 0;
|
||||
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) {
|
||||
switch (key) {
|
||||
case "trace":
|
||||
return RecordManager.getStatus().isTracing() ? 1 : 0;
|
||||
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 variables.getOrDefault(key, 0);
|
||||
private static boolean isVariable(ScriptExecutor scriptExecutor, String key) {
|
||||
switch (key) {
|
||||
case "trace":
|
||||
case "tnt":
|
||||
case "freeze":
|
||||
case "fire":
|
||||
return true;
|
||||
default:
|
||||
return scriptExecutor.variables.containsKey(key);
|
||||
}
|
||||
}
|
||||
|
||||
public boolean isVariable(String key) {
|
||||
switch (key) {
|
||||
case "trace":
|
||||
case "tnt":
|
||||
case "freeze":
|
||||
case "fire":
|
||||
return true;
|
||||
default:
|
||||
return variables.containsKey(key);
|
||||
}
|
||||
private static int parseValue(String value) {
|
||||
if (value.equalsIgnoreCase("true") || value.equalsIgnoreCase("yes")) {
|
||||
return 1;
|
||||
} else if (value.equalsIgnoreCase("false") || value.equalsIgnoreCase("no")) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
public int parseValue(String value) {
|
||||
if (value.equalsIgnoreCase("true") || value.equalsIgnoreCase("yes")) {
|
||||
return 1;
|
||||
} else if (value.equalsIgnoreCase("false") || value.equalsIgnoreCase("no")) {
|
||||
return 0;
|
||||
}
|
||||
try {
|
||||
return Integer.parseInt(value);
|
||||
} catch (NumberFormatException e) {
|
||||
return 0;
|
||||
}
|
||||
try {
|
||||
return Integer.parseInt(value);
|
||||
} catch (NumberFormatException e) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
In neuem Issue referenzieren
Einen Benutzer sperren