From 54fc9f075067d268686d41a085943439b688c62d Mon Sep 17 00:00:00 2001 From: Camotoy <20743703+Camotoy@users.noreply.github.com> Date: Wed, 15 Jun 2022 18:28:40 -0400 Subject: [PATCH] Rename MessageType; use Object2IntMap for Statistics --- .../mc/protocol/test/MinecraftProtocolTest.java | 4 ++-- pom.xml | 6 ++++++ .../mc/protocol/codec/MinecraftCodecHelper.java | 1 - .../game/{MessageType.java => BuiltinChatType.java} | 12 ++++++------ .../clientbound/ClientboundAwardStatsPacket.java | 10 ++++++---- .../clientbound/ClientboundPlayerChatPacket.java | 4 ++-- .../clientbound/ClientboundSystemChatPacket.java | 4 ++-- .../ingame/serverbound/ServerboundChatPacket.java | 8 ++++---- 8 files changed, 28 insertions(+), 21 deletions(-) rename src/main/java/com/github/steveice10/mc/protocol/data/game/{MessageType.java => BuiltinChatType.java} (71%) diff --git a/example/com/github/steveice10/mc/protocol/test/MinecraftProtocolTest.java b/example/com/github/steveice10/mc/protocol/test/MinecraftProtocolTest.java index b423df29..4ed8fa38 100644 --- a/example/com/github/steveice10/mc/protocol/test/MinecraftProtocolTest.java +++ b/example/com/github/steveice10/mc/protocol/test/MinecraftProtocolTest.java @@ -10,7 +10,7 @@ import com.github.steveice10.mc.protocol.MinecraftProtocol; import com.github.steveice10.mc.protocol.ServerLoginHandler; import com.github.steveice10.mc.protocol.codec.MinecraftCodec; import com.github.steveice10.mc.protocol.data.ProtocolState; -import com.github.steveice10.mc.protocol.data.game.MessageType; +import com.github.steveice10.mc.protocol.data.game.BuiltinChatType; import com.github.steveice10.mc.protocol.data.game.entity.player.GameMode; import com.github.steveice10.mc.protocol.data.status.PlayerInfo; import com.github.steveice10.mc.protocol.data.status.ServerStatusInfo; @@ -125,7 +125,7 @@ public class MinecraftProtocolTest { .append(Component.text("!") .color(NamedTextColor.GREEN)); - session.send(new ClientboundSystemChatPacket(msg, MessageType.SYSTEM.ordinal())); + session.send(new ClientboundSystemChatPacket(msg, BuiltinChatType.SYSTEM.ordinal())); } } }); diff --git a/pom.xml b/pom.xml index e27a44ef..5f1b7fc5 100644 --- a/pom.xml +++ b/pom.xml @@ -123,6 +123,12 @@ 1.1.1 compile + + com.nukkitx.fastutil + fastutil-object-int-maps + 8.5.2 + compile + junit junit diff --git a/src/main/java/com/github/steveice10/mc/protocol/codec/MinecraftCodecHelper.java b/src/main/java/com/github/steveice10/mc/protocol/codec/MinecraftCodecHelper.java index ef75e0bd..1d27c934 100644 --- a/src/main/java/com/github/steveice10/mc/protocol/codec/MinecraftCodecHelper.java +++ b/src/main/java/com/github/steveice10/mc/protocol/codec/MinecraftCodecHelper.java @@ -3,7 +3,6 @@ package com.github.steveice10.mc.protocol.codec; import com.github.steveice10.mc.protocol.data.DefaultComponentSerializer; 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.MessageType; import com.github.steveice10.mc.protocol.data.game.chunk.BitStorage; import com.github.steveice10.mc.protocol.data.game.chunk.ChunkSection; import com.github.steveice10.mc.protocol.data.game.chunk.DataPalette; diff --git a/src/main/java/com/github/steveice10/mc/protocol/data/game/MessageType.java b/src/main/java/com/github/steveice10/mc/protocol/data/game/BuiltinChatType.java similarity index 71% rename from src/main/java/com/github/steveice10/mc/protocol/data/game/MessageType.java rename to src/main/java/com/github/steveice10/mc/protocol/data/game/BuiltinChatType.java index 4b105d82..03255914 100644 --- a/src/main/java/com/github/steveice10/mc/protocol/data/game/MessageType.java +++ b/src/main/java/com/github/steveice10/mc/protocol/data/game/BuiltinChatType.java @@ -4,7 +4,7 @@ import java.util.HashMap; import java.util.Locale; import java.util.Map; -public enum MessageType { +public enum BuiltinChatType { CHAT, SYSTEM, GAME_INFO, @@ -16,24 +16,24 @@ public enum MessageType { private final String resourceLocation; - MessageType() { + BuiltinChatType() { this.resourceLocation = "minecraft:" + name().toLowerCase(Locale.ROOT); } public String getResourceLocation() { return resourceLocation; } - private static final Map BY_RESOURCE_LOCATION; + private static final Map BY_RESOURCE_LOCATION; static { - MessageType[] values = values(); + BuiltinChatType[] values = values(); BY_RESOURCE_LOCATION = new HashMap<>(values.length); - for (MessageType type : values) { + for (BuiltinChatType type : values) { BY_RESOURCE_LOCATION.put(type.getResourceLocation(), type); } } - public static MessageType from(String resourceLocation) { + public static BuiltinChatType from(String resourceLocation) { return BY_RESOURCE_LOCATION.get(resourceLocation); } } diff --git a/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/ClientboundAwardStatsPacket.java b/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/ClientboundAwardStatsPacket.java index da53cae4..66bddc53 100644 --- a/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/ClientboundAwardStatsPacket.java +++ b/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/ClientboundAwardStatsPacket.java @@ -5,6 +5,8 @@ import com.github.steveice10.mc.protocol.codec.MinecraftPacket; import com.github.steveice10.mc.protocol.data.game.entity.type.EntityType; import com.github.steveice10.mc.protocol.data.game.statistic.*; import io.netty.buffer.ByteBuf; +import it.unimi.dsi.fastutil.objects.Object2IntMap; +import it.unimi.dsi.fastutil.objects.Object2IntOpenHashMap; import lombok.AllArgsConstructor; import lombok.Data; import lombok.NonNull; @@ -18,9 +20,9 @@ import java.util.Map; @With @AllArgsConstructor public class ClientboundAwardStatsPacket implements MinecraftPacket { - private final @NonNull Map statistics = new HashMap<>(); //TODO Fastutil + private final @NonNull Object2IntMap statistics = new Object2IntOpenHashMap<>(); - public ClientboundAwardStatsPacket(ByteBuf in, MinecraftCodecHelper helper) throws IOException { + public ClientboundAwardStatsPacket(ByteBuf in, MinecraftCodecHelper helper) { int length = helper.readVarInt(in); for (int index = 0; index < length; index++) { StatisticCategory category = helper.readStatisticCategory(in); @@ -64,7 +66,7 @@ public class ClientboundAwardStatsPacket implements MinecraftPacket { @Override public void serialize(ByteBuf out, MinecraftCodecHelper helper) throws IOException { helper.writeVarInt(out, this.statistics.size()); - for (Map.Entry entry : statistics.entrySet()) { + for (Object2IntMap.Entry entry : statistics.object2IntEntrySet()) { Statistic statistic = entry.getKey(); StatisticCategory category; @@ -101,7 +103,7 @@ public class ClientboundAwardStatsPacket implements MinecraftPacket { } helper.writeStatisticCategory(out, category); helper.writeVarInt(out, statisticId); - helper.writeVarInt(out, entry.getValue()); + helper.writeVarInt(out, entry.getIntValue()); } } } diff --git a/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/ClientboundPlayerChatPacket.java b/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/ClientboundPlayerChatPacket.java index 4f1f3f44..05bb2e63 100644 --- a/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/ClientboundPlayerChatPacket.java +++ b/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/ClientboundPlayerChatPacket.java @@ -3,7 +3,7 @@ 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.DefaultComponentSerializer; -import com.github.steveice10.mc.protocol.data.game.MessageType; +import com.github.steveice10.mc.protocol.data.game.BuiltinChatType; import io.netty.buffer.ByteBuf; import lombok.AllArgsConstructor; import lombok.Data; @@ -22,7 +22,7 @@ public class ClientboundPlayerChatPacket implements MinecraftPacket { private final Component signedContent; private final @Nullable Component unsignedContent; /** - * Is {@link MessageType} defined in the order sent by the server in the login packet. + * Is {@link BuiltinChatType} defined in the order sent by the server in the login packet. */ private final int typeId; private final UUID senderUUID; diff --git a/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/ClientboundSystemChatPacket.java b/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/ClientboundSystemChatPacket.java index 94da6306..32fdc70d 100644 --- a/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/ClientboundSystemChatPacket.java +++ b/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/ClientboundSystemChatPacket.java @@ -3,7 +3,7 @@ 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.DefaultComponentSerializer; -import com.github.steveice10.mc.protocol.data.game.MessageType; +import com.github.steveice10.mc.protocol.data.game.BuiltinChatType; import io.netty.buffer.ByteBuf; import lombok.AllArgsConstructor; import lombok.Data; @@ -18,7 +18,7 @@ import java.io.IOException; public class ClientboundSystemChatPacket implements MinecraftPacket { private final Component content; /** - * Is {@link MessageType} defined in the order sent by the server in the login packet. + * Is {@link BuiltinChatType} defined in the order sent by the server in the login packet. */ private final int typeId; diff --git a/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/serverbound/ServerboundChatPacket.java b/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/serverbound/ServerboundChatPacket.java index 36171568..d9780375 100644 --- a/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/serverbound/ServerboundChatPacket.java +++ b/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/serverbound/ServerboundChatPacket.java @@ -5,8 +5,8 @@ import com.github.steveice10.mc.protocol.codec.MinecraftPacket; import io.netty.buffer.ByteBuf; import lombok.AllArgsConstructor; import lombok.Data; -import lombok.NonNull; import lombok.With; +import org.jetbrains.annotations.NotNull; import java.io.IOException; @@ -14,13 +14,13 @@ import java.io.IOException; @With @AllArgsConstructor public class ServerboundChatPacket implements MinecraftPacket { - private final @NonNull String message; + private final @NotNull String message; private final long timeStamp; private final long salt; - private final byte[] signature; + private final byte @NotNull[] signature; private final boolean signedPreview; - public ServerboundChatPacket(ByteBuf in, MinecraftCodecHelper helper) throws IOException { + public ServerboundChatPacket(ByteBuf in, MinecraftCodecHelper helper) { this.message = helper.readString(in); this.timeStamp = in.readLong(); this.salt = in.readLong();