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
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;
}
@@ -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();
}
}
@@ -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<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) {
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) {
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<Region> 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<Region> getNeighbours(Region region, boolean noCorners, boolean fastCache, Collection<Region> 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<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) {
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());
@@ -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);
@@ -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);
}
@@ -42,29 +42,21 @@ public class Tile {
this.tileZ = tileZ;
}
public Optional<Tile> valid() {
return Optional.ofNullable(validOrNull());
public static Optional<Tile> 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<Tile> fromLocation(Location location) {
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());
}
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);
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<Tile> add(int offsetX, int offsetZ) {
return fromTile(tileX + offsetX, tileZ + offsetZ);
}
@@ -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 {
@@ -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<Tile> 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);
}
}
@@ -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);
}
@@ -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);
}
@@ -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);
}