Added Chunk#setEnableCachePacket to improve memory usage when needed

This commit is contained in:
themode 2020-10-15 08:21:13 +02:00
parent 50326b676c
commit 74e37e681b
3 changed files with 36 additions and 5 deletions

View File

@ -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.
* <p>
* 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
* <p>
@ -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();

View File

@ -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<Data> blocksData = new Int2ObjectOpenHashMap<>(16 * 16); // Start with the size of a single row
// Key is still chunk coordinates (see #getBlockIndex)
protected Int2ObjectMap<Data> blocksData = new Int2ObjectOpenHashMap<>();
// Contains CustomBlocks' block index which are updatable
protected IntSet updatableBlocks = new IntOpenHashSet();

View File

@ -4,6 +4,7 @@ import net.minestom.server.utils.chunk.ChunkCallback;
/**
* Interface implemented to change the way chunks are loaded/saved.
* <p>
* See {@link MinestomBasicChunkLoader} for the default implementation used in {@link InstanceContainer}.
*/
public interface IChunkLoader {