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; package mc.core.network.proto_1_12_2;
import java.io.ByteArrayOutputStream; import java.io.ByteArrayOutputStream;
import java.io.IOException;
public class ByteArrayOutputNetStream extends NetOutputStream_p340 { public class ByteArrayOutputNetStream extends NetOutputStream_p340 {
private ByteArrayOutputStream baos = new ByteArrayOutputStream(); private ByteArrayOutputStream baos = new ByteArrayOutputStream();
@@ -61,6 +62,11 @@ public class ByteArrayOutputNetStream extends NetOutputStream_p340 {
writeLong(Double.doubleToLongBits(value)); writeLong(Double.doubleToLongBits(value));
} }
@Override
public void close() throws IOException {
baos.close();
}
public int size() { public int size() {
return baos.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.Chunk;
import mc.core.world.chunk.ChunkSection; import mc.core.world.chunk.ChunkSection;
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;
@@ -127,72 +128,75 @@ public class ChunkDataPacket implements SCPacket {
} }
netStream.writeVarInt(bitMask); // Primary Bit Mask 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++) { for (int h = 0; h < maxH; h++) {
ChunkSection chunkSection = null; ChunkSection chunkSection = null;
if (chunk != null) { if (chunk != null) {
chunkSection = chunk.getChunkSection(h); chunkSection = chunk.getChunkSection(h);
} else if (sectionList != null) { } else if (sectionList != null) {
chunkSection = sectionList.remove(0); chunkSection = sectionList.remove(0);
} }
if (chunkSection == null) { if (chunkSection == null) {
continue; continue;
} }
final PalettedChunkSection palettedChunkSection = new PalettedChunkSection(); final PalettedChunkSection palettedChunkSection = new PalettedChunkSection();
for (int y = 0; y < 16; y++) { for (int y = 0; y < 16; y++) {
for (int z = 0; z < 16; z++) { for (int z = 0; z < 16; z++) {
for (int x = 0; x < 16; x++) { for (int x = 0; x < 16; x++) {
Block block = chunkSection.getBlock(x, y, z); Block block = chunkSection.getBlock(x, y, z);
palettedChunkSection.addBlock( palettedChunkSection.addBlock(
block, block,
chunkSection.getSkyLight(x, y, z) chunkSection.getSkyLight(x, y, z)
); );
CompoundTag nbt = block.getNBTData(); CompoundTag nbt = block.getNBTData();
if (nbt != null) { if (nbt != null) {
nbtList.add(nbt); nbtList.add(nbt);
} }
if (biomeWrite) { if (biomeWrite) {
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 = false;
}
} }
} }
} }
} }
// <Chunk Section>
palettedChunkSection.writeToNetStream(data);
// </Chunk Section>
} }
// <Biomes>
data.writeBytes(biomes.toByteArray());
// </Biomes>
// <Chunk Section> netStream.writeVarInt(data.size()); // Size of Data
palettedChunkSection.writeToNetStream(data); netStream.writeBytes(data.toByteArray()); // Data
// </Chunk Section> 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 @Override

View File

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

View File

@@ -32,6 +32,9 @@ public class PacketDecoder extends ReplayingDecoder<CSPacket> {
@Override @Override
protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception { protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception {
State state = ctx.channel().attr(ATTR_STATE).get(); State state = ctx.channel().attr(ATTR_STATE).get();
/* FIXME: SONAR посчитал нужным здесь задействовать try-with-resources
* однако для ByteBuf не существует понятия `close`. Необходимо проанализировать необходимость
* использования реализации интерфейса InputStream и если её нет, то убрать. */
NetInputStream netStream = new WrapperNetInputStream(in); NetInputStream netStream = new WrapperNetInputStream(in);
int packetSize = netStream.readVarInt(); int packetSize = netStream.readVarInt();

View File

@@ -27,6 +27,9 @@ public class PacketEncoder extends MessageToByteEncoder<SCPacket> {
log.debug("Send {}:{}", state, packet); log.debug("Send {}:{}", state, packet);
try { try {
/* FIXME: SONAR посчитал нужным здесь задействовать try-with-resources
* однако для ByteBuf не существует понятия `close`. Необходимо проанализировать необходимость
* использования реализации интерфейса OutputStream и если её нет, то убрать. */
NetOutputStream netStream = new WrapperNetOutputStream(out); NetOutputStream netStream = new WrapperNetOutputStream(out);
netStream.writeVarInt(id); netStream.writeVarInt(id);
packet.writeSelf(netStream); packet.writeSelf(netStream);

View File

@@ -32,6 +32,9 @@ public class PacketPostEncoder extends MessageToByteEncoder<ByteBuf> {
} }
out.ensureWritable(sizeOfPktSize + pktSize); out.ensureWritable(sizeOfPktSize + pktSize);
/* FIXME: SONAR посчитал нужным здесь задействовать try-with-resources
* однако для ByteBuf не существует понятия `close`. Необходимо проанализировать необходимость
* использования реализации интерфейса OutputStream и если её нет, то убрать. */
(new WrapperNetOutputStream(out)).writeVarInt(pktSize); (new WrapperNetOutputStream(out)).writeVarInt(pktSize);
out.writeBytes(msg); out.writeBytes(msg);
} }