Improve several things

This commit is contained in:
2026-03-04 11:18:54 +01:00
parent 4444d47bfd
commit 7ed4b027c8
7 changed files with 67 additions and 38 deletions

View File

@@ -36,6 +36,7 @@ public enum RegionType {
SPAWN_PATH(false, false, true, ConnectionType.Path),
SPAWN_EXTENSION(false, false, false, ConnectionType.Closed),
PATH(false, false, false, ConnectionType.Path),
DRY(false, true, false, ConnectionType.Closed),
DRY_SPECIAL(false, false, false, ConnectionType.Closed),
WET(false, true, false, ConnectionType.Water),
@@ -47,10 +48,18 @@ public enum RegionType {
private final boolean cannotDelete;
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 {
Closed,
Path,
Water,
Global
Global,
}
}

View File

@@ -29,7 +29,6 @@ import de.steamwar.command.AbstractSWCommand;
import de.steamwar.command.SWCommand;
import org.bukkit.entity.Player;
import java.util.Optional;
import java.util.UUID;
import java.util.stream.Collectors;
@@ -44,9 +43,8 @@ public class DynamicRegionCommand extends SWCommand {
public void placeRegion(Player player) {
Region region = DynamicRegionSystem.INSTANCE.get(player.getLocation());
if (!region.getType().isGlobal()) return;
Optional<Tile> optionalTile = Tile.fromLocation(player.getLocation());
if (optionalTile.isEmpty()) return;
Tile tile = optionalTile.get();
Tile tile = Tile.fromLocation(player.getLocation()).validOrNull();
if (tile == null) return;
// TODO: Replace!
PathRegion dynamicRegion = new PathRegion(UUID.randomUUID(), tile.getMinX(), tile.getMinZ());

View File

@@ -28,6 +28,7 @@ import de.steamwar.bausystem.region.dynamic.global.GlobalRegion;
import lombok.NonNull;
import org.bukkit.Location;
import javax.annotation.Nullable;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.*;
@@ -98,8 +99,11 @@ public class DynamicRegionSystem implements RegionSystem {
return GlobalRegion.INSTANCE;
}
private Region get(int x, int z, boolean fastCache, Collection<Region> regions) {
Tile tile = Tile.fromXZ(x, z).orElse(null);
public @NonNull Region get(@Nullable Tile tile) {
return get(tile, true, Collections.emptySet());
}
private Region get(Tile tile, boolean fastCache, Collection<Region> regions) {
if (tile == null) {
return getGlobalRegion();
}
@@ -107,6 +111,8 @@ 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()
@@ -115,6 +121,11 @@ 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());
@@ -131,17 +142,22 @@ public class DynamicRegionSystem implements RegionSystem {
}
private Stream<Region> getNeighbours(Region region, boolean noCorners, boolean fastCache, Collection<Region> regions) {
Point minPoint = region.getArea().getMinPoint(false).subtract(18, 0, 18);
Point maxPoint = region.getArea().getMaxPoint(false).add(19, 0, 19);
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?
Set<Region> neighbours = new HashSet<>();
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 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) {
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();

View File

@@ -40,7 +40,6 @@ import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.SimpleFileVisitor;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.Optional;
import java.util.UUID;
import java.util.logging.Level;
@@ -139,12 +138,11 @@ public class DynamicRegionRepository {
}
RegionConstructorData constructorData = DynamicRegionSystem.constructorDataMap.get(regionClass);
Optional<Tile> optionalTile = Tile.fromTile(tileX, tileZ);
if (optionalTile.isEmpty()) {
Tile tile = Tile.fromTile(tileX, tileZ).validOrNull();
if (tile == null) {
RegionSystem.LOGGER.log(Level.SEVERE, "Failed to read region metadata file (tile is no longer in bounds)");
continue;
}
Tile tile = optionalTile.get();
Location minTileLocation = tile.getMinLocation();
Constructor<? extends DynamicRegion> regionConstructor;
@@ -228,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).get();
Tile tile = Tile.fromPoint(point);
writeMetaFile(regionDirectory, constructorData, tile);
}

View File

@@ -42,21 +42,29 @@ public class Tile {
this.tileZ = tileZ;
}
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 Optional<Tile> valid() {
return Optional.ofNullable(validOrNull());
}
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());
}
public static Optional<Tile> fromPoint(Point point) {
public static Tile fromPoint(Point point) {
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);
z = (int) Math.floor((z + tileOffset) / (double) tileSize);
return fromTile(x, z);
@@ -118,7 +126,7 @@ public class Tile {
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);
}

View File

@@ -37,7 +37,6 @@ 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 {
@@ -115,7 +114,7 @@ public class PathArea implements Region.Area {
@Override
public void reset(PasteBuilder pasteBuilder, boolean extension) {
if (surroundedByPath()) {
if (isGarden()) {
// 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 z = -1; z <= 1; z++) {
if (x == 0 && z == 0) continue;
Optional<Tile> optionalTile = this.tile.add(x, z);
if (optionalTile.isEmpty()) {
Tile tile = this.tile.add(x, z).validOrNull();
if (tile == null) {
return false;
}
if (DynamicRegionSystem.INSTANCE.get(optionalTile.get().getCenterLocation()).getType() != RegionType.PATH) {
if (!DynamicRegionSystem.INSTANCE.get(tile).getType().isPath()) {
return false;
}
}
@@ -221,12 +220,13 @@ public class PathArea implements Region.Area {
}
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) {
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())
.orElse(RegionType.ConnectionType.Global);
return DynamicRegionSystem.INSTANCE.get(tile).getType().getConnectionType();
}
private void paste(File file, Point minPoint, int rotate) {

View File

@@ -44,7 +44,7 @@ public class PathRegion extends DynamicRegion {
public PathRegion(UUID id, int minX, int 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);
}