From 5022da13a80f1210f2ec5e7246768d030eae61c7 Mon Sep 17 00:00:00 2001 From: jojo Date: Mon, 21 Dec 2020 15:39:57 +0100 Subject: [PATCH] Add if Add var Add info Variable code Add ExecutionPoints for limiting server performance --- .../bausystem/world/ScriptListener.java | 136 +++++++++++++++--- 1 file changed, 116 insertions(+), 20 deletions(-) diff --git a/BauSystem_Main/src/de/steamwar/bausystem/world/ScriptListener.java b/BauSystem_Main/src/de/steamwar/bausystem/world/ScriptListener.java index 23b21b5..6a84704 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/world/ScriptListener.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/world/ScriptListener.java @@ -22,9 +22,7 @@ package de.steamwar.bausystem.world; import de.steamwar.bausystem.BauSystem; import de.steamwar.bausystem.commands.CommandFire; import de.steamwar.bausystem.commands.CommandFreeze; -import de.steamwar.bausystem.commands.CommandNV; import de.steamwar.bausystem.commands.CommandTNT; -import de.steamwar.bausystem.tracer.TraceManager; import de.steamwar.bausystem.tracer.recorder.RecordManager; import de.steamwar.core.Core; import org.bukkit.Bukkit; @@ -85,6 +83,7 @@ public class ScriptListener implements Listener { private final Player player; private final List commands = new ArrayList<>(); private final Map jumpPoints = new HashMap<>(); + private final VariableHolder variableHolder = new VariableHolder(); private boolean lastCommandWasSleep = false; private int index = 0; @@ -120,13 +119,13 @@ public class ScriptListener implements Listener { player.sendMessage(BauSystem.PREFIX + "§cBitte füge ein sleep in dein Script ein."); return; } + if (executionPoints < 0) { + executionPoints = 0; + } if (command.toLowerCase().startsWith("sleep")) { - if (!lastCommandWasSleep) { - executionPoints -= 2; - } - lastCommandWasSleep = true; ScriptListener.sleepCommand(this, generateArgumentArray("sleep", command)); + lastCommandWasSleep = true; return; } lastCommandWasSleep = false; @@ -134,10 +133,12 @@ public class ScriptListener implements Listener { return; } if (command.toLowerCase().startsWith("jump")) { - executionPoints += 2; 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; } @@ -146,6 +147,19 @@ public class ScriptListener implements Listener { 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; + } PlayerCommandPreprocessEvent preprocessEvent = new PlayerCommandPreprocessEvent(player, "/" + command); Bukkit.getServer().getPluginManager().callEvent(preprocessEvent); @@ -177,38 +191,95 @@ 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); } private static int jumpCommand(ScriptExecutor scriptExecutor, String[] args) { if (args.length < 1) { + scriptExecutor.player.sendMessage(BauSystem.PREFIX + "§cEin Jump Punkt muss angegeben sein."); return -1; } return scriptExecutor.jumpPoints.getOrDefault(args[0], -1); } 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)) + ""; + } + } scriptExecutor.player.sendMessage("§eInfo§8» §7" + ChatColor.translateAlternateColorCodes('&', String.join(" ", args))); } - private class VariableHolder { + private static void variableCommand(ScriptExecutor scriptExecutor, String[] args) { + if (args.length < 1) { + scriptExecutor.player.sendMessage(BauSystem.PREFIX + "§cVariablen Namen und Zahlen/Boolsche Wert fehlt."); + return; + } + if (args.length < 2) { + scriptExecutor.player.sendMessage(BauSystem.PREFIX + "§cZahlen/Boolsche Wert fehlt."); + return; + } + switch (args[1]) { + case "inc": + case "increment": + case "++": + scriptExecutor.variableHolder.add(args[0], 1); + return; + case "dec": + case "decrement": + case "--": + scriptExecutor.variableHolder.add(args[0], -1); + return; + } + scriptExecutor.variableHolder.setValue(args[0], args[1]); + } + + private static int ifCommand(ScriptExecutor scriptExecutor, String[] args) { + if (args.length < 2) { + scriptExecutor.player.sendMessage(BauSystem.PREFIX + "§cDie ersten beiden Argumente sind Zahlen/Boolsche Wertde oder Variablen."); + return -1; + } + + int jumpTruePoint = scriptExecutor.jumpPoints.getOrDefault(args[2], -1); + int jumpFalsePoint = args.length > 3 ? scriptExecutor.jumpPoints.getOrDefault(args[3], -1) : -1; + + int firstValue; + int secondValue; + if (scriptExecutor.variableHolder.isVariable(args[0])) { + firstValue = scriptExecutor.variableHolder.getValue(args[0]); + } else { + firstValue = scriptExecutor.variableHolder.parseValue(args[0]); + } + if (scriptExecutor.variableHolder.isVariable(args[1])) { + secondValue = scriptExecutor.variableHolder.getValue(args[1]); + } else { + secondValue = scriptExecutor.variableHolder.parseValue(args[1]); + } + + if (firstValue == secondValue) { + return jumpTruePoint; + } else { + return jumpFalsePoint; + } + } + + private static class VariableHolder { private Map variables = new HashMap<>(); public void setValue(String key, String value) { - int internalValue = 0; - if (value.equalsIgnoreCase("true") || value.equalsIgnoreCase("yes")) { - internalValue = 1; - } else if (value.equalsIgnoreCase("false") || value.equalsIgnoreCase("no")) { - internalValue = 0; - } else { - try { - internalValue = Integer.parseInt(value); - } catch (NumberFormatException e) { - internalValue = 0; - } + variables.put(key, parseValue(value)); + } + + public void add(String key, int value) { + if (!isVariable(key)) { + variables.put(key, 0); } - variables.put(key, internalValue); + variables.put(key, variables.get(key) + value); } public int getValue(String key) { @@ -225,6 +296,31 @@ public class ScriptListener implements Listener { return variables.getOrDefault(key, 0); } + public boolean isVariable(String key) { + switch (key) { + case "trace": + case "tnt": + case "freeze": + case "fire": + return true; + default: + return variables.containsKey(key); + } + } + + 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; + } + } + } }