diff --git a/chunky/src/java/se/llbit/chunky/map/MapTile.java b/chunky/src/java/se/llbit/chunky/map/MapTile.java index 23ffc17821..2f00a9991a 100644 --- a/chunky/src/java/se/llbit/chunky/map/MapTile.java +++ b/chunky/src/java/se/llbit/chunky/map/MapTile.java @@ -70,14 +70,15 @@ public void draw(MapBuffer buffer, WorldMapLoader mapLoader, ChunkView view, } } } else { - boolean isValid = mapLoader.getWorld().currentDimension().regionExistsWithinRange(pos, view.yMin, view.yMax); - Region region = mapLoader.getWorld().currentDimension().getRegionWithinRange(pos, view.yMin, view.yMax); + RegionPosition regionPos = new RegionPosition(pos.x, pos.z); // intentionally don't convert, this position represented a region already. + boolean isValid = mapLoader.getWorld().currentDimension().regionExistsWithinRange(regionPos, view.yMin, view.yMax); + Region region = mapLoader.getWorld().currentDimension().getRegionWithinRange(regionPos, view.yMin, view.yMax); int pixelOffset = 0; for (int z = 0; z < 32; ++z) { for (int x = 0; x < 32; ++x) { Chunk chunk = region.getChunk(x, z); //Calculate the chunk position as empty chunks are (0, 0) - ChunkPosition pos = region.getPosition().chunkPositionFromRegion(x, z); + ChunkPosition pos = region.getPosition().asChunkPosition(x, z); pixels[pixelOffset] = chunk.biomeColor(); if (isValid && !(chunk instanceof EmptyRegionChunk) && selection.isSelected(pos)) { diff --git a/chunky/src/java/se/llbit/chunky/map/WorldMapLoader.java b/chunky/src/java/se/llbit/chunky/map/WorldMapLoader.java index 6516914f5c..daafe632e0 100644 --- a/chunky/src/java/se/llbit/chunky/map/WorldMapLoader.java +++ b/chunky/src/java/se/llbit/chunky/map/WorldMapLoader.java @@ -117,7 +117,7 @@ public void addWorldLoadListener(BiConsumer callback) { // Enqueue visible regions and chunks to be loaded. for (int rx = rx0; rx <= rx1; ++rx) { for (int rz = rz0; rz <= rz1; ++rz) { - regionQueue.add(new ChunkPosition(rx, rz)); + regionQueue.add(new RegionPosition(rx, rz)); } } } @@ -136,7 +136,7 @@ public synchronized void withWorld(Consumer fun) { } /** Called to notify the world loader that a region was changed. */ - public void regionUpdated(ChunkPosition region) { + public void regionUpdated(RegionPosition region) { regionQueue.add(region); } diff --git a/chunky/src/java/se/llbit/chunky/renderer/scene/Scene.java b/chunky/src/java/se/llbit/chunky/renderer/scene/Scene.java index bfd39a54a1..8d3690b5a3 100644 --- a/chunky/src/java/se/llbit/chunky/renderer/scene/Scene.java +++ b/chunky/src/java/se/llbit/chunky/renderer/scene/Scene.java @@ -814,12 +814,12 @@ public synchronized void loadChunks(TaskTracker taskTracker, World world, Collec emitterGrid = new Grid(gridSize); // Parse the regions first - force chunk lists to be populated! - Set regions = new HashSet<>(); + Set regions = new HashSet<>(); for (ChunkPosition cp : chunksToLoad) { regions.add(cp.getRegionPosition()); } - for (ChunkPosition region : regions) { + for (RegionPosition region : regions) { dimension.getRegion(region).parse(yMin, yMax); } } diff --git a/chunky/src/java/se/llbit/chunky/ui/ChunkMap.java b/chunky/src/java/se/llbit/chunky/ui/ChunkMap.java index 76b1f9d510..a7603177fc 100644 --- a/chunky/src/java/se/llbit/chunky/ui/ChunkMap.java +++ b/chunky/src/java/se/llbit/chunky/ui/ChunkMap.java @@ -46,14 +46,8 @@ import se.llbit.chunky.ui.controller.ChunkyFxController; import se.llbit.chunky.ui.dialogs.SelectChunksInRadiusDialog; import se.llbit.chunky.ui.elements.TextFieldLabelWrapper; -import se.llbit.chunky.world.Chunk; -import se.llbit.chunky.world.ChunkPosition; -import se.llbit.chunky.world.ChunkSelectionTracker; -import se.llbit.chunky.world.ChunkView; +import se.llbit.chunky.world.*; import se.llbit.chunky.world.Dimension; -import se.llbit.chunky.world.Icon; -import se.llbit.chunky.world.PlayerEntityData; -import se.llbit.chunky.world.World; import se.llbit.chunky.world.listeners.ChunkUpdateListener; import se.llbit.chunky.world.region.MCRegion; import se.llbit.log.Log; @@ -226,7 +220,7 @@ public ChunkMap(WorldMapLoader loader, ChunkyFxController controller, } } - @Override public void regionChunksUpdated(ChunkPosition region) { + @Override public void regionChunksUpdated(RegionPosition region) { if (view.chunkScale >= 16) { int minChunkX = region.x << 5; int minChunkZ = region.z << 5; @@ -241,7 +235,7 @@ public ChunkMap(WorldMapLoader loader, ChunkyFxController controller, } } - @Override public void regionChunksUpdated(ChunkPosition region, Collection chunks) { + @Override public void regionChunksUpdated(RegionPosition region, Collection chunks) { if (view.chunkScale >= 16) { for (ChunkPosition chunk : chunks) { mapBuffer.drawTile(mapLoader, chunk, chunkSelection); @@ -403,9 +397,10 @@ public void renderView(File targetFile, ProgressTracker progress) { } } - @Override public void regionUpdated(ChunkPosition region) { + @Override public void regionUpdated(RegionPosition region) { if (view.scale < 16) { - mapBuffer.drawTile(mapLoader, region, chunkSelection); + // intentionally don't convert, positions are all stored as ChunkPosition internally. + mapBuffer.drawTile(mapLoader, new ChunkPosition(region.x, region.z), chunkSelection); mapLoader.regionUpdated(region); repaintRatelimited(); } diff --git a/chunky/src/java/se/llbit/chunky/world/ChunkPosition.java b/chunky/src/java/se/llbit/chunky/world/ChunkPosition.java index b66b2d46b4..f82bf55769 100644 --- a/chunky/src/java/se/llbit/chunky/world/ChunkPosition.java +++ b/chunky/src/java/se/llbit/chunky/world/ChunkPosition.java @@ -43,7 +43,10 @@ public long getLong() { /** * @return The .mca name for the region with this position + * + * @deprecated Use {@link RegionPosition#getMcaName()}. Remove in 2.6 */ + @Deprecated public String getMcaName() { return String.format("r.%d.%d.mca", x, z); } @@ -51,12 +54,8 @@ public String getMcaName() { /** * @return The region position of this chunk position */ - public ChunkPosition getRegionPosition() { - return new ChunkPosition(x >> 5, z >> 5); - } - - public ChunkPosition chunkPositionFromRegion(int localX, int localZ) { - return new ChunkPosition((this.x << 5) | (localX & 0x1f), (this.z << 5) | (localZ & 0x1f)); + public RegionPosition getRegionPosition() { + return new RegionPosition(x >> 5, z >> 5); } /** @@ -99,14 +98,6 @@ public int hashCode() { return 31 * x + z; } - /** - * @deprecated Use {@link ChunkPosition#getRegionPosition()}. Remove in 2.6. - */ - @Deprecated - public ChunkPosition regionPosition() { - return getRegionPosition(); - } - /** * @deprecated Remove in 2.6. */ diff --git a/chunky/src/java/se/llbit/chunky/world/ChunkSelectionTracker.java b/chunky/src/java/se/llbit/chunky/world/ChunkSelectionTracker.java index 07ae15ad10..c0e3c56e22 100644 --- a/chunky/src/java/se/llbit/chunky/world/ChunkSelectionTracker.java +++ b/chunky/src/java/se/llbit/chunky/world/ChunkSelectionTracker.java @@ -74,7 +74,7 @@ private boolean setChunk(Dimension dimension, ChunkPosition pos, boolean selecte * Minimum and maximum are both INCLUSIVE * @return Whether the selection changed */ - private boolean setChunksWithinRegion(Dimension dimension, ChunkPosition regionPos, int minX, int maxX, int minZ, int maxZ, boolean selected) { + private boolean setChunksWithinRegion(Dimension dimension, RegionPosition regionPos, int minX, int maxX, int minZ, int maxZ, boolean selected) { BitSet selectedChunksForRegion = selectedChunksByRegion.computeIfAbsent(regionPos.getLong(), p -> new BitSet(MCRegion.CHUNKS_X * MCRegion.CHUNKS_Z)); Collection changedChunks = new ArrayList<>(); @@ -111,7 +111,7 @@ private boolean setChunksWithinRegion(Dimension dimension, ChunkPosition regionP /** * @return Whether the selection changed */ - private boolean setRegion(Dimension dimension, ChunkPosition regionPos, boolean selected) { + private boolean setRegion(Dimension dimension, RegionPosition regionPos, boolean selected) { return setChunks(dimension, regionPos.x << 5, regionPos.z << 5, (regionPos.x << 5) + 31, (regionPos.z << 5) + 31, selected); } @@ -157,7 +157,7 @@ private void notifyChunksUpdated(Collection chunks) { * * @param region the region with updated chunks */ - private void notifyRegionChunksUpdated(ChunkPosition region) { + private void notifyRegionChunksUpdated(RegionPosition region) { for (ChunkUpdateListener listener : chunkUpdateListeners) { listener.regionChunksUpdated(region); } @@ -169,7 +169,7 @@ private void notifyRegionChunksUpdated(ChunkPosition region) { * @param region the region with updated chunks * @param chunks the chunks within the region that have been updated */ - private void notifyRegionChunksUpdated(ChunkPosition region, Collection chunks) { + private void notifyRegionChunksUpdated(RegionPosition region, Collection chunks) { for (ChunkUpdateListener listener : chunkUpdateListeners) { listener.regionChunksUpdated(region, chunks); } @@ -235,7 +235,7 @@ public synchronized void selectChunk(Dimension dimension, int cx, int cz) { */ public synchronized void toggleRegion(Dimension dimension, int cx, int cz) { ChunkPosition chunk = new ChunkPosition(cx, cz); - setRegion(dimension, new ChunkPosition(cx >> 5, cz >> 5), !isChunkSelected(chunk)); + setRegion(dimension, chunk.getRegionPosition(), !isChunkSelected(chunk)); notifyChunkSelectionChange(); } @@ -277,10 +277,10 @@ public synchronized boolean setChunks(Dimension dimension, int minChunkX, int mi for (int regionZ = minRegionZ; regionZ < maxRegionZ + 1; regionZ++) { if(regionX >= minInnerRegionX && regionX < maxInnerRegionX && regionZ >= minInnerRegionZ && regionZ < maxInnerRegionZ) { // this region is an inner region, we set all chunks within it - selectionChanged |= setRegion(dimension, new ChunkPosition(regionX, regionZ), selected); + selectionChanged |= setRegion(dimension, new RegionPosition(regionX, regionZ), selected); } else { // this region is an outer region, we set only the chunks within the bounds of the selection area - selectionChanged |= setChunksWithinRegion(dimension, new ChunkPosition(regionX, regionZ), + selectionChanged |= setChunksWithinRegion(dimension, new RegionPosition(regionX, regionZ), Math.max(regionX << 5, minChunkX), Math.min(((regionX + 1) << 5) - 1, maxChunkX), Math.max(regionZ << 5, minChunkZ), Math.min(((regionZ + 1) << 5) - 1, maxChunkZ), selected); @@ -297,7 +297,7 @@ public synchronized boolean setChunks(Dimension dimension, int minChunkX, int mi for (int regionX = minRegionX; regionX < maxRegionX + 1; regionX++) { for (int regionZ = minRegionZ; regionZ < maxRegionZ + 1; regionZ++) { - selectionChanged |= setChunksWithinRegion(dimension, new ChunkPosition(regionX, regionZ), + selectionChanged |= setChunksWithinRegion(dimension, new RegionPosition(regionX, regionZ), Math.max(regionX << 5, minChunkX), Math.min(((regionX + 1) << 5) - 1, maxChunkX), Math.max(regionZ << 5, minChunkZ), Math.min(((regionZ + 1) << 5) - 1, maxChunkZ), selected); @@ -350,13 +350,13 @@ public synchronized void clearSelection() { if (!selectedChunksByRegion.isEmpty()) { //Collect all region positions currently selected (slightly weird because concurrent map) Enumeration keys = selectedChunksByRegion.keys(); - Collection regionPositions = new ArrayList<>(); + Collection regionPositions = new ArrayList<>(); while (keys.hasMoreElements()) { - regionPositions.add(new ChunkPosition(keys.nextElement())); + regionPositions.add(new RegionPosition(keys.nextElement())); } selectedChunksByRegion.clear(); - for (ChunkPosition regionPosition : regionPositions) { + for (RegionPosition regionPosition : regionPositions) { notifyRegionChunksUpdated(regionPosition); } notifyChunkSelectionChange(); @@ -395,13 +395,13 @@ public synchronized Collection getSelection() { public synchronized Map> getSelectionByRegion() { Map> selectedChunksByRegionPosition = new Object2ReferenceOpenHashMap<>(); selectedChunksByRegion.forEach((regionPosition, selectedChunksBitSet) -> { - ChunkPosition regionPos = new ChunkPosition(regionPosition); + RegionPosition regionPos = new RegionPosition(regionPosition); List positions = new ArrayList<>(); for (int localX = 0; localX < MCRegion.CHUNKS_X; localX++) { for (int localZ = 0; localZ < MCRegion.CHUNKS_Z; localZ++) { int idx = localX + (localZ * MCRegion.CHUNKS_X); if(selectedChunksBitSet.get(idx)) { - positions.add(new ChunkPosition((regionPos.x << 5) + localX, (regionPos.z << 5) + localZ)); + positions.add(regionPos.asChunkPosition(localX, localZ)); } } } diff --git a/chunky/src/java/se/llbit/chunky/world/ChunkView.java b/chunky/src/java/se/llbit/chunky/world/ChunkView.java index 9b798437c2..99001428df 100644 --- a/chunky/src/java/se/llbit/chunky/world/ChunkView.java +++ b/chunky/src/java/se/llbit/chunky/world/ChunkView.java @@ -194,7 +194,7 @@ public boolean isChunkVisible(int x, int z) { return px0 <= x && px1 >= x && pz0 <= z && pz1 >= z; } - public boolean isRegionVisible(ChunkPosition pos) { + public boolean isRegionVisible(RegionPosition pos) { return isRegionVisible(pos.x, pos.z); } diff --git a/chunky/src/java/se/llbit/chunky/world/CubicDimension.java b/chunky/src/java/se/llbit/chunky/world/CubicDimension.java index 117ddf631b..f44e1a9bb5 100644 --- a/chunky/src/java/se/llbit/chunky/world/CubicDimension.java +++ b/chunky/src/java/se/llbit/chunky/world/CubicDimension.java @@ -46,11 +46,11 @@ public ChunkData createChunkData(ChunkData chunkData, int chunkVersion) { } @Override - public Region createRegion(ChunkPosition pos) { + public Region createRegion(RegionPosition pos) { return new ImposterCubicRegion(pos, this); } - public synchronized Region getRegionWithinRange(ChunkPosition pos, int minY, int maxY) { + public synchronized Region getRegionWithinRange(RegionPosition pos, int minY, int maxY) { return regionMap.computeIfAbsent(pos.getLong(), p -> { // check if the region is present in the world directory Region region = EmptyRegion.instance; @@ -63,7 +63,7 @@ public synchronized Region getRegionWithinRange(ChunkPosition pos, int minY, int /** no choice but to iterate over every file in the directory */ @Override - public boolean regionExists(ChunkPosition pos) { + public boolean regionExists(RegionPosition pos) { File regionDirectory = getRegionDirectory(); try { Stream list = Files.list(regionDirectory.toPath()); @@ -84,7 +84,7 @@ public boolean regionExists(ChunkPosition pos) { } @Override - public boolean regionExistsWithinRange(ChunkPosition pos, int minY, int maxY) { + public boolean regionExistsWithinRange(RegionPosition pos, int minY, int maxY) { int cubicRegionX = pos.x << 1; int cubicRegionZ = pos.z << 1; @@ -104,7 +104,7 @@ public boolean regionExistsWithinRange(ChunkPosition pos, int minY, int maxY) { } /** Called when a new region has been discovered by the region parser. */ - public void regionDiscovered(ChunkPosition pos) { + public void regionDiscovered(RegionPosition pos) { synchronized (this) { regionMap.computeIfAbsent(pos.getLong(), (p) -> createRegion(pos)); } diff --git a/chunky/src/java/se/llbit/chunky/world/Dimension.java b/chunky/src/java/se/llbit/chunky/world/Dimension.java index 6bb5bb7fba..e3d2aa3e99 100644 --- a/chunky/src/java/se/llbit/chunky/world/Dimension.java +++ b/chunky/src/java/se/llbit/chunky/world/Dimension.java @@ -114,7 +114,7 @@ public ChunkData createChunkData(@Nullable ChunkData chunkData, int chunkVersion } } - public Region createRegion(ChunkPosition pos) { + public Region createRegion(RegionPosition pos) { return new MCRegion(pos, this); } @@ -126,7 +126,7 @@ public RegionChangeWatcher createRegionChangeWatcher(WorldMapLoader worldMapLoad * @param pos Region position * @return The region at the given position */ - public synchronized Region getRegion(ChunkPosition pos) { + public synchronized Region getRegion(RegionPosition pos) { return regionMap.computeIfAbsent(pos.getLong(), p -> { // check if the region is present in the world directory Region region = EmptyRegion.instance; @@ -137,12 +137,12 @@ public synchronized Region getRegion(ChunkPosition pos) { }); } - public Region getRegionWithinRange(ChunkPosition pos, int yMin, int yMax) { + public Region getRegionWithinRange(RegionPosition pos, int yMin, int yMax) { return getRegion(pos); } /** Set the region for the given position. */ - public synchronized void setRegion(ChunkPosition pos, Region region) { + public synchronized void setRegion(RegionPosition pos, Region region) { regionMap.put(pos.getLong(), region); } @@ -150,8 +150,8 @@ public synchronized void setRegion(ChunkPosition pos, Region region) { * @param pos region position * @return {@code true} if a region file exists for the given position */ - public boolean regionExists(ChunkPosition pos) { - File regionFile = new File(getRegionDirectory(), MCRegion.getFileName(pos)); + public boolean regionExists(RegionPosition pos) { + File regionFile = new File(getRegionDirectory(), pos.getMcaName()); return regionFile.exists(); } @@ -161,7 +161,7 @@ public boolean regionExists(ChunkPosition pos) { * @param maxY Maximum block Y (exclusive) * @return Whether the region exists */ - public boolean regionExistsWithinRange(ChunkPosition pos, int minY, int maxY) { + public boolean regionExistsWithinRange(RegionPosition pos, int minY, int maxY) { return this.regionExists(pos); } @@ -203,7 +203,7 @@ public Heightmap getHeightmap() { } /** Called when a new region has been discovered by the region parser. */ - public void regionDiscovered(ChunkPosition pos) { + public void regionDiscovered(RegionPosition pos) { synchronized (this) { regionMap.computeIfAbsent(pos.getLong(), p -> createRegion(pos)); } @@ -219,7 +219,7 @@ private void fireChunkUpdated(ChunkPosition chunk) { } /** Notify region update listeners. */ - private void fireRegionUpdated(ChunkPosition region) { + private void fireRegionUpdated(RegionPosition region) { synchronized (chunkUpdateListeners) { for (ChunkUpdateListener listener : chunkUpdateListeners) { listener.regionUpdated(region); @@ -237,7 +237,7 @@ public void chunkUpdated(ChunkPosition chunk) { } /** Called when a chunk has been updated. */ - public void regionUpdated(ChunkPosition region) { + public void regionUpdated(RegionPosition region) { fireRegionUpdated(region); } diff --git a/chunky/src/java/se/llbit/chunky/world/RegionPosition.java b/chunky/src/java/se/llbit/chunky/world/RegionPosition.java new file mode 100644 index 0000000000..8b70691973 --- /dev/null +++ b/chunky/src/java/se/llbit/chunky/world/RegionPosition.java @@ -0,0 +1,58 @@ +package se.llbit.chunky.world; + +import java.util.Objects; + +import static se.llbit.chunky.world.ChunkPosition.*; + +public class RegionPosition { + public final int x; + public final int z; + + public RegionPosition(int x, int z) { + this.x = x; + this.z = z; + } + + public RegionPosition(long position) { + this(longPositionX(position), longPositionZ(position)); + } + + /** + * @return This region position packed into a long. + */ + public long getLong() { + return positionToLong(x, z); + } + + /** + * @param localChunkX Chunk X coordinate relative to this region position + * @param localChunkZ Chunk Z coordinate relative to this region position + * @return Returns the global chunk position of the given region-local chunk coordinates + */ + public ChunkPosition asChunkPosition(int localChunkX, int localChunkZ) { + return new ChunkPosition( + this.x << 5 | (localChunkX & 0x1f), + this.z << 5 | (localChunkZ & 0x1f) + ); + } + + /** + * @return The .mca name for the region with this position + */ + public String getMcaName() { + return String.format("r.%d.%d.mca", x, z); + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + RegionPosition that = (RegionPosition) o; + return x == that.x && z == that.z; + } + + @Override + public int hashCode() { + return Objects.hash(x, z); + } +} diff --git a/chunky/src/java/se/llbit/chunky/world/World.java b/chunky/src/java/se/llbit/chunky/world/World.java index 51c8a4d089..ac3a2967ca 100644 --- a/chunky/src/java/se/llbit/chunky/world/World.java +++ b/chunky/src/java/se/llbit/chunky/world/World.java @@ -295,10 +295,10 @@ protected synchronized File getRegionDirectory(int dimension) { public synchronized void exportChunksToZip(File target, Collection chunks, ProgressTracker progress) throws IOException { - Map> regionMap = new HashMap<>(); + Map> regionMap = new HashMap<>(); for (ChunkPosition chunk : chunks) { - ChunkPosition regionPosition = chunk.getRegionPosition(); + RegionPosition regionPosition = chunk.getRegionPosition(); Set chunkSet = regionMap.computeIfAbsent(regionPosition, k -> new HashSet<>()); chunkSet.add(new ChunkPosition(chunk.x & 31, chunk.z & 31)); } @@ -315,12 +315,12 @@ public synchronized void exportChunksToZip(File target, Collection> entry : regionMap.entrySet()) { + for (Map.Entry> entry : regionMap.entrySet()) { if (progress.isInterrupted()) break; - ChunkPosition region = entry.getKey(); + RegionPosition region = entry.getKey(); appendRegionToZip(zout, currentDimension.getRegionDirectory(), region, regionDirectory + "/" + region.getMcaName(), entry.getValue()); @@ -342,10 +342,10 @@ public synchronized void exportWorldToZip(File target, ProgressTracker progress) throws IOException { System.out.println("exporting all dimensions to " + target.getName()); - final Collection> regions = new LinkedList<>(); + final Collection> regions = new LinkedList<>(); WorldScanner.Operator operator = (regionDirectory, x, z) -> - regions.add(new Pair<>(regionDirectory, new ChunkPosition(x, z))); + regions.add(new Pair<>(regionDirectory, new RegionPosition(x, z))); // TODO make this more dynamic File overworld = getRegionDirectory(OVERWORLD_DIMENSION); WorldScanner.findExistingChunks(overworld, operator); @@ -359,7 +359,7 @@ public synchronized void exportWorldToZip(File target, ProgressTracker progress) writeLevelDatToZip(zout); progress.setProgress(++work); - for (Pair region : regions) { + for (Pair region : regions) { if (progress.isInterrupted()) { break; @@ -396,7 +396,7 @@ private void writeLevelDatToZip(ZipOutputStream zout) throws IOException { } private void appendRegionToZip(ZipOutputStream zout, File regionDirectory, - ChunkPosition regionPos, String regionZipFileName, Set chunks) + RegionPosition regionPos, String regionZipFileName, Set chunks) throws IOException { zout.putNextEntry(new ZipEntry(regionZipFileName)); diff --git a/chunky/src/java/se/llbit/chunky/world/listeners/ChunkUpdateListener.java b/chunky/src/java/se/llbit/chunky/world/listeners/ChunkUpdateListener.java index d7e801deab..436abd9507 100644 --- a/chunky/src/java/se/llbit/chunky/world/listeners/ChunkUpdateListener.java +++ b/chunky/src/java/se/llbit/chunky/world/listeners/ChunkUpdateListener.java @@ -17,19 +17,20 @@ package se.llbit.chunky.world.listeners; import se.llbit.chunky.world.ChunkPosition; +import se.llbit.chunky.world.RegionPosition; import se.llbit.chunky.world.region.MCRegion; import java.util.Collection; public interface ChunkUpdateListener { - default void regionUpdated(ChunkPosition region) {} + default void regionUpdated(RegionPosition region) {} default void chunkUpdated(ChunkPosition chunkPosition) {} /** * All chunks within a region have been updated */ - default void regionChunksUpdated(ChunkPosition region) { + default void regionChunksUpdated(RegionPosition region) { int minChunkX = region.x << 5; int minChunkZ = region.z << 5; for (int chunkX = minChunkX; chunkX < minChunkX + MCRegion.CHUNKS_X; chunkX++) { @@ -42,7 +43,7 @@ default void regionChunksUpdated(ChunkPosition region) { /** * Some chunks within a region have been updated */ - default void regionChunksUpdated(ChunkPosition region, Collection chunks) { + default void regionChunksUpdated(RegionPosition region, Collection chunks) { for (ChunkPosition chunk : chunks) { this.chunkUpdated(chunk); } diff --git a/chunky/src/java/se/llbit/chunky/world/region/EmptyRegion.java b/chunky/src/java/se/llbit/chunky/world/region/EmptyRegion.java index 6ba587501e..b9c05be0f7 100644 --- a/chunky/src/java/se/llbit/chunky/world/region/EmptyRegion.java +++ b/chunky/src/java/se/llbit/chunky/world/region/EmptyRegion.java @@ -19,6 +19,7 @@ import se.llbit.chunky.world.Chunk; import se.llbit.chunky.world.ChunkPosition; import se.llbit.chunky.world.EmptyRegionChunk; +import se.llbit.chunky.world.RegionPosition; import java.util.Iterator; import java.util.NoSuchElementException; @@ -29,7 +30,7 @@ * @author Jesper Öqvist */ public class EmptyRegion implements Region { - private static final ChunkPosition position = new ChunkPosition(0,0); + private static final RegionPosition position = new RegionPosition(0,0); /** * Singleton instance. @@ -56,7 +57,7 @@ public void parse(int yMin, int yMax) { } } @Override - public ChunkPosition getPosition() { + public RegionPosition getPosition() { return position; } diff --git a/chunky/src/java/se/llbit/chunky/world/region/ImposterCubicRegion.java b/chunky/src/java/se/llbit/chunky/world/region/ImposterCubicRegion.java index 72f1b901d6..60ae6c2f82 100644 --- a/chunky/src/java/se/llbit/chunky/world/region/ImposterCubicRegion.java +++ b/chunky/src/java/se/llbit/chunky/world/region/ImposterCubicRegion.java @@ -31,7 +31,7 @@ public class ImposterCubicRegion implements Region { private final Chunk[] chunks = new Chunk[CHUNKS_COUNT]; /** The MC region position of this imposter */ - private final ChunkPosition mcRegionPos; + private final RegionPosition mcRegionPos; /** The minimum cubic region position of this imposter */ private final ChunkPosition min3drPosition; @@ -50,7 +50,7 @@ public class ImposterCubicRegion implements Region { * One flag per region column */ private final boolean[] anyUpdated = new boolean[DIAMETER_IN_CUBIC_REGIONS*DIAMETER_IN_CUBIC_REGIONS]; - public ImposterCubicRegion(ChunkPosition pos, CubicDimension dimension) { + public ImposterCubicRegion(RegionPosition pos, CubicDimension dimension) { this.dimension = dimension; mcRegionPos = pos; min3drPosition = new ChunkPosition(mcRegionToMinCubicRegion(pos.x), mcRegionToMinCubicRegion(pos.z)); @@ -251,7 +251,7 @@ private BitSet markColumnsWithCubes() { } @Override - public ChunkPosition getPosition() { + public RegionPosition getPosition() { return mcRegionPos; } diff --git a/chunky/src/java/se/llbit/chunky/world/region/MCRegion.java b/chunky/src/java/se/llbit/chunky/world/region/MCRegion.java index 199d71dbef..e6cee2dda4 100644 --- a/chunky/src/java/se/llbit/chunky/world/region/MCRegion.java +++ b/chunky/src/java/se/llbit/chunky/world/region/MCRegion.java @@ -62,7 +62,7 @@ public class MCRegion implements Region { private final static int SECTOR_SIZE = 4096; private final Chunk[] chunks = new Chunk[NUM_CHUNKS]; - private final ChunkPosition position; + private final RegionPosition position; private final Dimension dimension; private final String fileName; private long regionFileTime = 0; @@ -80,7 +80,7 @@ private static int getMCAChunkIndex(ChunkPosition chunkPos) { * * @param pos the region position */ - public MCRegion(ChunkPosition pos, Dimension dimension) { + public MCRegion(RegionPosition pos, Dimension dimension) { this.dimension = dimension; fileName = pos.getMcaName(); position = pos; @@ -181,7 +181,7 @@ public synchronized void parse(int minY, int maxY) { * @return The region position */ @Override - public final ChunkPosition getPosition() { + public final RegionPosition getPosition() { return position; } @@ -391,7 +391,7 @@ public void deleteChunkFromRegion(ChunkPosition chunkPos) { * * @throws IOException */ - public static synchronized void writeRegion(File regionDirectory, ChunkPosition regionPos, + public static synchronized void writeRegion(File regionDirectory, RegionPosition regionPos, DataOutputStream out, Set chunks) throws IOException { String fileName = regionPos.getMcaName(); File regionFile = new File(regionDirectory, fileName); diff --git a/chunky/src/java/se/llbit/chunky/world/region/MCRegionChangeWatcher.java b/chunky/src/java/se/llbit/chunky/world/region/MCRegionChangeWatcher.java index 2f2c7a8369..b3e9e3bcc0 100644 --- a/chunky/src/java/se/llbit/chunky/world/region/MCRegionChangeWatcher.java +++ b/chunky/src/java/se/llbit/chunky/world/region/MCRegionChangeWatcher.java @@ -23,6 +23,7 @@ import se.llbit.chunky.world.ChunkPosition; import se.llbit.chunky.world.ChunkView; import se.llbit.chunky.world.Dimension; +import se.llbit.chunky.world.RegionPosition; /** * Monitors filesystem for changes to region files. @@ -47,7 +48,7 @@ public MCRegionChangeWatcher(WorldMapLoader loader, MapView mapView) { ChunkView theView = view; for (int rx = theView.prx0; rx <= theView.prx1; ++rx) { for (int rz = theView.prz0; rz <= theView.prz1; ++rz) { - ChunkPosition pos = new ChunkPosition(rx, rz); + RegionPosition pos = new RegionPosition(rx, rz); Region region = dimension.getRegionWithinRange(pos, theView.yMin, theView.yMax); if (region.isEmpty()) { if (dimension.regionExistsWithinRange(pos, theView.yMin, theView.yMax)) { diff --git a/chunky/src/java/se/llbit/chunky/world/region/Region.java b/chunky/src/java/se/llbit/chunky/world/region/Region.java index 381e95f438..f12662ef1e 100644 --- a/chunky/src/java/se/llbit/chunky/world/region/Region.java +++ b/chunky/src/java/se/llbit/chunky/world/region/Region.java @@ -3,6 +3,7 @@ import se.llbit.chunky.plugin.PluginApi; import se.llbit.chunky.world.Chunk; import se.llbit.chunky.world.ChunkPosition; +import se.llbit.chunky.world.RegionPosition; public interface Region extends Iterable { /** @@ -37,7 +38,7 @@ default boolean isEmpty() { return false; } - ChunkPosition getPosition(); + RegionPosition getPosition(); boolean hasChanged(); diff --git a/chunky/src/java/se/llbit/chunky/world/region/RegionParser.java b/chunky/src/java/se/llbit/chunky/world/region/RegionParser.java index 299d896acf..b25f0771c0 100644 --- a/chunky/src/java/se/llbit/chunky/world/region/RegionParser.java +++ b/chunky/src/java/se/llbit/chunky/world/region/RegionParser.java @@ -53,7 +53,7 @@ public RegionParser(WorldMapLoader loader, RegionQueue queue, MapView mapView) { @Override public void run() { while (!isInterrupted()) { try { - ChunkPosition position = queue.poll(); + RegionPosition position = queue.poll(); if (position == null) { Log.warn("Region parser shutting down abnormally."); return; diff --git a/chunky/src/java/se/llbit/chunky/world/region/RegionQueue.java b/chunky/src/java/se/llbit/chunky/world/region/RegionQueue.java index f993509f9f..d891558d7f 100644 --- a/chunky/src/java/se/llbit/chunky/world/region/RegionQueue.java +++ b/chunky/src/java/se/llbit/chunky/world/region/RegionQueue.java @@ -17,6 +17,7 @@ package se.llbit.chunky.world.region; import se.llbit.chunky.world.ChunkPosition; +import se.llbit.chunky.world.RegionPosition; import java.util.HashSet; import java.util.LinkedList; @@ -30,10 +31,10 @@ */ public class RegionQueue { - private final Queue queue = new LinkedList<>(); - private final Set set = new HashSet<>(); + private final Queue queue = new LinkedList<>(); + private final Set set = new HashSet<>(); - public synchronized ChunkPosition poll() { + public synchronized RegionPosition poll() { try { while (queue.isEmpty()) { wait(); @@ -41,12 +42,12 @@ public synchronized ChunkPosition poll() { } catch (InterruptedException e) { return null; } - ChunkPosition position = queue.poll(); + RegionPosition position = queue.poll(); set.remove(position); return position; } - public synchronized boolean add(ChunkPosition position) { + public synchronized boolean add(RegionPosition position) { if (!set.contains(position)) { queue.add(position); set.add(position);