SteamWar/BauSystem
Archiviert
13
0
Add var
Add info Variable code
Add ExecutionPoints for limiting server performance
Dieser Commit ist enthalten in:
jojo 2020-12-21 15:39:57 +01:00
Ursprung 3ad8bc4a06
Commit 5022da13a8

Datei anzeigen

@ -22,9 +22,7 @@ package de.steamwar.bausystem.world;
import de.steamwar.bausystem.BauSystem; import de.steamwar.bausystem.BauSystem;
import de.steamwar.bausystem.commands.CommandFire; import de.steamwar.bausystem.commands.CommandFire;
import de.steamwar.bausystem.commands.CommandFreeze; import de.steamwar.bausystem.commands.CommandFreeze;
import de.steamwar.bausystem.commands.CommandNV;
import de.steamwar.bausystem.commands.CommandTNT; import de.steamwar.bausystem.commands.CommandTNT;
import de.steamwar.bausystem.tracer.TraceManager;
import de.steamwar.bausystem.tracer.recorder.RecordManager; import de.steamwar.bausystem.tracer.recorder.RecordManager;
import de.steamwar.core.Core; import de.steamwar.core.Core;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
@ -85,6 +83,7 @@ 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 boolean lastCommandWasSleep = false; private boolean lastCommandWasSleep = false;
private int index = 0; 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."); player.sendMessage(BauSystem.PREFIX + "§cBitte füge ein sleep in dein Script ein.");
return; return;
} }
if (executionPoints < 0) {
executionPoints = 0;
}
if (command.toLowerCase().startsWith("sleep")) { if (command.toLowerCase().startsWith("sleep")) {
if (!lastCommandWasSleep) {
executionPoints -= 2;
}
lastCommandWasSleep = true;
ScriptListener.sleepCommand(this, generateArgumentArray("sleep", command)); ScriptListener.sleepCommand(this, generateArgumentArray("sleep", command));
lastCommandWasSleep = true;
return; return;
} }
lastCommandWasSleep = false; lastCommandWasSleep = false;
@ -134,10 +133,12 @@ public class ScriptListener implements Listener {
return; return;
} }
if (command.toLowerCase().startsWith("jump")) { if (command.toLowerCase().startsWith("jump")) {
executionPoints += 2;
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 {
player.sendMessage(BauSystem.PREFIX + "§cUnbekannter Jump Punkt: " + command);
} }
continue; continue;
} }
@ -146,6 +147,19 @@ public class ScriptListener implements Listener {
ScriptListener.infoCommand(this, generateArgumentArray("info", command)); 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);
Bukkit.getServer().getPluginManager().callEvent(preprocessEvent); 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."); 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);
} }
private static int jumpCommand(ScriptExecutor scriptExecutor, String[] args) { private static int jumpCommand(ScriptExecutor scriptExecutor, String[] args) {
if (args.length < 1) { if (args.length < 1) {
scriptExecutor.player.sendMessage(BauSystem.PREFIX + "§cEin Jump Punkt muss angegeben sein.");
return -1; return -1;
} }
return scriptExecutor.jumpPoints.getOrDefault(args[0], -1); return scriptExecutor.jumpPoints.getOrDefault(args[0], -1);
} }
private static void infoCommand(ScriptExecutor scriptExecutor, String[] args) { 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))); 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<String, Integer> variables = new HashMap<>(); private Map<String, Integer> variables = new HashMap<>();
public void setValue(String key, String value) { public void setValue(String key, String value) {
int internalValue = 0; variables.put(key, parseValue(value));
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;
} }
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) { public int getValue(String key) {
@ -225,6 +296,31 @@ public class ScriptListener implements Listener {
return variables.getOrDefault(key, 0); 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;
}
}
} }
} }