diff --git a/Components/MineSharp.Commands/CommandTree.cs b/Components/MineSharp.Commands/CommandTree.cs index 5973f7cc..b8937ad4 100644 --- a/Components/MineSharp.Commands/CommandTree.cs +++ b/Components/MineSharp.Commands/CommandTree.cs @@ -1,3 +1,7 @@ +using MineSharp.Commands.Parser; +using MineSharp.Core.Common; +using MineSharp.Data; + namespace MineSharp.Commands; /* @@ -28,4 +32,32 @@ public string[] ExtractRootCommands() .Where(name => name != null) .ToArray()!; } + + + public static CommandTree Parse(PacketBuffer buffer, MinecraftData data) + { + int nodeCount = buffer.ReadVarInt(); + var nodes = new CommandNode[nodeCount]; + + for (int i = 0; i < nodes.Length; i++) + { + byte flags = buffer.ReadByte(); + int childCount = buffer.ReadVarInt(); + int[] children = new int[childCount]; + for (int j = 0; j < childCount; ++j) + children[j] = buffer.ReadVarInt(); + + int redirectNode = ((flags & 0x08) == 0x08) ? buffer.ReadVarInt() : -1; + + string? name = ((flags & 0x03) == 1 || (flags & 0x03) == 2) ? buffer.ReadString() : null; + + int parserId = ((flags & 0x03) == 2) ? buffer.ReadVarInt() : -1; + IParser? parser = CommandParserFactory.ReadParser(parserId, data, buffer); + + string? suggestionsType = ((flags & 0x10) == 0x10) ? buffer.ReadString() : null; + nodes[i] = new CommandNode(flags, children, redirectNode, name, parser, suggestionsType); + } + var rootIndex = buffer.ReadVarInt(); + return new CommandTree(rootIndex, nodes); + } } diff --git a/MineSharp.Bot/Plugins/ChatPlugin.cs b/MineSharp.Bot/Plugins/ChatPlugin.cs index d9e3814a..b07b0fa7 100644 --- a/MineSharp.Bot/Plugins/ChatPlugin.cs +++ b/MineSharp.Bot/Plugins/ChatPlugin.cs @@ -391,39 +391,9 @@ private Task SendCommand1_19_3(string command, List<(string, string)> arguments) acknowledgedBitfield)); } - /* - * Thanks to Minecraft-Console-Client - * https://github.com/MCCTeam/Minecraft-Console-Client - * - * This Method uses a lot of code from MinecraftClient/Protocol/Handlers/Packet/s2c/DeclareCommands.cs from MCC. - */ private Task HandleDeclareCommandsPacket(DeclareCommandsPacket packet) { - var buffer = packet.RawBuffer; - int nodeCount = buffer.ReadVarInt(); - var nodes = new CommandNode[nodeCount]; - - for (int i = 0; i < nodes.Length; i++) - { - byte flags = buffer.ReadByte(); - int childCount = buffer.ReadVarInt(); - int[] children = new int[childCount]; - for (int j = 0; j < childCount; ++j) - children[j] = buffer.ReadVarInt(); - - int redirectNode = ((flags & 0x08) == 0x08) ? buffer.ReadVarInt() : -1; - - string? name = ((flags & 0x03) == 1 || (flags & 0x03) == 2) ? buffer.ReadString() : null; - - int parserId = ((flags & 0x03) == 2) ? buffer.ReadVarInt() : -1; - IParser? parser = CommandParserFactory.ReadParser(parserId, this.Bot.Data, buffer); - - string? suggestionsType = ((flags & 0x10) == 0x10) ? buffer.ReadString() : null; - nodes[i] = new CommandNode(flags, children, redirectNode, name, parser, suggestionsType); - } - var rootIndex = buffer.ReadVarInt(); - this._commandTree = new CommandTree(rootIndex, nodes); - + this._commandTree = CommandTree.Parse(packet.RawBuffer, this.Bot.Data); return Task.CompletedTask; }