From 67df108f28ad708b6fd5f6ff0b2edd5993dfe9a7 Mon Sep 17 00:00:00 2001 From: Camotoy <20743703+Camotoy@users.noreply.github.com> Date: Thu, 15 Dec 2022 00:54:01 -0500 Subject: [PATCH] ClientboundCommands: SuggestionType is flexible on a protocol level --- .../mc/protocol/data/MagicValues.java | 7 ---- .../data/game/command/CommandNode.java | 3 +- .../data/game/command/SuggestionType.java | 32 +++++++++++++++++++ .../ClientboundCommandsPacket.java | 14 +++----- .../ClientboundPlayerInfoUpdatePacket.java | 5 ++- .../mc/protocol/data/MagicValuesTest.java | 3 -- .../ClientboundCommandsPacketTest.java | 2 +- 7 files changed, 42 insertions(+), 24 deletions(-) diff --git a/src/main/java/com/github/steveice10/mc/protocol/data/MagicValues.java b/src/main/java/com/github/steveice10/mc/protocol/data/MagicValues.java index acb6d4d7..ce51d7b3 100644 --- a/src/main/java/com/github/steveice10/mc/protocol/data/MagicValues.java +++ b/src/main/java/com/github/steveice10/mc/protocol/data/MagicValues.java @@ -9,13 +9,11 @@ import com.github.steveice10.mc.protocol.data.game.UnlockRecipesAction; import com.github.steveice10.mc.protocol.data.game.advancement.Advancement; import com.github.steveice10.mc.protocol.data.game.command.CommandParser; import com.github.steveice10.mc.protocol.data.game.command.CommandType; -import com.github.steveice10.mc.protocol.data.game.command.SuggestionType; import com.github.steveice10.mc.protocol.data.game.command.properties.StringProperties; import com.github.steveice10.mc.protocol.data.game.entity.EquipmentSlot; import com.github.steveice10.mc.protocol.data.game.entity.RotationOrigin; import com.github.steveice10.mc.protocol.data.game.entity.object.MinecartType; import com.github.steveice10.mc.protocol.data.game.entity.player.Animation; -import com.github.steveice10.mc.protocol.data.game.entity.player.GameMode; import com.github.steveice10.mc.protocol.data.game.entity.player.Hand; import com.github.steveice10.mc.protocol.data.game.entity.player.HandPreference; import com.github.steveice10.mc.protocol.data.game.entity.player.InteractAction; @@ -529,11 +527,6 @@ public class MagicValues { register(CommandParser.TEMPLATE_ROTATION, 46); register(CommandParser.UUID, 47); - register(SuggestionType.ASK_SERVER, "minecraft:ask_server"); - register(SuggestionType.ALL_RECIPES, "minecraft:all_recipes"); - register(SuggestionType.AVAILABLE_SOUNDS, "minecraft:available_sounds"); - register(SuggestionType.SUMMONABLE_ENTITIES, "minecraft:summonable_entities"); - register(StringProperties.SINGLE_WORD, 0); register(StringProperties.QUOTABLE_PHRASE, 1); register(StringProperties.GREEDY_PHRASE, 2); diff --git a/src/main/java/com/github/steveice10/mc/protocol/data/game/command/CommandNode.java b/src/main/java/com/github/steveice10/mc/protocol/data/game/command/CommandNode.java index 180e223c..c30f70a3 100644 --- a/src/main/java/com/github/steveice10/mc/protocol/data/game/command/CommandNode.java +++ b/src/main/java/com/github/steveice10/mc/protocol/data/game/command/CommandNode.java @@ -45,6 +45,7 @@ public class CommandNode { /** * Suggestions type, if present. + * See {@link SuggestionType} for vanilla defaults. */ - private final SuggestionType suggestionType; + private final String suggestionType; } diff --git a/src/main/java/com/github/steveice10/mc/protocol/data/game/command/SuggestionType.java b/src/main/java/com/github/steveice10/mc/protocol/data/game/command/SuggestionType.java index 78860dcf..56921231 100644 --- a/src/main/java/com/github/steveice10/mc/protocol/data/game/command/SuggestionType.java +++ b/src/main/java/com/github/steveice10/mc/protocol/data/game/command/SuggestionType.java @@ -1,8 +1,40 @@ package com.github.steveice10.mc.protocol.data.game.command; +import com.github.steveice10.mc.protocol.data.game.Identifier; +import org.jetbrains.annotations.NotNull; + +import java.util.HashMap; +import java.util.Locale; +import java.util.Map; + public enum SuggestionType { ASK_SERVER, ALL_RECIPES, AVAILABLE_SOUNDS, SUMMONABLE_ENTITIES; + + private final String resourceLocation; + + SuggestionType() { + this.resourceLocation = Identifier.formalize(name().toLowerCase(Locale.ROOT)); + } + + public String getResourceLocation() { + return resourceLocation; + } + + private static final Map VALUES = new HashMap<>(); + + @NotNull + public static SuggestionType byResourceLocation(String resourceLocation) { + // Vanilla behavior as of 1.19.3 + // 1.16.5 still has AVAILABLE_BIOMES and vanilla doesn't care + return VALUES.getOrDefault(resourceLocation, ASK_SERVER); + } + + static { + for (SuggestionType suggestionType : values()) { + VALUES.put(suggestionType.resourceLocation, suggestionType); + } + } } diff --git a/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/ClientboundCommandsPacket.java b/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/ClientboundCommandsPacket.java index 1fc996cb..6ebcc55a 100644 --- a/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/ClientboundCommandsPacket.java +++ b/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/ClientboundCommandsPacket.java @@ -3,11 +3,9 @@ package com.github.steveice10.mc.protocol.packet.ingame.clientbound; import com.github.steveice10.mc.protocol.codec.MinecraftCodecHelper; import com.github.steveice10.mc.protocol.codec.MinecraftPacket; import com.github.steveice10.mc.protocol.data.MagicValues; -import com.github.steveice10.mc.protocol.data.game.Identifier; import com.github.steveice10.mc.protocol.data.game.command.CommandNode; import com.github.steveice10.mc.protocol.data.game.command.CommandParser; import com.github.steveice10.mc.protocol.data.game.command.CommandType; -import com.github.steveice10.mc.protocol.data.game.command.SuggestionType; import com.github.steveice10.mc.protocol.data.game.command.properties.*; import io.netty.buffer.ByteBuf; import lombok.AllArgsConstructor; @@ -15,8 +13,6 @@ import lombok.Data; import lombok.NonNull; import lombok.With; -import java.io.IOException; - @Data @With @AllArgsConstructor @@ -35,7 +31,7 @@ public class ClientboundCommandsPacket implements MinecraftPacket { private final @NonNull CommandNode[] nodes; private final int firstNodeIndex; - public ClientboundCommandsPacket(ByteBuf in, MinecraftCodecHelper helper) throws IOException { + public ClientboundCommandsPacket(ByteBuf in, MinecraftCodecHelper helper) { this.nodes = new CommandNode[helper.readVarInt(in)]; for (int i = 0; i < this.nodes.length; i++) { byte flags = in.readByte(); @@ -145,9 +141,9 @@ public class ClientboundCommandsPacket implements MinecraftPacket { } } - SuggestionType suggestionType = null; + String suggestionType = null; if ((flags & FLAG_SUGGESTION_TYPE) != 0) { - suggestionType = MagicValues.key(SuggestionType.class, Identifier.formalize(helper.readString(in))); + suggestionType = helper.readResourceLocation(in); } this.nodes[i] = new CommandNode(type, executable, children, redirectIndex, name, parser, properties, suggestionType); @@ -157,7 +153,7 @@ public class ClientboundCommandsPacket implements MinecraftPacket { } @Override - public void serialize(ByteBuf out, MinecraftCodecHelper helper) throws IOException { + public void serialize(ByteBuf out, MinecraftCodecHelper helper) { helper.writeVarInt(out, this.nodes.length); for (CommandNode node : this.nodes) { int flags = MagicValues.value(Integer.class, node.getType()) & FLAG_TYPE_MASK; @@ -313,7 +309,7 @@ public class ClientboundCommandsPacket implements MinecraftPacket { } if (node.getSuggestionType() != null) { - helper.writeString(out, MagicValues.value(String.class, node.getSuggestionType())); + helper.writeResourceLocation(out, node.getSuggestionType()); } } diff --git a/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/ClientboundPlayerInfoUpdatePacket.java b/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/ClientboundPlayerInfoUpdatePacket.java index 703657e2..9fb58920 100644 --- a/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/ClientboundPlayerInfoUpdatePacket.java +++ b/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/ClientboundPlayerInfoUpdatePacket.java @@ -57,7 +57,7 @@ public class ClientboundPlayerInfoUpdatePacket implements MinecraftPacket { byte[] keyBytes = helper.readByteArray(in); entry.setKeySignature(helper.readByteArray(in)); - PublicKey publicKey = null; + PublicKey publicKey; try { publicKey = KeyFactory.getInstance("RSA").generatePublic(new X509EncodedKeySpec(keyBytes)); } catch (GeneralSecurityException e) { @@ -69,8 +69,7 @@ public class ClientboundPlayerInfoUpdatePacket implements MinecraftPacket { break; } case UPDATE_GAME_MODE: { - int rawGameMode = helper.readVarInt(in); - GameMode gameMode = GameMode.byId(Math.max(rawGameMode, 0)); + GameMode gameMode = GameMode.byId(helper.readVarInt(in)); entry.setGameMode(gameMode); break; diff --git a/src/test/java/com/github/steveice10/mc/protocol/data/MagicValuesTest.java b/src/test/java/com/github/steveice10/mc/protocol/data/MagicValuesTest.java index b6885ea4..1c3354ab 100644 --- a/src/test/java/com/github/steveice10/mc/protocol/data/MagicValuesTest.java +++ b/src/test/java/com/github/steveice10/mc/protocol/data/MagicValuesTest.java @@ -9,13 +9,11 @@ import com.github.steveice10.mc.protocol.data.game.UnlockRecipesAction; import com.github.steveice10.mc.protocol.data.game.advancement.Advancement; import com.github.steveice10.mc.protocol.data.game.command.CommandParser; import com.github.steveice10.mc.protocol.data.game.command.CommandType; -import com.github.steveice10.mc.protocol.data.game.command.SuggestionType; import com.github.steveice10.mc.protocol.data.game.command.properties.StringProperties; import com.github.steveice10.mc.protocol.data.game.entity.EquipmentSlot; import com.github.steveice10.mc.protocol.data.game.entity.RotationOrigin; import com.github.steveice10.mc.protocol.data.game.entity.object.MinecartType; import com.github.steveice10.mc.protocol.data.game.entity.player.Animation; -import com.github.steveice10.mc.protocol.data.game.entity.player.GameMode; import com.github.steveice10.mc.protocol.data.game.entity.player.Hand; import com.github.steveice10.mc.protocol.data.game.entity.player.HandPreference; import com.github.steveice10.mc.protocol.data.game.entity.player.InteractAction; @@ -152,7 +150,6 @@ public class MagicValuesTest { this.register(RecipeType.class, String.class); this.register(CommandType.class, Integer.class); this.register(CommandParser.class, Integer.class); - this.register(SuggestionType.class, String.class); this.register(StringProperties.class, Integer.class); this.register(PositionSourceType.class, String.class); } diff --git a/src/test/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/ClientboundCommandsPacketTest.java b/src/test/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/ClientboundCommandsPacketTest.java index 1b47f6e9..fccf799b 100644 --- a/src/test/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/ClientboundCommandsPacketTest.java +++ b/src/test/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/ClientboundCommandsPacketTest.java @@ -72,7 +72,7 @@ public class ClientboundCommandsPacketTest extends PacketTest { "Argument4", CommandParser.STRING, StringProperties.SINGLE_WORD, - SuggestionType.ALL_RECIPES + SuggestionType.ALL_RECIPES.getResourceLocation() ) }, 0