Improve several things

This commit is contained in:
2026-03-04 11:18:54 +01:00
parent b72a971742
commit bb37a89f38
7 changed files with 67 additions and 38 deletions
@@ -36,6 +36,7 @@ public enum RegionType {
SPAWN_PATH(false, false, true, ConnectionType.Path), SPAWN_PATH(false, false, true, ConnectionType.Path),
SPAWN_EXTENSION(false, false, false, ConnectionType.Closed), SPAWN_EXTENSION(false, false, false, ConnectionType.Closed),
PATH(false, false, false, ConnectionType.Path), PATH(false, false, false, ConnectionType.Path),
DRY(false, true, false, ConnectionType.Closed), DRY(false, true, false, ConnectionType.Closed),
DRY_SPECIAL(false, false, false, ConnectionType.Closed), DRY_SPECIAL(false, false, false, ConnectionType.Closed),
WET(false, true, false, ConnectionType.Water), WET(false, true, false, ConnectionType.Water),
@@ -47,10 +48,18 @@ public enum RegionType {
private final boolean cannotDelete; private final boolean cannotDelete;
private final ConnectionType connectionType; private final ConnectionType connectionType;
public boolean isSpawn() {
return this == SPAWN || this == SPAWN_PATH || this == SPAWN_EXTENSION;
}
public boolean isPath() {
return this == PATH || this == SPAWN_PATH;
}
public enum ConnectionType { public enum ConnectionType {
Closed, Closed,
Path, Path,
Water, Water,
Global Global,
} }
} }
@@ -29,7 +29,6 @@ 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.Optional;
import java.util.UUID; import java.util.UUID;
import java.util.stream.Collectors; import java.util.stream.Collectors;
@@ -44,9 +43,8 @@ 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;
Optional<Tile> optionalTile = Tile.fromLocation(player.getLocation()); Tile tile = Tile.fromLocation(player.getLocation()).validOrNull();
if (optionalTile.isEmpty()) return; if (tile == null) return;
Tile tile = optionalTile.get();
// TODO: Replace! // TODO: Replace!
PathRegion dynamicRegion = new PathRegion(UUID.randomUUID(), tile.getMinX(), tile.getMinZ()); PathRegion dynamicRegion = new PathRegion(UUID.randomUUID(), tile.getMinX(), tile.getMinZ());
@@ -28,6 +28,7 @@ import de.steamwar.bausystem.region.dynamic.global.GlobalRegion;
import lombok.NonNull; import lombok.NonNull;
import org.bukkit.Location; import org.bukkit.Location;
import javax.annotation.Nullable;
import java.io.BufferedReader; import java.io.BufferedReader;
import java.io.InputStreamReader; import java.io.InputStreamReader;
import java.util.*; import java.util.*;
@@ -98,8 +99,11 @@ public class DynamicRegionSystem implements RegionSystem {
return GlobalRegion.INSTANCE; return GlobalRegion.INSTANCE;
} }
private Region get(int x, int z, boolean fastCache, Collection<Region> regions) { public @NonNull Region get(@Nullable Tile tile) {
Tile tile = Tile.fromXZ(x, z).orElse(null); return get(tile, true, Collections.emptySet());
}
private Region get(Tile tile, boolean fastCache, Collection<Region> regions) {
if (tile == null) { if (tile == null) {
return getGlobalRegion(); return getGlobalRegion();
} }
@@ -107,6 +111,8 @@ 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()
@@ -115,6 +121,11 @@ 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());
@@ -131,10 +142,15 @@ 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) {
Point minPoint = region.getArea().getMinPoint(false).subtract(18, 0, 18); Tile min = Tile.fromPoint(region.getArea().getMinPoint(false)).add(-1, -1);
Point maxPoint = region.getArea().getMaxPoint(false).add(19, 0, 19); Tile max = Tile.fromPoint(region.getArea().getMaxPoint(false)).add(1, 1);
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 ? 18 : 0); x <= maxPoint.getX() - (noCorners ? 19 : 0); x += 19) { for (int x = minPoint.getX() + (noCorners ? 18 : 0); x <= maxPoint.getX() - (noCorners ? 19 : 0); x += 19) {
int minZ = minPoint.getZ(); int minZ = minPoint.getZ();
int maxZ = maxPoint.getZ(); int maxZ = maxPoint.getZ();
@@ -40,7 +40,6 @@ import java.nio.file.Files;
import java.nio.file.Path; import java.nio.file.Path;
import java.nio.file.SimpleFileVisitor; import java.nio.file.SimpleFileVisitor;
import java.nio.file.attribute.BasicFileAttributes; import java.nio.file.attribute.BasicFileAttributes;
import java.util.Optional;
import java.util.UUID; import java.util.UUID;
import java.util.logging.Level; import java.util.logging.Level;
@@ -139,12 +138,11 @@ public class DynamicRegionRepository {
} }
RegionConstructorData constructorData = DynamicRegionSystem.constructorDataMap.get(regionClass); RegionConstructorData constructorData = DynamicRegionSystem.constructorDataMap.get(regionClass);
Optional<Tile> optionalTile = Tile.fromTile(tileX, tileZ); Tile tile = Tile.fromTile(tileX, tileZ).validOrNull();
if (optionalTile.isEmpty()) { 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;
} }
Tile tile = optionalTile.get();
Location minTileLocation = tile.getMinLocation(); Location minTileLocation = tile.getMinLocation();
Constructor<? extends DynamicRegion> regionConstructor; Constructor<? extends DynamicRegion> regionConstructor;
@@ -228,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).get(); Tile tile = Tile.fromPoint(point);
writeMetaFile(regionDirectory, constructorData, tile); writeMetaFile(regionDirectory, constructorData, tile);
} }
@@ -42,21 +42,29 @@ public class Tile {
this.tileZ = tileZ; this.tileZ = tileZ;
} }
public static Optional<Tile> fromTile(int tileX, int tileZ) { public Optional<Tile> valid() {
if (tileX < minTile || tileZ < minTile) return Optional.empty(); return Optional.ofNullable(validOrNull());
if (tileX > maxTile || tileZ > maxTile) return Optional.empty();
return Optional.of(new Tile(tileX, tileZ));
} }
public static Optional<Tile> fromLocation(Location location) { 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) {
return fromXZ(location.getBlockX(), location.getBlockZ()); return fromXZ(location.getBlockX(), location.getBlockZ());
} }
public static Optional<Tile> fromPoint(Point point) { public static Tile fromPoint(Point point) {
return fromXZ(point.getX(), point.getZ()); return fromXZ(point.getX(), point.getZ());
} }
public static Optional<Tile> fromXZ(int x, int z) { public static 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);
@@ -118,7 +126,7 @@ public class Tile {
return getCenterLocation(tileX, tileZ); return getCenterLocation(tileX, tileZ);
} }
public Optional<Tile> add(int offsetX, int offsetZ) { public Tile add(int offsetX, int offsetZ) {
return fromTile(tileX + offsetX, tileZ + offsetZ); return fromTile(tileX + offsetX, tileZ + offsetZ);
} }
@@ -37,7 +37,6 @@ 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 {
@@ -115,7 +114,7 @@ public class PathArea implements Region.Area {
@Override @Override
public void reset(PasteBuilder pasteBuilder, boolean extension) { public void reset(PasteBuilder pasteBuilder, boolean extension) {
if (surroundedByPath()) { if (isGarden()) {
// TODO: Implement Garden case! // TODO: Implement Garden case!
} }
@@ -204,15 +203,15 @@ public class PathArea implements Region.Area {
} }
} }
private boolean surroundedByPath() { private boolean isGarden() {
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;
Optional<Tile> optionalTile = this.tile.add(x, z); Tile tile = this.tile.add(x, z).validOrNull();
if (optionalTile.isEmpty()) { if (tile == null) {
return false; return false;
} }
if (DynamicRegionSystem.INSTANCE.get(optionalTile.get().getCenterLocation()).getType() != RegionType.PATH) { if (!DynamicRegionSystem.INSTANCE.get(tile).getType().isPath()) {
return false; return false;
} }
} }
@@ -221,12 +220,13 @@ public class PathArea implements Region.Area {
} }
private RegionType.ConnectionType getConnectionType(Side side, Side optionalSide) { private RegionType.ConnectionType getConnectionType(Side side, Side optionalSide) {
Optional<Tile> optionalTile = this.tile.add(side.tileOffsetX, side.tileOffsetZ); Tile optionalTile = this.tile.add(side.tileOffsetX, side.tileOffsetZ).validOrNull();
if (optionalTile == null) return RegionType.ConnectionType.Global;
if (optionalSide != null) { if (optionalSide != null) {
optionalTile = optionalTile.flatMap(tile -> tile.add(optionalSide.tileOffsetX, optionalSide.tileOffsetZ)); optionalTile = optionalTile.add(optionalSide.tileOffsetX, optionalSide.tileOffsetZ);
if (optionalTile == null) return RegionType.ConnectionType.Global;
} }
return optionalTile.map(tile -> DynamicRegionSystem.INSTANCE.get(tile.getCenterLocation()).getType().getConnectionType()) return DynamicRegionSystem.INSTANCE.get(tile).getType().getConnectionType();
.orElse(RegionType.ConnectionType.Global);
} }
private void paste(File file, Point minPoint, int rotate) { private void paste(File file, Point minPoint, int rotate) {
@@ -44,7 +44,7 @@ public class PathRegion extends DynamicRegion {
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).orElseThrow(), this); area = new PathArea(Tile.fromXZ(minX, minZ).valid().orElseThrow(), this);
regionData = new PathRegionData(this); regionData = new PathRegionData(this);
} }