From bdeaa673839ef4b92f767eeec55fa8efea94d02f Mon Sep 17 00:00:00 2001 From: yoyosource Date: Sun, 18 Apr 2021 18:37:32 +0200 Subject: [PATCH] Add RegionLoader.save --- .../src/de/steamwar/bausystem/BauSystem.java | 2 ++ .../bausystem/region/GlobalRegion.java | 4 +-- .../steamwar/bausystem/region/Prototype.java | 2 +- .../de/steamwar/bausystem/region/Region.java | 26 ++++++++++++++++++- .../bausystem/region/loader/RegionLoader.java | 20 ++++++++++---- 5 files changed, 45 insertions(+), 9 deletions(-) diff --git a/BauSystem_Main/src/de/steamwar/bausystem/BauSystem.java b/BauSystem_Main/src/de/steamwar/bausystem/BauSystem.java index 2dcb4ac7..f9042ec1 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/BauSystem.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/BauSystem.java @@ -48,5 +48,7 @@ public class BauSystem extends JavaPlugin implements Listener { @Override public void onDisable() { LinkageUtils.unlink(); + + RegionLoader.save(); } } \ No newline at end of file diff --git a/BauSystem_Main/src/de/steamwar/bausystem/region/GlobalRegion.java b/BauSystem_Main/src/de/steamwar/bausystem/region/GlobalRegion.java index cae2bec9..5f236e85 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/region/GlobalRegion.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/region/GlobalRegion.java @@ -30,8 +30,8 @@ public class GlobalRegion extends Region { @Getter static GlobalRegion instance; - public GlobalRegion(FlagStorage flagStorage) { - super(null, new YAPIONObject(), flagStorage); + public GlobalRegion(FlagStorage flagStorage, YAPIONObject regionData) { + super(null, new YAPIONObject(), flagStorage, regionData); instance = this; } diff --git a/BauSystem_Main/src/de/steamwar/bausystem/region/Prototype.java b/BauSystem_Main/src/de/steamwar/bausystem/region/Prototype.java index b57c0b0a..31ce8786 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/region/Prototype.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/region/Prototype.java @@ -150,7 +150,7 @@ public class Prototype { } else { flagStorage = new FlagStorage(); } - return new Region(prototype, regionConfig, flagStorage); + return new Region(prototype, regionConfig, flagStorage, regionData); } } diff --git a/BauSystem_Main/src/de/steamwar/bausystem/region/Region.java b/BauSystem_Main/src/de/steamwar/bausystem/region/Region.java index fdd814f7..503e5334 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/region/Region.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/region/Region.java @@ -19,13 +19,16 @@ package de.steamwar.bausystem.region; +import de.steamwar.bausystem.region.flags.Flag; import de.steamwar.bausystem.region.utils.RegionExtensionType; import de.steamwar.bausystem.region.utils.RegionType; import lombok.Getter; +import lombok.NonNull; import org.bukkit.Location; import yapion.hierarchy.types.YAPIONObject; import yapion.hierarchy.types.YAPIONType; import yapion.hierarchy.types.YAPIONValue; +import yapion.serializing.YAPIONSerializer; import java.util.ArrayList; import java.util.HashSet; @@ -46,6 +49,8 @@ public class Region { return GlobalRegion.instance; } + private YAPIONObject regionData; + private Prototype prototype; private Set prototypes; @@ -68,7 +73,8 @@ public class Region { private FlagStorage flagStorage; - public Region(Prototype prototype, YAPIONObject regionConfig, FlagStorage flagStorage) { + public Region(Prototype prototype, YAPIONObject regionConfig, FlagStorage flagStorage, YAPIONObject regionData) { + this.regionData = regionData; if (prototype != null) { REGION_LIST.add(this); } @@ -159,4 +165,22 @@ public class Region { public String getDisplayName() { return prototype != null ? prototype.getDisplayName() : ""; } + + public void setPrototype(@NonNull Prototype prototype) { + if (this.prototype == null) { + return; + } + regionData.add("prototype", prototype.getName()); + generatePrototypeData(prototype, minPoint); + } + + public void set(Flag flagType, Flag.Value value) { + if (flagStorage.set(flagType, value)) { + regionData.add("flagStorage", YAPIONSerializer.serialize(flagStorage)); + } + } + + public & Flag.Value> Flag.Value get(Flag flagType) { + return flagStorage.get(flagType); + } } \ No newline at end of file diff --git a/BauSystem_Main/src/de/steamwar/bausystem/region/loader/RegionLoader.java b/BauSystem_Main/src/de/steamwar/bausystem/region/loader/RegionLoader.java index 4dbb33ef..19f86a56 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/region/loader/RegionLoader.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/region/loader/RegionLoader.java @@ -24,6 +24,7 @@ import de.steamwar.bausystem.region.GlobalRegion; import de.steamwar.bausystem.region.Prototype; import lombok.experimental.UtilityClass; import org.bukkit.Bukkit; +import yapion.hierarchy.output.FileOutput; import yapion.hierarchy.types.YAPIONObject; import yapion.hierarchy.types.YAPIONType; import yapion.parser.YAPIONParser; @@ -38,6 +39,16 @@ import java.util.logging.Level; @UtilityClass public class RegionLoader { + private YAPIONObject optionsYapionObject; + + public void save() { + try { + optionsYapionObject.toYAPION(new FileOutput(new File(Bukkit.getWorlds().get(0).getWorldFolder(), "options.yapion"))).close(); + } catch (IOException e) { + // Ignored + } + } + public void load() { File file = new File(Bukkit.getWorlds().get(0).getWorldFolder(), "regions.yapion"); YAPIONObject yapionObject = null; @@ -50,14 +61,13 @@ public class RegionLoader { } File optionsFile = new File(Bukkit.getWorlds().get(0).getWorldFolder(), "options.yapion"); - YAPIONObject optionsYapionObject = new YAPIONObject(); + optionsYapionObject = new YAPIONObject(); try (BufferedInputStream bufferedInputStream = new BufferedInputStream(new FileInputStream(optionsFile))) { optionsYapionObject = YAPIONParser.parse(bufferedInputStream); } catch (IOException e) { // Ignored } - YAPIONObject finalOptionsYapionObject = optionsYapionObject; yapionObject.forEach((key, yapionAnyType) -> { if (key.equals("global")) { return; @@ -68,8 +78,8 @@ public class RegionLoader { YAPIONObject regionConfig = (YAPIONObject) yapionAnyType; YAPIONObject regionData = new YAPIONObject(); - if (finalOptionsYapionObject.containsKey(key, YAPIONType.OBJECT)) { - regionData = finalOptionsYapionObject.getObject(key); + if (optionsYapionObject.containsKey(key, YAPIONType.OBJECT)) { + regionData = optionsYapionObject.getObject(key); } Prototype.generateRegion(regionConfig, regionData); @@ -82,6 +92,6 @@ public class RegionLoader { } else { flagStorage = new FlagStorage(); } - new GlobalRegion(flagStorage); + new GlobalRegion(flagStorage, globalOptions); } }