SteamWar/BauSystem2.0
Archiviert
12
0

YeetAPION: Simulators
Einige Prüfungen sind fehlgeschlagen
SteamWarCI Build failed

Dieser Commit ist enthalten in:
Lixfel 2023-06-05 15:42:29 +02:00
Ursprung 485d8a65d5
Commit 9d5fd6eb88
7 geänderte Dateien mit 85 neuen und 268 gelöschten Zeilen

Datei anzeigen

@ -63,6 +63,10 @@ public class Config extends YamlConfig {
@ConfigField @ConfigField
private List<String> depthCounter; private List<String> depthCounter;
@Getter
@ConfigField
private boolean simulatorAutoTrace;
@Getter @Getter
@ConfigField @ConfigField
private final Map<String, Integer> baugui = new HashMap<>(); private final Map<String, Integer> baugui = new HashMap<>();

Datei anzeigen

@ -21,24 +21,18 @@ package de.steamwar.bausystem.features.simulator;
import de.steamwar.bausystem.BauSystem; import de.steamwar.bausystem.BauSystem;
import de.steamwar.bausystem.SWUtils; import de.steamwar.bausystem.SWUtils;
import de.steamwar.bausystem.features.simulator.tnt.TNTElement;
import de.steamwar.bausystem.utils.ItemUtils; import de.steamwar.bausystem.utils.ItemUtils;
import de.steamwar.inventory.SWItem; import de.steamwar.inventory.SWItem;
import de.steamwar.linkage.Linked; import de.steamwar.linkage.Linked;
import de.steamwar.linkage.api.Disable; import de.steamwar.linkage.api.Disable;
import de.steamwar.linkage.api.Enable; import de.steamwar.linkage.api.Enable;
import de.steamwar.sql.SteamwarUser;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.Material; import org.bukkit.Material;
import org.bukkit.NamespacedKey; import org.bukkit.NamespacedKey;
import org.bukkit.World; import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta; import org.bukkit.inventory.meta.ItemMeta;
import yapion.exceptions.YAPIONException;
import yapion.hierarchy.types.YAPIONArray;
import yapion.hierarchy.types.YAPIONObject;
import yapion.parser.YAPIONParser;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
@ -46,17 +40,15 @@ import java.util.Arrays;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
import java.util.Set; import java.util.Set;
import java.util.stream.Collectors;
@Linked @Linked
public class SimulatorStorage implements Enable, Disable { public class SimulatorStorage implements Enable, Disable {
public static final World WORLD = Bukkit.getWorlds().get(0);
private static final File simulatorsDir = new File(Bukkit.getWorlds().get(0).getWorldFolder(), "simulators"); private static final File simulatorsDir = new File(Bukkit.getWorlds().get(0).getWorldFolder(), "simulators");
private static NamespacedKey simulatorSelection = SWUtils.getNamespaceKey("simulator_selection"); private static final NamespacedKey simulatorSelection = SWUtils.getNamespaceKey("simulator_selection");
private static Map<String, TNTSimulator> tntSimulators = new HashMap<>(); private static final Map<String, TNTSimulator> tntSimulators = new HashMap<>();
public static void createNewSimulator(String name) { public static void createNewSimulator(String name) {
tntSimulators.put(name, new TNTSimulator()); tntSimulators.put(name, new TNTSimulator());
@ -107,11 +99,11 @@ public class SimulatorStorage implements Enable, Disable {
if (tntSimulator != null) { if (tntSimulator != null) {
tntSimulator.close(); tntSimulator.close();
} }
new File(simulatorsDir, name + ".simulator").delete(); new File(simulatorsDir, name + ".yml").delete();
} }
public static void copySimulator(TNTSimulator tntSimulator, String name) { public static void copySimulator(TNTSimulator tntSimulator, String name) {
tntSimulators.put(name, new TNTSimulator(tntSimulator.toYAPION())); tntSimulators.put(name, new TNTSimulator(tntSimulator.toYaml()));
} }
public static void removeSimulator(ItemStack itemStack) { public static void removeSimulator(ItemStack itemStack) {
@ -139,48 +131,11 @@ public class SimulatorStorage implements Enable, Disable {
if (files == null) return; if (files == null) return;
for (File file : files) { for (File file : files) {
YAPIONObject yapionObject; String name = file.getName();
try { if(!name.endsWith(".yml"))
yapionObject = YAPIONParser.parse(file);
} catch (YAPIONException | IOException e) {
continue; continue;
}
if (file.getName().endsWith(".yapion")) {
String name = file.getName().substring(0, file.getName().length() - 7);
try {
SteamwarUser steamwarUser = SteamwarUser.get(Integer.parseInt(name));
convert(file, steamwarUser);
} catch (Exception e) {
file.delete();
}
} else {
String name = file.getName().substring(0, file.getName().length() - ".simulator".length());
tntSimulators.put(name, new TNTSimulator(yapionObject));
}
}
}
private static void convert(File file, SteamwarUser steamwarUser) { tntSimulators.put(name.substring(0, name.length() - 4), new TNTSimulator(YamlConfiguration.loadConfiguration(file)));
YAPIONObject yapionObject;
try {
yapionObject = YAPIONParser.parse(file);
} catch (YAPIONException | IOException e) {
return;
}
try {
file.delete();
} catch (Exception e) {
e.printStackTrace();
}
for (String s : yapionObject.getKeys()) {
String newName = steamwarUser.getUserName() + (s.isEmpty() ? "" : "_" + s);
YAPIONArray content = yapionObject.getArray(s);
if (content.isEmpty()) continue;
TNTSimulator tntSimulator = new TNTSimulator();
for (YAPIONObject element : content.streamObject().collect(Collectors.toList())) {
tntSimulator.getTntElementList().add(new TNTElement(element, null, tntSimulator.getEntityServer()));
}
tntSimulators.put(newName, tntSimulator);
} }
} }
@ -188,9 +143,9 @@ public class SimulatorStorage implements Enable, Disable {
public void disable() { public void disable() {
for (Map.Entry<String, TNTSimulator> entry : tntSimulators.entrySet()) { for (Map.Entry<String, TNTSimulator> entry : tntSimulators.entrySet()) {
try { try {
entry.getValue().toYAPION().toFile(new File(simulatorsDir, entry.getKey() + ".simulator")); entry.getValue().toYaml().save(new File(simulatorsDir, entry.getKey() + ".yml"));
} catch (Exception e) { } catch (IOException e) {
e.printStackTrace(); throw new SecurityException(e);
} }
} }
} }

Datei anzeigen

@ -20,6 +20,8 @@
package de.steamwar.bausystem.features.simulator; package de.steamwar.bausystem.features.simulator;
import de.steamwar.bausystem.BauSystem; import de.steamwar.bausystem.BauSystem;
import de.steamwar.bausystem.config.ConfigField;
import de.steamwar.bausystem.config.YamlConfig;
import de.steamwar.bausystem.configplayer.Config; import de.steamwar.bausystem.configplayer.Config;
import de.steamwar.bausystem.features.simulator.gui.TNTElementGUI; import de.steamwar.bausystem.features.simulator.gui.TNTElementGUI;
import de.steamwar.bausystem.features.simulator.gui.TNTSimulatorGui; import de.steamwar.bausystem.features.simulator.gui.TNTSimulatorGui;
@ -36,10 +38,8 @@ import de.steamwar.entity.REntityServer;
import lombok.Getter; import lombok.Getter;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.Material; import org.bukkit.Material;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import yapion.hierarchy.types.YAPIONArray;
import yapion.hierarchy.types.YAPIONObject;
import yapion.hierarchy.types.YAPIONType;
import java.util.*; import java.util.*;
import java.util.concurrent.atomic.AtomicBoolean; import java.util.concurrent.atomic.AtomicBoolean;
@ -47,40 +47,29 @@ import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Collectors; import java.util.stream.Collectors;
@Getter @Getter
public class TNTSimulator { public class TNTSimulator extends YamlConfig {
private Set<Player> players = new HashSet<>(); static {
private REntityServer entityServer = new REntityServer(); register(TNTGroup.class);
register(TNTElement.class);
}
private final Set<Player> players = new HashSet<>();
private final REntityServer entityServer = new REntityServer();
@ConfigField
private Material material = Material.TNT; private Material material = Material.TNT;
private List<SimulatorElement> tntElementList = new ArrayList<>(); @ConfigField
private final List<SimulatorElement> tntElementList = new ArrayList<>();
public TNTSimulator() { public TNTSimulator() {}
} public TNTSimulator(ConfigurationSection section) {
super(section);
public TNTSimulator(YAPIONObject yapionObject) { for(SimulatorElement element : tntElementList)
material = Material.valueOf(yapionObject.getStringOrDefault("material", Material.TNT.name())); element.init(entityServer, null);
YAPIONArray yapionArray = yapionObject.getArrayOrDefault("tntElements", new YAPIONArray());
for (YAPIONObject element : yapionArray.streamObject().collect(Collectors.toList())) {
if (element.containsKey("elements", YAPIONType.ARRAY)) {
tntElementList.add(new TNTGroup(element, entityServer));
} else {
tntElementList.add(new TNTElement(element, null, entityServer));
}
}
}
public YAPIONObject toYAPION() {
YAPIONObject yapionObject = new YAPIONObject();
yapionObject.add("material", material.name());
YAPIONArray yapionArray = new YAPIONArray();
for (SimulatorElement element : tntElementList) {
yapionArray.add(element.toYAPION());
}
yapionObject.add("tntElements", yapionArray);
return yapionObject;
} }
public void close() { public void close() {
@ -218,7 +207,7 @@ public class TNTSimulator {
AtomicBoolean needsAutoTrace = new AtomicBoolean(); AtomicBoolean needsAutoTrace = new AtomicBoolean();
players.forEach(player -> { players.forEach(player -> {
boolean simulatorAutoTrace = Config.getInstance().get(player).getPlainValueOrDefault("simulatorAutoTrace", false); boolean simulatorAutoTrace = Config.get(player).isSimulatorAutoTrace();
if (simulatorAutoTrace) { if (simulatorAutoTrace) {
needsAutoTrace.set(true); needsAutoTrace.set(true);
player.performCommand("trace show"); player.performCommand("trace show");

Datei anzeigen

@ -22,21 +22,23 @@ package de.steamwar.bausystem.features.simulator.tnt;
import de.steamwar.bausystem.region.Region; import de.steamwar.bausystem.region.Region;
import de.steamwar.bausystem.shared.Pair; import de.steamwar.bausystem.shared.Pair;
import de.steamwar.entity.REntity; import de.steamwar.entity.REntity;
import de.steamwar.entity.REntityServer;
import de.steamwar.inventory.SWItem; import de.steamwar.inventory.SWItem;
import org.bukkit.Location; import org.bukkit.Location;
import org.bukkit.Material; import org.bukkit.Material;
import org.bukkit.configuration.serialization.ConfigurationSerializable;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.util.Vector; import org.bukkit.util.Vector;
import yapion.hierarchy.types.YAPIONObject;
import java.util.*; import java.util.*;
public interface SimulatorElement { public interface SimulatorElement extends ConfigurationSerializable {
Map<SimulatorElement, Set<Runnable>> observer = new HashMap<>(); Map<SimulatorElement, Set<Runnable>> observer = new HashMap<>();
Map<Runnable, Runnable> closeObserver = new HashMap<>(); Map<Runnable, Runnable> closeObserver = new HashMap<>();
YAPIONObject toYAPION(); void init(REntityServer entityServer, TNTGroup tntGroup);
List<REntity> getEntities(); List<REntity> getEntities();
void getEntity(List<SimulatorElement> elements, REntity entity); void getEntity(List<SimulatorElement> elements, REntity entity);

Datei anzeigen

@ -20,8 +20,9 @@
package de.steamwar.bausystem.features.simulator.tnt; package de.steamwar.bausystem.features.simulator.tnt;
import de.steamwar.bausystem.BauSystem; import de.steamwar.bausystem.BauSystem;
import de.steamwar.bausystem.config.ConfigField;
import de.steamwar.bausystem.config.YamlConfig;
import de.steamwar.bausystem.features.simulator.OrderUtils; import de.steamwar.bausystem.features.simulator.OrderUtils;
import de.steamwar.bausystem.features.simulator.SimulatorStorage;
import de.steamwar.bausystem.region.Region; import de.steamwar.bausystem.region.Region;
import de.steamwar.bausystem.region.flags.Flag; import de.steamwar.bausystem.region.flags.Flag;
import de.steamwar.bausystem.region.flags.flagvalues.FreezeMode; import de.steamwar.bausystem.region.flags.flagvalues.FreezeMode;
@ -41,81 +42,62 @@ import org.bukkit.World;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.entity.TNTPrimed; import org.bukkit.entity.TNTPrimed;
import org.bukkit.util.Vector; import org.bukkit.util.Vector;
import yapion.hierarchy.types.YAPIONObject;
import java.util.*; import java.util.*;
@Getter @Getter
public class TNTElement implements SimulatorElement { public class TNTElement extends YamlConfig implements SimulatorElement {
private static final World WORLD = Bukkit.getWorlds().get(0); private static final World WORLD = Bukkit.getWorlds().get(0);
private final REntityServer entityServer; private REntityServer entityServer;
private RFallingBlockEntity entity; private RFallingBlockEntity entity;
TNTGroup tntGroup = null; @Setter
private TNTGroup tntGroup;
private final Vector position; @ConfigField
private Vector position;
@ConfigField
private int fuseTicks = 80; private int fuseTicks = 80;
@ConfigField
private int count = 1; private int count = 1;
@ConfigField
private int tickOffset = 0; private int tickOffset = 0;
@Setter @Setter
@ConfigField
private boolean xVelocity = false; private boolean xVelocity = false;
@Setter @Setter
@ConfigField
private boolean yVelocity = false; private boolean yVelocity = false;
@Setter @Setter
@ConfigField
private boolean zVelocity = false; private boolean zVelocity = false;
@ConfigField
private Material order = Material.REPEATER; private Material order = Material.REPEATER;
@ConfigField
private Material material = Material.TNT; private Material material = Material.TNT;
@ConfigField
private boolean disabled = false; private boolean disabled = false;
public TNTElement(Vector position, TNTGroup tntGroup, REntityServer entityServer) { public TNTElement(Vector position, TNTGroup tntGroup, REntityServer entityServer) {
this.entityServer = entityServer;
this.tntGroup = tntGroup;
this.position = position; this.position = position;
initEntity(); init(entityServer, tntGroup);
} }
public TNTElement(YAPIONObject yapionObject, TNTGroup tntGroup, REntityServer entityServer) { public TNTElement(Map<String, Object> map) {
this.entityServer = entityServer; super(map);
this.tntGroup = tntGroup;
this.position = new Vector(yapionObject.getDoubleOrDefault("x", yapionObject.getDoubleOrDefault("positionX", 0)), yapionObject.getDoubleOrDefault("y", yapionObject.getDoubleOrDefault("positionY", 0)), yapionObject.getDoubleOrDefault("z", yapionObject.getDoubleOrDefault("positionZ", 0)));
this.disabled = yapionObject.getBooleanOrDefault("disabled", false);
this.fuseTicks = yapionObject.getIntOrDefault("fuseTicks", 80);
this.count = yapionObject.getIntOrDefault("count", 1);
this.tickOffset = yapionObject.getIntOrDefault("tickOffset", 0);
this.xVelocity = yapionObject.getBooleanOrDefault("xVelocity", false);
this.yVelocity = yapionObject.getBooleanOrDefault("yVelocity", false);
this.zVelocity = yapionObject.getBooleanOrDefault("zVelocity", false);
this.order = Material.valueOf(yapionObject.getStringOrDefault("order", yapionObject.getBooleanOrDefault("comparator", false) ? Material.COMPARATOR.name() : Material.REPEATER.name()));
this.material = Material.valueOf(yapionObject.getStringOrDefault("material", Material.TNT.name()));
initEntity();
}
private void initEntity() {
this.entity = new RFallingBlockEntity(entityServer, getPosition().toLocation(WORLD), Material.TNT);
this.entity.setNoGravity(true);
_updatePosition();
} }
@Override @Override
public YAPIONObject toYAPION() { public void init(REntityServer entityServer, TNTGroup tntGroup) {
YAPIONObject yapionObject = new YAPIONObject(); this.entityServer = entityServer;
yapionObject.add("x", position.getX()); this.tntGroup = tntGroup;
yapionObject.add("y", position.getY()); this.entity = new RFallingBlockEntity(entityServer, getPosition().toLocation(WORLD), Material.TNT);
yapionObject.add("z", position.getZ()); this.entity.setNoGravity(true);
yapionObject.add("fuseTicks", fuseTicks); _updatePosition();
yapionObject.add("count", count);
yapionObject.add("tickOffset", tickOffset);
yapionObject.add("xVelocity", xVelocity);
yapionObject.add("yVelocity", yVelocity);
yapionObject.add("zVelocity", zVelocity);
yapionObject.add("order", order.name());
yapionObject.add("material", material.name());
yapionObject.add("disabled", disabled);
return yapionObject;
} }
@Override @Override
@ -191,7 +173,7 @@ public class TNTElement implements SimulatorElement {
@Override @Override
public boolean locations(Map<Integer, Map<Integer, Set<Pair<Runnable, Integer>>>> result, Region region, Location radius) { public boolean locations(Map<Integer, Map<Integer, Set<Pair<Runnable, Integer>>>> result, Region region, Location radius) {
if (disabled) return false; if (disabled) return false;
Location location = getPosition().toLocation(SimulatorStorage.WORLD); Location location = getPosition().toLocation(WORLD);
if (region.isGlobal() && location.distanceSquared(radius) > 10000) { if (region.isGlobal() && location.distanceSquared(radius) > 10000) {
return false; return false;
} }
@ -199,14 +181,14 @@ public class TNTElement implements SimulatorElement {
return false; return false;
} }
Region thisRegion = Region.getRegion(location); Region thisRegion = Region.getRegion(location);
if (thisRegion.getFlagStorage().get(Flag.FREEZE) == FreezeMode.ACTIVE) { if (thisRegion.get(Flag.FREEZE) == FreezeMode.ACTIVE) {
return true; return true;
} }
result.computeIfAbsent(getTickOffset(), ignore -> new HashMap<>()) result.computeIfAbsent(getTickOffset(), ignore -> new HashMap<>())
.computeIfAbsent(OrderUtils.order(order), ignore -> new HashSet<>()) .computeIfAbsent(OrderUtils.order(order), ignore -> new HashSet<>())
.add(new Pair<>(() -> { .add(new Pair<>(() -> {
SimulatorStorage.WORLD.spawn(location, TNTPrimed.class, tntPrimed -> { WORLD.spawn(location, TNTPrimed.class, tntPrimed -> {
tntPrimed.setFuseTicks(fuseTicks); tntPrimed.setFuseTicks(fuseTicks);
if (!xVelocity) tntPrimed.setVelocity(tntPrimed.getVelocity().setX(0)); if (!xVelocity) tntPrimed.setVelocity(tntPrimed.getVelocity().setX(0));
if (!yVelocity) tntPrimed.setVelocity(tntPrimed.getVelocity().setY(0)); if (!yVelocity) tntPrimed.setVelocity(tntPrimed.getVelocity().setY(0));

Datei anzeigen

@ -20,6 +20,8 @@
package de.steamwar.bausystem.features.simulator.tnt; package de.steamwar.bausystem.features.simulator.tnt;
import de.steamwar.bausystem.BauSystem; import de.steamwar.bausystem.BauSystem;
import de.steamwar.bausystem.config.ConfigField;
import de.steamwar.bausystem.config.YamlConfig;
import de.steamwar.bausystem.region.Region; import de.steamwar.bausystem.region.Region;
import de.steamwar.bausystem.shared.Pair; import de.steamwar.bausystem.shared.Pair;
import de.steamwar.entity.REntity; import de.steamwar.entity.REntity;
@ -30,8 +32,6 @@ import org.bukkit.Location;
import org.bukkit.Material; import org.bukkit.Material;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.util.Vector; import org.bukkit.util.Vector;
import yapion.hierarchy.types.YAPIONArray;
import yapion.hierarchy.types.YAPIONObject;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
@ -40,50 +40,36 @@ import java.util.Set;
import java.util.stream.Collectors; import java.util.stream.Collectors;
@Getter @Getter
public class TNTGroup implements SimulatorElement { public class TNTGroup extends YamlConfig implements SimulatorElement {
private final Vector position; @ConfigField
private Vector position;
@ConfigField
private int tickOffset = 0; private int tickOffset = 0;
@ConfigField
private Material material = Material.BARREL; private Material material = Material.BARREL;
@ConfigField
private boolean disabled = false; private boolean disabled = false;
private List<TNTElement> elements = new ArrayList<>(); @ConfigField
private final List<TNTElement> elements = new ArrayList<>();
public TNTGroup(Vector position) { public TNTGroup(Vector position) {
this.position = position; this.position = position;
} }
public TNTGroup(YAPIONObject yapionObject, REntityServer entityServer) { public TNTGroup(Map<String, Object> map) {
this.position = new Vector(yapionObject.getDoubleOrDefault("x", 0), yapionObject.getDoubleOrDefault("y", 0), yapionObject.getDoubleOrDefault("z", 0)); super(map);
this.tickOffset = yapionObject.getIntOrDefault("tickOffset", 0);
this.material = Material.getMaterial(yapionObject.getStringOrDefault("material", "BARREL"));
this.disabled = yapionObject.getBooleanOrDefault("disabled", false);
YAPIONArray elements = yapionObject.getArrayOrDefault("elements", new YAPIONArray());
for (YAPIONObject element : elements.streamObject().collect(Collectors.toList())) {
TNTElement tntElement = new TNTElement(element, this, entityServer);
this.elements.add(tntElement);
tntElement._updatePosition();
}
} }
@Override @Override
public YAPIONObject toYAPION() { public void init(REntityServer entityServer, TNTGroup tntGroup) {
YAPIONObject yapionObject = new YAPIONObject();
yapionObject.add("x", position.getX());
yapionObject.add("y", position.getY());
yapionObject.add("z", position.getZ());
yapionObject.add("tickOffset", tickOffset);
yapionObject.add("material", material.name());
yapionObject.add("disabled", disabled);
YAPIONArray yapionArray = new YAPIONArray();
for(TNTElement element : elements) { for(TNTElement element : elements) {
yapionArray.add(element.toYAPION()); element.init(entityServer, this);
} }
yapionObject.add("elements", yapionArray);
return yapionObject;
} }
public void add(TNTElement tntElement) { public void add(TNTElement tntElement) {
tntElement.tntGroup = this; tntElement.setTntGroup(this);
elements.add(tntElement); elements.add(tntElement);
} }

Datei anzeigen

@ -1,101 +0,0 @@
/*
* 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 <https://www.gnu.org/licenses/>.
*/
package de.steamwar.bausystem.worlddata;
import de.steamwar.sql.SteamwarUser;
import lombok.SneakyThrows;
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.InputStreamCharsets;
import yapion.parser.YAPIONParser;
import yapion.parser.options.FileOptions;
import java.io.File;
import java.util.List;
@UtilityClass
public class SimulatorData {
private final File simulatorsDir = new File(Bukkit.getWorlds().get(0).getWorldFolder(), "simulators");
static {
simulatorsDir.mkdirs();
}
@SneakyThrows
public YAPIONObject getSimulator(Player player) {
File file = getFile(player);
if (!file.exists()) {
return new YAPIONObject();
}
return YAPIONParser.parse(file, new FileOptions().charset(InputStreamCharsets.UTF_8));
}
@SneakyThrows
private void internalSaveSimulator(Player player, YAPIONObject yapionObject) {
File file = getFile(player);
yapionObject.toYAPION(new FileOutput(file)).close();
}
@SneakyThrows
void saveSimulator(SteamwarUser steamwarUser, YAPIONObject yapionObject) {
File file = new File(simulatorsDir, steamwarUser.getId() + ".yapion");
yapionObject.toYAPION(new FileOutput(file)).close();
}
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) {
SteamwarUser steamwarUser = SteamwarUser.get(player.getUniqueId());
return new File(simulatorsDir, steamwarUser.getId() + ".yapion");
}
}