From e453a0f9b510b5cf2916f46cbb4702d7ca0b3b7b Mon Sep 17 00:00:00 2001 From: themode Date: Fri, 20 Nov 2020 08:39:06 +0100 Subject: [PATCH] Added Chunk#getLastChangeTime --- .../net/minestom/server/instance/Chunk.java | 11 ++++++ .../server/instance/DynamicChunk.java | 34 ++++++++++++++----- 2 files changed, 36 insertions(+), 9 deletions(-) diff --git a/src/main/java/net/minestom/server/instance/Chunk.java b/src/main/java/net/minestom/server/instance/Chunk.java index 7b5cad348..92e37c495 100644 --- a/src/main/java/net/minestom/server/instance/Chunk.java +++ b/src/main/java/net/minestom/server/instance/Chunk.java @@ -191,6 +191,17 @@ public abstract class Chunk implements Viewable, DataContainer { @NotNull public abstract Set getBlockEntities(); + /** + * Gets the last time that this chunk changed. + *

+ * "Change" means here data used in {@link ChunkDataPacket}. + * It is necessary to see if the cached version of this chunk can be used + * instead of re writing and compressing everything. + * + * @return the last change time in milliseconds + */ + public abstract long getLastChangeTime(); + /** * Serializes the chunk into bytes. * diff --git a/src/main/java/net/minestom/server/instance/DynamicChunk.java b/src/main/java/net/minestom/server/instance/DynamicChunk.java index 4def0f0b1..80929c9e9 100644 --- a/src/main/java/net/minestom/server/instance/DynamicChunk.java +++ b/src/main/java/net/minestom/server/instance/DynamicChunk.java @@ -57,6 +57,8 @@ public class DynamicChunk extends Chunk { // Block entities protected final Set blockEntities = new CopyOnWriteArraySet<>(); + private long lastChangeTime; + public DynamicChunk(@Nullable Biome[] biomes, int chunkX, int chunkZ, @NotNull PaletteStorage blockPalette, @NotNull PaletteStorage customBlockPalette) { super(biomes, chunkX, chunkZ, true); @@ -86,8 +88,8 @@ public class DynamicChunk extends Chunk { // True if the block is not complete air without any custom block capabilities final boolean hasBlock = blockStateId != 0 || customBlockId != 0; - this.blockPalette.setBlockAt(x, y, z, blockStateId); - this.customBlockPalette.setBlockAt(x, y, z, customBlockId); + setBlockAt(blockPalette, x, y, z, blockStateId); + setBlockAt(customBlockPalette, x, y, z, customBlockId); if (!hasBlock) { // Block has been deleted, clear cache and return @@ -155,23 +157,23 @@ public class DynamicChunk extends Chunk { @Override public short getBlockStateId(int x, int y, int z) { - return this.blockPalette.getBlockAt(x, y, z); + return getBlockAt(blockPalette, x, y, z); } @Override public short getCustomBlockId(int x, int y, int z) { - return customBlockPalette.getBlockAt(x, y, z); + return getBlockAt(customBlockPalette, x, y, z); } @Override protected void refreshBlockValue(int x, int y, int z, short blockStateId, short customBlockId) { - this.blockPalette.setBlockAt(x, y, z, blockStateId); - this.customBlockPalette.setBlockAt(x, y, z, customBlockId); + setBlockAt(blockPalette, x, y, z, blockStateId); + setBlockAt(customBlockPalette, x, y, z, customBlockId); } @Override protected void refreshBlockStateId(int x, int y, int z, short blockStateId) { - this.blockPalette.setBlockAt(x, y, z, blockStateId); + setBlockAt(blockPalette, x, y, z, blockStateId); } @Override @@ -195,6 +197,11 @@ public class DynamicChunk extends Chunk { return blockEntities; } + @Override + public long getLastChangeTime() { + return lastChangeTime; + } + /** * Serialize this {@link Chunk} based on {@link #readChunk(BinaryReader, ChunkCallback)} *

@@ -242,8 +249,8 @@ public class DynamicChunk extends Chunk { for (byte z = 0; z < CHUNK_SIZE_Z; z++) { final int index = getBlockIndex(x, y, z); - final short blockStateId = blockPalette.getBlockAt(x, y, z); - final short customBlockId = customBlockPalette.getBlockAt(x, y, z); + final short blockStateId = getBlockAt(blockPalette, x, y, z); + final short customBlockId = getBlockAt(customBlockPalette, x, y, z); // No block at the position if (blockStateId == 0 && customBlockId == 0) @@ -400,4 +407,13 @@ public class DynamicChunk extends Chunk { return dynamicChunk; } + + private short getBlockAt(@NotNull PaletteStorage paletteStorage, int x, int y, int z) { + return paletteStorage.getBlockAt(x, y, z); + } + + private void setBlockAt(@NotNull PaletteStorage paletteStorage, int x, int y, int z, short blockId) { + paletteStorage.setBlockAt(x, y, z, blockId); + this.lastChangeTime = System.currentTimeMillis(); + } } \ No newline at end of file