Archived
0

Sonar: [squid:S2095] Use try-with-resources

This commit is contained in:
2019-02-11 14:11:24 +03:00
parent f08190fba7
commit d47fb586ca
6 changed files with 73 additions and 49 deletions

View File

@@ -1,6 +1,7 @@
package mc.core.network.proto_1_12_2;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
public class ByteArrayOutputNetStream extends NetOutputStream_p340 {
private ByteArrayOutputStream baos = new ByteArrayOutputStream();
@@ -61,6 +62,11 @@ public class ByteArrayOutputNetStream extends NetOutputStream_p340 {
writeLong(Double.doubleToLongBits(value));
}
@Override
public void close() throws IOException {
baos.close();
}
public int size() {
return baos.size();
}

View File

@@ -13,6 +13,7 @@ import mc.core.world.block.BlockType;
import mc.core.world.chunk.Chunk;
import mc.core.world.chunk.ChunkSection;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
@@ -127,72 +128,75 @@ public class ChunkDataPacket implements SCPacket {
}
netStream.writeVarInt(bitMask); // Primary Bit Mask
final ByteArrayOutputNetStream data = new ByteArrayOutputNetStream();
try (ByteArrayOutputNetStream data = new ByteArrayOutputNetStream();
ByteArrayOutputNetStream biomes = new ByteArrayOutputNetStream()) {
final ByteArrayOutputNetStream biomes = new ByteArrayOutputNetStream();
boolean biomeWrite = true;
boolean biomeWrite = true;
List<CompoundTag> nbtList = new ArrayList<>();
List<CompoundTag> nbtList = new ArrayList<>();
for (int h = 0; h < maxH; h++) {
ChunkSection chunkSection = null;
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);
}
if (chunk != null) {
chunkSection = chunk.getChunkSection(h);
} else if (sectionList != null) {
chunkSection = sectionList.remove(0);
}
if (chunkSection == null) {
continue;
}
if (chunkSection == null) {
continue;
}
final PalettedChunkSection palettedChunkSection = new PalettedChunkSection();
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);
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)
);
palettedChunkSection.addBlock(
block,
chunkSection.getSkyLight(x, y, z)
);
CompoundTag nbt = block.getNBTData();
if (nbt != null) {
nbtList.add(nbt);
}
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;
if (biomeWrite) {
biomes.writeByte(chunk.getBiome(
(chunk.getX() << 4) + x,
(chunk.getZ() << 4) + z
).getId());
if (x == 15 && z == 15) {
biomeWrite = false;
}
}
}
}
}
// <Chunk Section>
palettedChunkSection.writeToNetStream(data);
// </Chunk Section>
}
// <Biomes>
data.writeBytes(biomes.toByteArray());
// </Biomes>
// <Chunk Section>
palettedChunkSection.writeToNetStream(data);
// </Chunk Section>
netStream.writeVarInt(data.size()); // Size of Data
netStream.writeBytes(data.toByteArray()); // Data
netStream.writeVarInt(nbtList.size()); // Number of block entities
// <NBT>
for (CompoundTag compoundTag : nbtList) {
netStream.writeNBT(compoundTag);
}
// </NBT>
} catch (IOException e) {
log.error("", e);
}
// <Biomes>
data.writeBytes(biomes.toByteArray());
// </Biomes>
netStream.writeVarInt(data.size()); // Size of Data
netStream.writeBytes(data.toByteArray()); // Data
netStream.writeVarInt(nbtList.size()); // Number of block entities
// <NBT>
for (CompoundTag compoundTag : nbtList) {
netStream.writeNBT(compoundTag);
}
// </NBT>
}
@Override

View File

@@ -81,4 +81,9 @@ public class ByteArrayInputNetStream extends NetInputStream_p340 {
public void skipBytes(int count) {
throw new UnsupportedOperationException();
}
@Override
public void close() throws IOException {
bais.close();
}
}