Skip to content

Commit ff1e38a

Browse files
committed
Fix Map-Updates not working correctly
1 parent 2dd7a0a commit ff1e38a

File tree

9 files changed

+75
-52
lines changed

9 files changed

+75
-52
lines changed

BlueMapAPI

BlueMapCommon/src/main/java/de/bluecolored/bluemap/common/plugin/Plugin.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -553,7 +553,7 @@ public synchronized void startWatchingMap(BmMap map) {
553553
stopWatchingMap(map);
554554

555555
try {
556-
RegionFileWatchService watcher = new RegionFileWatchService(renderManager, map, false);
556+
RegionFileWatchService watcher = new RegionFileWatchService(renderManager, map);
557557
watcher.start();
558558
regionFileWatchServices.put(map.getId(), watcher);
559559
} catch (IOException ex) {

BlueMapCommon/src/main/java/de/bluecolored/bluemap/common/plugin/RegionFileWatchService.java

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -47,18 +47,16 @@ public class RegionFileWatchService extends Thread {
4747
private final RenderManager renderManager;
4848
private final WatchService watchService;
4949

50-
private final boolean verbose;
5150
private volatile boolean closed;
5251

5352
private Timer delayTimer;
5453

5554
@DebugDump
5655
private final Map<Vector2i, TimerTask> scheduledUpdates;
5756

58-
public RegionFileWatchService(RenderManager renderManager, BmMap map, boolean verbose) throws IOException {
57+
public RegionFileWatchService(RenderManager renderManager, BmMap map) throws IOException {
5958
this.renderManager = renderManager;
6059
this.map = map;
61-
this.verbose = verbose;
6260
this.closed = false;
6361
this.scheduledUpdates = new HashMap<>();
6462

@@ -68,14 +66,17 @@ public RegionFileWatchService(RenderManager renderManager, BmMap map, boolean ve
6866
FileHelper.createDirectories(folder);
6967

7068
this.watchService = folder.getFileSystem().newWatchService();
71-
7269
folder.register(this.watchService, StandardWatchEventKinds.ENTRY_CREATE, StandardWatchEventKinds.ENTRY_MODIFY);
70+
71+
Logger.global.logDebug("Created region-file watch-service for map '" + map.getId() + "' at '" + folder + "'.");
7372
}
7473

7574
@Override
7675
public void run() {
7776
if (delayTimer == null) delayTimer = new Timer("BlueMap-RegionFileWatchService-DelayTimer", true);
7877

78+
Logger.global.logDebug("Started watching map '" + map.getId() + "' for updates...");
79+
7980
try {
8081
while (!closed) {
8182
WatchKey key = this.watchService.take();
@@ -95,14 +96,15 @@ public void run() {
9596

9697
if (!key.reset()) return;
9798
}
98-
} catch ( ClosedWatchServiceException ignore) {
99+
} catch (ClosedWatchServiceException ignore) {
99100
} catch (InterruptedException iex) {
100101
Thread.currentThread().interrupt();
101-
}
102-
103-
if (!closed) {
104-
Logger.global.logWarning("Region-file watch-service for map '" + map.getId() +
105-
"' stopped unexpectedly! (This map might not update automatically from now on)");
102+
} finally {
103+
Logger.global.logDebug("Stopped watching map '" + map.getId() + "' for updates.");
104+
if (!closed) {
105+
Logger.global.logWarning("Region-file watch-service for map '" + map.getId() +
106+
"' stopped unexpectedly! (This map might not update automatically from now on)");
107+
}
106108
}
107109
}
108110

@@ -118,7 +120,7 @@ private synchronized void updateRegion(String regionFileName) {
118120
int rZ = Integer.parseInt(filenameParts[2]);
119121
Vector2i regionPos = new Vector2i(rX, rZ);
120122

121-
// we only want to start the render when there were no changes on a file for 10 seconds
123+
// we only want to start the render when there were no changes on a file for 5 seconds
122124
TimerTask task = scheduledUpdates.remove(regionPos);
123125
if (task != null) task.cancel();
124126

@@ -130,12 +132,12 @@ public void run() {
130132
scheduledUpdates.remove(regionPos);
131133
renderManager.scheduleRenderTask(task);
132134

133-
if (verbose) Logger.global.logInfo("Scheduled update for region-file: " + regionPos + " (Map: " + map.getId() + ")");
135+
Logger.global.logDebug("Scheduled update for region-file: " + regionPos + " (Map: " + map.getId() + ")");
134136
}
135137
}
136138
};
137139
scheduledUpdates.put(regionPos, task);
138-
delayTimer.schedule(task, 10000);
140+
delayTimer.schedule(task, 5000);
139141
} catch (NumberFormatException ignore) {}
140142
}
141143

BlueMapCommon/src/main/java/de/bluecolored/bluemap/common/plugin/commands/Commands.java

Lines changed: 29 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -53,14 +53,16 @@
5353
import de.bluecolored.bluemap.core.map.BmMap;
5454
import de.bluecolored.bluemap.core.map.MapRenderState;
5555
import de.bluecolored.bluemap.core.storage.Storage;
56-
import de.bluecolored.bluemap.core.world.block.Block;
56+
import de.bluecolored.bluemap.core.world.Chunk;
5757
import de.bluecolored.bluemap.core.world.World;
58+
import de.bluecolored.bluemap.core.world.block.Block;
5859

5960
import java.io.IOException;
6061
import java.nio.file.Path;
6162
import java.util.*;
6263
import java.util.function.Function;
6364
import java.util.function.Predicate;
65+
import java.util.stream.Stream;
6466

6567
public class Commands<S> {
6668

@@ -501,24 +503,39 @@ public int debugBlockCommand(CommandContext<S> context) {
501503
Block<?> block = new Block<>(world, blockPos.getX(), blockPos.getY(), blockPos.getZ());
502504
Block<?> blockBelow = new Block<>(world, blockPos.getX(), blockPos.getY() - 1, blockPos.getZ());
503505

504-
// populate lazy-loaded values
505-
block.getBlockState();
506-
block.getBiomeId();
507-
block.getLightData();
508-
509-
blockBelow.getBlockState();
510-
blockBelow.getBiomeId();
511-
blockBelow.getLightData();
512-
513506
source.sendMessages(Arrays.asList(
514-
Text.of(TextColor.GOLD, "Block at you: ", TextColor.WHITE, block),
515-
Text.of(TextColor.GOLD, "Block below you: ", TextColor.WHITE, blockBelow)
507+
Text.of(TextColor.GOLD, "Block at you: \n", formatBlock(block)),
508+
Text.of(TextColor.GOLD, "Block below you: \n", formatBlock(blockBelow))
516509
));
517510
}, "BlueMap-Plugin-DebugBlockCommand").start();
518511

519512
return 1;
520513
}
521514

515+
private Text formatBlock(Block<?> block) {
516+
World world = block.getWorld();
517+
Chunk chunk = block.getChunk();
518+
519+
Map<String, Object> lines = new LinkedHashMap<>();
520+
lines.put("world-id", world.getId());
521+
lines.put("world-name", world.getName());
522+
lines.put("chunk-is-generated", chunk.isGenerated());
523+
lines.put("chunk-has-lightdata", chunk.hasLightData());
524+
lines.put("chunk-inhabited-time", chunk.getInhabitedTime());
525+
lines.put("block-state", block.getBlockState());
526+
lines.put("biome", block.getBiomeId());
527+
lines.put("position", block.getX() + " | " + block.getY() + " | " + block.getZ());
528+
lines.put("block-light", block.getBlockLightLevel());
529+
lines.put("sun-light", block.getSunLightLevel());
530+
531+
Object[] textElements = lines.entrySet().stream()
532+
.flatMap(e -> Stream.of(TextColor.GRAY, e.getKey(), ": ", TextColor.WHITE, e.getValue(), "\n"))
533+
.toArray(Object[]::new);
534+
textElements[textElements.length - 1] = "";
535+
536+
return Text.of(textElements);
537+
}
538+
522539
public int debugDumpCommand(CommandContext<S> context) {
523540
final CommandSource source = commandSourceInterface.apply(context.getSource());
524541

BlueMapCommon/src/main/java/de/bluecolored/bluemap/common/rendermanager/MapUpdateTask.java

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -106,16 +106,7 @@ private static List<Vector2i> getRegions(BmMap map) {
106106
private static List<Vector2i> getRegions(BmMap map, Vector2i center, int radius) {
107107
World world = map.getWorld();
108108
Grid regionGrid = world.getRegionGrid();
109-
110-
Predicate<Vector2i> regionFilter = r -> {
111-
Vector2i cellMin = regionGrid.getCellMin(r);
112-
if (cellMin.getX() > map.getMapSettings().getMaxPos().getX()) return false;
113-
if (cellMin.getY() > map.getMapSettings().getMaxPos().getZ()) return false;
114-
115-
Vector2i cellMax = regionGrid.getCellMax(r);
116-
if (cellMax.getX() < map.getMapSettings().getMinPos().getX()) return false;
117-
return cellMax.getY() >= map.getMapSettings().getMinPos().getZ();
118-
};
109+
Predicate<Vector2i> regionFilter = map.getMapSettings().getRenderBoundariesCellFilter(regionGrid);
119110

120111
if (center == null || radius < 0) {
121112
return world.listRegions().stream()

BlueMapCommon/src/main/java/de/bluecolored/bluemap/common/rendermanager/WorldRegionRenderTask.java

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ private synchronized void init() {
8888

8989
Grid tileGrid = map.getHiresModelManager().getTileGrid();
9090
Grid chunkGrid = map.getWorld().getChunkGrid();
91+
Predicate<Vector2i> boundsTileFilter = map.getMapSettings().getRenderBoundariesCellFilter(tileGrid);
9192

9293
for (Vector2i chunk : chunks) {
9394
Vector2i tileMin = chunkGrid.getCellMin(chunk, tileGrid);
@@ -103,16 +104,6 @@ private synchronized void init() {
103104
map.getWorld().invalidateChunkCache(chunk.getX(), chunk.getY());
104105
}
105106

106-
Predicate<Vector2i> boundsTileFilter = t -> {
107-
Vector2i cellMin = tileGrid.getCellMin(t);
108-
if (cellMin.getX() > map.getMapSettings().getMaxPos().getX()) return false;
109-
if (cellMin.getY() > map.getMapSettings().getMaxPos().getZ()) return false;
110-
111-
Vector2i cellMax = tileGrid.getCellMax(t);
112-
if (cellMax.getX() < map.getMapSettings().getMinPos().getX()) return false;
113-
return cellMax.getY() >= map.getMapSettings().getMinPos().getZ();
114-
};
115-
116107
this.tileCount = tileSet.size();
117108
this.tiles = tileSet.stream()
118109
.sorted(WorldRegionRenderTask::compareVec2L) //sort with longs to avoid overflow (comparison uses distanceSquared)

BlueMapCore/src/main/java/de/bluecolored/bluemap/core/map/hires/RenderSettings.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,11 @@
2424
*/
2525
package de.bluecolored.bluemap.core.map.hires;
2626

27+
import com.flowpowered.math.vector.Vector2i;
2728
import com.flowpowered.math.vector.Vector3i;
29+
import de.bluecolored.bluemap.core.util.Grid;
30+
31+
import java.util.function.Predicate;
2832

2933
public interface RenderSettings {
3034

@@ -101,6 +105,22 @@ default boolean isInsideRenderBoundaries(int x, int y, int z) {
101105
y <= max.getY();
102106
}
103107

108+
/**
109+
* Returns a predicate which is filtering out all cells of a {@link Grid}
110+
* that are completely outside the render boundaries.
111+
*/
112+
default Predicate<Vector2i> getRenderBoundariesCellFilter(Grid grid) {
113+
return t -> {
114+
Vector2i cellMin = grid.getCellMin(t);
115+
if (cellMin.getX() > getMaxPos().getX()) return false;
116+
if (cellMin.getY() > getMaxPos().getZ()) return false;
117+
118+
Vector2i cellMax = grid.getCellMax(t);
119+
if (cellMax.getX() < getMinPos().getX()) return false;
120+
return cellMax.getY() >= getMinPos().getZ();
121+
};
122+
}
123+
104124
boolean isSaveHiresLayer();
105125

106126
}

BlueMapCore/src/main/java/de/bluecolored/bluemap/core/world/mca/region/MCARegion.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@
3434
import lombok.Getter;
3535
import lombok.ToString;
3636

37-
import java.io.EOFException;
3837
import java.io.IOException;
3938
import java.nio.ByteBuffer;
4039
import java.nio.channels.FileChannel;
@@ -121,7 +120,7 @@ public void iterateAllChunks(ChunkConsumer consumer) throws IOException {
121120
// iterate over all chunks
122121
for (int x = 0; x < 32; x++) {
123122
for (int z = 0; z < 32; z++) {
124-
int xzChunk = z * 32 + x;
123+
int xzChunk = (z & 0b11111) << 5 | (x & 0b11111);
125124

126125
int size = header[xzChunk * 4 + 3] * 4096;
127126
if (size == 0) continue;
@@ -136,7 +135,7 @@ public void iterateAllChunks(ChunkConsumer consumer) throws IOException {
136135
timestamp |= header[i] & 0xFF;
137136

138137
// load chunk only if consumers filter returns true
139-
if (consumer.filter(chunkX, chunkZ, timestamp)) {
138+
if (consumer.filter(chunkX, chunkZ, timestamp * 1000L)) {
140139
i = xzChunk * 4;
141140
int offset = header[i++] << 16;
142141
offset |= (header[i++] & 0xFF) << 8;
@@ -190,7 +189,10 @@ private static void readFully(ReadableByteChannel src, ByteBuffer bb, int off, i
190189

191190
do {
192191
int read = src.read(bb);
193-
if (read < 0) throw new EOFException();
192+
if (read < 0)
193+
// zero out all the remaining data from the buffer
194+
while (bb.remaining() > 0)
195+
bb.put((byte) 0);
194196
} while (bb.remaining() > 0);
195197
}
196198

implementations/cli/src/main/java/de/bluecolored/bluemap/cli/BlueMapCLI.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ public void renderMaps(BlueMapService blueMap, boolean watch, boolean forceRende
9696
if (watch) {
9797
for (BmMap map : maps.values()) {
9898
try {
99-
RegionFileWatchService watcher = new RegionFileWatchService(renderManager, map, true);
99+
RegionFileWatchService watcher = new RegionFileWatchService(renderManager, map);
100100
watcher.start();
101101
regionFileWatchServices.add(watcher);
102102
} catch (IOException ex) {

0 commit comments

Comments
 (0)