This commit is contained in:
themode 2020-11-16 17:36:31 +01:00
parent 392e702108
commit 12822c508f
2 changed files with 24 additions and 12 deletions

View File

@ -35,17 +35,22 @@ public class ChunkBatch implements InstanceBatch {
// Need to be synchronized manually // Need to be synchronized manually
// Format: blockIndex/blockStateId/customBlockId (32/16/16 bits) // Format: blockIndex/blockStateId/customBlockId (32/16/16 bits)
private final LongList blocks = new LongArrayList(); private LongList blocks;
// Need to be synchronized manually // Need to be synchronized manually
// block index - data // block index - data
private final Int2ObjectMap<Data> blockDataMap = new Int2ObjectOpenHashMap<>(); private Int2ObjectMap<Data> blockDataMap;
public ChunkBatch(@NotNull InstanceContainer instance, @NotNull Chunk chunk, public ChunkBatch(@NotNull InstanceContainer instance, @NotNull Chunk chunk,
boolean generationBatch) { boolean generationBatch) {
this.instance = instance; this.instance = instance;
this.chunk = chunk; this.chunk = chunk;
this.generationBatch = generationBatch; this.generationBatch = generationBatch;
if (!generationBatch) {
this.blocks = new LongArrayList();
this.blockDataMap = new Int2ObjectOpenHashMap<>();
}
} }
@Override @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) { private void addBlockData(byte x, int y, byte z, short blockStateId, short customBlockId, @Nullable Data data) {
if (isGenerationBatch()) { if (isGenerationBatch()) {
// Directly place the block
chunk.UNSAFE_setBlock(x, y, z, blockStateId, customBlockId, data, CustomBlockUtils.hasUpdate(customBlockId)); chunk.UNSAFE_setBlock(x, y, z, blockStateId, customBlockId, data, CustomBlockUtils.hasUpdate(customBlockId));
} else { } else {
// Cache the entry to be placed later during flush
final int index = ChunkUtils.getBlockIndex(x, y, z); final int index = ChunkUtils.getBlockIndex(x, y, z);
if (data != null) { if (data != null) {
@ -120,6 +127,7 @@ public class ChunkBatch implements InstanceBatch {
} }
} }
// Safe callback
instance.scheduleNextTick(inst -> { instance.scheduleNextTick(inst -> {
OptionalCallback.execute(callback, chunk); OptionalCallback.execute(callback, chunk);
}); });
@ -133,6 +141,7 @@ public class ChunkBatch implements InstanceBatch {
* @param callback the callback to execute once the blocks are placed * @param callback the callback to execute once the blocks are placed
*/ */
public void flush(@Nullable ChunkCallback callback) { public void flush(@Nullable ChunkCallback callback) {
Check.stateCondition(generationBatch, "#flush is not support for generation batch.");
BLOCK_BATCH_POOL.execute(() -> singleThreadFlush(callback, true)); 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 * @param callback the callback to execute once the blocks are placed
*/ */
public void unsafeFlush(@Nullable ChunkCallback callback) { public void unsafeFlush(@Nullable ChunkCallback callback) {
Check.stateCondition(generationBatch, "#unsafeFlush is not support for generation batch.");
BLOCK_BATCH_POOL.execute(() -> singleThreadFlush(callback, false)); 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. * Resets the chunk batch by removing all the entries.
*/ */
public void clearData() { public void clearData() {
Check.stateCondition(generationBatch, "#clearData is not support for generation batch.");
synchronized (blocks) { synchronized (blocks) {
this.blocks.clear(); this.blocks.clear();
} }

View File

@ -31,10 +31,10 @@ public class PacketCompressor extends ByteToMessageCodec<ByteBuf> {
private final int threshold; private final int threshold;
private byte[] buffer = new byte[8192]; private final byte[] buffer = new byte[8192];
private Deflater deflater = new Deflater(); private final Deflater deflater = new Deflater();
private Inflater inflater = new Inflater(); private final Inflater inflater = new Inflater();
public PacketCompressor(int threshold) { public PacketCompressor(int threshold) {
this.threshold = threshold; this.threshold = threshold;
@ -80,14 +80,15 @@ public class PacketCompressor extends ByteToMessageCodec<ByteBuf> {
// TODO optimize to do not initialize arrays each time // TODO optimize to do not initialize arrays each time
byte[] abyte = new byte[buf.readableBytes()]; byte[] input = new byte[buf.readableBytes()];
buf.readBytes(abyte); buf.readBytes(input);
inflater.setInput(abyte); inflater.setInput(input);
byte[] abyte1 = new byte[i];
inflater.inflate(abyte1); byte[] output = new byte[i];
out.add(Unpooled.wrappedBuffer(abyte1));
inflater.inflate(output);
out.add(Unpooled.wrappedBuffer(output));
inflater.reset(); inflater.reset();
} }