Skip to content

Commit

Permalink
reading the command tree is now in MineSharp.Commands
Browse files Browse the repository at this point in the history
  • Loading branch information
psu-de committed Nov 29, 2023
1 parent bcb9d10 commit ed29e99
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 31 deletions.
32 changes: 32 additions & 0 deletions Components/MineSharp.Commands/CommandTree.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
using MineSharp.Commands.Parser;
using MineSharp.Core.Common;
using MineSharp.Data;

namespace MineSharp.Commands;

/*
Expand Down Expand Up @@ -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);
}
}
32 changes: 1 addition & 31 deletions MineSharp.Bot/Plugins/ChatPlugin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down

0 comments on commit ed29e99

Please sign in to comment.