SteamWar/BauSystem2.0
Archiviert
12
0

Add SimulatorCommand.saveCommand and other
Alle Prüfungen waren erfolgreich
SteamWarCI Build successful

Signed-off-by: yoyosource <yoyosource@nidido.de>
Dieser Commit ist enthalten in:
yoyosource 2021-12-09 23:28:51 +01:00
Ursprung 2d3dc10e23
Commit c8ad93a00f
3 geänderte Dateien mit 98 neuen und 12 gelöschten Zeilen

Datei anzeigen

@ -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 <Name>" + ColorConfig.OTHER + " - " + ColorConfig.BASE + "Speichert den Simulator");
p.sendMessage(ColorConfig.OTHER + "/" + ColorConfig.HIGHLIGHT + "simulator load <Name>" + 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 <Name>" + 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<String> 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<String> simulatorListTypeMapper() {
return new TypeMapper<String>() {
@Override
public String map(CommandSender commandSender, String[] previousArguments, String s) {
if (SimulatorData.listSimulator(((Player) commandSender)).contains(s)) {
return s;
}
return null;
}
@Override
public List<String> tabCompletes(CommandSender commandSender, String[] strings, String s) {
return SimulatorData.listSimulator(((Player) commandSender));
}
};
}
}

Datei anzeigen

@ -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();
}

Datei anzeigen

@ -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<String> listSimulator(Player player) {
List<String> 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) {