From 2116e1ee8d4fa064f713b0771893a6d2889ed6ad Mon Sep 17 00:00:00 2001 From: YoyoNow Date: Wed, 4 Mar 2026 19:19:08 +0100 Subject: [PATCH] Fix runtime errors --- .../steamwar/bausystem/region/RegionType.java | 35 ++++++++++------- .../region/DynamicRegionCommand.java | 10 ++--- .../bausystem/region/DynamicRegionSystem.java | 39 +++++++------------ .../region/dynamic/DynamicRegion.java | 6 ++- .../dynamic/DynamicRegionRepository.java | 4 +- .../bausystem/region/dynamic/Tile.java | 24 ++++-------- .../region/dynamic/path/PathArea.java | 16 ++++---- .../region/dynamic/path/PathRegion.java | 8 +++- .../region/dynamic/special/dry/DryRegion.java | 8 +++- .../region/dynamic/special/wet/WetRegion.java | 8 +++- 10 files changed, 79 insertions(+), 79 deletions(-) diff --git a/BauSystem/BauSystem_Main/src/de/steamwar/bausystem/region/RegionType.java b/BauSystem/BauSystem_Main/src/de/steamwar/bausystem/region/RegionType.java index ee596787..a204a68d 100644 --- a/BauSystem/BauSystem_Main/src/de/steamwar/bausystem/region/RegionType.java +++ b/BauSystem/BauSystem_Main/src/de/steamwar/bausystem/region/RegionType.java @@ -26,28 +26,37 @@ import lombok.RequiredArgsConstructor; @Getter public enum RegionType { - GLOBAL(true, false, true, ConnectionType.Global), + GLOBAL(ConnectionType.Global), /** * This should not be used by the DynamicRegionSystem */ - NORMAL(false, true, false, ConnectionType.Closed), + NORMAL(ConnectionType.Closed), - SPAWN(false, false, true, ConnectionType.Closed), - SPAWN_PATH(false, false, true, ConnectionType.Path), - SPAWN_EXTENSION(false, false, false, ConnectionType.Closed), - PATH(false, false, false, ConnectionType.Path), + SPAWN(ConnectionType.Closed), + SPAWN_PATH(ConnectionType.Path), + SPAWN_EXTENSION(ConnectionType.Closed), + PATH(ConnectionType.Path), - DRY(false, true, false, ConnectionType.Closed), - DRY_SPECIAL(false, false, false, ConnectionType.Closed), - WET(false, true, false, ConnectionType.Water), - WET_SPECIAL(false, false, false, ConnectionType.Water), + DRY(ConnectionType.Closed), + DRY_SPECIAL(ConnectionType.Closed), + WET(ConnectionType.Water), + WET_SPECIAL(ConnectionType.Water), ; - private final boolean global; - private final boolean createBackup; - private final boolean cannotDelete; private final ConnectionType connectionType; + public boolean isGlobal() { + return this == GLOBAL; + } + + public boolean isDeletable() { + return this == NORMAL || this == SPAWN_EXTENSION || this == PATH || this == DRY || this == DRY_SPECIAL || this == WET || this == WET_SPECIAL; + } + + public boolean isSpecial() { + return this == DRY_SPECIAL || this == WET_SPECIAL; + } + public boolean isSpawn() { return this == SPAWN || this == SPAWN_PATH || this == SPAWN_EXTENSION; } diff --git a/BauSystem/BauSystem_RegionDynamic/src/de/steamwar/bausystem/region/DynamicRegionCommand.java b/BauSystem/BauSystem_RegionDynamic/src/de/steamwar/bausystem/region/DynamicRegionCommand.java index bef8c7c9..d0c5547b 100644 --- a/BauSystem/BauSystem_RegionDynamic/src/de/steamwar/bausystem/region/DynamicRegionCommand.java +++ b/BauSystem/BauSystem_RegionDynamic/src/de/steamwar/bausystem/region/DynamicRegionCommand.java @@ -23,14 +23,12 @@ import de.steamwar.bausystem.features.region.RegionCommand; import de.steamwar.bausystem.region.dynamic.DynamicRegion; import de.steamwar.bausystem.region.dynamic.Tile; import de.steamwar.bausystem.region.dynamic.path.PathRegion; -import de.steamwar.bausystem.region.dynamic.path.PathRegionData; import de.steamwar.bausystem.utils.PasteBuilder; import de.steamwar.command.AbstractSWCommand; import de.steamwar.command.SWCommand; import org.bukkit.entity.Player; import java.util.UUID; -import java.util.stream.Collectors; @AbstractSWCommand.PartOf(RegionCommand.class) public class DynamicRegionCommand extends SWCommand { @@ -43,22 +41,20 @@ public class DynamicRegionCommand extends SWCommand { public void placeRegion(Player player) { Region region = DynamicRegionSystem.INSTANCE.get(player.getLocation()); if (!region.getType().isGlobal()) return; - Tile tile = Tile.fromLocation(player.getLocation()).validOrNull(); + Tile tile = Tile.fromLocation(player.getLocation()).orElse(null); if (tile == null) return; // TODO: Replace! PathRegion dynamicRegion = new PathRegion(UUID.randomUUID(), tile.getMinX(), tile.getMinZ()); - dynamicRegion.setRegionData(new PathRegionData(dynamicRegion)); dynamicRegion.getArea().reset(new PasteBuilder(new PasteBuilder.FileProvider(dynamicRegion.getArea().getResetFile())), false); - DynamicRegionSystem.INSTANCE.getNeighbours(dynamicRegion).collect(Collectors.toList()) - .forEach(r -> r.update(dynamicRegion)); + DynamicRegionSystem.INSTANCE.getNeighbours(dynamicRegion).forEach(r -> r.update(dynamicRegion)); } @Register({"dynamic", "delete"}) public void deleteRegion(Player player) { Region region = DynamicRegionSystem.INSTANCE.get(player.getLocation()); - if (region.getType().isCannotDelete()) return; + if (!region.getType().isDeletable()) return; ((DynamicRegion) region).delete(); } } diff --git a/BauSystem/BauSystem_RegionDynamic/src/de/steamwar/bausystem/region/DynamicRegionSystem.java b/BauSystem/BauSystem_RegionDynamic/src/de/steamwar/bausystem/region/DynamicRegionSystem.java index a59a886a..dd8bc8cc 100644 --- a/BauSystem/BauSystem_RegionDynamic/src/de/steamwar/bausystem/region/DynamicRegionSystem.java +++ b/BauSystem/BauSystem_RegionDynamic/src/de/steamwar/bausystem/region/DynamicRegionSystem.java @@ -38,6 +38,7 @@ import java.util.stream.Stream; public class DynamicRegionSystem implements RegionSystem { + private static final int TILE_SIZE_ADJUSTED = Tile.tileSize - 1; public static DynamicRegionSystem INSTANCE; private static final Map regionCache = new LinkedHashMap<>(16, 0.75f, true) { @@ -100,10 +101,12 @@ public class DynamicRegionSystem implements RegionSystem { } public @NonNull Region get(@Nullable Tile tile) { - return get(tile, true, Collections.emptySet()); + if (tile == null) return getGlobalRegion(); + return get(tile.getCenterLocation().getBlockX(), tile.getCenterLocation().getBlockZ(), true, Collections.emptySet()); } - private Region get(Tile tile, boolean fastCache, Collection regions) { + private Region get(int x, int z, boolean fastCache, Collection regions) { + Tile tile = Tile.fromXZ(x, z).orElse(null); if (tile == null) { return getGlobalRegion(); } @@ -111,8 +114,6 @@ public class DynamicRegionSystem implements RegionSystem { Region region = regionCache.get(tile.getId()); if (fastCache || regions.contains(region)) return region; } - int x = tile.getMinX(); - int z = tile.getMinZ(); Region region = regions.stream() .filter(rg -> rg.getArea().inRegion(x, z, false)) .findFirst() @@ -121,11 +122,6 @@ public class DynamicRegionSystem implements RegionSystem { return region; } - private Region get(int x, int z, boolean fastCache, Collection regions) { - Tile tile = Tile.fromXZ(x, z).validOrNull(); - return get(tile, fastCache, regions); - } - @Override public @NonNull Region get(@NonNull Location location) { return get(location.getBlockX(), location.getBlockZ(), true, regionMap.values()); @@ -142,27 +138,18 @@ public class DynamicRegionSystem implements RegionSystem { } private Stream getNeighbours(Region region, boolean noCorners, boolean fastCache, Collection regions) { - Tile min = Tile.fromPoint(region.getArea().getMinPoint(false)).add(-1, -1); - Tile max = Tile.fromPoint(region.getArea().getMaxPoint(false)).add(1, 1); - if (min == null || max == null) return Stream.empty(); // TODO: Maybe fix this? + Point minPoint = region.getArea().getMinPoint(false).subtract(TILE_SIZE_ADJUSTED, 0, TILE_SIZE_ADJUSTED); + Point maxPoint = region.getArea().getMaxPoint(false).add(Tile.tileSize, 0, Tile.tileSize); Set neighbours = new HashSet<>(); - for (int x = min.getTileX() + (noCorners ? 1 : 0); x <= max.getTileX() - (noCorners ? 1 : 0); x++) { - + for (int x = minPoint.getX() + (noCorners ? TILE_SIZE_ADJUSTED : 0); x <= maxPoint.getX() - (noCorners ? Tile.tileSize : 0); x += Tile.tileSize) { + neighbours.add(get(x, minPoint.getZ(), fastCache, regions)); + neighbours.add(get(x, maxPoint.getZ(), fastCache, regions)); } - for (int x = minPoint.getX() + (noCorners ? 18 : 0); x <= maxPoint.getX() - (noCorners ? 19 : 0); x += 19) { - int minZ = minPoint.getZ(); - int maxZ = maxPoint.getZ(); - neighbours.add(get(x, minZ, fastCache, regions)); - neighbours.add(get(x, maxZ, fastCache, regions)); - } - - for (int z = minPoint.getZ() + 18; z <= maxPoint.getZ() - 19; z += 19) { - int minX = minPoint.getX(); - int maxX = maxPoint.getX(); - neighbours.add(get(minX, z, fastCache, regions)); - neighbours.add(get(maxX, z, fastCache, regions)); + for (int z = minPoint.getZ() + TILE_SIZE_ADJUSTED; z <= maxPoint.getZ() - Tile.tileSize; z += Tile.tileSize) { + neighbours.add(get(minPoint.getX(), z, fastCache, regions)); + neighbours.add(get(maxPoint.getX(), z, fastCache, regions)); } neighbours.remove(getGlobalRegion()); diff --git a/BauSystem/BauSystem_RegionDynamic/src/de/steamwar/bausystem/region/dynamic/DynamicRegion.java b/BauSystem/BauSystem_RegionDynamic/src/de/steamwar/bausystem/region/dynamic/DynamicRegion.java index 83e3e2df..e38973a8 100644 --- a/BauSystem/BauSystem_RegionDynamic/src/de/steamwar/bausystem/region/dynamic/DynamicRegion.java +++ b/BauSystem/BauSystem_RegionDynamic/src/de/steamwar/bausystem/region/dynamic/DynamicRegion.java @@ -48,9 +48,13 @@ public abstract class DynamicRegion implements Region { this.id = id; this.minX = minX; this.minZ = minZ; + init(); DynamicRegionSystem.INSTANCE.add(this); + saveRegion(); } + public abstract void init(); + public void update(DynamicRegion updateFrom) { // TODO: Implement further } @@ -61,7 +65,7 @@ public abstract class DynamicRegion implements Region { } public void delete() { - if (getType().isCannotDelete()) return; + if (!getType().isDeletable()) return; DynamicRegionSystem.INSTANCE.remove(this); DynamicRegionRepository.deleteRegion(this); diff --git a/BauSystem/BauSystem_RegionDynamic/src/de/steamwar/bausystem/region/dynamic/DynamicRegionRepository.java b/BauSystem/BauSystem_RegionDynamic/src/de/steamwar/bausystem/region/dynamic/DynamicRegionRepository.java index a5bf3345..75929c87 100644 --- a/BauSystem/BauSystem_RegionDynamic/src/de/steamwar/bausystem/region/dynamic/DynamicRegionRepository.java +++ b/BauSystem/BauSystem_RegionDynamic/src/de/steamwar/bausystem/region/dynamic/DynamicRegionRepository.java @@ -138,7 +138,7 @@ public class DynamicRegionRepository { } RegionConstructorData constructorData = DynamicRegionSystem.constructorDataMap.get(regionClass); - Tile tile = Tile.fromTile(tileX, tileZ).validOrNull(); + Tile tile = Tile.fromTile(tileX, tileZ).orElse(null); if (tile == null) { RegionSystem.LOGGER.log(Level.SEVERE, "Failed to read region metadata file (tile is no longer in bounds)"); continue; @@ -226,7 +226,7 @@ public class DynamicRegionRepository { if (!region.getType().isGlobal()) { RegionConstructorData constructorData = DynamicRegionSystem.constructorDataMap.get(region.getClass()); Point point = region.getArea().getMinPoint(false); - Tile tile = Tile.fromPoint(point); + Tile tile = Tile.fromPoint(point).get(); writeMetaFile(regionDirectory, constructorData, tile); } diff --git a/BauSystem/BauSystem_RegionDynamic/src/de/steamwar/bausystem/region/dynamic/Tile.java b/BauSystem/BauSystem_RegionDynamic/src/de/steamwar/bausystem/region/dynamic/Tile.java index ef316203..b3594eb6 100644 --- a/BauSystem/BauSystem_RegionDynamic/src/de/steamwar/bausystem/region/dynamic/Tile.java +++ b/BauSystem/BauSystem_RegionDynamic/src/de/steamwar/bausystem/region/dynamic/Tile.java @@ -42,29 +42,21 @@ public class Tile { this.tileZ = tileZ; } - public Optional valid() { - return Optional.ofNullable(validOrNull()); + public static Optional fromTile(int tileX, int tileZ) { + if (tileX < minTile || tileZ < minTile) return Optional.empty(); + if (tileX > maxTile || tileZ > maxTile) return Optional.empty(); + return Optional.of(new Tile(tileX, tileZ)); } - public Tile validOrNull() { - if (tileX < minTile || tileZ < minTile) return null; - if (tileX > maxTile || tileZ > maxTile) return null; - return this; - } - - public static Tile fromTile(int tileX, int tileZ) { - return new Tile(tileX, tileZ); - } - - public static Tile fromLocation(Location location) { + public static Optional fromLocation(Location location) { return fromXZ(location.getBlockX(), location.getBlockZ()); } - public static Tile fromPoint(Point point) { + public static Optional fromPoint(Point point) { return fromXZ(point.getX(), point.getZ()); } - public static Tile fromXZ(int x, int z) { + public static Optional fromXZ(int x, int z) { x = (int) Math.floor((x + tileOffset) / (double) tileSize); z = (int) Math.floor((z + tileOffset) / (double) tileSize); return fromTile(x, z); @@ -126,7 +118,7 @@ public class Tile { return getCenterLocation(tileX, tileZ); } - public Tile add(int offsetX, int offsetZ) { + public Optional add(int offsetX, int offsetZ) { return fromTile(tileX + offsetX, tileZ + offsetZ); } diff --git a/BauSystem/BauSystem_RegionDynamic/src/de/steamwar/bausystem/region/dynamic/path/PathArea.java b/BauSystem/BauSystem_RegionDynamic/src/de/steamwar/bausystem/region/dynamic/path/PathArea.java index 33581e20..2571860c 100644 --- a/BauSystem/BauSystem_RegionDynamic/src/de/steamwar/bausystem/region/dynamic/path/PathArea.java +++ b/BauSystem/BauSystem_RegionDynamic/src/de/steamwar/bausystem/region/dynamic/path/PathArea.java @@ -33,6 +33,7 @@ import org.bukkit.Bukkit; import org.jetbrains.annotations.Nullable; import java.io.File; +import java.util.Optional; import java.util.UUID; public class PathArea implements Region.Area { @@ -46,8 +47,8 @@ public class PathArea implements Region.Area { private enum Side { North(0, -1, 7, 0, 0), South(0, 1, 7, 14, 180), - West(-1, 0, 0, 7, 90), - East(1, 0, 14, 7, 270), + West(-1, 0, 0, 7, 90), + East(1, 0, 14, 7, 270), ; private final int tileOffsetX; @@ -203,7 +204,7 @@ public class PathArea implements Region.Area { for (int x = -1; x <= 1; x++) { for (int z = -1; z <= 1; z++) { if (x == 0 && z == 0) continue; - Tile tile = this.tile.add(x, z).validOrNull(); + Tile tile = this.tile.add(x, z).orElse(null); if (tile == null) { return false; } @@ -216,12 +217,11 @@ public class PathArea implements Region.Area { } private RegionType.ConnectionType getConnectionType(Side side, Side optionalSide) { - Tile optionalTile = this.tile.add(side.tileOffsetX, side.tileOffsetZ).validOrNull(); - if (optionalTile == null) return RegionType.ConnectionType.Global; + Optional optionalTile = this.tile.add(side.tileOffsetX, side.tileOffsetZ); if (optionalSide != null) { - optionalTile = optionalTile.add(optionalSide.tileOffsetX, optionalSide.tileOffsetZ); - if (optionalTile == null) return RegionType.ConnectionType.Global; + optionalTile = optionalTile.flatMap(tile -> tile.add(optionalSide.tileOffsetX, optionalSide.tileOffsetZ)); } - return DynamicRegionSystem.INSTANCE.get(tile).getType().getConnectionType(); + return optionalTile.map(tile -> DynamicRegionSystem.INSTANCE.get(tile.getCenterLocation()).getType().getConnectionType()) + .orElse(RegionType.ConnectionType.Global); } } diff --git a/BauSystem/BauSystem_RegionDynamic/src/de/steamwar/bausystem/region/dynamic/path/PathRegion.java b/BauSystem/BauSystem_RegionDynamic/src/de/steamwar/bausystem/region/dynamic/path/PathRegion.java index dcc99500..19c75906 100644 --- a/BauSystem/BauSystem_RegionDynamic/src/de/steamwar/bausystem/region/dynamic/path/PathRegion.java +++ b/BauSystem/BauSystem_RegionDynamic/src/de/steamwar/bausystem/region/dynamic/path/PathRegion.java @@ -40,11 +40,15 @@ import java.util.UUID; ) public class PathRegion extends DynamicRegion { - private final PathArea area; + private PathArea area; public PathRegion(UUID id, int minX, int minZ) { super(id, minX, minZ); - area = new PathArea(Tile.fromXZ(minX, minZ).valid().orElseThrow(), this); + } + + @Override + public void init() { + area = new PathArea(Tile.fromXZ(minX, minZ).orElseThrow(), this); regionData = new PathRegionData(this); } diff --git a/BauSystem/BauSystem_RegionDynamic/src/de/steamwar/bausystem/region/dynamic/special/dry/DryRegion.java b/BauSystem/BauSystem_RegionDynamic/src/de/steamwar/bausystem/region/dynamic/special/dry/DryRegion.java index beb46d81..34a51ebb 100644 --- a/BauSystem/BauSystem_RegionDynamic/src/de/steamwar/bausystem/region/dynamic/special/dry/DryRegion.java +++ b/BauSystem/BauSystem_RegionDynamic/src/de/steamwar/bausystem/region/dynamic/special/dry/DryRegion.java @@ -44,11 +44,15 @@ public class DryRegion extends DynamicRegion { private static final VariantSelector DRY = VariantSelector.Get(new File(SPECIAL_PATH_DIR, "dry")); - private final SpecialArea area; + private SpecialArea area; public DryRegion(UUID id, int minX, int minZ) { super(id, minX, minZ); - area = new SpecialArea(Tile.fromXZ(minX, minZ).valid().orElseThrow(), this, DRY); + } + + @Override + public void init() { + area = new SpecialArea(Tile.fromXZ(minX, minZ).orElseThrow(), this, DRY); regionData = new SpecialRegionData(this); } diff --git a/BauSystem/BauSystem_RegionDynamic/src/de/steamwar/bausystem/region/dynamic/special/wet/WetRegion.java b/BauSystem/BauSystem_RegionDynamic/src/de/steamwar/bausystem/region/dynamic/special/wet/WetRegion.java index cd6963e8..93709831 100644 --- a/BauSystem/BauSystem_RegionDynamic/src/de/steamwar/bausystem/region/dynamic/special/wet/WetRegion.java +++ b/BauSystem/BauSystem_RegionDynamic/src/de/steamwar/bausystem/region/dynamic/special/wet/WetRegion.java @@ -44,11 +44,15 @@ public class WetRegion extends DynamicRegion { private static final VariantSelector WET = VariantSelector.Get(new File(SPECIAL_PATH_DIR, "wet")); - private final SpecialArea area; + private SpecialArea area; public WetRegion(UUID id, int minX, int minZ) { super(id, minX, minZ); - area = new SpecialArea(Tile.fromXZ(minX, minZ).valid().orElseThrow(), this, WET); + } + + @Override + public void init() { + area = new SpecialArea(Tile.fromXZ(minX, minZ).orElseThrow(), this, WET); regionData = new SpecialRegionData(this); }