From 06333d634e3f28a81786bb474819c821463f67c4 Mon Sep 17 00:00:00 2001 From: YoyoNow Date: Sat, 2 Aug 2025 12:35:11 +0200 Subject: [PATCH] Fix RegionConfig Implement FixedRegion Add RegionHistory.Impl --- .../bausystem/region/RegionConfig.java | 3 +- .../bausystem/region/RegionHistory.java | 44 ++++ .../steamwar/bausystem/shared/SizedStack.java | 6 + .../bausystem/region/FixedRegionSystem.java | 4 + .../region/fixed/FixedFlagStorage.java | 8 +- .../bausystem/region/fixed/FixedRegion.java | 204 +++++++++++++++++- .../bausystem/region/fixed/Prototype.java | 8 +- 7 files changed, 257 insertions(+), 20 deletions(-) diff --git a/BauSystem/BauSystem_Main/src/de/steamwar/bausystem/region/RegionConfig.java b/BauSystem/BauSystem_Main/src/de/steamwar/bausystem/region/RegionConfig.java index 5c7da406..71e347c6 100644 --- a/BauSystem/BauSystem_Main/src/de/steamwar/bausystem/region/RegionConfig.java +++ b/BauSystem/BauSystem_Main/src/de/steamwar/bausystem/region/RegionConfig.java @@ -19,6 +19,7 @@ package de.steamwar.bausystem.region; +import de.steamwar.core.FlatteningWrapper; import lombok.AccessLevel; import lombok.Getter; import lombok.NoArgsConstructor; @@ -117,7 +118,7 @@ public class RegionConfig { private void load(ConfigurationSection section) { Active = section.getBoolean("Active", false); ObfuscateWith = Material.getMaterial(section.getString("ObfuscateWith", "END_STONE")); - HiddenBlocks = section.getStringList("HiddenBlocks").stream().map(Material::getMaterial).collect(Collectors.toUnmodifiableSet()); + HiddenBlocks = section.getStringList("HiddenBlocks").stream().map(FlatteningWrapper.impl::getMaterial).filter(Objects::nonNull).collect(Collectors.toUnmodifiableSet()); HiddenBlockEntities = Collections.unmodifiableSet(new HashSet<>(section.getStringList("HiddenBlockEntities"))); } } diff --git a/BauSystem/BauSystem_Main/src/de/steamwar/bausystem/region/RegionHistory.java b/BauSystem/BauSystem_Main/src/de/steamwar/bausystem/region/RegionHistory.java index 2eb79247..ef597cd2 100644 --- a/BauSystem/BauSystem_Main/src/de/steamwar/bausystem/region/RegionHistory.java +++ b/BauSystem/BauSystem_Main/src/de/steamwar/bausystem/region/RegionHistory.java @@ -20,7 +20,11 @@ package de.steamwar.bausystem.region; import com.sk89q.worldedit.EditSession; +import com.sk89q.worldedit.WorldEdit; +import com.sk89q.worldedit.bukkit.BukkitWorld; +import de.steamwar.bausystem.shared.SizedStack; import lombok.NonNull; +import org.bukkit.Bukkit; public interface RegionHistory { @@ -45,4 +49,44 @@ public interface RegionHistory { return false; } }; + + class Impl implements RegionHistory { + + private SizedStack undoSessions; + private SizedStack redoSessions; + + public Impl(int size) { + this.undoSessions = new SizedStack<>(size); + this.redoSessions = new SizedStack<>(size); + } + + @Override + public void remember(@NonNull EditSession editSession) { + undoSessions.push(editSession); + if (redoSessions.empty()) return; + redoSessions.clear(); + } + + @Override + public boolean undo() { + EditSession session = undoSessions.pop(); + if (session == null) return false; + try (EditSession e = WorldEdit.getInstance().getEditSessionFactory().getEditSession(new BukkitWorld(Bukkit.getWorlds().get(0)), -1)) { + session.undo(e); + redoSessions.push(e); + } + return true; + } + + @Override + public boolean redo() { + EditSession session = redoSessions.pop(); + if (session == null) return false; + try (EditSession e = WorldEdit.getInstance().getEditSessionFactory().getEditSession(new BukkitWorld(Bukkit.getWorlds().get(0)), -1)) { + session.redo(e); + undoSessions.push(e); + } + return true; + } + } } diff --git a/BauSystem/BauSystem_Main/src/de/steamwar/bausystem/shared/SizedStack.java b/BauSystem/BauSystem_Main/src/de/steamwar/bausystem/shared/SizedStack.java index 7d503b2e..6aa7b619 100644 --- a/BauSystem/BauSystem_Main/src/de/steamwar/bausystem/shared/SizedStack.java +++ b/BauSystem/BauSystem_Main/src/de/steamwar/bausystem/shared/SizedStack.java @@ -130,6 +130,12 @@ public class SizedStack { return size; } + public void clear() { + this.data = (T[]) new Object[this.maxSize]; + this.head = 0; + this.size = 0; + } + @Override public String toString() { final StringBuilder result = new StringBuilder("["); diff --git a/BauSystem/BauSystem_RegionFixed/src/de/steamwar/bausystem/region/FixedRegionSystem.java b/BauSystem/BauSystem_RegionFixed/src/de/steamwar/bausystem/region/FixedRegionSystem.java index 22ca45c5..8a817870 100644 --- a/BauSystem/BauSystem_RegionFixed/src/de/steamwar/bausystem/region/FixedRegionSystem.java +++ b/BauSystem/BauSystem_RegionFixed/src/de/steamwar/bausystem/region/FixedRegionSystem.java @@ -36,6 +36,10 @@ public class FixedRegionSystem implements RegionSystem { private static final Map REGION_MAP = new HashMap<>(); + public static void addRegion(Region region) { + REGION_MAP.put(region.getID(), region); + } + @Override public void load() { PrototypeLoader.load(); diff --git a/BauSystem/BauSystem_RegionFixed/src/de/steamwar/bausystem/region/fixed/FixedFlagStorage.java b/BauSystem/BauSystem_RegionFixed/src/de/steamwar/bausystem/region/fixed/FixedFlagStorage.java index 5a26c0fa..c860f903 100644 --- a/BauSystem/BauSystem_RegionFixed/src/de/steamwar/bausystem/region/fixed/FixedFlagStorage.java +++ b/BauSystem/BauSystem_RegionFixed/src/de/steamwar/bausystem/region/fixed/FixedFlagStorage.java @@ -23,7 +23,6 @@ import de.steamwar.bausystem.region.FlagOptional; import de.steamwar.bausystem.region.FlagStorage; import de.steamwar.bausystem.region.RegionFlagPolicy; import de.steamwar.bausystem.region.flags.Flag; -import de.steamwar.bausystem.region.flags.TestblockMode; import de.steamwar.bausystem.worlddata.WorldData; import de.steamwar.core.Core; import lombok.NonNull; @@ -34,12 +33,10 @@ import java.util.Map; public class FixedFlagStorage implements FlagStorage { - private TestblockMode testBlockMode; private Map, Flag.Value> flagMap = new HashMap<>(); private YAPIONObject data; - public FixedFlagStorage(TestblockMode testblockMode, YAPIONObject data) { - this.testBlockMode = testblockMode; + public FixedFlagStorage(YAPIONObject data) { this.data = data; for (final Flag flag : Flag.getFlags()) { if (!has(flag).isWritable()) continue; @@ -79,9 +76,6 @@ public class FixedFlagStorage implements FlagStorage { @Override public @NonNull & Flag.Value> FlagOptional get(@NonNull Flag flag) { - if (flag.oneOf(Flag.TESTBLOCK)) { - return FlagOptional.of((Flag) flag, testBlockMode); - } return FlagOptional.of(flag, (T) flagMap.get(flag)); } diff --git a/BauSystem/BauSystem_RegionFixed/src/de/steamwar/bausystem/region/fixed/FixedRegion.java b/BauSystem/BauSystem_RegionFixed/src/de/steamwar/bausystem/region/fixed/FixedRegion.java index 2f914dc5..7409de36 100644 --- a/BauSystem/BauSystem_RegionFixed/src/de/steamwar/bausystem/region/fixed/FixedRegion.java +++ b/BauSystem/BauSystem_RegionFixed/src/de/steamwar/bausystem/region/fixed/FixedRegion.java @@ -19,9 +19,18 @@ package de.steamwar.bausystem.region.fixed; +import com.sk89q.worldedit.EditSession; +import de.steamwar.bausystem.BauSystem; import de.steamwar.bausystem.region.*; +import de.steamwar.bausystem.region.flags.Flag; +import de.steamwar.bausystem.region.flags.TestblockMode; +import de.steamwar.bausystem.utils.PasteBuilder; +import de.steamwar.core.Core; import lombok.NonNull; +import yapion.hierarchy.types.YAPIONObject; +import javax.annotation.Nullable; +import java.io.File; import java.nio.charset.StandardCharsets; import java.util.UUID; @@ -31,10 +40,191 @@ public class FixedRegion implements Region { private FixedFlagStorage flagStorage; private Prototype prototype; - public FixedRegion(String name, FixedFlagStorage flagStorage, Prototype prototype) { + private final Area area; + private final Area build; + private final Area testblock; + private final int floorLevel; + private final int waterLevel; + private final RegionConfig regionConfig; + private final RegionHistory regionHistory = new RegionHistory.Impl(20); + + public FixedRegion(String name, FixedFlagStorage flagStorage, Prototype prototype, YAPIONObject regionConfig, YAPIONObject regionData) { uuid = UUID.nameUUIDFromBytes(name.getBytes(StandardCharsets.UTF_8)); this.flagStorage = flagStorage; this.prototype = prototype; + System.out.println(name + " " + prototype + " " + flagStorage); + + Point minPoint; + if (regionConfig.containsKey("minX", Integer.class) && regionConfig.containsKey("minY", Integer.class) && regionConfig.containsKey("minZ", Integer.class)) { + minPoint = new Point(regionConfig.getInt("minX"), regionConfig.getInt("minY"), regionConfig.getInt("minZ")); + } else { + throw new IllegalArgumentException("minX and minY and minZ are required"); + } + Point maxPoint = minPoint.add(prototype.getSizeX() - 1, prototype.getSizeY() - 1, prototype.getSizeZ() - 1); + + area = new Area() { + @Override + public @NonNull Point getMinPoint(boolean extension) { + return minPoint; + } + + @Override + public @NonNull Point getMaxPoint(boolean extension) { + return maxPoint; + } + + @Override + public @NonNull Point getCopyPoint() { + return minPoint; + } + + @Nullable + @Override + public EditSession copy(boolean extension) { + return null; // TODO: Implement + } + + @Nullable + @Override + public File getResetFile() { + return null; // TODO: Implement + } + + @Override + public void reset(PasteBuilder pasteBuilder, boolean extension) { + // TODO: Implement + } + }; + + if (prototype.getBuild() != null) { + Point minPointBuild = minPoint.add(prototype.getBuild().getOffsetX(), prototype.getBuild().getOffsetY(), prototype.getBuild().getOffsetZ()); + Point maxPointBuild = minPointBuild.add(prototype.getBuild().getSizeX() - 1, prototype.getBuild().getSizeY() - 1, prototype.getBuild().getSizeZ() - 1); + + Point minPointBuildExtension = minPointBuild.subtract(prototype.getBuild().getExtensionNegativeX(), prototype.getBuild().getExtensionNegativeY(), prototype.getBuild().getExtensionNegativeZ()); + Point maxPointBuildExtension = maxPointBuild.add(prototype.getBuild().getExtensionPositiveX(), prototype.getBuild().getExtensionPositiveY(), prototype.getBuild().getExtensionPositiveZ()); + + Point copyPoint; + if (!prototype.getBuild().isHasCopyPoint() && (prototype.getCopyPointOffsetX() != 0 || prototype.getCopyPointOffsetY() != 0 || prototype.getCopyPointOffsetZ() != 0)) { + copyPoint = minPoint.add(prototype.getCopyPointOffsetX(), prototype.getCopyPointOffsetY(), prototype.getCopyPointOffsetZ()); + } else if (prototype.getBuild().getCopyOffsetX() != 0 || prototype.getBuild().getCopyOffsetY() != 0 || prototype.getBuild().getCopyOffsetZ() != 0) { + copyPoint = minPointBuild.add(prototype.getBuild().getCopyOffsetX(), prototype.getBuild().getCopyOffsetY(), prototype.getBuild().getCopyOffsetZ()); + } else { + copyPoint = minPointBuild.add(prototype.getBuild().getSizeX() / 2, 0, prototype.getBuild().getSizeZ()); + } + + build = new Area() { + @Override + public @NonNull Point getMinPoint(boolean extension) { + return extension ? minPointBuildExtension : minPointBuild; + } + + @Override + public @NonNull Point getMaxPoint(boolean extension) { + return extension ? maxPointBuildExtension : maxPointBuild; + } + + @Override + public @NonNull Point getCopyPoint() { + return copyPoint; + } + + @Nullable + @Override + public EditSession copy(boolean extension) { + return null; // TODO: Implement + } + + @Nullable + @Override + public File getResetFile() { + return null; // TODO: Implement + } + + @Override + public void reset(PasteBuilder pasteBuilder, boolean extension) { + // TODO: Implement + } + }; + } else { + build = Area.EMPTY; + } + + if (prototype.getTestblock() != null) { + Point minPointTestblock = minPoint.add(prototype.getTestblock().getOffsetX(), prototype.getTestblock().getOffsetY(), prototype.getTestblock().getOffsetZ()); + Point maxPointTestblock = minPointTestblock.add(prototype.getTestblock().getSizeX() - 1, prototype.getTestblock().getSizeY() - 1, prototype.getTestblock().getSizeZ() - 1); + + Point minPointTestblockExtension = minPointTestblock.subtract(prototype.getTestblock().getExtensionNegativeX(), prototype.getTestblock().getExtensionNegativeY(), prototype.getTestblock().getExtensionNegativeZ()); + Point maxPointTestblockExtension = maxPointTestblock.add(prototype.getTestblock().getExtensionPositiveX(), prototype.getTestblock().getExtensionPositiveY(), prototype.getTestblock().getExtensionPositiveZ()); + + Point copyPoint; + if (!prototype.getTestblock().isHasCopyPoint() && (prototype.getCopyPointOffsetX() != 0 || prototype.getCopyPointOffsetY() != 0 || prototype.getCopyPointOffsetZ() != 0)) { + copyPoint = minPoint.add(prototype.getCopyPointOffsetX(), prototype.getCopyPointOffsetY(), prototype.getCopyPointOffsetZ()); + } else if (prototype.getTestblock().getCopyOffsetX() != 0 || prototype.getTestblock().getCopyOffsetY() != 0 || prototype.getTestblock().getCopyOffsetZ() != 0) { + copyPoint = minPointTestblock.add(prototype.getTestblock().getCopyOffsetX(), prototype.getTestblock().getCopyOffsetY(), prototype.getTestblock().getCopyOffsetZ()); + } else { + copyPoint = minPointTestblock.add(prototype.getTestblock().getSizeX() / 2, 0, prototype.getTestblock().getSizeZ()); + } + + testblock = new Area() { + @Override + public @NonNull Point getMinPoint(boolean extension) { + return extension ? minPointTestblockExtension : minPointTestblock; + } + + @Override + public @NonNull Point getMaxPoint(boolean extension) { + return extension ? maxPointTestblockExtension : maxPointTestblock; + } + + @Override + public @NonNull Point getCopyPoint() { + return copyPoint; + } + + @Nullable + @Override + public EditSession copy(boolean extension) { + return null; // TODO: Implement + } + + @Nullable + @Override + public File getResetFile() { + return null; // TODO: Implement + } + + @Override + public void reset(PasteBuilder pasteBuilder, boolean extension) { + // TODO: Implement + } + }; + } else { + testblock = Area.EMPTY; + } + + if (!testblock.isEmpty() && !build.isEmpty()) { + if (testblock.getMinPoint(false).getZ() > build.getMinPoint(false).getZ()) { + flagStorage.getBackedMap().put(Flag.TESTBLOCK, TestblockMode.SOUTH); + } else { + flagStorage.getBackedMap().put(Flag.TESTBLOCK, TestblockMode.NORTH); + } + } else { + flagStorage.getBackedMap().put(Flag.TESTBLOCK, TestblockMode.NO_VALUE); + } + + floorLevel = prototype.getFloorOffset() != 0 ? minPoint.getY() + prototype.getFloorOffset() : 0; + waterLevel = prototype.getWaterOffset() != 0 ? minPoint.getX() + prototype.getWaterOffset() : 0; + + File found = null; + File baseFile = new File(BauSystem.getInstance().getDataFolder().getParentFile(), "FightSystem"); + for (int version = Core.getVersion(); version >= 15; version--) { + File specific = new File(baseFile, prototype.getDisplayName() + version + ".yml"); + if (specific.exists()) { + found = specific; + break; + } + } + this.regionConfig = new RegionConfig(found); } @Override @@ -54,31 +244,31 @@ public class FixedRegion implements Region { @Override public @NonNull Area getArea() { - return null; + return area; } @Override public @NonNull Area getBuildArea() { - return null; + return build; } @Override public @NonNull Area getTestblockArea() { - return null; + return testblock; } @Override public @NonNull RegionConfig getGameModeConfig() { - return null; + return regionConfig; } @Override public @NonNull RegionHistory getHistory() { - return null; + return regionHistory; } @Override public @NonNull RegionBackups getBackups() { - return null; + return RegionBackups.EMPTY; } } diff --git a/BauSystem/BauSystem_RegionFixed/src/de/steamwar/bausystem/region/fixed/Prototype.java b/BauSystem/BauSystem_RegionFixed/src/de/steamwar/bausystem/region/fixed/Prototype.java index 804e25f4..3e4a7248 100644 --- a/BauSystem/BauSystem_RegionFixed/src/de/steamwar/bausystem/region/fixed/Prototype.java +++ b/BauSystem/BauSystem_RegionFixed/src/de/steamwar/bausystem/region/fixed/Prototype.java @@ -19,8 +19,7 @@ package de.steamwar.bausystem.region.fixed; -import de.steamwar.bausystem.region.FlagStorage; -import de.steamwar.bausystem.region.flags.TestblockMode; +import de.steamwar.bausystem.region.FixedRegionSystem; import lombok.AllArgsConstructor; import lombok.Getter; import yapion.hierarchy.types.YAPIONObject; @@ -217,8 +216,7 @@ public class Prototype { } else { prototype = PROTOTYPE_MAP.get(regionConfig.getPlainValue("prototype")); } - FlagStorage flagStorage = new FixedFlagStorage(TestblockMode.NO_VALUE, regionData.getObjectOrSetDefault("flagStorage", new YAPIONObject())); - System.out.println(name + " " + prototype + " " + flagStorage); - // return new Region(name, prototype, regionConfig, flagStorage, regionData); + FixedFlagStorage flagStorage = new FixedFlagStorage(regionData.getObjectOrSetDefault("flagStorage", new YAPIONObject())); + FixedRegionSystem.addRegion(new FixedRegion(name, flagStorage, prototype, regionConfig, regionData)); } }