From a264fecc844cebf2462d706300f24af66743eba8 Mon Sep 17 00:00:00 2001 From: BuildTools <46540330+willkroboth@users.noreply.github.com> Date: Fri, 21 Jul 2023 11:57:38 -0400 Subject: [PATCH] Address some SonarCloud code smells in new network classes --- .../network/FriendlyByteBuffer.java | 22 +++++++++---------- ...ToServerUpdateRequirementsPacketTests.java | 2 +- .../network/CommandAPIMessengerTests.java | 2 +- .../test/network/FriendlyByteBufferTests.java | 2 +- .../ProtocolVersionTooOldPacketTests.java | 2 +- .../test/network/SetVersionPacketTests.java | 2 +- 6 files changed, 16 insertions(+), 16 deletions(-) diff --git a/commandapi-core/src/main/java/dev/jorel/commandapi/network/FriendlyByteBuffer.java b/commandapi-core/src/main/java/dev/jorel/commandapi/network/FriendlyByteBuffer.java index 6cfc62ca9f..10c32929bf 100644 --- a/commandapi-core/src/main/java/dev/jorel/commandapi/network/FriendlyByteBuffer.java +++ b/commandapi-core/src/main/java/dev/jorel/commandapi/network/FriendlyByteBuffer.java @@ -28,7 +28,7 @@ public class FriendlyByteBuffer { private int readIndex = 0; private int writeIndex = 0; - private final List bytes = new ArrayList<>(); + private final List byteBuffer = new ArrayList<>(); /** * Creates a new {@link FriendlyByteBuffer} with no bytes, ready to be written to. @@ -157,9 +157,9 @@ public boolean isWriteIndexOutOfBounds(int writeIndex) { * {@link IndexOutOfBoundsException} when accessing bytes at the write index. */ private void padBytesWithZerosUntilWriteIndex() { - if(this.writeIndex > this.bytes.size()) { - List paddingZeros = Collections.nCopies(this.writeIndex - this.bytes.size(), (byte) 0); - this.bytes.addAll(paddingZeros); + if(this.writeIndex > this.byteBuffer.size()) { + List paddingZeros = Collections.nCopies(this.writeIndex - this.byteBuffer.size(), (byte) 0); + this.byteBuffer.addAll(paddingZeros); } } @@ -167,7 +167,7 @@ private IllegalStateException unexpectedByteJustRead(String message) { return new IllegalStateException( message + "\n" + "At position " + (this.readIndex - 1) + "\n" + - this.bytes.subList(0, this.readIndex) + " <-- HERE " + this.bytes.subList(this.readIndex, this.writeIndex) + this.byteBuffer.subList(0, this.readIndex) + " <-- HERE " + this.byteBuffer.subList(this.readIndex, this.writeIndex) ); } @@ -239,7 +239,7 @@ public void resetIndices() { public byte[] toByteArray() throws IllegalStateException { this.checkWriteIndexIsInBounds(); - return Bytes.toArray(this.bytes.subList(0, this.writeIndex)); + return Bytes.toArray(this.byteBuffer.subList(0, this.writeIndex)); } /** @@ -252,7 +252,7 @@ public byte[] getRemainingBytes() throws IllegalStateException { this.checkWriteIndexIsInBounds(); this.checkReadIndexIsInBounds(); - return Bytes.toArray(this.bytes.subList(this.readIndex, this.writeIndex)); + return Bytes.toArray(this.byteBuffer.subList(this.readIndex, this.writeIndex)); } /** @@ -282,10 +282,10 @@ public int countReadableBytes() { public void writeByte(byte b) throws IllegalStateException { this.checkWriteIndexIsInBounds(); - if(this.writeIndex == this.bytes.size()) { - this.bytes.add(b); + if(this.writeIndex == this.byteBuffer.size()) { + this.byteBuffer.add(b); } else { - this.bytes.set(this.writeIndex, b); + this.byteBuffer.set(this.writeIndex, b); } this.writeIndex++; @@ -309,7 +309,7 @@ public void writeByte(int i) throws IllegalStateException { */ public byte readByte() throws IllegalStateException{ this.checkReadIndexIsInBounds(); - byte b = this.bytes.get(this.readIndex); + byte b = this.byteBuffer.get(this.readIndex); this.readIndex++; return b; diff --git a/commandapi-platforms/commandapi-bukkit/commandapi-bukkit-test/commandapi-bukkit-test-tests/src/test/java/dev/jorel/commandapi/test/network/ClientToServerUpdateRequirementsPacketTests.java b/commandapi-platforms/commandapi-bukkit/commandapi-bukkit-test/commandapi-bukkit-test-tests/src/test/java/dev/jorel/commandapi/test/network/ClientToServerUpdateRequirementsPacketTests.java index b2d5ccb627..afbdb404c5 100644 --- a/commandapi-platforms/commandapi-bukkit/commandapi-bukkit-test/commandapi-bukkit-test-tests/src/test/java/dev/jorel/commandapi/test/network/ClientToServerUpdateRequirementsPacketTests.java +++ b/commandapi-platforms/commandapi-bukkit/commandapi-bukkit-test/commandapi-bukkit-test-tests/src/test/java/dev/jorel/commandapi/test/network/ClientToServerUpdateRequirementsPacketTests.java @@ -7,7 +7,7 @@ import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; -public class ClientToServerUpdateRequirementsPacketTests extends NetworkTestBase { +class ClientToServerUpdateRequirementsPacketTests extends NetworkTestBase { /********* * Setup * diff --git a/commandapi-platforms/commandapi-bukkit/commandapi-bukkit-test/commandapi-bukkit-test-tests/src/test/java/dev/jorel/commandapi/test/network/CommandAPIMessengerTests.java b/commandapi-platforms/commandapi-bukkit/commandapi-bukkit-test/commandapi-bukkit-test-tests/src/test/java/dev/jorel/commandapi/test/network/CommandAPIMessengerTests.java index 0c9ce12a38..f156890ce7 100644 --- a/commandapi-platforms/commandapi-bukkit/commandapi-bukkit-test/commandapi-bukkit-test-tests/src/test/java/dev/jorel/commandapi/test/network/CommandAPIMessengerTests.java +++ b/commandapi-platforms/commandapi-bukkit/commandapi-bukkit-test/commandapi-bukkit-test-tests/src/test/java/dev/jorel/commandapi/test/network/CommandAPIMessengerTests.java @@ -10,7 +10,7 @@ import static org.junit.jupiter.api.Assertions.*; -public class CommandAPIMessengerTests extends NetworkTestBase { +class CommandAPIMessengerTests extends NetworkTestBase { /********* * Setup * diff --git a/commandapi-platforms/commandapi-bukkit/commandapi-bukkit-test/commandapi-bukkit-test-tests/src/test/java/dev/jorel/commandapi/test/network/FriendlyByteBufferTests.java b/commandapi-platforms/commandapi-bukkit/commandapi-bukkit-test/commandapi-bukkit-test-tests/src/test/java/dev/jorel/commandapi/test/network/FriendlyByteBufferTests.java index b21ed59ef1..16928b08b0 100644 --- a/commandapi-platforms/commandapi-bukkit/commandapi-bukkit-test/commandapi-bukkit-test-tests/src/test/java/dev/jorel/commandapi/test/network/FriendlyByteBufferTests.java +++ b/commandapi-platforms/commandapi-bukkit/commandapi-bukkit-test/commandapi-bukkit-test-tests/src/test/java/dev/jorel/commandapi/test/network/FriendlyByteBufferTests.java @@ -11,7 +11,7 @@ import static org.junit.jupiter.api.Assertions.*; -public class FriendlyByteBufferTests extends TestBase { +class FriendlyByteBufferTests extends TestBase { /********* * Setup * diff --git a/commandapi-platforms/commandapi-bukkit/commandapi-bukkit-test/commandapi-bukkit-test-tests/src/test/java/dev/jorel/commandapi/test/network/ProtocolVersionTooOldPacketTests.java b/commandapi-platforms/commandapi-bukkit/commandapi-bukkit-test/commandapi-bukkit-test-tests/src/test/java/dev/jorel/commandapi/test/network/ProtocolVersionTooOldPacketTests.java index 7f7c571cae..b4a4b451c1 100644 --- a/commandapi-platforms/commandapi-bukkit/commandapi-bukkit-test/commandapi-bukkit-test-tests/src/test/java/dev/jorel/commandapi/test/network/ProtocolVersionTooOldPacketTests.java +++ b/commandapi-platforms/commandapi-bukkit/commandapi-bukkit-test/commandapi-bukkit-test-tests/src/test/java/dev/jorel/commandapi/test/network/ProtocolVersionTooOldPacketTests.java @@ -9,7 +9,7 @@ import static org.junit.jupiter.api.Assertions.*; -public class ProtocolVersionTooOldPacketTests extends NetworkTestBase { +class ProtocolVersionTooOldPacketTests extends NetworkTestBase { /********* * Setup * diff --git a/commandapi-platforms/commandapi-bukkit/commandapi-bukkit-test/commandapi-bukkit-test-tests/src/test/java/dev/jorel/commandapi/test/network/SetVersionPacketTests.java b/commandapi-platforms/commandapi-bukkit/commandapi-bukkit-test/commandapi-bukkit-test-tests/src/test/java/dev/jorel/commandapi/test/network/SetVersionPacketTests.java index dde4ad8c37..0b8e0fe8e9 100644 --- a/commandapi-platforms/commandapi-bukkit/commandapi-bukkit-test/commandapi-bukkit-test-tests/src/test/java/dev/jorel/commandapi/test/network/SetVersionPacketTests.java +++ b/commandapi-platforms/commandapi-bukkit/commandapi-bukkit-test/commandapi-bukkit-test-tests/src/test/java/dev/jorel/commandapi/test/network/SetVersionPacketTests.java @@ -9,7 +9,7 @@ import org.junit.jupiter.api.Test; import static org.junit.jupiter.api.Assertions.*; -public class SetVersionPacketTests extends NetworkTestBase { +class SetVersionPacketTests extends NetworkTestBase { /********* * Setup *