diff --git a/BauSystem_Main/src/de/steamwar/bausystem/BauSystem.java b/BauSystem_Main/src/de/steamwar/bausystem/BauSystem.java index dd1817f..d7920a5 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/BauSystem.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/BauSystem.java @@ -97,6 +97,8 @@ public class BauSystem extends JavaPlugin implements Listener { getCommand("detonator").setExecutor(new CommandDetonator()); getCommand("detonator").setTabCompleter(new CommandDetonatorTabCompleter()); getCommand("script").setExecutor(new CommandScript()); + getCommand("scriptvars").setExecutor(new CommandScriptVars()); + getCommand("scriptvars").setTabCompleter(new CommandScriptVarsTabCompleter()); getCommand("simulator").setExecutor(new CommandSimulator()); getCommand("simulator").setTabCompleter(new CommandSimulatorTabCompleter()); getCommand("gui").setExecutor(new CommandGUI()); diff --git a/BauSystem_Main/src/de/steamwar/bausystem/commands/CommandScript.java b/BauSystem_Main/src/de/steamwar/bausystem/commands/CommandScript.java index 95ddb92..e4db45b 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/commands/CommandScript.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/commands/CommandScript.java @@ -47,7 +47,10 @@ public class CommandScript implements CommandExecutor { pages.add("§6Variablen§8\n\nMit Variablen kann man sich Zahlen speichern. Man definiert diese mit 'var '.\n\nBeispiel:\n§9# Setze i zu 0\nvar i 0§8\n\nEs gibt einige spezial values. Dazu zählen"); pages.add("§8'true', 'yes', 'false' und 'no', welche für 1, 1, 0 und 0 stehen.\n\nMan kann eine Variable auch um einen erhöhen oder verkleinern. Hierfür schreibt man statt einer Zahl '++', 'inc' oder '--', 'dec'.\n\nBeispiel:\n§9var i ++"); pages.add("§8Variablen kann man referenzieren\ndurch '<' vor dem Variablennamen und '>' nach diesem. Diese kann man in jedem Befehl verwenden.\n\nBeispiel:\n§9# Stacked um 10\nvar stacks 10\n/stack "); + pages.add("§8Um Variablen über das Script ausführen zu speichern gibt es 'global' und 'unglobal' als Befehle. Der erste speichert eine locale Variable global und das zweite löscht eine globale wieder."); + pages.add("§8Des weiteren kann man Lokale Variablen mit 'unvar' löschen. Nach dem verlassen einer Welt werden alle Globalen Variablen gelöscht. Globale Variablen kann man mit '/scriptvars' einsehen"); pages.add("§6Konstanten§8\n\nNeben den variablen gibt es noch 5 Konstante Werte, welche nicht mit dem 'var' Befehl verändert werden können.\n\nDiese sind:\n- trace/autotrace\n- tnt\n- freeze\n- fire"); + pages.add("§8Des weiteren gibt es 3 weitere Variablen, welche explizit Spieler gebunden sind\n\nDiese sind:\n- x\n- y\n- z"); pages.add("§6Abfragen§8\n\nMit Abfragen kann man nur Gleichheit von 2 Werten überprüft werden. Hierfür verwendet man\n'if '.\nNach den zwei Werten kann man ein oder 2 Jump-Points schreiben\n'if [...] (JP)'."); pages.add("§8Ein Jump-Point ist eine Zeile Script, wohin man springen kann. Dieser wird mit einem '.' am Anfang der Zeile beschrieben und direkt danach der Jump-Point Namen ohne Leerzeichen.\n\nBeispiel:\n§9# Jump-Point X\n.X§8"); pages.add("§8Um zu einem Jump-Point ohne Abfrage zu springen kann man den\n'jump ' Befehl verwenden."); diff --git a/BauSystem_Main/src/de/steamwar/bausystem/commands/CommandScriptVars.java b/BauSystem_Main/src/de/steamwar/bausystem/commands/CommandScriptVars.java new file mode 100644 index 0000000..7c32e91 --- /dev/null +++ b/BauSystem_Main/src/de/steamwar/bausystem/commands/CommandScriptVars.java @@ -0,0 +1,67 @@ +package de.steamwar.bausystem.commands; + +import de.steamwar.bausystem.BauSystem; +import de.steamwar.bausystem.world.ScriptListener; +import org.bukkit.command.Command; +import org.bukkit.command.CommandExecutor; +import org.bukkit.command.CommandSender; +import org.bukkit.entity.Player; + +import java.util.HashMap; +import java.util.Map; + +public class CommandScriptVars implements CommandExecutor { + + @Override + public boolean onCommand(CommandSender sender, Command command, String s, String[] args) { + if (!(sender instanceof Player)) { + return false; + } + Player player = (Player) sender; + if (args.length == 0) { + Map globalVariables = ScriptListener.GLOBAL_VARIABLES.get(player); + if (globalVariables == null) { + player.sendMessage(BauSystem.PREFIX + "§cKeine globalen Variablen definiert"); + return false; + } + int i = 0; + player.sendMessage(BauSystem.PREFIX + globalVariables.size() + " Variable(n)"); + for (Map.Entry var : globalVariables.entrySet()) { + if (i++ >= 40) break; + player.sendMessage("- " + var.getKey() + "=" + var.getValue()); + } + return false; + } + String varName = args[0]; + if (args.length == 1) { + Map globalVariables = ScriptListener.GLOBAL_VARIABLES.get(player); + if (globalVariables == null) { + player.sendMessage(BauSystem.PREFIX + "§cKeine globalen Variablen definiert"); + return false; + } + if (!globalVariables.containsKey(varName)) { + player.sendMessage(BauSystem.PREFIX + "§cUnbekannte Variable"); + return false; + } + player.sendMessage(BauSystem.PREFIX + varName + "=" + globalVariables.get(varName)); + } + switch (args[2].toLowerCase()) { + case "delete": + case "clear": + case "remove": + if (!ScriptListener.GLOBAL_VARIABLES.containsKey(player)) { + player.sendMessage(BauSystem.PREFIX + "§cKeine globalen Variablen definiert"); + break; + } + ScriptListener.GLOBAL_VARIABLES.get(player).remove(varName); + player.sendMessage(BauSystem.PREFIX + "Variable " + varName + " gelöscht"); + break; + default: + int value = ScriptListener.parseValue(args[2]); + ScriptListener.GLOBAL_VARIABLES.computeIfAbsent(player, p -> new HashMap<>()).put(varName, value); + player.sendMessage(BauSystem.PREFIX + varName + " auf " + value + " gesetzt"); + } + return false; + } + +} diff --git a/BauSystem_Main/src/de/steamwar/bausystem/commands/CommandScriptVarsTabCompleter.java b/BauSystem_Main/src/de/steamwar/bausystem/commands/CommandScriptVarsTabCompleter.java new file mode 100644 index 0000000..f9fa6ef --- /dev/null +++ b/BauSystem_Main/src/de/steamwar/bausystem/commands/CommandScriptVarsTabCompleter.java @@ -0,0 +1,34 @@ +package de.steamwar.bausystem.commands; + +import de.steamwar.bausystem.SWUtils; +import de.steamwar.bausystem.world.ScriptListener; +import org.bukkit.command.Command; +import org.bukkit.command.CommandSender; +import org.bukkit.command.TabCompleter; +import org.bukkit.entity.Player; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.HashMap; +import java.util.List; + +public class CommandScriptVarsTabCompleter implements TabCompleter { + + @Override + public List onTabComplete(CommandSender sender, Command command, String s, String[] args) { + if (!(sender instanceof Player)) { + return new ArrayList<>(); + } + Player player = (Player) sender; + if (args.length == 1) { + List variables = new ArrayList<>(ScriptListener.GLOBAL_VARIABLES.getOrDefault(player, new HashMap<>()).keySet()); + return SWUtils.manageList(variables, args); + } + if (args.length != 2) { + return new ArrayList<>(); + } + int value = ScriptListener.GLOBAL_VARIABLES.getOrDefault(player, new HashMap<>()).getOrDefault(args[0], 0); + return SWUtils.manageList(Arrays.asList(value + "", "true", "false", "yes", "no", "delete", "clear", "remove"), args); + } + +} diff --git a/BauSystem_Main/src/de/steamwar/bausystem/world/ScriptListener.java b/BauSystem_Main/src/de/steamwar/bausystem/world/ScriptListener.java index f20994a..1f229dc 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/world/ScriptListener.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/world/ScriptListener.java @@ -34,16 +34,32 @@ import org.bukkit.event.Listener; import org.bukkit.event.block.Action; import org.bukkit.event.player.PlayerCommandPreprocessEvent; import org.bukkit.event.player.PlayerInteractEvent; +import org.bukkit.event.player.PlayerQuitEvent; import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.meta.BookMeta; import java.util.*; +import java.util.function.Function; import java.util.function.IntBinaryOperator; import java.util.function.IntUnaryOperator; import java.util.logging.Level; public class ScriptListener implements Listener { + public static final Map> GLOBAL_VARIABLES = new HashMap<>(); + private static final Map> CONSTANTS = new HashMap<>(); + + static { + CONSTANTS.put("trace", player -> RecordStateMachine.getRecordStatus().isTracing() ? 1 : 0); + CONSTANTS.put("autotrace", player -> RecordStateMachine.getRecordStatus().isAutoTrace() ? 1 : 0); + CONSTANTS.put("tnt", player -> Region.getRegion(player.getLocation()).getTntMode() == CommandTNT.TNTMode.OFF ? 0 : 1); + CONSTANTS.put("freeze", player -> Region.getRegion(player.getLocation()).isFreeze() ? 1 : 0); + CONSTANTS.put("fire", player -> Region.getRegion(player.getLocation()).isFire() ? 1 : 0); + CONSTANTS.put("x", player -> player.getLocation().getBlockX()); + CONSTANTS.put("y", player -> player.getLocation().getBlockY()); + CONSTANTS.put("z", player -> player.getLocation().getBlockZ()); + } + private Set playerSet = new HashSet<>(); public ScriptListener() { @@ -74,6 +90,11 @@ public class ScriptListener implements Listener { new ScriptExecutor((BookMeta) item.getItemMeta(), event.getPlayer()); } + @EventHandler + public void onPlayerQuit(PlayerQuitEvent event) { + GLOBAL_VARIABLES.remove(event.getPlayer()); + } + private boolean isNoBook(ItemStack item) { return VersionedCallable.call(new VersionedCallable<>(() -> ScriptListener_12.isNoBook(item), 8), new VersionedCallable<>(() -> ScriptListener_15.isNoBook(item), 15)); @@ -146,6 +167,15 @@ public class ScriptListener implements Listener { case "var": ScriptListener.variableCommand(this, generateArgumentArray("var", this, command)); continue; + case "unvar": + ScriptListener.unvariableCommand(this, generateArgumentArray("unvar", this, command)); + continue; + case "global": + ScriptListener.globalCommand(this, generateArgumentArray("global", this, command)); + continue; + case "unglobal": + ScriptListener.unglobalCommand(this, generateArgumentArray("unglobal", this, command)); + continue; case "add": ScriptListener.arithmeticCommand(this, generateArgumentArray("add", this, command), (left, right) -> left + right); continue; @@ -246,7 +276,7 @@ public class ScriptListener implements Listener { String s = String.join(" ", args); Set variables = new HashSet<>(scriptExecutor.variables.keySet()); - variables.addAll(Arrays.asList("trace", "autotrace", "tnt", "freeze", "fire")); + variables.addAll(CONSTANTS.keySet()); for (String variable : variables) { s = s.replace("<" + variable + ">", getValue(scriptExecutor, variable) + ""); } @@ -283,6 +313,44 @@ public class ScriptListener implements Listener { setValue(scriptExecutor, args[0], parseValue(args[1])); } + private static void unvariableCommand(ScriptExecutor scriptExecutor, String[] args) { + if (args.length < 1) { + scriptExecutor.player.sendMessage(BauSystem.PREFIX + "§cVariablen Namen fehlt."); + return; + } + if (!isLocalVariable(scriptExecutor, args[0])) { + scriptExecutor.player.sendMessage(BauSystem.PREFIX + "§cVariable is nicht definiert"); + return; + } + scriptExecutor.variables.remove(args[0]); + } + + private static void globalCommand(ScriptExecutor scriptExecutor, String[] args) { + if (args.length < 1) { + scriptExecutor.player.sendMessage(BauSystem.PREFIX + "§cVariablen Namen fehlt."); + return; + } + if (!isLocalVariable(scriptExecutor, args[0])) { + scriptExecutor.player.sendMessage(BauSystem.PREFIX + "§cVariable is nicht definiert"); + return; + } + GLOBAL_VARIABLES.computeIfAbsent(scriptExecutor.player, player -> new HashMap<>()).put(args[0], scriptExecutor.variables.getOrDefault(args[0], 0)); + } + + private static void unglobalCommand(ScriptExecutor scriptExecutor, String[] args) { + if (args.length < 1) { + scriptExecutor.player.sendMessage(BauSystem.PREFIX + "§cVariablen Namen fehlt."); + return; + } + if (!isGlobalVariable(scriptExecutor, args[0])) { + scriptExecutor.player.sendMessage(BauSystem.PREFIX + "§cVariable is nicht definiert"); + return; + } + if (GLOBAL_VARIABLES.containsKey(scriptExecutor.player)) { + GLOBAL_VARIABLES.get(scriptExecutor.player).remove(args[0]); + } + } + private static int ifCommand(ScriptExecutor scriptExecutor, String[] args) { if (args.length < 2) { scriptExecutor.player.sendMessage(BauSystem.PREFIX + "§cDie ersten beiden Argumente sind Zahlen/Boolsche Werte oder Variablen."); @@ -389,37 +457,37 @@ public class ScriptListener implements Listener { } private static int getValue(ScriptExecutor scriptExecutor, String key) { - Region region = Region.getRegion(scriptExecutor.player.getLocation()); - switch (key) { - case "trace": - return RecordStateMachine.getRecordStatus().isTracing() ? 1 : 0; - case "autotrace": - return RecordStateMachine.getRecordStatus().isAutoTrace() ? 1 : 0; - case "tnt": - return region.getTntMode() == CommandTNT.TNTMode.OFF ? 0 : 1; - case "freeze": - return !region.isFreeze() ? 0 : 1; - case "fire": - return !region.isFire() ? 0 : 1; - default: - return scriptExecutor.variables.getOrDefault(key, 0); + if (CONSTANTS.containsKey(key)) { + return CONSTANTS.get(key).apply(scriptExecutor.player); } + if (GLOBAL_VARIABLES.containsKey(scriptExecutor.player) && GLOBAL_VARIABLES.get(scriptExecutor.player).containsKey(key)) { + return GLOBAL_VARIABLES.get(scriptExecutor.player).get(key); + } + return scriptExecutor.variables.getOrDefault(key, 0); } private static boolean isVariable(ScriptExecutor scriptExecutor, String key) { - switch (key) { - case "trace": - case "autotrace": - case "tnt": - case "freeze": - case "fire": - return true; - default: - return scriptExecutor.variables.containsKey(key); + if (CONSTANTS.containsKey(key)) { + return true; } + if (GLOBAL_VARIABLES.containsKey(scriptExecutor.player) && GLOBAL_VARIABLES.get(scriptExecutor.player).containsKey(key)) { + return true; + } + return scriptExecutor.variables.containsKey(key); } - private static int parseValue(String value) { + private static boolean isLocalVariable(ScriptExecutor scriptExecutor, String key) { + return isVariable(scriptExecutor, key) && !isGlobalVariable(scriptExecutor, key); + } + + private static boolean isGlobalVariable(ScriptExecutor scriptExecutor, String key) { + if (!GLOBAL_VARIABLES.containsKey(scriptExecutor.player)) { + return false; + } + return GLOBAL_VARIABLES.get(scriptExecutor.player).containsKey(key); + } + + public static int parseValue(String value) { if (value.equalsIgnoreCase("true") || value.equalsIgnoreCase("yes")) { return 1; } else if (value.equalsIgnoreCase("false") || value.equalsIgnoreCase("no")) { diff --git a/BauSystem_Main/src/plugin.yml b/BauSystem_Main/src/plugin.yml index 113cb9a..7020039 100644 --- a/BauSystem_Main/src/plugin.yml +++ b/BauSystem_Main/src/plugin.yml @@ -36,6 +36,7 @@ commands: detonator: aliases: dt script: + scriptvars: simulator: aliases: sim gui: \ No newline at end of file