Fix runtime errors

This commit is contained in:
2026-03-04 19:19:08 +01:00
parent 09be2b434d
commit 2116e1ee8d
10 changed files with 79 additions and 79 deletions
@@ -26,28 +26,37 @@ import lombok.RequiredArgsConstructor;
@Getter @Getter
public enum RegionType { public enum RegionType {
GLOBAL(true, false, true, ConnectionType.Global), GLOBAL(ConnectionType.Global),
/** /**
* This should not be used by the DynamicRegionSystem * This should not be used by the DynamicRegionSystem
*/ */
NORMAL(false, true, false, ConnectionType.Closed), NORMAL(ConnectionType.Closed),
SPAWN(false, false, true, ConnectionType.Closed), SPAWN(ConnectionType.Closed),
SPAWN_PATH(false, false, true, ConnectionType.Path), SPAWN_PATH(ConnectionType.Path),
SPAWN_EXTENSION(false, false, false, ConnectionType.Closed), SPAWN_EXTENSION(ConnectionType.Closed),
PATH(false, false, false, ConnectionType.Path), PATH(ConnectionType.Path),
DRY(false, true, false, ConnectionType.Closed), DRY(ConnectionType.Closed),
DRY_SPECIAL(false, false, false, ConnectionType.Closed), DRY_SPECIAL(ConnectionType.Closed),
WET(false, true, false, ConnectionType.Water), WET(ConnectionType.Water),
WET_SPECIAL(false, false, false, ConnectionType.Water), WET_SPECIAL(ConnectionType.Water),
; ;
private final boolean global;
private final boolean createBackup;
private final boolean cannotDelete;
private final ConnectionType connectionType; 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() { public boolean isSpawn() {
return this == SPAWN || this == SPAWN_PATH || this == SPAWN_EXTENSION; return this == SPAWN || this == SPAWN_PATH || this == SPAWN_EXTENSION;
} }
@@ -23,14 +23,12 @@ import de.steamwar.bausystem.features.region.RegionCommand;
import de.steamwar.bausystem.region.dynamic.DynamicRegion; import de.steamwar.bausystem.region.dynamic.DynamicRegion;
import de.steamwar.bausystem.region.dynamic.Tile; import de.steamwar.bausystem.region.dynamic.Tile;
import de.steamwar.bausystem.region.dynamic.path.PathRegion; 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.bausystem.utils.PasteBuilder;
import de.steamwar.command.AbstractSWCommand; import de.steamwar.command.AbstractSWCommand;
import de.steamwar.command.SWCommand; import de.steamwar.command.SWCommand;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import java.util.UUID; import java.util.UUID;
import java.util.stream.Collectors;
@AbstractSWCommand.PartOf(RegionCommand.class) @AbstractSWCommand.PartOf(RegionCommand.class)
public class DynamicRegionCommand extends SWCommand { public class DynamicRegionCommand extends SWCommand {
@@ -43,22 +41,20 @@ public class DynamicRegionCommand extends SWCommand {
public void placeRegion(Player player) { public void placeRegion(Player player) {
Region region = DynamicRegionSystem.INSTANCE.get(player.getLocation()); Region region = DynamicRegionSystem.INSTANCE.get(player.getLocation());
if (!region.getType().isGlobal()) return; if (!region.getType().isGlobal()) return;
Tile tile = Tile.fromLocation(player.getLocation()).validOrNull(); Tile tile = Tile.fromLocation(player.getLocation()).orElse(null);
if (tile == null) return; if (tile == null) return;
// TODO: Replace! // TODO: Replace!
PathRegion dynamicRegion = new PathRegion(UUID.randomUUID(), tile.getMinX(), tile.getMinZ()); 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); dynamicRegion.getArea().reset(new PasteBuilder(new PasteBuilder.FileProvider(dynamicRegion.getArea().getResetFile())), false);
DynamicRegionSystem.INSTANCE.getNeighbours(dynamicRegion).collect(Collectors.toList()) DynamicRegionSystem.INSTANCE.getNeighbours(dynamicRegion).forEach(r -> r.update(dynamicRegion));
.forEach(r -> r.update(dynamicRegion));
} }
@Register({"dynamic", "delete"}) @Register({"dynamic", "delete"})
public void deleteRegion(Player player) { public void deleteRegion(Player player) {
Region region = DynamicRegionSystem.INSTANCE.get(player.getLocation()); Region region = DynamicRegionSystem.INSTANCE.get(player.getLocation());
if (region.getType().isCannotDelete()) return; if (!region.getType().isDeletable()) return;
((DynamicRegion) region).delete(); ((DynamicRegion) region).delete();
} }
} }
@@ -38,6 +38,7 @@ import java.util.stream.Stream;
public class DynamicRegionSystem implements RegionSystem { public class DynamicRegionSystem implements RegionSystem {
private static final int TILE_SIZE_ADJUSTED = Tile.tileSize - 1;
public static DynamicRegionSystem INSTANCE; public static DynamicRegionSystem INSTANCE;
private static final Map<Long, Region> regionCache = new LinkedHashMap<>(16, 0.75f, true) { private static final Map<Long, Region> regionCache = new LinkedHashMap<>(16, 0.75f, true) {
@@ -100,10 +101,12 @@ public class DynamicRegionSystem implements RegionSystem {
} }
public @NonNull Region get(@Nullable Tile tile) { 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<Region> regions) { private Region get(int x, int z, boolean fastCache, Collection<Region> regions) {
Tile tile = Tile.fromXZ(x, z).orElse(null);
if (tile == null) { if (tile == null) {
return getGlobalRegion(); return getGlobalRegion();
} }
@@ -111,8 +114,6 @@ public class DynamicRegionSystem implements RegionSystem {
Region region = regionCache.get(tile.getId()); Region region = regionCache.get(tile.getId());
if (fastCache || regions.contains(region)) return region; if (fastCache || regions.contains(region)) return region;
} }
int x = tile.getMinX();
int z = tile.getMinZ();
Region region = regions.stream() Region region = regions.stream()
.filter(rg -> rg.getArea().inRegion(x, z, false)) .filter(rg -> rg.getArea().inRegion(x, z, false))
.findFirst() .findFirst()
@@ -121,11 +122,6 @@ public class DynamicRegionSystem implements RegionSystem {
return region; return region;
} }
private Region get(int x, int z, boolean fastCache, Collection<Region> regions) {
Tile tile = Tile.fromXZ(x, z).validOrNull();
return get(tile, fastCache, regions);
}
@Override @Override
public @NonNull Region get(@NonNull Location location) { public @NonNull Region get(@NonNull Location location) {
return get(location.getBlockX(), location.getBlockZ(), true, regionMap.values()); return get(location.getBlockX(), location.getBlockZ(), true, regionMap.values());
@@ -142,27 +138,18 @@ public class DynamicRegionSystem implements RegionSystem {
} }
private Stream<Region> getNeighbours(Region region, boolean noCorners, boolean fastCache, Collection<Region> regions) { private Stream<Region> getNeighbours(Region region, boolean noCorners, boolean fastCache, Collection<Region> regions) {
Tile min = Tile.fromPoint(region.getArea().getMinPoint(false)).add(-1, -1); Point minPoint = region.getArea().getMinPoint(false).subtract(TILE_SIZE_ADJUSTED, 0, TILE_SIZE_ADJUSTED);
Tile max = Tile.fromPoint(region.getArea().getMaxPoint(false)).add(1, 1); Point maxPoint = region.getArea().getMaxPoint(false).add(Tile.tileSize, 0, Tile.tileSize);
if (min == null || max == null) return Stream.empty(); // TODO: Maybe fix this?
Set<Region> neighbours = new HashSet<>(); Set<Region> 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) { for (int z = minPoint.getZ() + TILE_SIZE_ADJUSTED; z <= maxPoint.getZ() - Tile.tileSize; z += Tile.tileSize) {
int minZ = minPoint.getZ(); neighbours.add(get(minPoint.getX(), z, fastCache, regions));
int maxZ = maxPoint.getZ(); neighbours.add(get(maxPoint.getX(), z, fastCache, regions));
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));
} }
neighbours.remove(getGlobalRegion()); neighbours.remove(getGlobalRegion());
@@ -48,9 +48,13 @@ public abstract class DynamicRegion implements Region {
this.id = id; this.id = id;
this.minX = minX; this.minX = minX;
this.minZ = minZ; this.minZ = minZ;
init();
DynamicRegionSystem.INSTANCE.add(this); DynamicRegionSystem.INSTANCE.add(this);
saveRegion();
} }
public abstract void init();
public void update(DynamicRegion updateFrom) { public void update(DynamicRegion updateFrom) {
// TODO: Implement further // TODO: Implement further
} }
@@ -61,7 +65,7 @@ public abstract class DynamicRegion implements Region {
} }
public void delete() { public void delete() {
if (getType().isCannotDelete()) return; if (!getType().isDeletable()) return;
DynamicRegionSystem.INSTANCE.remove(this); DynamicRegionSystem.INSTANCE.remove(this);
DynamicRegionRepository.deleteRegion(this); DynamicRegionRepository.deleteRegion(this);
@@ -138,7 +138,7 @@ public class DynamicRegionRepository {
} }
RegionConstructorData constructorData = DynamicRegionSystem.constructorDataMap.get(regionClass); RegionConstructorData constructorData = DynamicRegionSystem.constructorDataMap.get(regionClass);
Tile tile = Tile.fromTile(tileX, tileZ).validOrNull(); Tile tile = Tile.fromTile(tileX, tileZ).orElse(null);
if (tile == null) { if (tile == null) {
RegionSystem.LOGGER.log(Level.SEVERE, "Failed to read region metadata file (tile is no longer in bounds)"); RegionSystem.LOGGER.log(Level.SEVERE, "Failed to read region metadata file (tile is no longer in bounds)");
continue; continue;
@@ -226,7 +226,7 @@ public class DynamicRegionRepository {
if (!region.getType().isGlobal()) { if (!region.getType().isGlobal()) {
RegionConstructorData constructorData = DynamicRegionSystem.constructorDataMap.get(region.getClass()); RegionConstructorData constructorData = DynamicRegionSystem.constructorDataMap.get(region.getClass());
Point point = region.getArea().getMinPoint(false); Point point = region.getArea().getMinPoint(false);
Tile tile = Tile.fromPoint(point); Tile tile = Tile.fromPoint(point).get();
writeMetaFile(regionDirectory, constructorData, tile); writeMetaFile(regionDirectory, constructorData, tile);
} }
@@ -42,29 +42,21 @@ public class Tile {
this.tileZ = tileZ; this.tileZ = tileZ;
} }
public Optional<Tile> valid() { public static Optional<Tile> fromTile(int tileX, int tileZ) {
return Optional.ofNullable(validOrNull()); 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() { public static Optional<Tile> fromLocation(Location location) {
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) {
return fromXZ(location.getBlockX(), location.getBlockZ()); return fromXZ(location.getBlockX(), location.getBlockZ());
} }
public static Tile fromPoint(Point point) { public static Optional<Tile> fromPoint(Point point) {
return fromXZ(point.getX(), point.getZ()); return fromXZ(point.getX(), point.getZ());
} }
public static Tile fromXZ(int x, int z) { public static Optional<Tile> fromXZ(int x, int z) {
x = (int) Math.floor((x + tileOffset) / (double) tileSize); x = (int) Math.floor((x + tileOffset) / (double) tileSize);
z = (int) Math.floor((z + tileOffset) / (double) tileSize); z = (int) Math.floor((z + tileOffset) / (double) tileSize);
return fromTile(x, z); return fromTile(x, z);
@@ -126,7 +118,7 @@ public class Tile {
return getCenterLocation(tileX, tileZ); return getCenterLocation(tileX, tileZ);
} }
public Tile add(int offsetX, int offsetZ) { public Optional<Tile> add(int offsetX, int offsetZ) {
return fromTile(tileX + offsetX, tileZ + offsetZ); return fromTile(tileX + offsetX, tileZ + offsetZ);
} }
@@ -33,6 +33,7 @@ import org.bukkit.Bukkit;
import org.jetbrains.annotations.Nullable; import org.jetbrains.annotations.Nullable;
import java.io.File; import java.io.File;
import java.util.Optional;
import java.util.UUID; import java.util.UUID;
public class PathArea implements Region.Area { public class PathArea implements Region.Area {
@@ -46,8 +47,8 @@ public class PathArea implements Region.Area {
private enum Side { private enum Side {
North(0, -1, 7, 0, 0), North(0, -1, 7, 0, 0),
South(0, 1, 7, 14, 180), South(0, 1, 7, 14, 180),
West(-1, 0, 0, 7, 90), West(-1, 0, 0, 7, 90),
East(1, 0, 14, 7, 270), East(1, 0, 14, 7, 270),
; ;
private final int tileOffsetX; private final int tileOffsetX;
@@ -203,7 +204,7 @@ public class PathArea implements Region.Area {
for (int x = -1; x <= 1; x++) { for (int x = -1; x <= 1; x++) {
for (int z = -1; z <= 1; z++) { for (int z = -1; z <= 1; z++) {
if (x == 0 && z == 0) continue; 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) { if (tile == null) {
return false; return false;
} }
@@ -216,12 +217,11 @@ public class PathArea implements Region.Area {
} }
private RegionType.ConnectionType getConnectionType(Side side, Side optionalSide) { private RegionType.ConnectionType getConnectionType(Side side, Side optionalSide) {
Tile optionalTile = this.tile.add(side.tileOffsetX, side.tileOffsetZ).validOrNull(); Optional<Tile> optionalTile = this.tile.add(side.tileOffsetX, side.tileOffsetZ);
if (optionalTile == null) return RegionType.ConnectionType.Global;
if (optionalSide != null) { if (optionalSide != null) {
optionalTile = optionalTile.add(optionalSide.tileOffsetX, optionalSide.tileOffsetZ); optionalTile = optionalTile.flatMap(tile -> tile.add(optionalSide.tileOffsetX, optionalSide.tileOffsetZ));
if (optionalTile == null) return RegionType.ConnectionType.Global;
} }
return DynamicRegionSystem.INSTANCE.get(tile).getType().getConnectionType(); return optionalTile.map(tile -> DynamicRegionSystem.INSTANCE.get(tile.getCenterLocation()).getType().getConnectionType())
.orElse(RegionType.ConnectionType.Global);
} }
} }
@@ -40,11 +40,15 @@ import java.util.UUID;
) )
public class PathRegion extends DynamicRegion { public class PathRegion extends DynamicRegion {
private final PathArea area; private PathArea area;
public PathRegion(UUID id, int minX, int minZ) { public PathRegion(UUID id, int minX, int minZ) {
super(id, minX, 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); regionData = new PathRegionData(this);
} }
@@ -44,11 +44,15 @@ public class DryRegion extends DynamicRegion {
private static final VariantSelector DRY = VariantSelector.Get(new File(SPECIAL_PATH_DIR, "dry")); 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) { public DryRegion(UUID id, int minX, int minZ) {
super(id, minX, 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); regionData = new SpecialRegionData(this);
} }
@@ -44,11 +44,15 @@ public class WetRegion extends DynamicRegion {
private static final VariantSelector WET = VariantSelector.Get(new File(SPECIAL_PATH_DIR, "wet")); 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) { public WetRegion(UUID id, int minX, int minZ) {
super(id, minX, 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); regionData = new SpecialRegionData(this);
} }