From c8ad93a00ff4e1fcf30e4e7fda41cb6a78ce0157 Mon Sep 17 00:00:00 2001 From: yoyosource Date: Thu, 9 Dec 2021 23:28:51 +0100 Subject: [PATCH] Add SimulatorCommand.saveCommand and other Signed-off-by: yoyosource --- .../features/simulator/SimulatorCommand.java | 64 +++++++++++++++++++ .../features/simulator/TNTSimulator.java | 9 +-- .../bausystem/worlddata/SimulatorData.java | 37 +++++++++-- 3 files changed, 98 insertions(+), 12 deletions(-) diff --git a/BauSystem_Main/src/de/steamwar/bausystem/features/simulator/SimulatorCommand.java b/BauSystem_Main/src/de/steamwar/bausystem/features/simulator/SimulatorCommand.java index c718d6c8..cdcaec09 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/features/simulator/SimulatorCommand.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/features/simulator/SimulatorCommand.java @@ -26,9 +26,14 @@ import de.steamwar.bausystem.SWUtils; import de.steamwar.bausystem.config.ColorConfig; import de.steamwar.bausystem.linkage.LinkageType; import de.steamwar.bausystem.linkage.Linked; +import de.steamwar.bausystem.worlddata.SimulatorData; import de.steamwar.command.SWCommand; +import de.steamwar.command.TypeMapper; +import org.bukkit.command.CommandSender; import org.bukkit.entity.Player; +import java.util.List; + @Linked(LinkageType.COMMAND) public class SimulatorCommand extends SWCommand { @@ -42,6 +47,10 @@ public class SimulatorCommand extends SWCommand { p.sendMessage(ColorConfig.OTHER + "/" + ColorConfig.HIGHLIGHT + "simulator start" + ColorConfig.OTHER + " - " + ColorConfig.BASE + "Startet die Simulation"); p.sendMessage(ColorConfig.OTHER + "/" + ColorConfig.HIGHLIGHT + "simulator gui" + ColorConfig.OTHER + " - " + ColorConfig.BASE + "Öffnet die GUI"); p.sendMessage(ColorConfig.OTHER + "/" + ColorConfig.HIGHLIGHT + "simulator delete" + ColorConfig.OTHER + " - " + ColorConfig.BASE + "Löscht alle TNT"); + p.sendMessage(ColorConfig.OTHER + "/" + ColorConfig.HIGHLIGHT + "simulator save " + ColorConfig.OTHER + " - " + ColorConfig.BASE + "Speichert den Simulator"); + p.sendMessage(ColorConfig.OTHER + "/" + ColorConfig.HIGHLIGHT + "simulator load " + ColorConfig.OTHER + " - " + ColorConfig.BASE + "Lädt den Simulator"); + p.sendMessage(ColorConfig.OTHER + "/" + ColorConfig.HIGHLIGHT + "simulator list" + ColorConfig.OTHER + " - " + ColorConfig.BASE + "Listet alle gespeicherten Simulator"); + p.sendMessage(ColorConfig.OTHER + "/" + ColorConfig.HIGHLIGHT + "simulator remove " + ColorConfig.OTHER + " - " + ColorConfig.BASE + "Löscht den Simulator"); } @Register @@ -66,6 +75,44 @@ public class SimulatorCommand extends SWCommand { public void deleteCommand(Player p) { if (cannotUse(p)) return; TNTSimulator.get(p).delete(); + p.sendMessage(ColorConfig.BASE + "Alle TNT wurden gelöscht"); + } + + @Register({"save"}) + public void saveCommand(Player p, String name) { + if (cannotUse(p)) return; + TNTSimulator.get(p).save(); + SimulatorData.saveSimulator(p, name); + p.sendMessage(ColorConfig.BASE + "Simulator " + ColorConfig.HIGHLIGHT + name + ColorConfig.BASE + " wurde gespeichert"); + } + + @Register({"load"}) + public void loadCommand(Player p, @Mapper("SavedSimulators") String name) { + if (cannotUse(p)) return; + TNTSimulator.get(p).delete(); + SimulatorData.loadSimulator(p, name); + p.sendMessage(ColorConfig.BASE + "Simulator " + ColorConfig.HIGHLIGHT + name + ColorConfig.BASE + " wurde geladen"); + } + + @Register({"remove"}) + public void removeCommand(Player p, @Mapper("SavedSimulators") String name) { + if (cannotUse(p)) return; + SimulatorData.removeSimulator(p, name); + p.sendMessage(ColorConfig.BASE + "Simulator " + ColorConfig.HIGHLIGHT + name + ColorConfig.BASE + " wurde gelöscht"); + } + + @Register({"list"}) + public void listCommand(Player p) { + if (cannotUse(p)) return; + List sims = SimulatorData.listSimulator(p); + if (sims.isEmpty()) { + p.sendMessage(ColorConfig.BASE + "Keine gespeicherten Simulatoren"); + return; + } + p.sendMessage(ColorConfig.BASE + "Gespeicherte Simulatoren:"); + for (String s : sims) { + p.sendMessage(ColorConfig.OTHER + "/" + ColorConfig.HIGHLIGHT + "simulator load " + s); + } } private boolean cannotUse(Player player) { @@ -76,4 +123,21 @@ public class SimulatorCommand extends SWCommand { return false; } + @Mapper(value = "SavedSimulators", local = true) + public TypeMapper simulatorListTypeMapper() { + return new TypeMapper() { + @Override + public String map(CommandSender commandSender, String[] previousArguments, String s) { + if (SimulatorData.listSimulator(((Player) commandSender)).contains(s)) { + return s; + } + return null; + } + + @Override + public List tabCompletes(CommandSender commandSender, String[] strings, String s) { + return SimulatorData.listSimulator(((Player) commandSender)); + } + }; + } } diff --git a/BauSystem_Main/src/de/steamwar/bausystem/features/simulator/TNTSimulator.java b/BauSystem_Main/src/de/steamwar/bausystem/features/simulator/TNTSimulator.java index f4c162a2..eb8d028c 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/features/simulator/TNTSimulator.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/features/simulator/TNTSimulator.java @@ -80,22 +80,19 @@ public class TNTSimulator { simulator.spawns.forEach(tntSpawn -> { yapionArray.add(tntSpawn.output()); }); - if (yapionArray.isEmpty()) { - SimulatorData.removeSimulator(player); - } else { - SimulatorData.saveSimulator(player, new YAPIONObject().add("", yapionArray)); - } + SimulatorData.saveSimulator(player, yapionArray); simulator.hide(); } public void saveAndDelete() { save(); TNT_SIMULATOR_MAP.remove(player); + SimulatorData.removeSimulator(player, ""); } public void delete() { TNTSimulator simulator = TNT_SIMULATOR_MAP.remove(player); - SimulatorData.removeSimulator(player); + SimulatorData.removeSimulator(player, ""); simulator.hide(); } diff --git a/BauSystem_Main/src/de/steamwar/bausystem/worlddata/SimulatorData.java b/BauSystem_Main/src/de/steamwar/bausystem/worlddata/SimulatorData.java index dc2695e3..e9ef8f5b 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/worlddata/SimulatorData.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/worlddata/SimulatorData.java @@ -25,10 +25,12 @@ import lombok.experimental.UtilityClass; import org.bukkit.Bukkit; import org.bukkit.entity.Player; import yapion.hierarchy.output.FileOutput; +import yapion.hierarchy.types.YAPIONArray; import yapion.hierarchy.types.YAPIONObject; import yapion.parser.YAPIONParser; import java.io.File; +import java.util.List; @UtilityClass public class SimulatorData { @@ -49,7 +51,7 @@ public class SimulatorData { } @SneakyThrows - public void saveSimulator(Player player, YAPIONObject yapionObject) { + private void internalSaveSimulator(Player player, YAPIONObject yapionObject) { File file = getFile(player); yapionObject.toYAPION(new FileOutput(file)).close(); } @@ -60,11 +62,34 @@ public class SimulatorData { yapionObject.toYAPION(new FileOutput(file)).close(); } - public void removeSimulator(Player player) { - File file = getFile(player); - if (file.exists()) { - file.delete(); - } + public List listSimulator(Player player) { + List strings = getSimulator(player).getKeys(); + strings.remove(""); + return strings; + } + + public void saveSimulator(Player player, String name) { + YAPIONObject yapionObject = getSimulator(player); + yapionObject.put(name, yapionObject.getArray("").copy()); + internalSaveSimulator(player, yapionObject); + } + + public void saveSimulator(Player player, YAPIONArray simulator) { + YAPIONObject yapionObject = getSimulator(player); + yapionObject.put("", simulator); + internalSaveSimulator(player, yapionObject); + } + + public void loadSimulator(Player player, String name) { + YAPIONObject yapionObject = getSimulator(player); + yapionObject.put("", yapionObject.getArray(name).copy()); + internalSaveSimulator(player, yapionObject); + } + + public void removeSimulator(Player player, String name) { + YAPIONObject yapionObject = getSimulator(player); + yapionObject.remove(name); + internalSaveSimulator(player, yapionObject); } private File getFile(Player player) {