diff --git a/src/main/java/net/minestom/server/instance/Chunk.java b/src/main/java/net/minestom/server/instance/Chunk.java index 9bb24c655..2d2dfe970 100644 --- a/src/main/java/net/minestom/server/instance/Chunk.java +++ b/src/main/java/net/minestom/server/instance/Chunk.java @@ -65,6 +65,7 @@ public abstract class Chunk implements Viewable, DataContainer { private boolean shouldGenerate; // Packet cache + private volatile boolean enableCachePacket; protected volatile boolean packetUpdated; private ByteBuf fullDataPacket; @@ -83,6 +84,9 @@ public abstract class Chunk implements Viewable, DataContainer { this.chunkZ = chunkZ; this.shouldGenerate = shouldGenerate; + // true by default + this.enableCachePacket = true; + if (biomes != null && biomes.length == BIOME_COUNT) { this.biomes = biomes; } else { @@ -273,6 +277,30 @@ public abstract class Chunk implements Viewable, DataContainer { return shouldGenerate; } + /** + * Get if this chunk automatically cache the latest {@link ChunkDataPacket} version. + *
+ * Retrieved with {@link #retrieveDataBuffer(Consumer)}. + * + * @return true if the chunk automatically cache the chunk packet + */ + public boolean enableCachePacket() { + return enableCachePacket; + } + + /** + * Enable or disable the automatic {@link ChunkDataPacket} caching. + * + * @param enableCachePacket true to enable to chunk packet caching + */ + public synchronized void setEnableCachePacket(boolean enableCachePacket) { + this.enableCachePacket = enableCachePacket; + if (enableCachePacket && fullDataPacket != null) { + this.fullDataPacket.release(); + this.fullDataPacket = null; + } + } + /** * Get the cached data packet *
@@ -285,7 +313,7 @@ public abstract class Chunk implements Viewable, DataContainer {
}
/**
- * Set the cached {@link ChunkDataPacket} of this chunk
+ * Set the cached {@link ChunkDataPacket} of this chunk.
*
* @param fullDataPacket the new cached chunk packet
*/
@@ -313,7 +341,9 @@ public abstract class Chunk implements Viewable, DataContainer {
if (data == null || !packetUpdated) {
// Packet has never been wrote or is outdated, write it
PacketWriterUtils.writeCallbackPacket(getFreshFullDataPacket(), packet -> {
- setFullDataPacket(packet);
+ if (enableCachePacket) {
+ setFullDataPacket(packet);
+ }
consumer.accept(packet);
});
} else {
@@ -504,7 +534,7 @@ public abstract class Chunk implements Viewable, DataContainer {
* Get the {@link ChunkDataPacket} to update a single chunk section
*
* @param section the chunk section to update
- * @return the {@link ChunkDataPacket} to update a single chunk sectionl
+ * @return the {@link ChunkDataPacket} to update a single chunk section
*/
protected ChunkDataPacket getChunkSectionUpdatePacket(int section) {
ChunkDataPacket chunkDataPacket = getFreshPartialDataPacket();
diff --git a/src/main/java/net/minestom/server/instance/DynamicChunk.java b/src/main/java/net/minestom/server/instance/DynamicChunk.java
index 526158452..82d2c2a3d 100644
--- a/src/main/java/net/minestom/server/instance/DynamicChunk.java
+++ b/src/main/java/net/minestom/server/instance/DynamicChunk.java
@@ -38,8 +38,8 @@ public class DynamicChunk extends Chunk {
protected final short[] customBlocksId = new short[CHUNK_SIZE_X * CHUNK_SIZE_Y * CHUNK_SIZE_Z];
// Used to get all blocks with data (no null)
- // Key is still chunk coord
- protected Int2ObjectMap blocksData = new Int2ObjectOpenHashMap<>(16 * 16); // Start with the size of a single row
+ // Key is still chunk coordinates (see #getBlockIndex)
+ protected Int2ObjectMap blocksData = new Int2ObjectOpenHashMap<>();
// Contains CustomBlocks' block index which are updatable
protected IntSet updatableBlocks = new IntOpenHashSet();
diff --git a/src/main/java/net/minestom/server/instance/IChunkLoader.java b/src/main/java/net/minestom/server/instance/IChunkLoader.java
index 3905a878e..aec40dec5 100644
--- a/src/main/java/net/minestom/server/instance/IChunkLoader.java
+++ b/src/main/java/net/minestom/server/instance/IChunkLoader.java
@@ -4,6 +4,7 @@ import net.minestom.server.utils.chunk.ChunkCallback;
/**
* Interface implemented to change the way chunks are loaded/saved.
+ *
* See {@link MinestomBasicChunkLoader} for the default implementation used in {@link InstanceContainer}.
*/
public interface IChunkLoader {