diff --git a/BauSystem_Main/src/de/steamwar/bausystem/features/script/ScriptVarsCommand.java b/BauSystem_Main/src/de/steamwar/bausystem/features/script/ScriptVarsCommand.java new file mode 100644 index 00000000..a3b615a9 --- /dev/null +++ b/BauSystem_Main/src/de/steamwar/bausystem/features/script/ScriptVarsCommand.java @@ -0,0 +1,141 @@ +/* + * This file is a part of the SteamWar software. + * + * Copyright (C) 2021 SteamWar.de-Serverteam + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +package de.steamwar.bausystem.features.script; + +import de.steamwar.bausystem.BauSystem; +import de.steamwar.bausystem.features.script.variables.Context; +import de.steamwar.bausystem.features.script.variables.Value; +import de.steamwar.bausystem.linkage.LinkageType; +import de.steamwar.bausystem.linkage.Linked; +import de.steamwar.command.SWCommand; +import de.steamwar.command.SWCommandUtils; +import de.steamwar.command.TypeMapper; +import org.bukkit.entity.Player; + +import java.util.*; + +@Linked(LinkageType.COMMAND) +public class ScriptVarsCommand extends SWCommand { + + public ScriptVarsCommand() { + super("scriptvars"); + } + + @Register(help = true) + public void genericHelp(Player p, String... args) { + p.sendMessage("§8/§escriptvars §8- §7Zähle alle globalen Variablen auf"); + p.sendMessage("§8/§escriptvars §8[§7Variable§8] §8- §7Gebe den Wert der Variable zurück"); + p.sendMessage("§8/§escriptvars §8[§7Variable§8] §8[§7Value§8] §8- §7Setzte eine Variable auf einen Wert"); + p.sendMessage("§8/§escriptvars §8[§7Variable§8] §8<§7remove§8|§7delete§8|§7clear§8> §8- §7Lösche eine Variable"); + } + + @Register + public void genericCommand(Player p) { + Context context = ScriptListener.getGlobalContext(p); + if (context.allVariables().isEmpty()) { + p.sendMessage(BauSystem.PREFIX + "§cKeine globalen Variablen definiert"); + return; + } + int i = 0; + p.sendMessage(BauSystem.PREFIX + context.allVariables().size() + " Variable(n)"); + for (Map.Entry var : context.entrySet()) { + if (i++ >= 40) break; + p.sendMessage("- " + var.getKey() + "=" + var.getValue().asString()); + } + } + + @Register + public void getCommand(Player p, String varName) { + Context context = ScriptListener.getGlobalContext(p); + if (context.allVariables().isEmpty()) { + p.sendMessage(BauSystem.PREFIX + "§cKeine globalen Variablen definiert"); + return; + } + if (!context.hasValue(varName)) { + p.sendMessage(BauSystem.PREFIX + "§cUnbekannte Variable"); + return; + } + p.sendMessage(BauSystem.PREFIX + varName + "=" + context.getValue(varName).asString()); + } + + @Register + public void setValueCommand(Player p, String varName, long value) { + Context context = ScriptListener.getGlobalContext(p); + context.putValue(varName, new Value.LongValue(value)); + p.sendMessage(BauSystem.PREFIX + varName + " auf " + value + " gesetzt"); + } + + @Register + public void setValueCommand(Player p, String varName, boolean value) { + Context context = ScriptListener.getGlobalContext(p); + context.putValue(varName, new Value.BooleanValue(value)); + p.sendMessage(BauSystem.PREFIX + varName + " auf " + value + " gesetzt"); + } + + @Register + public void setValueCommand(Player p, String varName, String... value) { + Context context = ScriptListener.getGlobalContext(p); + String s = String.join(" ", value); + context.putValue(varName, new Value.StringValue(s)); + p.sendMessage(BauSystem.PREFIX + varName + " auf " + s + " gesetzt"); + } + + @Register + public void removeCommand(Player p, String varName, @Mapper(value = "Delete") String remove) { + Context context = ScriptListener.getGlobalContext(p); + if (context.allVariables().isEmpty()) { + p.sendMessage(BauSystem.PREFIX + "§cKeine globalen Variablen definiert"); + return; + } + context.removeValue(varName); + p.sendMessage(BauSystem.PREFIX + "Variable " + varName + " gelöscht"); + } + + @ClassMapper(value = String.class, local = true) + public TypeMapper stringTypeMapper() { + return SWCommandUtils.createMapper(s -> s, (commandSender, s) -> { + if (commandSender instanceof Player) { + Player player = (Player) commandSender; + return new ArrayList<>(ScriptListener.getGlobalContext(player).allVariables()); + } + return null; + }); + } + + @Mapper(value = "Delete", local = true) + public TypeMapper clearStringTypeMapper() { + List tabCompletes = Arrays.asList("delete", "clear", "remove"); + return SWCommandUtils.createMapper(s -> { + if (s.equalsIgnoreCase("delete") || s.equalsIgnoreCase("clear") || s.equalsIgnoreCase("remove")) { + return s; + } + return null; + }, s -> tabCompletes); + } + + @ClassMapper(value = boolean.class, local = true) + public TypeMapper integerTypeMapper() { + List tabCompletes = Arrays.asList("true", "false"); + return SWCommandUtils.createMapper(s -> { + if (s.equalsIgnoreCase("remove") || s.equalsIgnoreCase("clear") || s.equalsIgnoreCase("delete")) return null; + return s.equalsIgnoreCase("true"); + }, s -> tabCompletes); + } +} diff --git a/BauSystem_Main/src/de/steamwar/bausystem/features/script/command/string/.gitkeep b/BauSystem_Main/src/de/steamwar/bausystem/features/script/command/string/.gitkeep new file mode 100644 index 00000000..e69de29b diff --git a/BauSystem_Main/src/de/steamwar/bausystem/features/script/variables/Context.java b/BauSystem_Main/src/de/steamwar/bausystem/features/script/variables/Context.java index 8b0c94d0..a5bb62f8 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/features/script/variables/Context.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/features/script/variables/Context.java @@ -27,4 +27,8 @@ public class Context { public Set allVariables() { return variables.keySet(); } + + public Set> entrySet() { + return variables.entrySet(); + } }