From bc93d96787445b982e38d9344776bb3f5c1f466b Mon Sep 17 00:00:00 2001 From: yoyosource Date: Mon, 5 Apr 2021 13:07:10 +0200 Subject: [PATCH] Add Region persistence --- .../src/de/steamwar/bausystem/BauSystem.java | 2 +- .../de/steamwar/bausystem/world/Region.java | 56 +++++++++++++------ 2 files changed, 41 insertions(+), 17 deletions(-) diff --git a/BauSystem_Main/src/de/steamwar/bausystem/BauSystem.java b/BauSystem_Main/src/de/steamwar/bausystem/BauSystem.java index 8e3570d..85430f9 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/BauSystem.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/BauSystem.java @@ -118,7 +118,7 @@ public class BauSystem extends JavaPlugin implements Listener { @Override public void onDisable() { - Region.saveColors(); + Region.save(); } public static BauSystem getPlugin() { diff --git a/BauSystem_Main/src/de/steamwar/bausystem/world/Region.java b/BauSystem_Main/src/de/steamwar/bausystem/world/Region.java index a0170bb..76ee3da 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/world/Region.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/world/Region.java @@ -48,13 +48,13 @@ public class Region { private static final List regions = new ArrayList<>(); private static boolean buildArea = false; private static boolean extensionArea = false; - private static JsonObject colors = new JsonObject(); + private static JsonObject regionsObject = new JsonObject(); static { - File colorsFile = new File(Bukkit.getWorlds().get(0).getWorldFolder(), "colors.json"); - if (colorsFile.exists()) { + File regionsFile = new File(Bukkit.getWorlds().get(0).getWorldFolder(), "regions.json"); + if (regionsFile.exists()) { try { - colors = new JsonParser().parse(new FileReader(colorsFile)).getAsJsonObject(); + regionsObject = new JsonParser().parse(new FileReader(regionsFile)).getAsJsonObject(); } catch (JsonSyntaxException | IOException e) { Bukkit.getLogger().log(Level.WARNING, "Item JSON error"); } @@ -101,8 +101,8 @@ public class Region { } } - public static void saveColors() { - File colorsFile = new File(Bukkit.getWorlds().get(0).getWorldFolder(), "colors.json"); + public static void save() { + File colorsFile = new File(Bukkit.getWorlds().get(0).getWorldFolder(), "regions.json"); if (!colorsFile.exists()) { try { colorsFile.createNewFile(); @@ -112,7 +112,7 @@ public class Region { } } try (FileOutputStream fileOutputStream = new FileOutputStream(colorsFile)) { - fileOutputStream.write(colors.toString().getBytes()); + fileOutputStream.write(regionsObject.toString().getBytes()); } catch (IOException e) { e.printStackTrace(); // Ignored @@ -129,6 +129,8 @@ public class Region { private SizedStack undosessions; private SizedStack redosessions; + private JsonObject regionOptions = new JsonObject(); + private TNTMode tntMode = Region.buildAreaEnabled() ? TNTMode.ONLY_TB : TNTMode.OFF; private boolean freeze = false; private boolean fire = false; @@ -137,16 +139,35 @@ public class Region { private Region(ConfigurationSection config) { name = config.getName(); - if (colors.has(name)) { - String colorName = colors.getAsJsonPrimitive(name).getAsString(); - try { - color = Color.valueOf(colorName); - } catch (Exception e) { - // Ignored + if (regionsObject.has(name)) { + regionOptions = regionsObject.getAsJsonObject(name); + if (regionOptions.has("tnt")) { + String tntName = regionsObject.getAsJsonObject(name).getAsJsonPrimitive("tnt").getAsString(); + try { + tntMode = TNTMode.valueOf(tntName); + } catch (Exception e) { + // Ignored + } } - if (color == null) { - color = Color.YELLOW; + + if (regionOptions.has("fire")) { + fire = regionOptions.getAsJsonPrimitive("fire").getAsBoolean(); } + + if (regionOptions.has("freeze")) { + freeze = regionOptions.getAsJsonPrimitive("freeze").getAsBoolean(); + } + + if (regionOptions.has("color")) { + String colorName = regionOptions.getAsJsonPrimitive("color").getAsString(); + try { + color = Color.valueOf(colorName); + } catch (Exception e) { + // Ignored + } + } + } else { + regionsObject.add(name, regionOptions); } minX = config.getInt("minX"); @@ -174,7 +195,7 @@ public class Region { public void setColor(Color color) { this.color = color; - colors.add(name, new JsonPrimitive(color.name())); + regionOptions.add("color", new JsonPrimitive(color.name())); } private void setLinkedRegion(Consumer regionConsumer) { @@ -201,6 +222,7 @@ public class Region { public void setTntMode(TNTMode tntMode) { this.tntMode = tntMode; setLinkedRegion(region -> region.tntMode = tntMode); + regionOptions.add("tnt", new JsonPrimitive(tntMode.name())); } public boolean isFreeze() { @@ -210,6 +232,7 @@ public class Region { public void setFreeze(boolean freeze) { this.freeze = freeze; setLinkedRegion(region -> region.freeze = freeze); + regionOptions.add("freeze", new JsonPrimitive(freeze)); } public boolean isFire() { @@ -219,6 +242,7 @@ public class Region { public void setFire(boolean fire) { this.fire = fire; setLinkedRegion(region -> region.fire = fire); + regionOptions.add("fire", new JsonPrimitive(fire)); } public boolean inRegion(Location l) {