From 3a25bc34f47838006653c46f4d51a081878168ad Mon Sep 17 00:00:00 2001 From: DmitriyMX Date: Mon, 11 Feb 2019 15:07:56 +0300 Subject: [PATCH] Sonar: [squid:S3776] make the method easier --- .../packets/serverside/ChunkDataPacket.java | 126 +++++++++++------- 1 file changed, 75 insertions(+), 51 deletions(-) diff --git a/proto_1.12.2/src/main/java/mc/core/network/proto_1_12_2/packets/serverside/ChunkDataPacket.java b/proto_1.12.2/src/main/java/mc/core/network/proto_1_12_2/packets/serverside/ChunkDataPacket.java index 9c694b2..5a2b24d 100644 --- a/proto_1.12.2/src/main/java/mc/core/network/proto_1_12_2/packets/serverside/ChunkDataPacket.java +++ b/proto_1.12.2/src/main/java/mc/core/network/proto_1_12_2/packets/serverside/ChunkDataPacket.java @@ -17,6 +17,7 @@ import java.io.IOException; import java.util.ArrayList; import java.util.Comparator; import java.util.List; +import java.util.concurrent.atomic.AtomicBoolean; /* Packet structure @@ -89,19 +90,13 @@ public class ChunkDataPacket implements SCPacket { this.sectionList = sectionList; } - @Override - public void writeSelf(NetOutputStream netStream) { - if (sectionList == null && chunk == null) { - log.warn("Empty chunk data!"); //TODO для такого нужна заглушка - return; - } - - netStream.writeInt(x); // Chunk X - netStream.writeInt(z); // Chunk Y - netStream.writeBoolean(initChunk); // Init Chunk - + /** + * @return max height chunk + */ + private int calcAndWriteBitMask(NetOutputStream netStream) { int maxH = 0; int bitMask = 0; + if (sectionList == null && chunk != null) { for (int h = 15; h >= 0; h--) { bitMask = bitMask << 1; @@ -126,60 +121,89 @@ public class ChunkDataPacket implements SCPacket { } } } + netStream.writeVarInt(bitMask); // Primary Bit Mask + return maxH; + } + + private ChunkSection getChunkSection(int h) { + if (chunk != null) { + return chunk.getChunkSection(h); + } else if (sectionList != null) { + return sectionList.remove(0); + } + + return null; + } + + private PalettedChunkSection createPalette(final ChunkSection chunkSection, + final List nbtList, + final AtomicBoolean biomeWrite, + final ByteArrayOutputNetStream biomes) { + + final PalettedChunkSection palettedChunkSection = new PalettedChunkSection(); + + for (int y = 0; y < 16; y++) { + for (int z = 0; z < 16; z++) { + for (int x = 0; x < 16; x++) { + Block block = chunkSection.getBlock(x, y, z); + + palettedChunkSection.addBlock( + block, + chunkSection.getSkyLight(x, y, z) + ); + + CompoundTag nbt = block.getNBTData(); + if (nbt != null) { + nbtList.add(nbt); + } + + if (biomeWrite.get()) { + biomes.writeByte(chunk.getBiome( + (chunk.getX() << 4) + x, + (chunk.getZ() << 4) + z + ).getId()); + if (x == 15 && z == 15) { + biomeWrite.set(false); + } + } + } + } + } + + return palettedChunkSection; + } + + @Override + public void writeSelf(NetOutputStream netStream) { + if (sectionList == null && chunk == null) { + log.warn("Empty chunk data!"); //TODO для такого нужна заглушка + return; + } + + netStream.writeInt(x); // Chunk X + netStream.writeInt(z); // Chunk Y + netStream.writeBoolean(initChunk); // Init Chunk + + final int maxH = calcAndWriteBitMask(netStream); + try (ByteArrayOutputNetStream data = new ByteArrayOutputNetStream(); ByteArrayOutputNetStream biomes = new ByteArrayOutputNetStream()) { - boolean biomeWrite = true; + AtomicBoolean biomeWrite = new AtomicBoolean(true); List nbtList = new ArrayList<>(); for (int h = 0; h < maxH; h++) { - ChunkSection chunkSection = null; - - if (chunk != null) { - chunkSection = chunk.getChunkSection(h); - } else if (sectionList != null) { - chunkSection = sectionList.remove(0); - } - + final ChunkSection chunkSection = getChunkSection(h); if (chunkSection == null) { continue; } - final PalettedChunkSection palettedChunkSection = new PalettedChunkSection(); - - for (int y = 0; y < 16; y++) { - for (int z = 0; z < 16; z++) { - for (int x = 0; x < 16; x++) { - Block block = chunkSection.getBlock(x, y, z); - - palettedChunkSection.addBlock( - block, - chunkSection.getSkyLight(x, y, z) - ); - - CompoundTag nbt = block.getNBTData(); - if (nbt != null) { - nbtList.add(nbt); - } - - if (biomeWrite) { - biomes.writeByte(chunk.getBiome( - (chunk.getX() << 4) + x, - (chunk.getZ() << 4) + z - ).getId()); - if (x == 15 && z == 15) { - biomeWrite = false; - } - } - } - } - } - // - palettedChunkSection.writeToNetStream(data); + createPalette(chunkSection, nbtList, biomeWrite, biomes) + .writeToNetStream(data); // } //