diff --git a/BauSystem_Main/src/de/steamwar/bausystem/BauSystem.java b/BauSystem_Main/src/de/steamwar/bausystem/BauSystem.java index f989a3b6..2dcb4ac7 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/BauSystem.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/BauSystem.java @@ -22,6 +22,7 @@ package de.steamwar.bausystem; import de.steamwar.bausystem.config.ColorConfig; import de.steamwar.bausystem.linkage.LinkageUtils; import de.steamwar.bausystem.region.loader.PrototypeLoader; +import de.steamwar.bausystem.region.loader.RegionLoader; import lombok.Getter; import org.bukkit.event.Listener; import org.bukkit.plugin.java.JavaPlugin; @@ -39,6 +40,7 @@ public class BauSystem extends JavaPlugin implements Listener { SWUtils.setBausystem(instance); PrototypeLoader.load(); + RegionLoader.load(); LinkageUtils.link(); } diff --git a/BauSystem_Main/src/de/steamwar/bausystem/region/FlagStorage.java b/BauSystem_Main/src/de/steamwar/bausystem/region/FlagStorage.java index e31aa420..a08db55b 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/region/FlagStorage.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/region/FlagStorage.java @@ -19,17 +19,11 @@ package de.steamwar.bausystem.region; -import com.google.gson.JsonObject; -import com.google.gson.JsonPrimitive; import de.steamwar.bausystem.region.flags.Flag; +import yapion.annotations.object.YAPIONData; import java.util.EnumMap; import java.util.Map; -import yapion.annotations.object.YAPIONData; -import yapion.hierarchy.api.groups.YAPIONAnyType; -import yapion.hierarchy.types.YAPIONObject; -import yapion.hierarchy.types.YAPIONValue; - @YAPIONData public class FlagStorage { diff --git a/BauSystem_Main/src/de/steamwar/bausystem/region/GlobalRegion.java b/BauSystem_Main/src/de/steamwar/bausystem/region/GlobalRegion.java index 06f0e25b..cae2bec9 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/region/GlobalRegion.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/region/GlobalRegion.java @@ -19,5 +19,25 @@ package de.steamwar.bausystem.region; -public class GlobalRegion { +import de.steamwar.bausystem.region.utils.RegionExtensionType; +import de.steamwar.bausystem.region.utils.RegionType; +import lombok.Getter; +import org.bukkit.Location; +import yapion.hierarchy.types.YAPIONObject; + +public class GlobalRegion extends Region { + + @Getter + static GlobalRegion instance; + + public GlobalRegion(FlagStorage flagStorage) { + super(null, new YAPIONObject(), flagStorage); + + instance = this; + } + + @Override + public boolean inRegion(Location location, RegionType regionType, RegionExtensionType regionExtensionType) { + return true; + } } diff --git a/BauSystem_Main/src/de/steamwar/bausystem/region/Prototype.java b/BauSystem_Main/src/de/steamwar/bausystem/region/Prototype.java index 9571754c..b57c0b0a 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/region/Prototype.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/region/Prototype.java @@ -19,9 +19,11 @@ package de.steamwar.bausystem.region; +import de.steamwar.bausystem.region.flags.Flag; import lombok.Getter; import yapion.hierarchy.types.YAPIONObject; import yapion.hierarchy.types.YAPIONType; +import yapion.serializing.YAPIONDeserializer; import java.io.File; import java.util.HashMap; @@ -30,7 +32,7 @@ import java.util.Map; @Getter public class Prototype { - private static final Map PROTOTYPE_MAP = new HashMap<>(); + static final Map PROTOTYPE_MAP = new HashMap<>(); private final String name; private final String displayName; @@ -136,7 +138,19 @@ public class Prototype { } public static Region generateRegion(YAPIONObject regionConfig, YAPIONObject regionData) { - return null; + Prototype prototype; + if (regionData.containsKey("prototype", String.class)) { + prototype = PROTOTYPE_MAP.get(regionData.getPlainValue("prototype")); + } else { + prototype = PROTOTYPE_MAP.get(regionConfig.getPlainValue("prototype")); + } + FlagStorage flagStorage; + if (regionData.containsKey("flagStorage", YAPIONType.OBJECT)) { + flagStorage = (FlagStorage) YAPIONDeserializer.deserialize(regionData.getObject("flagStorage")); + } else { + flagStorage = new FlagStorage(); + } + return new Region(prototype, regionConfig, flagStorage); } } diff --git a/BauSystem_Main/src/de/steamwar/bausystem/region/Region.java b/BauSystem_Main/src/de/steamwar/bausystem/region/Region.java index e482fc89..78b24ebc 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/region/Region.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/region/Region.java @@ -19,15 +19,35 @@ package de.steamwar.bausystem.region; +import de.steamwar.bausystem.region.utils.RegionExtensionType; +import de.steamwar.bausystem.region.utils.RegionType; import lombok.Getter; +import org.bukkit.Location; +import yapion.hierarchy.types.YAPIONObject; +import yapion.hierarchy.types.YAPIONType; +import yapion.hierarchy.types.YAPIONValue; +import java.util.ArrayList; +import java.util.HashSet; +import java.util.List; import java.util.Set; @Getter public class Region { + private static final List REGION_LIST = new ArrayList<>(); + + public static Region getRegion(Location location) { + for (Region region : REGION_LIST) { + if (region.inRegion(location, RegionType.NORMAL, RegionExtensionType.NORMAL)) { + return region; + } + } + return GlobalRegion.instance; + } + private Prototype prototype; - private Set alternativePrototypes; + private Set prototypes; private Point minPoint; private Point maxPoint; @@ -48,4 +68,35 @@ public class Region { private FlagStorage flagStorage; + public Region(Prototype prototype, YAPIONObject regionConfig, FlagStorage flagStorage) { + if (prototype != null) { + REGION_LIST.add(this); + } + + prototypes = new HashSet<>(); + if (regionConfig.containsKey("prototypes", YAPIONType.ARRAY)) { + regionConfig.getArray("prototypes").forEach(yapionAnyType -> { + if (yapionAnyType instanceof YAPIONValue) { + prototypes.add(Prototype.PROTOTYPE_MAP.get(((YAPIONValue) yapionAnyType).get())); + } + }); + } + this.flagStorage = flagStorage; + generatePrototypeData(prototype); + } + + private void generatePrototypeData(Prototype prototype) { + if (prototype != null && !prototypes.contains(prototype)) { + return; + } + this.prototype = prototype; + + // TODO: implement generation + } + + public boolean inRegion(Location location, RegionType regionType, RegionExtensionType regionExtensionType) { + // TODO: implement inRegion + return false; + } + } diff --git a/BauSystem_Main/src/de/steamwar/bausystem/region/loader/PrototypeLoader.java b/BauSystem_Main/src/de/steamwar/bausystem/region/loader/PrototypeLoader.java index c504f073..8ecc6d0b 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/region/loader/PrototypeLoader.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/region/loader/PrototypeLoader.java @@ -22,6 +22,8 @@ package de.steamwar.bausystem.region.loader; import de.steamwar.bausystem.region.Prototype; import lombok.experimental.UtilityClass; import org.bukkit.Bukkit; +import yapion.hierarchy.output.StringOutput; +import yapion.hierarchy.output.SystemOutput; import yapion.hierarchy.types.YAPIONObject; import yapion.parser.YAPIONParser; 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 3ce6a823..4dbb33ef 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/region/loader/RegionLoader.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/region/loader/RegionLoader.java @@ -19,12 +19,69 @@ package de.steamwar.bausystem.region.loader; +import de.steamwar.bausystem.region.FlagStorage; +import de.steamwar.bausystem.region.GlobalRegion; +import de.steamwar.bausystem.region.Prototype; import lombok.experimental.UtilityClass; +import org.bukkit.Bukkit; +import yapion.hierarchy.types.YAPIONObject; +import yapion.hierarchy.types.YAPIONType; +import yapion.parser.YAPIONParser; +import yapion.serializing.YAPIONDeserializer; + +import java.io.BufferedInputStream; +import java.io.File; +import java.io.FileInputStream; +import java.io.IOException; +import java.util.logging.Level; @UtilityClass public class RegionLoader { public void load() { + File file = new File(Bukkit.getWorlds().get(0).getWorldFolder(), "regions.yapion"); + YAPIONObject yapionObject = null; + try (BufferedInputStream bufferedInputStream = new BufferedInputStream(new FileInputStream(file))) { + yapionObject = YAPIONParser.parse(bufferedInputStream); + } catch (IOException e) { + Bukkit.getLogger().log(Level.SEVERE, e.getMessage(), e); + Bukkit.shutdown(); + return; + } + File optionsFile = new File(Bukkit.getWorlds().get(0).getWorldFolder(), "options.yapion"); + 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; + } + if (!(yapionAnyType instanceof YAPIONObject)) { + return; + } + + YAPIONObject regionConfig = (YAPIONObject) yapionAnyType; + YAPIONObject regionData = new YAPIONObject(); + if (finalOptionsYapionObject.containsKey(key, YAPIONType.OBJECT)) { + regionData = finalOptionsYapionObject.getObject(key); + } + + Prototype.generateRegion(regionConfig, regionData); + }); + + YAPIONObject globalOptions = optionsYapionObject.getObject("global"); + FlagStorage flagStorage; + if (globalOptions != null && globalOptions.containsKey("flagStorage", YAPIONType.OBJECT)) { + flagStorage = (FlagStorage) YAPIONDeserializer.deserialize(globalOptions.getObject("flagStorage")); + } else { + flagStorage = new FlagStorage(); + } + new GlobalRegion(flagStorage); } } diff --git a/BauSystem_Main/src/de/steamwar/bausystem/region/utils/RegionExtensionType.java b/BauSystem_Main/src/de/steamwar/bausystem/region/utils/RegionExtensionType.java new file mode 100644 index 00000000..446e0d97 --- /dev/null +++ b/BauSystem_Main/src/de/steamwar/bausystem/region/utils/RegionExtensionType.java @@ -0,0 +1,25 @@ +/* + * This file is a part of the SteamWar software. + * + * Copyright (C) 2020 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 . + */ + +package de.steamwar.bausystem.region.utils; + +public enum RegionExtensionType { + NORMAL, + EXTENSION +} diff --git a/BauSystem_Main/src/de/steamwar/bausystem/region/utils/RegionType.java b/BauSystem_Main/src/de/steamwar/bausystem/region/utils/RegionType.java new file mode 100644 index 00000000..e3b45030 --- /dev/null +++ b/BauSystem_Main/src/de/steamwar/bausystem/region/utils/RegionType.java @@ -0,0 +1,26 @@ +/* + * This file is a part of the SteamWar software. + * + * Copyright (C) 2020 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 . + */ + +package de.steamwar.bausystem.region.utils; + +public enum RegionType { + NORMAL, + BUILD, + TESTBLOCK +} diff --git a/BauSystem_Main/src/de/steamwar/bausystem/utils/.gitkeep b/BauSystem_Main/src/de/steamwar/bausystem/utils/.gitkeep deleted file mode 100644 index e69de29b..00000000