forked from SteamWar/SteamWar
Improve several things
This commit is contained in:
+3
-5
@@ -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);
|
||||
}
|
||||
|
||||
+16
-8
@@ -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);
|
||||
}
|
||||
|
||||
|
||||
+10
-10
@@ -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) {
|
||||
|
||||
+1
-1
@@ -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);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user