From 0200788b22c561e264194f7551b4e84c50c532bf Mon Sep 17 00:00:00 2001 From: TheMode Date: Fri, 11 Jun 2021 16:19:17 +0200 Subject: [PATCH] Add dedicated method to ignore string/component size --- .../server/data/SerializableData.java | 2 +- .../server/data/SerializableDataImpl.java | 2 +- .../server/data/type/InventoryData.java | 4 +-- .../minestom/server/data/type/StringData.java | 2 +- .../net/minestom/server/entity/Metadata.java | 6 ++-- .../server/extras/velocity/VelocityProxy.java | 2 +- .../server/handshake/ResponsePacket.java | 2 +- .../server/login/EncryptionRequestPacket.java | 2 +- .../server/login/LoginDisconnectPacket.java | 2 +- .../login/LoginPluginRequestPacket.java | 2 +- .../server/login/LoginSuccessPacket.java | 4 +-- .../server/play/AdvancementsPacket.java | 16 ++++----- .../packet/server/play/BossBarPacket.java | 10 +++--- .../packet/server/play/ChatMessagePacket.java | 2 +- .../server/play/CraftRecipeResponse.java | 2 +- .../server/play/DeathCombatEventPacket.java | 2 +- .../server/play/DeclareCommandsPacket.java | 16 ++++----- .../server/play/DeclareRecipesPacket.java | 18 +++++----- .../packet/server/play/DisconnectPacket.java | 3 +- .../server/play/DisplayScoreboardPacket.java | 2 +- .../server/play/EntityPropertiesPacket.java | 2 +- .../packet/server/play/JoinGamePacket.java | 34 +++++++++---------- .../packet/server/play/MapDataPacket.java | 2 +- .../server/play/NamedSoundEffectPacket.java | 2 +- .../packet/server/play/OpenWindowPacket.java | 2 +- .../packet/server/play/PlayerInfoPacket.java | 26 +++++++------- .../play/PlayerListHeaderAndFooterPacket.java | 4 +-- .../server/play/PluginMessagePacket.java | 4 +-- .../server/play/ResourcePackSendPacket.java | 4 +-- .../packet/server/play/RespawnPacket.java | 2 +- .../play/ScoreboardObjectivePacket.java | 6 ++-- .../play/SelectAdvancementTabPacket.java | 7 ++-- .../packet/server/play/StopSoundPacket.java | 6 ++-- .../packet/server/play/TabCompletePacket.java | 6 ++-- .../packet/server/play/TagsPacket.java | 4 +-- .../packet/server/play/TeamsPacket.java | 26 +++++++------- .../server/play/UnlockRecipesPacket.java | 6 ++-- .../packet/server/play/UpdateScorePacket.java | 6 ++-- .../server/utils/binary/BinaryReader.java | 16 ++++----- 39 files changed, 136 insertions(+), 130 deletions(-) diff --git a/src/main/java/net/minestom/server/data/SerializableData.java b/src/main/java/net/minestom/server/data/SerializableData.java index 4df2dd039..ac38a2282 100644 --- a/src/main/java/net/minestom/server/data/SerializableData.java +++ b/src/main/java/net/minestom/server/data/SerializableData.java @@ -108,7 +108,7 @@ public abstract class SerializableData extends Data { { final int dataIndexSize = binaryReader.readVarInt(); for (int i = 0; i < dataIndexSize; i++) { - final String className = binaryReader.readSizedString(Integer.MAX_VALUE); + final String className = binaryReader.readSizedString(); final short classIndex = binaryReader.readShort(); typeToIndexMap.put(className, classIndex); } diff --git a/src/main/java/net/minestom/server/data/SerializableDataImpl.java b/src/main/java/net/minestom/server/data/SerializableDataImpl.java index 256283764..2f9112eed 100644 --- a/src/main/java/net/minestom/server/data/SerializableDataImpl.java +++ b/src/main/java/net/minestom/server/data/SerializableDataImpl.java @@ -192,7 +192,7 @@ public class SerializableDataImpl extends SerializableData { } // Get the key - final String name = reader.readSizedString(Integer.MAX_VALUE); + final String name = reader.readSizedString(); // Get the data final Object value; diff --git a/src/main/java/net/minestom/server/data/type/InventoryData.java b/src/main/java/net/minestom/server/data/type/InventoryData.java index c5a9a212c..8c3d2447b 100644 --- a/src/main/java/net/minestom/server/data/type/InventoryData.java +++ b/src/main/java/net/minestom/server/data/type/InventoryData.java @@ -28,8 +28,8 @@ public class InventoryData extends DataType { @NotNull @Override public Inventory decode(@NotNull BinaryReader reader) { - final Component title = reader.readComponent(Integer.MAX_VALUE); - final InventoryType inventoryType = InventoryType.valueOf(reader.readSizedString(Integer.MAX_VALUE)); + final Component title = reader.readComponent(); + final InventoryType inventoryType = InventoryType.valueOf(reader.readSizedString()); final int size = inventoryType.getSize(); Inventory inventory = new Inventory(inventoryType, title); diff --git a/src/main/java/net/minestom/server/data/type/StringData.java b/src/main/java/net/minestom/server/data/type/StringData.java index 9b8387543..99d0a3a5f 100644 --- a/src/main/java/net/minestom/server/data/type/StringData.java +++ b/src/main/java/net/minestom/server/data/type/StringData.java @@ -15,6 +15,6 @@ public class StringData extends DataType { @NotNull @Override public String decode(@NotNull BinaryReader reader) { - return reader.readSizedString(Integer.MAX_VALUE); + return reader.readSizedString(); } } diff --git a/src/main/java/net/minestom/server/entity/Metadata.java b/src/main/java/net/minestom/server/entity/Metadata.java index db2d60484..969c6fc0b 100644 --- a/src/main/java/net/minestom/server/entity/Metadata.java +++ b/src/main/java/net/minestom/server/entity/Metadata.java @@ -43,7 +43,7 @@ public class Metadata { } public static Value String(@NotNull String value) { - return new Value<>(TYPE_STRING, value, writer -> writer.writeSizedString(value), reader -> reader.readSizedString(Integer.MAX_VALUE)); + return new Value<>(TYPE_STRING, value, writer -> writer.writeSizedString(value), BinaryReader::readSizedString); } @Deprecated @@ -71,7 +71,7 @@ public class Metadata { } public static Value Chat(@NotNull Component value) { - return new Value<>(TYPE_CHAT, value, writer -> writer.writeSizedString(AdventureSerializer.serialize(value)), reader -> reader.readComponent(Integer.MAX_VALUE)); + return new Value<>(TYPE_CHAT, value, writer -> writer.writeComponent(value), BinaryReader::readComponent); } public static Value OptChat(@Nullable Component value) { @@ -84,7 +84,7 @@ public class Metadata { }, reader -> { boolean present = reader.readBoolean(); if (present) { - return reader.readComponent(Integer.MAX_VALUE); + return reader.readComponent(); } return null; }); diff --git a/src/main/java/net/minestom/server/extras/velocity/VelocityProxy.java b/src/main/java/net/minestom/server/extras/velocity/VelocityProxy.java index 1fa70c0a5..517c45459 100644 --- a/src/main/java/net/minestom/server/extras/velocity/VelocityProxy.java +++ b/src/main/java/net/minestom/server/extras/velocity/VelocityProxy.java @@ -73,7 +73,7 @@ public final class VelocityProxy { } public static InetAddress readAddress(@NotNull BinaryReader reader) { - return InetAddresses.forString(reader.readSizedString(Integer.MAX_VALUE)); + return InetAddresses.forString(reader.readSizedString()); } public static PlayerSkin readSkin(@NotNull BinaryReader reader) { diff --git a/src/main/java/net/minestom/server/network/packet/server/handshake/ResponsePacket.java b/src/main/java/net/minestom/server/network/packet/server/handshake/ResponsePacket.java index 04d6e431c..972ed8423 100644 --- a/src/main/java/net/minestom/server/network/packet/server/handshake/ResponsePacket.java +++ b/src/main/java/net/minestom/server/network/packet/server/handshake/ResponsePacket.java @@ -16,7 +16,7 @@ public class ResponsePacket implements ServerPacket { @Override public void read(@NotNull BinaryReader reader) { - jsonResponse = reader.readSizedString(Integer.MAX_VALUE); + jsonResponse = reader.readSizedString(); } @Override diff --git a/src/main/java/net/minestom/server/network/packet/server/login/EncryptionRequestPacket.java b/src/main/java/net/minestom/server/network/packet/server/login/EncryptionRequestPacket.java index 3be4080af..eafaec561 100644 --- a/src/main/java/net/minestom/server/network/packet/server/login/EncryptionRequestPacket.java +++ b/src/main/java/net/minestom/server/network/packet/server/login/EncryptionRequestPacket.java @@ -39,7 +39,7 @@ public class EncryptionRequestPacket implements ServerPacket { @Override public void read(@NotNull BinaryReader reader) { - reader.readSizedString(Integer.MAX_VALUE); // server id, apparently empty + reader.readSizedString(); // server id, apparently empty publicKey = ByteArrayData.decodeByteArray(reader); nonce = ByteArrayData.decodeByteArray(reader); diff --git a/src/main/java/net/minestom/server/network/packet/server/login/LoginDisconnectPacket.java b/src/main/java/net/minestom/server/network/packet/server/login/LoginDisconnectPacket.java index ef253592c..040031240 100644 --- a/src/main/java/net/minestom/server/network/packet/server/login/LoginDisconnectPacket.java +++ b/src/main/java/net/minestom/server/network/packet/server/login/LoginDisconnectPacket.java @@ -38,7 +38,7 @@ public class LoginDisconnectPacket implements ComponentHoldingServerPacket { @Override public void read(@NotNull BinaryReader reader) { - kickMessage = reader.readComponent(Integer.MAX_VALUE); + kickMessage = reader.readComponent(); } @Override diff --git a/src/main/java/net/minestom/server/network/packet/server/login/LoginPluginRequestPacket.java b/src/main/java/net/minestom/server/network/packet/server/login/LoginPluginRequestPacket.java index 793624e41..5e9d355ed 100644 --- a/src/main/java/net/minestom/server/network/packet/server/login/LoginPluginRequestPacket.java +++ b/src/main/java/net/minestom/server/network/packet/server/login/LoginPluginRequestPacket.java @@ -24,7 +24,7 @@ public class LoginPluginRequestPacket implements ServerPacket { @Override public void read(@NotNull BinaryReader reader) { messageId = reader.readVarInt(); - channel = reader.readSizedString(Integer.MAX_VALUE); + channel = reader.readSizedString(); data = reader.readRemainingBytes(); } diff --git a/src/main/java/net/minestom/server/network/packet/server/login/LoginSuccessPacket.java b/src/main/java/net/minestom/server/network/packet/server/login/LoginSuccessPacket.java index d8db2a82d..501ba1168 100644 --- a/src/main/java/net/minestom/server/network/packet/server/login/LoginSuccessPacket.java +++ b/src/main/java/net/minestom/server/network/packet/server/login/LoginSuccessPacket.java @@ -17,7 +17,7 @@ public class LoginSuccessPacket implements ServerPacket { * DO NOT USE. */ private LoginSuccessPacket() { - this(new UUID(0,0), ""); + this(new UUID(0, 0), ""); } public LoginSuccessPacket(@NotNull UUID uuid, @NotNull String username) { @@ -34,7 +34,7 @@ public class LoginSuccessPacket implements ServerPacket { @Override public void read(@NotNull BinaryReader reader) { uuid = reader.readUuid(); - username = reader.readSizedString(Integer.MAX_VALUE); + username = reader.readSizedString(); } @Override diff --git a/src/main/java/net/minestom/server/network/packet/server/play/AdvancementsPacket.java b/src/main/java/net/minestom/server/network/packet/server/play/AdvancementsPacket.java index b8b3d5d32..1d85fd22a 100644 --- a/src/main/java/net/minestom/server/network/packet/server/play/AdvancementsPacket.java +++ b/src/main/java/net/minestom/server/network/packet/server/play/AdvancementsPacket.java @@ -112,7 +112,7 @@ public class AdvancementsPacket implements ComponentHoldingServerPacket { @Override public void read(@NotNull BinaryReader reader) { - key = reader.readSizedString(Integer.MAX_VALUE); + key = reader.readSizedString(); value = new Advancement(); value.read(reader); } @@ -151,7 +151,7 @@ public class AdvancementsPacket implements ComponentHoldingServerPacket { public void read(@NotNull BinaryReader reader) { boolean hasParent = reader.readBoolean(); if (hasParent) { - parentIdentifier = reader.readSizedString(Integer.MAX_VALUE); + parentIdentifier = reader.readSizedString(); } else { parentIdentifier = null; } @@ -164,7 +164,7 @@ public class AdvancementsPacket implements ComponentHoldingServerPacket { displayData = null; } - criterions = reader.readSizedStringArray(Integer.MAX_VALUE); + criterions = reader.readSizedStringArray(); int requirementCount = reader.readVarInt(); requirements = new Requirement[requirementCount]; @@ -201,13 +201,13 @@ public class AdvancementsPacket implements ComponentHoldingServerPacket { @Override public void read(@NotNull BinaryReader reader) { - title = reader.readComponent(Integer.MAX_VALUE); - description = reader.readComponent(Integer.MAX_VALUE); + title = reader.readComponent(); + description = reader.readComponent(); icon = reader.readItemStack(); frameType = FrameType.values()[reader.readVarInt()]; flags = reader.readInt(); if ((flags & 0x1) != 0) { - backgroundTexture = reader.readSizedString(Integer.MAX_VALUE); + backgroundTexture = reader.readSizedString(); } else { backgroundTexture = null; } @@ -243,7 +243,7 @@ public class AdvancementsPacket implements ComponentHoldingServerPacket { @Override public void read(@NotNull BinaryReader reader) { - key = reader.readSizedString(Integer.MAX_VALUE); + key = reader.readSizedString(); value = new AdvancementProgress(); value.read(reader); } @@ -283,7 +283,7 @@ public class AdvancementsPacket implements ComponentHoldingServerPacket { @Override public void read(@NotNull BinaryReader reader) { - criterionIdentifier = reader.readSizedString(Integer.MAX_VALUE); + criterionIdentifier = reader.readSizedString(); criterionProgress = new CriterionProgress(); criterionProgress.read(reader); } diff --git a/src/main/java/net/minestom/server/network/packet/server/play/BossBarPacket.java b/src/main/java/net/minestom/server/network/packet/server/play/BossBarPacket.java index b711fb29b..13d52a7f5 100644 --- a/src/main/java/net/minestom/server/network/packet/server/play/BossBarPacket.java +++ b/src/main/java/net/minestom/server/network/packet/server/play/BossBarPacket.java @@ -26,7 +26,8 @@ public class BossBarPacket implements ComponentHoldingServerPacket { public BossBar.Overlay overlay = BossBar.Overlay.PROGRESS; public byte flags; - public BossBarPacket() {} + public BossBarPacket() { + } @Override public void write(@NotNull BinaryWriter writer) { @@ -67,7 +68,7 @@ public class BossBarPacket implements ComponentHoldingServerPacket { switch (action) { case ADD: - title = reader.readComponent(Integer.MAX_VALUE); + title = reader.readComponent(); health = reader.readFloat(); color = BossBar.Color.values()[reader.readVarInt()]; overlay = BossBar.Overlay.values()[reader.readVarInt()]; @@ -80,7 +81,7 @@ public class BossBarPacket implements ComponentHoldingServerPacket { health = reader.readFloat(); break; case UPDATE_TITLE: - title = reader.readComponent(Integer.MAX_VALUE); + title = reader.readComponent(); break; case UPDATE_STYLE: color = BossBar.Color.values()[reader.readVarInt()]; @@ -127,7 +128,8 @@ public class BossBarPacket implements ComponentHoldingServerPacket { packet.flags = flags; return packet; } - default: return this; + default: + return this; } } diff --git a/src/main/java/net/minestom/server/network/packet/server/play/ChatMessagePacket.java b/src/main/java/net/minestom/server/network/packet/server/play/ChatMessagePacket.java index 246edb55d..bd9599a41 100644 --- a/src/main/java/net/minestom/server/network/packet/server/play/ChatMessagePacket.java +++ b/src/main/java/net/minestom/server/network/packet/server/play/ChatMessagePacket.java @@ -47,7 +47,7 @@ public class ChatMessagePacket implements ComponentHoldingServerPacket { @Override public void read(@NotNull BinaryReader reader) { - message = reader.readComponent(Integer.MAX_VALUE); + message = reader.readComponent(); position = ChatPosition.fromPacketID(reader.readByte()); uuid = reader.readUuid(); } diff --git a/src/main/java/net/minestom/server/network/packet/server/play/CraftRecipeResponse.java b/src/main/java/net/minestom/server/network/packet/server/play/CraftRecipeResponse.java index 5868f3c85..cb6db367a 100644 --- a/src/main/java/net/minestom/server/network/packet/server/play/CraftRecipeResponse.java +++ b/src/main/java/net/minestom/server/network/packet/server/play/CraftRecipeResponse.java @@ -24,7 +24,7 @@ public class CraftRecipeResponse implements ServerPacket { @Override public void read(@NotNull BinaryReader reader) { windowId = reader.readByte(); - recipe = reader.readSizedString(Integer.MAX_VALUE); + recipe = reader.readSizedString(); } @Override diff --git a/src/main/java/net/minestom/server/network/packet/server/play/DeathCombatEventPacket.java b/src/main/java/net/minestom/server/network/packet/server/play/DeathCombatEventPacket.java index e8ff537d2..038512226 100644 --- a/src/main/java/net/minestom/server/network/packet/server/play/DeathCombatEventPacket.java +++ b/src/main/java/net/minestom/server/network/packet/server/play/DeathCombatEventPacket.java @@ -25,7 +25,7 @@ public class DeathCombatEventPacket implements ServerPacket { public void read(@NotNull BinaryReader reader) { this.playerId = reader.readVarInt(); this.entityId = reader.readInt(); - this.message = reader.readComponent(Integer.MAX_VALUE); + this.message = reader.readComponent(); } @Override diff --git a/src/main/java/net/minestom/server/network/packet/server/play/DeclareCommandsPacket.java b/src/main/java/net/minestom/server/network/packet/server/play/DeclareCommandsPacket.java index 47972539c..c0112f5a0 100644 --- a/src/main/java/net/minestom/server/network/packet/server/play/DeclareCommandsPacket.java +++ b/src/main/java/net/minestom/server/network/packet/server/play/DeclareCommandsPacket.java @@ -83,16 +83,16 @@ public class DeclareCommandsPacket implements ServerPacket { } if (isLiteral() || isArgument()) { - name = reader.readSizedString(Integer.MAX_VALUE); + name = reader.readSizedString(); } - if(isArgument()) { - parser = reader.readSizedString(Integer.MAX_VALUE); + if (isArgument()) { + parser = reader.readSizedString(); properties = getProperties(reader, parser); } if ((flags & 0x10) != 0) { - suggestionsType = reader.readSizedString(Integer.MAX_VALUE); + suggestionsType = reader.readSizedString(); } } @@ -101,10 +101,10 @@ public class DeclareCommandsPacket implements ServerPacket { case "brigadier:double": return reader.extractBytes(() -> { byte flags = reader.readByte(); - if((flags & 0x01) == 0x01) { + if ((flags & 0x01) == 0x01) { reader.readDouble(); // min } - if((flags & 0x02) == 0x02) { + if ((flags & 0x02) == 0x02) { reader.readDouble(); // max } }); @@ -112,10 +112,10 @@ public class DeclareCommandsPacket implements ServerPacket { case "brigadier:integer": return reader.extractBytes(() -> { byte flags = reader.readByte(); - if((flags & 0x01) == 0x01) { + if ((flags & 0x01) == 0x01) { reader.readInt(); // min } - if((flags & 0x02) == 0x02) { + if ((flags & 0x02) == 0x02) { reader.readInt(); // max } }); diff --git a/src/main/java/net/minestom/server/network/packet/server/play/DeclareRecipesPacket.java b/src/main/java/net/minestom/server/network/packet/server/play/DeclareRecipesPacket.java index 3768ad4ff..8e9f3692a 100644 --- a/src/main/java/net/minestom/server/network/packet/server/play/DeclareRecipesPacket.java +++ b/src/main/java/net/minestom/server/network/packet/server/play/DeclareRecipesPacket.java @@ -32,8 +32,8 @@ public class DeclareRecipesPacket implements ServerPacket { int recipeCount = reader.readVarInt(); recipes = new DeclaredRecipe[recipeCount]; for (int i = 0; i < recipeCount; i++) { - String type = reader.readSizedString(Integer.MAX_VALUE); - String id = reader.readSizedString(Integer.MAX_VALUE); + String type = reader.readSizedString(); + String id = reader.readSizedString(); switch (type) { case "crafting_shapeless": @@ -136,7 +136,7 @@ public class DeclareRecipesPacket implements ServerPacket { @Override public void read(@NotNull BinaryReader reader) { - group = reader.readSizedString(Integer.MAX_VALUE); + group = reader.readSizedString(); int count = reader.readVarInt(); ingredients = new Ingredient[count]; for (int i = 0; i < count; i++) { @@ -193,7 +193,7 @@ public class DeclareRecipesPacket implements ServerPacket { public void read(@NotNull BinaryReader reader) { width = reader.readVarInt(); height = reader.readVarInt(); - group = reader.readSizedString(Integer.MAX_VALUE); + group = reader.readSizedString(); ingredients = new Ingredient[width * height]; for (int i = 0; i < width * height; i++) { ingredients[i] = new Ingredient(); @@ -245,7 +245,7 @@ public class DeclareRecipesPacket implements ServerPacket { @Override public void read(@NotNull BinaryReader reader) { - group = reader.readSizedString(Integer.MAX_VALUE); + group = reader.readSizedString(); ingredient = new Ingredient(); ingredient.read(reader); result = reader.readItemStack(); @@ -296,7 +296,7 @@ public class DeclareRecipesPacket implements ServerPacket { @Override public void read(@NotNull BinaryReader reader) { - group = reader.readSizedString(Integer.MAX_VALUE); + group = reader.readSizedString(); ingredient = new Ingredient(); ingredient.read(reader); result = reader.readItemStack(); @@ -347,7 +347,7 @@ public class DeclareRecipesPacket implements ServerPacket { @Override public void read(@NotNull BinaryReader reader) { - group = reader.readSizedString(Integer.MAX_VALUE); + group = reader.readSizedString(); ingredient = new Ingredient(); ingredient.read(reader); result = reader.readItemStack(); @@ -398,7 +398,7 @@ public class DeclareRecipesPacket implements ServerPacket { @Override public void read(@NotNull BinaryReader reader) { - group = reader.readSizedString(Integer.MAX_VALUE); + group = reader.readSizedString(); ingredient = new Ingredient(); ingredient.read(reader); result = reader.readItemStack(); @@ -441,7 +441,7 @@ public class DeclareRecipesPacket implements ServerPacket { @Override public void read(@NotNull BinaryReader reader) { - group = reader.readSizedString(Integer.MAX_VALUE); + group = reader.readSizedString(); ingredient = new Ingredient(); ingredient.read(reader); result = reader.readItemStack(); diff --git a/src/main/java/net/minestom/server/network/packet/server/play/DisconnectPacket.java b/src/main/java/net/minestom/server/network/packet/server/play/DisconnectPacket.java index 6260b1c9d..4f5725b43 100644 --- a/src/main/java/net/minestom/server/network/packet/server/play/DisconnectPacket.java +++ b/src/main/java/net/minestom/server/network/packet/server/play/DisconnectPacket.java @@ -17,6 +17,7 @@ public class DisconnectPacket implements ComponentHoldingServerPacket { /** * Creates a new disconnect packet with a given message. + * * @param message the message */ public DisconnectPacket(@NotNull Component message) { @@ -34,7 +35,7 @@ public class DisconnectPacket implements ComponentHoldingServerPacket { @Override public void read(@NotNull BinaryReader reader) { - message = reader.readComponent(Integer.MAX_VALUE); + message = reader.readComponent(); } @Override diff --git a/src/main/java/net/minestom/server/network/packet/server/play/DisplayScoreboardPacket.java b/src/main/java/net/minestom/server/network/packet/server/play/DisplayScoreboardPacket.java index db95a5885..ed0d36174 100644 --- a/src/main/java/net/minestom/server/network/packet/server/play/DisplayScoreboardPacket.java +++ b/src/main/java/net/minestom/server/network/packet/server/play/DisplayScoreboardPacket.java @@ -24,7 +24,7 @@ public class DisplayScoreboardPacket implements ServerPacket { @Override public void read(@NotNull BinaryReader reader) { position = reader.readByte(); - scoreName = reader.readSizedString(Integer.MAX_VALUE); + scoreName = reader.readSizedString(); } @Override diff --git a/src/main/java/net/minestom/server/network/packet/server/play/EntityPropertiesPacket.java b/src/main/java/net/minestom/server/network/packet/server/play/EntityPropertiesPacket.java index 1ba990437..06c8989f9 100644 --- a/src/main/java/net/minestom/server/network/packet/server/play/EntityPropertiesPacket.java +++ b/src/main/java/net/minestom/server/network/packet/server/play/EntityPropertiesPacket.java @@ -76,7 +76,7 @@ public class EntityPropertiesPacket implements ServerPacket { @Override public void read(@NotNull BinaryReader reader) { - String key = reader.readSizedString(Integer.MAX_VALUE); + String key = reader.readSizedString(); attribute = Attribute.fromKey(key); value = reader.readDouble(); diff --git a/src/main/java/net/minestom/server/network/packet/server/play/JoinGamePacket.java b/src/main/java/net/minestom/server/network/packet/server/play/JoinGamePacket.java index 60763e276..321204703 100644 --- a/src/main/java/net/minestom/server/network/packet/server/play/JoinGamePacket.java +++ b/src/main/java/net/minestom/server/network/packet/server/play/JoinGamePacket.java @@ -40,7 +40,7 @@ public class JoinGamePacket implements ServerPacket { writer.writeBoolean(hardcore); writer.writeByte(gameMode.getId()); - if(previousGameMode == null) { + if (previousGameMode == null) { writer.writeByte(gameMode.getId()); } else { writer.writeByte(previousGameMode.getId()); @@ -80,23 +80,23 @@ public class JoinGamePacket implements ServerPacket { int worldCount = reader.readVarInt(); Check.stateCondition(worldCount != 1, "Only 1 world is supported per JoinGamePacket by Minestom for the moment."); //for (int i = 0; i < worldCount; i++) { - String worldName = reader.readSizedString(Integer.MAX_VALUE); - try { - NBTCompound dimensionCodec = (NBTCompound) reader.readTag(); - dimensionType = DimensionType.fromNBT((NBTCompound) reader.readTag()); + String worldName = reader.readSizedString(); + try { + NBTCompound dimensionCodec = (NBTCompound) reader.readTag(); + dimensionType = DimensionType.fromNBT((NBTCompound) reader.readTag()); - String dimensionName = reader.readSizedString(Integer.MAX_VALUE); - hashedSeed = reader.readLong(); - maxPlayers = reader.readVarInt(); - viewDistance = reader.readVarInt(); - reducedDebugInfo = reader.readBoolean(); - enableRespawnScreen = reader.readBoolean(); - isDebug = reader.readBoolean(); - isFlat = reader.readBoolean(); - } catch (IOException | NBTException e) { - MinecraftServer.getExceptionManager().handleException(e); - // TODO: should we throw as the packet is invalid? - } + String dimensionName = reader.readSizedString(); + hashedSeed = reader.readLong(); + maxPlayers = reader.readVarInt(); + viewDistance = reader.readVarInt(); + reducedDebugInfo = reader.readBoolean(); + enableRespawnScreen = reader.readBoolean(); + isDebug = reader.readBoolean(); + isFlat = reader.readBoolean(); + } catch (IOException | NBTException e) { + MinecraftServer.getExceptionManager().handleException(e); + // TODO: should we throw as the packet is invalid? + } //} } diff --git a/src/main/java/net/minestom/server/network/packet/server/play/MapDataPacket.java b/src/main/java/net/minestom/server/network/packet/server/play/MapDataPacket.java index 3aa3d4390..d14ec40df 100644 --- a/src/main/java/net/minestom/server/network/packet/server/play/MapDataPacket.java +++ b/src/main/java/net/minestom/server/network/packet/server/play/MapDataPacket.java @@ -161,7 +161,7 @@ public class MapDataPacket implements ComponentHoldingServerPacket { boolean hasDisplayName = reader.readBoolean(); if (hasDisplayName) { - displayName = reader.readComponent(Integer.MAX_VALUE); + displayName = reader.readComponent(); } else { displayName = null; } diff --git a/src/main/java/net/minestom/server/network/packet/server/play/NamedSoundEffectPacket.java b/src/main/java/net/minestom/server/network/packet/server/play/NamedSoundEffectPacket.java index 2a4f5ca5a..ec15cd0b3 100644 --- a/src/main/java/net/minestom/server/network/packet/server/play/NamedSoundEffectPacket.java +++ b/src/main/java/net/minestom/server/network/packet/server/play/NamedSoundEffectPacket.java @@ -34,7 +34,7 @@ public class NamedSoundEffectPacket implements ServerPacket { @Override public void read(@NotNull BinaryReader reader) { - soundName = reader.readSizedString(Integer.MAX_VALUE); + soundName = reader.readSizedString(); soundSource = Source.values()[reader.readVarInt()]; x = reader.readInt() / 8; y = reader.readInt() / 8; diff --git a/src/main/java/net/minestom/server/network/packet/server/play/OpenWindowPacket.java b/src/main/java/net/minestom/server/network/packet/server/play/OpenWindowPacket.java index 20c3eb619..f8ca80130 100644 --- a/src/main/java/net/minestom/server/network/packet/server/play/OpenWindowPacket.java +++ b/src/main/java/net/minestom/server/network/packet/server/play/OpenWindowPacket.java @@ -31,7 +31,7 @@ public class OpenWindowPacket implements ServerPacket { public void read(@NotNull BinaryReader reader) { windowId = reader.readVarInt(); windowType = reader.readVarInt(); - title = reader.readComponent(Integer.MAX_VALUE); + title = reader.readComponent(); } @Override diff --git a/src/main/java/net/minestom/server/network/packet/server/play/PlayerInfoPacket.java b/src/main/java/net/minestom/server/network/packet/server/play/PlayerInfoPacket.java index 60f7eab09..5f54c802a 100644 --- a/src/main/java/net/minestom/server/network/packet/server/play/PlayerInfoPacket.java +++ b/src/main/java/net/minestom/server/network/packet/server/play/PlayerInfoPacket.java @@ -68,7 +68,7 @@ public class PlayerInfoPacket implements ComponentHoldingServerPacket { break; default: - throw new IllegalArgumentException("Unsupported action encountered: "+action.name()); + throw new IllegalArgumentException("Unsupported action encountered: " + action.name()); } playerInfos.set(i, info); @@ -92,7 +92,8 @@ public class PlayerInfoPacket implements ComponentHoldingServerPacket { } } return components; - default: return Collections.emptyList(); + default: + return Collections.emptyList(); } } @@ -110,7 +111,8 @@ public class PlayerInfoPacket implements ComponentHoldingServerPacket { playerInfos.add(playerInfo); } } - default: return this; + default: + return this; } } @@ -163,7 +165,7 @@ public class PlayerInfoPacket implements ComponentHoldingServerPacket { AddPlayer(UUID uuid, BinaryReader reader) { super(uuid); - name = reader.readSizedString(Integer.MAX_VALUE); + name = reader.readSizedString(); int propertyCount = reader.readVarInt(); properties = new ArrayList<>(propertyCount); @@ -175,8 +177,8 @@ public class PlayerInfoPacket implements ComponentHoldingServerPacket { ping = reader.readVarInt(); boolean hasDisplayName = reader.readBoolean(); - if(hasDisplayName) { - displayName = reader.readComponent(Integer.MAX_VALUE); + if (hasDisplayName) { + displayName = reader.readComponent(); } else { displayName = null; } @@ -235,12 +237,12 @@ public class PlayerInfoPacket implements ComponentHoldingServerPacket { } Property(BinaryReader reader) { - name = reader.readSizedString(Integer.MAX_VALUE); - value = reader.readSizedString(Integer.MAX_VALUE); + name = reader.readSizedString(); + value = reader.readSizedString(); boolean hasSignature = reader.readBoolean(); - if(hasSignature) { - signature = reader.readSizedString(Integer.MAX_VALUE); + if (hasSignature) { + signature = reader.readSizedString(); } } @@ -308,8 +310,8 @@ public class PlayerInfoPacket implements ComponentHoldingServerPacket { UpdateDisplayName(UUID uuid, BinaryReader reader) { super(uuid); boolean hasDisplayName = reader.readBoolean(); - if(hasDisplayName) { - displayName = reader.readComponent(Integer.MAX_VALUE); + if (hasDisplayName) { + displayName = reader.readComponent(); } else { displayName = null; } diff --git a/src/main/java/net/minestom/server/network/packet/server/play/PlayerListHeaderAndFooterPacket.java b/src/main/java/net/minestom/server/network/packet/server/play/PlayerListHeaderAndFooterPacket.java index 0226ad399..28361a613 100644 --- a/src/main/java/net/minestom/server/network/packet/server/play/PlayerListHeaderAndFooterPacket.java +++ b/src/main/java/net/minestom/server/network/packet/server/play/PlayerListHeaderAndFooterPacket.java @@ -52,8 +52,8 @@ public class PlayerListHeaderAndFooterPacket implements ComponentHoldingServerPa } public void read(@NotNull BinaryReader reader) { - header = reader.readComponent(Integer.MAX_VALUE); - footer = reader.readComponent(Integer.MAX_VALUE); + header = reader.readComponent(); + footer = reader.readComponent(); } @Override diff --git a/src/main/java/net/minestom/server/network/packet/server/play/PluginMessagePacket.java b/src/main/java/net/minestom/server/network/packet/server/play/PluginMessagePacket.java index 96d8de55c..634371142 100644 --- a/src/main/java/net/minestom/server/network/packet/server/play/PluginMessagePacket.java +++ b/src/main/java/net/minestom/server/network/packet/server/play/PluginMessagePacket.java @@ -20,8 +20,8 @@ public class PluginMessagePacket implements ServerPacket { @Override public void read(@NotNull BinaryReader reader) { - channel = reader.readSizedString(Integer.MAX_VALUE); - data = reader.getRemainingBytes(); + channel = reader.readSizedString(); + data = reader.readRemainingBytes(); } @Override diff --git a/src/main/java/net/minestom/server/network/packet/server/play/ResourcePackSendPacket.java b/src/main/java/net/minestom/server/network/packet/server/play/ResourcePackSendPacket.java index 12493c29c..c33d24c4c 100644 --- a/src/main/java/net/minestom/server/network/packet/server/play/ResourcePackSendPacket.java +++ b/src/main/java/net/minestom/server/network/packet/server/play/ResourcePackSendPacket.java @@ -37,8 +37,8 @@ public class ResourcePackSendPacket implements ServerPacket { @Override public void read(@NotNull BinaryReader reader) { - this.url = reader.readSizedString(Integer.MAX_VALUE); - this.hash = reader.readSizedString(Integer.MAX_VALUE); + this.url = reader.readSizedString(); + this.hash = reader.readSizedString(); this.forced = reader.readBoolean(); if (forced) { this.forcedMessage = reader.readComponent(); diff --git a/src/main/java/net/minestom/server/network/packet/server/play/RespawnPacket.java b/src/main/java/net/minestom/server/network/packet/server/play/RespawnPacket.java index ecea46d66..6eb5b2e20 100644 --- a/src/main/java/net/minestom/server/network/packet/server/play/RespawnPacket.java +++ b/src/main/java/net/minestom/server/network/packet/server/play/RespawnPacket.java @@ -47,7 +47,7 @@ public class RespawnPacket implements ServerPacket { dimensionType = DimensionType.fromNBT((NBTCompound) reader.readTag()); // dimension type name - reader.readSizedString(Integer.MAX_VALUE); + reader.readSizedString(); hashedSeed = reader.readLong(); gameMode = GameMode.values()[reader.readByte()]; diff --git a/src/main/java/net/minestom/server/network/packet/server/play/ScoreboardObjectivePacket.java b/src/main/java/net/minestom/server/network/packet/server/play/ScoreboardObjectivePacket.java index 37260ec3a..530b3cb95 100644 --- a/src/main/java/net/minestom/server/network/packet/server/play/ScoreboardObjectivePacket.java +++ b/src/main/java/net/minestom/server/network/packet/server/play/ScoreboardObjectivePacket.java @@ -52,11 +52,11 @@ public class ScoreboardObjectivePacket implements ComponentHoldingServerPacket { @Override public void read(@NotNull BinaryReader reader) { - objectiveName = reader.readSizedString(Integer.MAX_VALUE); + objectiveName = reader.readSizedString(); mode = reader.readByte(); - if(mode == 0 || mode == 2) { - objectiveValue = reader.readComponent(Integer.MAX_VALUE); + if (mode == 0 || mode == 2) { + objectiveValue = reader.readComponent(); type = Type.values()[reader.readVarInt()]; } } diff --git a/src/main/java/net/minestom/server/network/packet/server/play/SelectAdvancementTabPacket.java b/src/main/java/net/minestom/server/network/packet/server/play/SelectAdvancementTabPacket.java index 73159d029..657797fe1 100644 --- a/src/main/java/net/minestom/server/network/packet/server/play/SelectAdvancementTabPacket.java +++ b/src/main/java/net/minestom/server/network/packet/server/play/SelectAdvancementTabPacket.java @@ -12,7 +12,8 @@ public class SelectAdvancementTabPacket implements ServerPacket { @Nullable public String identifier; - public SelectAdvancementTabPacket() {} + public SelectAdvancementTabPacket() { + } @Override public void write(@NotNull BinaryWriter writer) { @@ -27,8 +28,8 @@ public class SelectAdvancementTabPacket implements ServerPacket { @Override public void read(@NotNull BinaryReader reader) { boolean hasID = reader.readBoolean(); - if(hasID) { - identifier = reader.readSizedString(Integer.MAX_VALUE); + if (hasID) { + identifier = reader.readSizedString(); } else { identifier = null; } diff --git a/src/main/java/net/minestom/server/network/packet/server/play/StopSoundPacket.java b/src/main/java/net/minestom/server/network/packet/server/play/StopSoundPacket.java index 721d86542..08de7c0b5 100644 --- a/src/main/java/net/minestom/server/network/packet/server/play/StopSoundPacket.java +++ b/src/main/java/net/minestom/server/network/packet/server/play/StopSoundPacket.java @@ -28,11 +28,11 @@ public class StopSoundPacket implements ServerPacket { @Override public void read(@NotNull BinaryReader reader) { flags = reader.readByte(); - if(flags == 3 || flags == 1) { + if (flags == 3 || flags == 1) { source = reader.readVarInt(); } - if(flags == 2 || flags == 3) { - sound = reader.readSizedString(Integer.MAX_VALUE); + if (flags == 2 || flags == 3) { + sound = reader.readSizedString(); } } diff --git a/src/main/java/net/minestom/server/network/packet/server/play/TabCompletePacket.java b/src/main/java/net/minestom/server/network/packet/server/play/TabCompletePacket.java index a153fd811..f690ae0dc 100644 --- a/src/main/java/net/minestom/server/network/packet/server/play/TabCompletePacket.java +++ b/src/main/java/net/minestom/server/network/packet/server/play/TabCompletePacket.java @@ -50,11 +50,11 @@ public class TabCompletePacket implements ComponentHoldingServerPacket { int matchCount = reader.readVarInt(); matches = new Match[matchCount]; for (int i = 0; i < matchCount; i++) { - String match = reader.readSizedString(Integer.MAX_VALUE); + String match = reader.readSizedString(); boolean hasTooltip = reader.readBoolean(); Component tooltip = null; - if(hasTooltip) { - tooltip = reader.readComponent(Integer.MAX_VALUE); + if (hasTooltip) { + tooltip = reader.readComponent(); } Match newMatch = new Match(); newMatch.match = match; diff --git a/src/main/java/net/minestom/server/network/packet/server/play/TagsPacket.java b/src/main/java/net/minestom/server/network/packet/server/play/TagsPacket.java index d9ae3d1e1..8a0927542 100644 --- a/src/main/java/net/minestom/server/network/packet/server/play/TagsPacket.java +++ b/src/main/java/net/minestom/server/network/packet/server/play/TagsPacket.java @@ -147,14 +147,14 @@ public class TagsPacket implements ServerPacket { final int typeCount = reader.readVarInt(); for (int i = 0; i < typeCount; i++) { // Read tag type - final Tag.BasicTypes tagType = Tag.BasicTypes.fromIdentifer(reader.readSizedString(Integer.MAX_VALUE)); + final Tag.BasicTypes tagType = Tag.BasicTypes.fromIdentifer(reader.readSizedString()); if (tagType == null) { throw new IllegalArgumentException("Tag type could not be resolved"); } final int tagCount = reader.readVarInt(); for (int j = 0; j < tagCount; j++) { - final String tagName = reader.readSizedString(Integer.MAX_VALUE); + final String tagName = reader.readSizedString(); final int[] entries = reader.readVarIntArray(); // TODO convert } diff --git a/src/main/java/net/minestom/server/network/packet/server/play/TeamsPacket.java b/src/main/java/net/minestom/server/network/packet/server/play/TeamsPacket.java index b46d15ab7..915885dd1 100644 --- a/src/main/java/net/minestom/server/network/packet/server/play/TeamsPacket.java +++ b/src/main/java/net/minestom/server/network/packet/server/play/TeamsPacket.java @@ -106,19 +106,19 @@ public class TeamsPacket implements ComponentHoldingServerPacket { @Override public void read(@NotNull BinaryReader reader) { - teamName = reader.readSizedString(Integer.MAX_VALUE); + teamName = reader.readSizedString(); action = Action.values()[reader.readByte()]; switch (action) { case CREATE_TEAM: case UPDATE_TEAM_INFO: - this.teamDisplayName = reader.readComponent(Integer.MAX_VALUE); + this.teamDisplayName = reader.readComponent(); this.friendlyFlags = reader.readByte(); - nameTagVisibility = NameTagVisibility.fromIdentifier(reader.readSizedString(Integer.MAX_VALUE)); - collisionRule = CollisionRule.fromIdentifier(reader.readSizedString(Integer.MAX_VALUE)); + nameTagVisibility = NameTagVisibility.fromIdentifier(reader.readSizedString()); + collisionRule = CollisionRule.fromIdentifier(reader.readSizedString()); this.teamColor = NamedTextColor.ofExact(reader.readVarInt()); - this.teamPrefix = reader.readComponent(Integer.MAX_VALUE); - this.teamSuffix = reader.readComponent(Integer.MAX_VALUE); + this.teamPrefix = reader.readComponent(); + this.teamSuffix = reader.readComponent(); break; case REMOVE_TEAM: @@ -126,7 +126,7 @@ public class TeamsPacket implements ComponentHoldingServerPacket { } if (action == Action.CREATE_TEAM || action == Action.ADD_PLAYERS_TEAM || action == Action.REMOVE_PLAYERS_TEAM) { - entities = reader.readSizedStringArray(Integer.MAX_VALUE); + entities = reader.readSizedStringArray(); } } @@ -232,11 +232,11 @@ public class TeamsPacket implements ComponentHoldingServerPacket { @NotNull public static NameTagVisibility fromIdentifier(String identifier) { - for(NameTagVisibility v : values()) { - if(v.getIdentifier().equals(identifier)) + for (NameTagVisibility v : values()) { + if (v.getIdentifier().equals(identifier)) return v; } - Check.fail("Identifier for NameTagVisibility is invalid: "+identifier); + Check.fail("Identifier for NameTagVisibility is invalid: " + identifier); return null; } @@ -288,11 +288,11 @@ public class TeamsPacket implements ComponentHoldingServerPacket { @NotNull public static CollisionRule fromIdentifier(String identifier) { - for(CollisionRule v : values()) { - if(v.getIdentifier().equals(identifier)) + for (CollisionRule v : values()) { + if (v.getIdentifier().equals(identifier)) return v; } - Check.fail("Identifier for CollisionRule is invalid: "+identifier); + Check.fail("Identifier for CollisionRule is invalid: " + identifier); return null; } diff --git a/src/main/java/net/minestom/server/network/packet/server/play/UnlockRecipesPacket.java b/src/main/java/net/minestom/server/network/packet/server/play/UnlockRecipesPacket.java index f43f3cb8a..4114c4f46 100644 --- a/src/main/java/net/minestom/server/network/packet/server/play/UnlockRecipesPacket.java +++ b/src/main/java/net/minestom/server/network/packet/server/play/UnlockRecipesPacket.java @@ -62,14 +62,14 @@ public class UnlockRecipesPacket implements ServerPacket { int length = reader.readVarInt(); recipesId = new String[length]; for (int i = 0; i < length; i++) { - recipesId[i] = reader.readSizedString(Integer.MAX_VALUE); + recipesId[i] = reader.readSizedString(); } - if(mode == 0) { + if (mode == 0) { int initRecipesLength = reader.readVarInt(); initRecipesId = new String[initRecipesLength]; for (int i = 0; i < length; i++) { - initRecipesId[i] = reader.readSizedString(Integer.MAX_VALUE); + initRecipesId[i] = reader.readSizedString(); } } } diff --git a/src/main/java/net/minestom/server/network/packet/server/play/UpdateScorePacket.java b/src/main/java/net/minestom/server/network/packet/server/play/UpdateScorePacket.java index 7b5c3a619..c56e610a8 100644 --- a/src/main/java/net/minestom/server/network/packet/server/play/UpdateScorePacket.java +++ b/src/main/java/net/minestom/server/network/packet/server/play/UpdateScorePacket.java @@ -33,10 +33,10 @@ public class UpdateScorePacket implements ServerPacket { @Override public void read(@NotNull BinaryReader reader) { - entityName = reader.readSizedString(Integer.MAX_VALUE); + entityName = reader.readSizedString(); action = reader.readByte(); - objectiveName = reader.readSizedString(Integer.MAX_VALUE); - if(action != 1) { + objectiveName = reader.readSizedString(); + if (action != 1) { value = reader.readVarInt(); } } diff --git a/src/main/java/net/minestom/server/utils/binary/BinaryReader.java b/src/main/java/net/minestom/server/utils/binary/BinaryReader.java index 5bee2ceb9..02e87e4bb 100644 --- a/src/main/java/net/minestom/server/utils/binary/BinaryReader.java +++ b/src/main/java/net/minestom/server/utils/binary/BinaryReader.java @@ -117,6 +117,10 @@ public class BinaryReader extends InputStream { return str; } + public String readSizedString() { + return readSizedString(Integer.MAX_VALUE); + } + public byte[] readBytes(int length) { ByteBuf buf = buffer.readBytes(length); byte[] bytes = new byte[buf.readableBytes()]; @@ -134,6 +138,10 @@ public class BinaryReader extends InputStream { return strings; } + public String[] readSizedStringArray() { + return readSizedStringArray(Integer.MAX_VALUE); + } + public int[] readVarIntArray() { final int size = readVarInt(); int[] array = new int[size]; @@ -152,14 +160,6 @@ public class BinaryReader extends InputStream { return array; } - /** - * @deprecated Use {@link #readRemainingBytes()} (same semantics, but more consistent naming) - */ - @Deprecated - public byte[] getRemainingBytes() { - return readRemainingBytes(); - } - public byte[] readRemainingBytes() { return readBytes(available()); }