From 63ebb4e4af1ebed8d1f2e0ea3a08b66c4a29d775 Mon Sep 17 00:00:00 2001 From: MiGoYAm Date: Sat, 11 Dec 2021 17:48:48 +0100 Subject: [PATCH] Revert "Optimize imports and delete command declaration packet" This reverts commit 6d2ff1a6b9300d6b84bac6c448ed0f23bc75afb0. --- .../limbo/configuration/LimboConfig.java | 5 +- .../connection/ClientChannelInitializer.java | 2 +- .../limbo/connection/ClientConnection.java | 11 +++- .../connection/pipeline/PacketDecoder.java | 3 +- .../packets/play/PacketDeclareCommands.java | 56 +++++++++++++++++++ .../packets/status/PacketStatusRequest.java | 3 +- .../packets/status/PacketStatusResponse.java | 3 +- .../nanit/limbo/protocol/registry/State.java | 13 ++++- 8 files changed, 79 insertions(+), 17 deletions(-) create mode 100644 src/main/java/ru/nanit/limbo/protocol/packets/play/PacketDeclareCommands.java diff --git a/src/main/java/ru/nanit/limbo/configuration/LimboConfig.java b/src/main/java/ru/nanit/limbo/configuration/LimboConfig.java index 17cef9fd..486ea46c 100644 --- a/src/main/java/ru/nanit/limbo/configuration/LimboConfig.java +++ b/src/main/java/ru/nanit/limbo/configuration/LimboConfig.java @@ -7,10 +7,7 @@ import ru.nanit.limbo.server.data.*; import ru.nanit.limbo.util.Colors; -import java.io.BufferedReader; -import java.io.FileNotFoundException; -import java.io.IOException; -import java.io.InputStream; +import java.io.*; import java.net.SocketAddress; import java.nio.file.Files; import java.nio.file.Path; diff --git a/src/main/java/ru/nanit/limbo/connection/ClientChannelInitializer.java b/src/main/java/ru/nanit/limbo/connection/ClientChannelInitializer.java index 879eca16..827fc999 100644 --- a/src/main/java/ru/nanit/limbo/connection/ClientChannelInitializer.java +++ b/src/main/java/ru/nanit/limbo/connection/ClientChannelInitializer.java @@ -4,9 +4,9 @@ import io.netty.channel.ChannelInitializer; import io.netty.channel.ChannelPipeline; import io.netty.handler.timeout.ReadTimeoutHandler; +import ru.nanit.limbo.connection.pipeline.VarIntFrameDecoder; import ru.nanit.limbo.connection.pipeline.PacketDecoder; import ru.nanit.limbo.connection.pipeline.PacketEncoder; -import ru.nanit.limbo.connection.pipeline.VarIntFrameDecoder; import ru.nanit.limbo.connection.pipeline.VarIntLengthEncoder; import ru.nanit.limbo.server.LimboServer; diff --git a/src/main/java/ru/nanit/limbo/connection/ClientConnection.java b/src/main/java/ru/nanit/limbo/connection/ClientConnection.java index 58e09cec..a7db176c 100644 --- a/src/main/java/ru/nanit/limbo/connection/ClientConnection.java +++ b/src/main/java/ru/nanit/limbo/connection/ClientConnection.java @@ -10,13 +10,13 @@ import io.netty.channel.ChannelHandlerContext; import io.netty.channel.ChannelInboundHandlerAdapter; import ru.nanit.limbo.LimboConstants; -import ru.nanit.limbo.connection.pipeline.PacketDecoder; -import ru.nanit.limbo.connection.pipeline.PacketEncoder; import ru.nanit.limbo.protocol.ByteMessage; import ru.nanit.limbo.protocol.PreEncodedPacket; -import ru.nanit.limbo.protocol.packets.PacketHandshake; import ru.nanit.limbo.protocol.packets.login.*; import ru.nanit.limbo.protocol.packets.play.*; +import ru.nanit.limbo.connection.pipeline.PacketDecoder; +import ru.nanit.limbo.connection.pipeline.PacketEncoder; +import ru.nanit.limbo.protocol.packets.PacketHandshake; import ru.nanit.limbo.protocol.packets.status.PacketStatusPing; import ru.nanit.limbo.protocol.packets.status.PacketStatusRequest; import ru.nanit.limbo.protocol.packets.status.PacketStatusResponse; @@ -33,6 +33,7 @@ import java.net.SocketAddress; import java.security.InvalidKeyException; import java.security.MessageDigest; +import java.util.Collections; import java.util.UUID; import java.util.concurrent.ThreadLocalRandom; @@ -399,11 +400,15 @@ public static void initPackets(LimboServer server) { info.setGameMode(server.getConfig().getGameMode()); info.setUuid(uuid); + PacketDeclareCommands declareCommands = new PacketDeclareCommands(); + declareCommands.setCommands(Collections.singletonList("limbo")); + PACKET_LOGIN_SUCCESS = PreEncodedPacket.of(loginSuccess); PACKET_JOIN_GAME = PreEncodedPacket.of(joinGame); PACKET_PLAYER_ABILITIES = PreEncodedPacket.of(playerAbilities); PACKET_PLAYER_POS = PreEncodedPacket.of(positionAndLook); PACKET_PLAYER_INFO = PreEncodedPacket.of(info); + PACKET_DECLARE_COMMANDS = PreEncodedPacket.of(declareCommands); if (server.getConfig().isUseBrandName()){ PacketPluginMessage pluginMessage = new PacketPluginMessage(); diff --git a/src/main/java/ru/nanit/limbo/connection/pipeline/PacketDecoder.java b/src/main/java/ru/nanit/limbo/connection/pipeline/PacketDecoder.java index 62d499be..e25eff8e 100644 --- a/src/main/java/ru/nanit/limbo/connection/pipeline/PacketDecoder.java +++ b/src/main/java/ru/nanit/limbo/connection/pipeline/PacketDecoder.java @@ -3,8 +3,7 @@ import io.netty.buffer.ByteBuf; import io.netty.channel.ChannelHandlerContext; import io.netty.handler.codec.MessageToMessageDecoder; -import ru.nanit.limbo.protocol.ByteMessage; -import ru.nanit.limbo.protocol.Packet; +import ru.nanit.limbo.protocol.*; import ru.nanit.limbo.protocol.registry.State; import ru.nanit.limbo.protocol.registry.Version; import ru.nanit.limbo.util.Logger; diff --git a/src/main/java/ru/nanit/limbo/protocol/packets/play/PacketDeclareCommands.java b/src/main/java/ru/nanit/limbo/protocol/packets/play/PacketDeclareCommands.java new file mode 100644 index 00000000..c804b51f --- /dev/null +++ b/src/main/java/ru/nanit/limbo/protocol/packets/play/PacketDeclareCommands.java @@ -0,0 +1,56 @@ +package ru.nanit.limbo.protocol.packets.play; + +import ru.nanit.limbo.protocol.ByteMessage; +import ru.nanit.limbo.protocol.PacketOut; +import ru.nanit.limbo.protocol.registry.Version; + +import java.util.List; + +/** + * Packet for 1.13+ + */ +public class PacketDeclareCommands implements PacketOut { + + private List commands; + + public void setCommands(List commands) { + this.commands = commands; + } + + @Override + public void encode(ByteMessage msg, Version version) { + msg.writeVarInt(commands.size() * 2 + 1); // +1 because declaring root node + + // Declare root node + + msg.writeByte(0); + msg.writeVarInt(commands.size()); + + for (int i = 1; i <= commands.size() * 2; i++) { + msg.writeVarInt(i++); + } + + // Declare other commands + + int i = 1; + for (String cmd : commands) { + msg.writeByte(1 | 0x04); + msg.writeVarInt(1); + msg.writeVarInt(i + 1); + msg.writeString(cmd); + i++; + + msg.writeByte(2 | 0x04 | 0x10); + msg.writeVarInt(1); + msg.writeVarInt(i); + msg.writeString("arg"); + msg.writeString("brigadier:string"); + msg.writeVarInt(0); + msg.writeString("minecraft:ask_server"); + i++; + } + + msg.writeVarInt(0); + } + +} diff --git a/src/main/java/ru/nanit/limbo/protocol/packets/status/PacketStatusRequest.java b/src/main/java/ru/nanit/limbo/protocol/packets/status/PacketStatusRequest.java index ff3b92ef..ed18d344 100644 --- a/src/main/java/ru/nanit/limbo/protocol/packets/status/PacketStatusRequest.java +++ b/src/main/java/ru/nanit/limbo/protocol/packets/status/PacketStatusRequest.java @@ -1,7 +1,6 @@ package ru.nanit.limbo.protocol.packets.status; -import ru.nanit.limbo.protocol.ByteMessage; -import ru.nanit.limbo.protocol.PacketIn; +import ru.nanit.limbo.protocol.*; import ru.nanit.limbo.protocol.registry.Version; public class PacketStatusRequest implements PacketIn { diff --git a/src/main/java/ru/nanit/limbo/protocol/packets/status/PacketStatusResponse.java b/src/main/java/ru/nanit/limbo/protocol/packets/status/PacketStatusResponse.java index df081426..bacbfd1a 100644 --- a/src/main/java/ru/nanit/limbo/protocol/packets/status/PacketStatusResponse.java +++ b/src/main/java/ru/nanit/limbo/protocol/packets/status/PacketStatusResponse.java @@ -1,7 +1,6 @@ package ru.nanit.limbo.protocol.packets.status; -import ru.nanit.limbo.protocol.ByteMessage; -import ru.nanit.limbo.protocol.PacketOut; +import ru.nanit.limbo.protocol.*; import ru.nanit.limbo.protocol.registry.Version; import ru.nanit.limbo.server.LimboServer; diff --git a/src/main/java/ru/nanit/limbo/protocol/registry/State.java b/src/main/java/ru/nanit/limbo/protocol/registry/State.java index d071139b..9dbdde3a 100644 --- a/src/main/java/ru/nanit/limbo/protocol/registry/State.java +++ b/src/main/java/ru/nanit/limbo/protocol/registry/State.java @@ -1,18 +1,18 @@ package ru.nanit.limbo.protocol.registry; import ru.nanit.limbo.protocol.Packet; -import ru.nanit.limbo.protocol.packets.PacketHandshake; +import ru.nanit.limbo.protocol.packets.*; import ru.nanit.limbo.protocol.packets.login.*; import ru.nanit.limbo.protocol.packets.play.*; import ru.nanit.limbo.protocol.packets.status.PacketStatusPing; import ru.nanit.limbo.protocol.packets.status.PacketStatusRequest; import ru.nanit.limbo.protocol.packets.status.PacketStatusResponse; +import static ru.nanit.limbo.protocol.registry.Version.*; + import java.util.*; import java.util.function.Supplier; -import static ru.nanit.limbo.protocol.registry.Version.*; - public enum State { HANDSHAKING(0) { @@ -70,6 +70,13 @@ public enum State { map(0x0F, V1_17, V1_18) ); + clientBound.register(PacketDeclareCommands::new, + map(0x11, V1_13, V1_14_4), + map(0x12, V1_15, V1_15_2), + map(0x11, V1_16, V1_16_1), + map(0x10, V1_16_2, V1_16_4), + map(0x12, V1_17, V1_18) + ); clientBound.register(PacketJoinGame::new, map(0x01, V1_8, V1_8), map(0x23, V1_9, V1_12_2),