Archived
0

Sonar: [squid:S3776] make the method easier

This commit is contained in:
2019-02-11 15:07:56 +03:00
parent b95dd2dfdc
commit 3a25bc34f4

View File

@@ -17,6 +17,7 @@ import java.io.IOException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Comparator; import java.util.Comparator;
import java.util.List; import java.util.List;
import java.util.concurrent.atomic.AtomicBoolean;
/* /*
Packet structure Packet structure
@@ -89,19 +90,13 @@ public class ChunkDataPacket implements SCPacket {
this.sectionList = sectionList; this.sectionList = sectionList;
} }
@Override /**
public void writeSelf(NetOutputStream netStream) { * @return max height chunk
if (sectionList == null && chunk == null) { */
log.warn("Empty chunk data!"); //TODO для такого нужна заглушка private int calcAndWriteBitMask(NetOutputStream netStream) {
return;
}
netStream.writeInt(x); // Chunk X
netStream.writeInt(z); // Chunk Y
netStream.writeBoolean(initChunk); // Init Chunk
int maxH = 0; int maxH = 0;
int bitMask = 0; int bitMask = 0;
if (sectionList == null && chunk != null) { if (sectionList == null && chunk != null) {
for (int h = 15; h >= 0; h--) { for (int h = 15; h >= 0; h--) {
bitMask = bitMask << 1; bitMask = bitMask << 1;
@@ -126,28 +121,27 @@ public class ChunkDataPacket implements SCPacket {
} }
} }
} }
netStream.writeVarInt(bitMask); // Primary Bit Mask netStream.writeVarInt(bitMask); // Primary Bit Mask
try (ByteArrayOutputNetStream data = new ByteArrayOutputNetStream(); return maxH;
ByteArrayOutputNetStream biomes = new ByteArrayOutputNetStream()) { }
boolean biomeWrite = true;
List<CompoundTag> nbtList = new ArrayList<>();
for (int h = 0; h < maxH; h++) {
ChunkSection chunkSection = null;
private ChunkSection getChunkSection(int h) {
if (chunk != null) { if (chunk != null) {
chunkSection = chunk.getChunkSection(h); return chunk.getChunkSection(h);
} else if (sectionList != null) { } else if (sectionList != null) {
chunkSection = sectionList.remove(0); return sectionList.remove(0);
} }
if (chunkSection == null) { return null;
continue;
} }
private PalettedChunkSection createPalette(final ChunkSection chunkSection,
final List<CompoundTag> nbtList,
final AtomicBoolean biomeWrite,
final ByteArrayOutputNetStream biomes) {
final PalettedChunkSection palettedChunkSection = new PalettedChunkSection(); final PalettedChunkSection palettedChunkSection = new PalettedChunkSection();
for (int y = 0; y < 16; y++) { for (int y = 0; y < 16; y++) {
@@ -165,21 +159,51 @@ public class ChunkDataPacket implements SCPacket {
nbtList.add(nbt); nbtList.add(nbt);
} }
if (biomeWrite) { if (biomeWrite.get()) {
biomes.writeByte(chunk.getBiome( biomes.writeByte(chunk.getBiome(
(chunk.getX() << 4) + x, (chunk.getX() << 4) + x,
(chunk.getZ() << 4) + z (chunk.getZ() << 4) + z
).getId()); ).getId());
if (x == 15 && z == 15) { if (x == 15 && z == 15) {
biomeWrite = false; 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()) {
AtomicBoolean biomeWrite = new AtomicBoolean(true);
List<CompoundTag> nbtList = new ArrayList<>();
for (int h = 0; h < maxH; h++) {
final ChunkSection chunkSection = getChunkSection(h);
if (chunkSection == null) {
continue;
}
// <Chunk Section> // <Chunk Section>
palettedChunkSection.writeToNetStream(data); createPalette(chunkSection, nbtList, biomeWrite, biomes)
.writeToNetStream(data);
// </Chunk Section> // </Chunk Section>
} }
// <Biomes> // <Biomes>