From 12822c508fa95863dca431922b8dccda75f8162e Mon Sep 17 00:00:00 2001 From: themode Date: Mon, 16 Nov 2020 17:36:31 +0100 Subject: [PATCH] Cleanup --- .../server/instance/batch/ChunkBatch.java | 17 ++++++++++++++--- .../network/netty/codec/PacketCompressor.java | 19 ++++++++++--------- 2 files changed, 24 insertions(+), 12 deletions(-) diff --git a/src/main/java/net/minestom/server/instance/batch/ChunkBatch.java b/src/main/java/net/minestom/server/instance/batch/ChunkBatch.java index f240f8699..40440a5e7 100644 --- a/src/main/java/net/minestom/server/instance/batch/ChunkBatch.java +++ b/src/main/java/net/minestom/server/instance/batch/ChunkBatch.java @@ -35,17 +35,22 @@ public class ChunkBatch implements InstanceBatch { // Need to be synchronized manually // Format: blockIndex/blockStateId/customBlockId (32/16/16 bits) - private final LongList blocks = new LongArrayList(); + private LongList blocks; // Need to be synchronized manually // block index - data - private final Int2ObjectMap blockDataMap = new Int2ObjectOpenHashMap<>(); + private Int2ObjectMap blockDataMap; public ChunkBatch(@NotNull InstanceContainer instance, @NotNull Chunk chunk, boolean generationBatch) { this.instance = instance; this.chunk = chunk; this.generationBatch = generationBatch; + + if (!generationBatch) { + this.blocks = new LongArrayList(); + this.blockDataMap = new Int2ObjectOpenHashMap<>(); + } } @Override @@ -66,10 +71,12 @@ public class ChunkBatch implements InstanceBatch { } private void addBlockData(byte x, int y, byte z, short blockStateId, short customBlockId, @Nullable Data data) { - if (isGenerationBatch()) { + // Directly place the block chunk.UNSAFE_setBlock(x, y, z, blockStateId, customBlockId, data, CustomBlockUtils.hasUpdate(customBlockId)); } else { + // Cache the entry to be placed later during flush + final int index = ChunkUtils.getBlockIndex(x, y, z); if (data != null) { @@ -120,6 +127,7 @@ public class ChunkBatch implements InstanceBatch { } } + // Safe callback instance.scheduleNextTick(inst -> { OptionalCallback.execute(callback, chunk); }); @@ -133,6 +141,7 @@ public class ChunkBatch implements InstanceBatch { * @param callback the callback to execute once the blocks are placed */ public void flush(@Nullable ChunkCallback callback) { + Check.stateCondition(generationBatch, "#flush is not support for generation batch."); BLOCK_BATCH_POOL.execute(() -> singleThreadFlush(callback, true)); } @@ -144,6 +153,7 @@ public class ChunkBatch implements InstanceBatch { * @param callback the callback to execute once the blocks are placed */ public void unsafeFlush(@Nullable ChunkCallback callback) { + Check.stateCondition(generationBatch, "#unsafeFlush is not support for generation batch."); BLOCK_BATCH_POOL.execute(() -> singleThreadFlush(callback, false)); } @@ -151,6 +161,7 @@ public class ChunkBatch implements InstanceBatch { * Resets the chunk batch by removing all the entries. */ public void clearData() { + Check.stateCondition(generationBatch, "#clearData is not support for generation batch."); synchronized (blocks) { this.blocks.clear(); } diff --git a/src/main/java/net/minestom/server/network/netty/codec/PacketCompressor.java b/src/main/java/net/minestom/server/network/netty/codec/PacketCompressor.java index deaf9e937..4e9c8ed8c 100644 --- a/src/main/java/net/minestom/server/network/netty/codec/PacketCompressor.java +++ b/src/main/java/net/minestom/server/network/netty/codec/PacketCompressor.java @@ -31,10 +31,10 @@ public class PacketCompressor extends ByteToMessageCodec { private final int threshold; - private byte[] buffer = new byte[8192]; + private final byte[] buffer = new byte[8192]; - private Deflater deflater = new Deflater(); - private Inflater inflater = new Inflater(); + private final Deflater deflater = new Deflater(); + private final Inflater inflater = new Inflater(); public PacketCompressor(int threshold) { this.threshold = threshold; @@ -80,14 +80,15 @@ public class PacketCompressor extends ByteToMessageCodec { // TODO optimize to do not initialize arrays each time - byte[] abyte = new byte[buf.readableBytes()]; - buf.readBytes(abyte); + byte[] input = new byte[buf.readableBytes()]; + buf.readBytes(input); - inflater.setInput(abyte); - byte[] abyte1 = new byte[i]; + inflater.setInput(input); - inflater.inflate(abyte1); - out.add(Unpooled.wrappedBuffer(abyte1)); + byte[] output = new byte[i]; + + inflater.inflate(output); + out.add(Unpooled.wrappedBuffer(output)); inflater.reset(); }