From f8aefaf129bf97e57b84176f3eb22123bceb7389 Mon Sep 17 00:00:00 2001 From: BruceChen Date: Wed, 26 Oct 2022 08:54:54 +0800 Subject: [PATCH] init --- MinecraftClient/Command.cs | 23 +++ MinecraftClient/Commands/Animation.cs | 47 ++++- MinecraftClient/Commands/Bed.cs | 5 + MinecraftClient/Commands/BlockInfo.cs | 5 + MinecraftClient/Commands/Bots.cs | 5 + MinecraftClient/Commands/ChangeSlot.cs | 5 + MinecraftClient/Commands/Chunk.cs | 5 + MinecraftClient/Commands/CommandSource.cs | 13 ++ MinecraftClient/Commands/Connect.cs | 5 + MinecraftClient/Commands/Debug.cs | 5 + MinecraftClient/Commands/Dig.cs | 5 + MinecraftClient/Commands/DropItem.cs | 5 + MinecraftClient/Commands/Enchant.cs | 5 + MinecraftClient/Commands/Entitycmd.cs | 5 + MinecraftClient/Commands/ExecIf.cs | 5 + MinecraftClient/Commands/ExecMulti.cs | 5 + MinecraftClient/Commands/Exit.cs | 5 + MinecraftClient/Commands/Health.cs | 5 + MinecraftClient/Commands/Inventory.cs | 5 + MinecraftClient/Commands/List.cs | 5 + MinecraftClient/Commands/Log.cs | 5 + MinecraftClient/Commands/Look.cs | 5 + MinecraftClient/Commands/Move.cs | 5 + MinecraftClient/Commands/Reco.cs | 5 + MinecraftClient/Commands/Reload.cs | 5 + MinecraftClient/Commands/Respawn.cs | 5 + MinecraftClient/Commands/Script.cs | 5 + MinecraftClient/Commands/Send.cs | 5 + MinecraftClient/Commands/Set.cs | 5 + MinecraftClient/Commands/SetRnd.cs | 5 + MinecraftClient/Commands/Sneak.cs | 5 + MinecraftClient/Commands/Tps.cs | 5 + MinecraftClient/Commands/UseItem.cs | 5 + MinecraftClient/Commands/Useblock.cs | 5 + MinecraftClient/ConsoleIO.cs | 1 + MinecraftClient/McClient.cs | 166 +++++++++++------- MinecraftClient/MinecraftClient.csproj | 1 + .../Protocol/Handlers/Protocol18.cs | 7 + MinecraftClient/Scripting/ChatBot.cs | 6 + 39 files changed, 353 insertions(+), 66 deletions(-) create mode 100644 MinecraftClient/Commands/CommandSource.cs diff --git a/MinecraftClient/Command.cs b/MinecraftClient/Command.cs index 627542c6..467e0cf8 100644 --- a/MinecraftClient/Command.cs +++ b/MinecraftClient/Command.cs @@ -1,6 +1,9 @@ using System; using System.Collections.Generic; using System.Text; +using Brigadier.NET; +using Microsoft.Extensions.Logging; +using MinecraftClient.Commands; namespace MinecraftClient { @@ -22,6 +25,8 @@ namespace MinecraftClient /// public abstract string CmdDesc { get; } + public abstract void RegisterCommand(McClient handler, CommandDispatcher dispatcher); + /// /// Get the translated version of command description. /// @@ -32,6 +37,24 @@ namespace MinecraftClient return CmdUsage + s + Translations.TryGet(CmdDesc); } + public void LogUsage(Logger.ILogger logger) + { + logger.Info($"{Translations.Get("error.usage")}: {Settings.Config.Main.Advanced.InternalCmdChar.ToChar()}{CmdUsage}"); + } + + + public static int LogExecuteResult(Logger.ILogger logger, bool result) + { + logger.Info(Translations.Get(result ? "general.done" : "general.fail")); + return result ? 1 : 0; + } + + public static int LogExecuteResult(Logger.ILogger logger, int result) + { + logger.Info(Translations.Get(result > 0 ? "general.done" : "general.fail")); + return result; + } + /// /// Usage message, eg: 'name [args]' /// diff --git a/MinecraftClient/Commands/Animation.cs b/MinecraftClient/Commands/Animation.cs index cdfa8ad7..a7caec2c 100644 --- a/MinecraftClient/Commands/Animation.cs +++ b/MinecraftClient/Commands/Animation.cs @@ -1,4 +1,7 @@ -using System.Collections.Generic; +using System; +using System.Collections.Generic; +using Brigadier.NET; +using Brigadier.NET.Builder; namespace MinecraftClient.Commands { @@ -8,6 +11,48 @@ namespace MinecraftClient.Commands public override string CmdUsage { get { return "animation "; } } public override string CmdDesc { get { return "cmd.animation.desc"; } } + public override void RegisterCommand(McClient handler, CommandDispatcher dispatcher) + { + dispatcher.Register(l => + l.Literal("help").Then(l => + l.Literal(CmdName).Executes(c => { + LogUsage(handler.Log); + return 1; + }) + ) + ); + + dispatcher.Register(l => + l.Literal(CmdName).Then(l => + l.Literal("mainhand") + .Executes(c => { + return LogExecuteResult(handler.Log, handler.DoAnimation(0)); + }) + ) + ); + dispatcher.Register(l => + l.Literal(CmdName).Then(l => + l.Literal("0") + .Redirect(dispatcher.GetRoot().GetChild(CmdName).GetChild("mainhand")) + ) + ); + + dispatcher.Register(l => + l.Literal(CmdName).Then(l => + l.Literal("offhand") + .Executes(c => { + return LogExecuteResult(handler.Log, handler.DoAnimation(1)); + }) + ) + ); + dispatcher.Register(l => + l.Literal(CmdName).Then(l => + l.Literal("1") + .Redirect(dispatcher.GetRoot().GetChild(CmdName).GetChild("offhand")) + ) + ); + } + public override string Run(McClient handler, string command, Dictionary? localVars) { if (HasArg(command)) diff --git a/MinecraftClient/Commands/Bed.cs b/MinecraftClient/Commands/Bed.cs index ea2b4b1b..9a77b94a 100644 --- a/MinecraftClient/Commands/Bed.cs +++ b/MinecraftClient/Commands/Bed.cs @@ -3,6 +3,7 @@ using System.Collections.Generic; using System.Globalization; using System.Linq; using System.Threading.Tasks; +using Brigadier.NET; using MinecraftClient.Mapping; namespace MinecraftClient.Commands @@ -13,6 +14,10 @@ namespace MinecraftClient.Commands public override string CmdUsage { get { return "bed leave|sleep |sleep "; } } public override string CmdDesc { get { return "cmd.bed.desc"; } } + public override void RegisterCommand(McClient handler, CommandDispatcher dispatcher) + { + } + public override string Run(McClient handler, string command, Dictionary? localVars) { string[] args = GetArgs(command); diff --git a/MinecraftClient/Commands/BlockInfo.cs b/MinecraftClient/Commands/BlockInfo.cs index 5aa8ec75..51051c1d 100644 --- a/MinecraftClient/Commands/BlockInfo.cs +++ b/MinecraftClient/Commands/BlockInfo.cs @@ -1,5 +1,6 @@ using System.Collections.Generic; using System.Text; +using Brigadier.NET; using MinecraftClient.Mapping; namespace MinecraftClient.Commands @@ -10,6 +11,10 @@ namespace MinecraftClient.Commands public override string CmdUsage { get { return "blockinfo [-s]"; } } public override string CmdDesc { get { return "cmd.blockinfo.desc"; } } + public override void RegisterCommand(McClient handler, CommandDispatcher dispatcher) + { + } + public override string Run(McClient handler, string command, Dictionary? localVars) { if (!handler.GetTerrainEnabled()) diff --git a/MinecraftClient/Commands/Bots.cs b/MinecraftClient/Commands/Bots.cs index 46864dd1..b8b23848 100644 --- a/MinecraftClient/Commands/Bots.cs +++ b/MinecraftClient/Commands/Bots.cs @@ -1,6 +1,7 @@ using System; using System.Collections.Generic; using System.Text; +using Brigadier.NET; namespace MinecraftClient.Commands { @@ -10,6 +11,10 @@ namespace MinecraftClient.Commands public override string CmdUsage { get { return "bots [list|unload ]"; } } public override string CmdDesc { get { return "cmd.bots.desc"; } } + public override void RegisterCommand(McClient handler, CommandDispatcher dispatcher) + { + } + public override string Run(McClient handler, string command, Dictionary? localVars) { if (HasArg(command)) diff --git a/MinecraftClient/Commands/ChangeSlot.cs b/MinecraftClient/Commands/ChangeSlot.cs index f278cac7..5280cb67 100644 --- a/MinecraftClient/Commands/ChangeSlot.cs +++ b/MinecraftClient/Commands/ChangeSlot.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using Brigadier.NET; namespace MinecraftClient.Commands { @@ -9,6 +10,10 @@ namespace MinecraftClient.Commands public override string CmdUsage { get { return "changeslot <1-9>"; } } public override string CmdDesc { get { return "cmd.changeSlot.desc"; } } + public override void RegisterCommand(McClient handler, CommandDispatcher dispatcher) + { + } + public override string Run(McClient handler, string command, Dictionary? localVars) { if (!handler.GetInventoryEnabled()) diff --git a/MinecraftClient/Commands/Chunk.cs b/MinecraftClient/Commands/Chunk.cs index 4007d4e2..c6116928 100644 --- a/MinecraftClient/Commands/Chunk.cs +++ b/MinecraftClient/Commands/Chunk.cs @@ -2,6 +2,7 @@ using System.Collections.Generic; using System.Globalization; using System.Text; +using Brigadier.NET; using MinecraftClient.Mapping; namespace MinecraftClient.Commands @@ -12,6 +13,10 @@ namespace MinecraftClient.Commands public override string CmdUsage { get { return "chunk status [chunkX chunkZ|locationX locationY locationZ]"; } } public override string CmdDesc { get { return "cmd.chunk.desc"; } } + public override void RegisterCommand(McClient handler, CommandDispatcher dispatcher) + { + } + public override string Run(McClient handler, string command, Dictionary? localVars) { if (HasArg(command)) diff --git a/MinecraftClient/Commands/CommandSource.cs b/MinecraftClient/Commands/CommandSource.cs new file mode 100644 index 00000000..89f1f1ff --- /dev/null +++ b/MinecraftClient/Commands/CommandSource.cs @@ -0,0 +1,13 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace MinecraftClient.Commands +{ + public class CommandSource + { + public string UserName { get; set; } = string.Empty; + } +} diff --git a/MinecraftClient/Commands/Connect.cs b/MinecraftClient/Commands/Connect.cs index 12c94a3f..a58fba48 100644 --- a/MinecraftClient/Commands/Connect.cs +++ b/MinecraftClient/Commands/Connect.cs @@ -1,4 +1,5 @@ using System.Collections.Generic; +using Brigadier.NET; namespace MinecraftClient.Commands { @@ -8,6 +9,10 @@ namespace MinecraftClient.Commands public override string CmdUsage { get { return "connect [account]"; } } public override string CmdDesc { get { return "cmd.connect.desc"; } } + public override void RegisterCommand(McClient handler, CommandDispatcher dispatcher) + { + } + public override string Run(McClient? handler, string command, Dictionary? localVars) { if (HasArg(command)) diff --git a/MinecraftClient/Commands/Debug.cs b/MinecraftClient/Commands/Debug.cs index c2bbda8a..91d925ff 100644 --- a/MinecraftClient/Commands/Debug.cs +++ b/MinecraftClient/Commands/Debug.cs @@ -1,4 +1,5 @@ using System.Collections.Generic; +using Brigadier.NET; namespace MinecraftClient.Commands { @@ -8,6 +9,10 @@ namespace MinecraftClient.Commands public override string CmdUsage { get { return "debug [on|off]"; } } public override string CmdDesc { get { return "cmd.debug.desc"; } } + public override void RegisterCommand(McClient handler, CommandDispatcher dispatcher) + { + } + public override string Run(McClient handler, string command, Dictionary? localVars) { if (HasArg(command)) diff --git a/MinecraftClient/Commands/Dig.cs b/MinecraftClient/Commands/Dig.cs index 59b9d79d..1fc408a5 100644 --- a/MinecraftClient/Commands/Dig.cs +++ b/MinecraftClient/Commands/Dig.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using Brigadier.NET; using MinecraftClient.Mapping; namespace MinecraftClient.Commands @@ -10,6 +11,10 @@ namespace MinecraftClient.Commands public override string CmdUsage { get { return "dig "; } } public override string CmdDesc { get { return "cmd.dig.desc"; } } + public override void RegisterCommand(McClient handler, CommandDispatcher dispatcher) + { + } + public override string Run(McClient handler, string command, Dictionary? localVars) { if (!handler.GetTerrainEnabled()) diff --git a/MinecraftClient/Commands/DropItem.cs b/MinecraftClient/Commands/DropItem.cs index 54f93254..430ff675 100644 --- a/MinecraftClient/Commands/DropItem.cs +++ b/MinecraftClient/Commands/DropItem.cs @@ -1,6 +1,7 @@ using System; using System.Collections.Generic; using System.Linq; +using Brigadier.NET; using MinecraftClient.Inventory; namespace MinecraftClient.Commands @@ -13,6 +14,10 @@ namespace MinecraftClient.Commands public override string CmdUsage { get { return "/dropitem "; } } + public override void RegisterCommand(McClient handler, CommandDispatcher dispatcher) + { + } + public override string Run(McClient handler, string command, Dictionary? localVars) { if (!handler.GetInventoryEnabled()) diff --git a/MinecraftClient/Commands/Enchant.cs b/MinecraftClient/Commands/Enchant.cs index a75317e2..376c31c5 100644 --- a/MinecraftClient/Commands/Enchant.cs +++ b/MinecraftClient/Commands/Enchant.cs @@ -1,5 +1,6 @@ using System.Collections.Generic; using System.Linq; +using Brigadier.NET; using MinecraftClient.Inventory; namespace MinecraftClient.Commands @@ -10,6 +11,10 @@ namespace MinecraftClient.Commands public override string CmdUsage { get { return "enchant "; } } public override string CmdDesc { get { return "cmd.enchant.desc"; } } + public override void RegisterCommand(McClient handler, CommandDispatcher dispatcher) + { + } + public override string Run(McClient handler, string command, Dictionary? localVars) { if (!handler.GetInventoryEnabled()) diff --git a/MinecraftClient/Commands/Entitycmd.cs b/MinecraftClient/Commands/Entitycmd.cs index 64bfa6c1..229b7fe7 100644 --- a/MinecraftClient/Commands/Entitycmd.cs +++ b/MinecraftClient/Commands/Entitycmd.cs @@ -2,6 +2,7 @@ using System; using System.Collections.Generic; using System.Globalization; using System.Text; +using Brigadier.NET; using MinecraftClient.Inventory; using MinecraftClient.Mapping; @@ -13,6 +14,10 @@ namespace MinecraftClient.Commands public override string CmdUsage { get { return "entity "; } } public override string CmdDesc { get { return ""; } } + public override void RegisterCommand(McClient handler, CommandDispatcher dispatcher) + { + } + public override string Run(McClient handler, string command, Dictionary? localVars) { if (handler.GetEntityHandlingEnabled()) diff --git a/MinecraftClient/Commands/ExecIf.cs b/MinecraftClient/Commands/ExecIf.cs index 06b8619f..b05acceb 100644 --- a/MinecraftClient/Commands/ExecIf.cs +++ b/MinecraftClient/Commands/ExecIf.cs @@ -1,6 +1,7 @@ using System; using System.Collections.Generic; using System.Linq; +using Brigadier.NET; using DynamicExpresso; namespace MinecraftClient.Commands @@ -11,6 +12,10 @@ namespace MinecraftClient.Commands public override string CmdUsage { get { return "execif ---> "; } } public override string CmdDesc { get { return "cmd.execif.desc"; } } + public override void RegisterCommand(McClient handler, CommandDispatcher dispatcher) + { + } + public override string Run(McClient handler, string command, Dictionary? localVars) { if (HasArg(command)) diff --git a/MinecraftClient/Commands/ExecMulti.cs b/MinecraftClient/Commands/ExecMulti.cs index a329da10..40804550 100644 --- a/MinecraftClient/Commands/ExecMulti.cs +++ b/MinecraftClient/Commands/ExecMulti.cs @@ -1,6 +1,7 @@ using System; using System.Collections.Generic; using System.Linq; +using Brigadier.NET; namespace MinecraftClient.Commands { @@ -10,6 +11,10 @@ namespace MinecraftClient.Commands public override string CmdUsage { get { return "execmulti -> -> -> ..."; } } public override string CmdDesc { get { return "cmd.execmulti.desc"; } } + public override void RegisterCommand(McClient handler, CommandDispatcher dispatcher) + { + } + public override string Run(McClient handler, string command, Dictionary? localVars) { if (HasArg(command)) diff --git a/MinecraftClient/Commands/Exit.cs b/MinecraftClient/Commands/Exit.cs index 7bca6496..f189e604 100644 --- a/MinecraftClient/Commands/Exit.cs +++ b/MinecraftClient/Commands/Exit.cs @@ -1,4 +1,5 @@ using System.Collections.Generic; +using Brigadier.NET; namespace MinecraftClient.Commands { @@ -8,6 +9,10 @@ namespace MinecraftClient.Commands public override string CmdUsage { get { return "exit"; } } public override string CmdDesc { get { return "cmd.exit.desc"; } } + public override void RegisterCommand(McClient handler, CommandDispatcher dispatcher) + { + } + public override string Run(McClient? handler, string command, Dictionary? localVars) { Program.Exit(); diff --git a/MinecraftClient/Commands/Health.cs b/MinecraftClient/Commands/Health.cs index 68af9b32..01db69f0 100644 --- a/MinecraftClient/Commands/Health.cs +++ b/MinecraftClient/Commands/Health.cs @@ -1,4 +1,5 @@ using System.Collections.Generic; +using Brigadier.NET; namespace MinecraftClient.Commands { @@ -8,6 +9,10 @@ namespace MinecraftClient.Commands public override string CmdUsage { get { return "health"; } } public override string CmdDesc { get { return "cmd.health.desc"; } } + public override void RegisterCommand(McClient handler, CommandDispatcher dispatcher) + { + } + public override string Run(McClient handler, string command, Dictionary? localVars) { return Translations.Get("cmd.health.response", handler.GetHealth(), handler.GetSaturation(), handler.GetLevel(), handler.GetTotalExperience()); diff --git a/MinecraftClient/Commands/Inventory.cs b/MinecraftClient/Commands/Inventory.cs index f5f1a6c2..6f8877eb 100644 --- a/MinecraftClient/Commands/Inventory.cs +++ b/MinecraftClient/Commands/Inventory.cs @@ -3,6 +3,7 @@ using System.Collections.Generic; using System.Globalization; using System.Linq; using System.Text; +using Brigadier.NET; using MinecraftClient.Inventory; namespace MinecraftClient.Commands @@ -13,6 +14,10 @@ namespace MinecraftClient.Commands public override string CmdUsage { get { return GetBasicUsage(); } } public override string CmdDesc { get { return "cmd.inventory.desc"; } } + public override void RegisterCommand(McClient handler, CommandDispatcher dispatcher) + { + } + public override string Run(McClient handler, string command, Dictionary? localVars) { if (handler.GetInventoryEnabled()) diff --git a/MinecraftClient/Commands/List.cs b/MinecraftClient/Commands/List.cs index f261d670..e833e8a9 100644 --- a/MinecraftClient/Commands/List.cs +++ b/MinecraftClient/Commands/List.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using Brigadier.NET; namespace MinecraftClient.Commands { @@ -9,6 +10,10 @@ namespace MinecraftClient.Commands public override string CmdUsage { get { return "list"; } } public override string CmdDesc { get { return "cmd.list.desc"; } } + public override void RegisterCommand(McClient handler, CommandDispatcher dispatcher) + { + } + public override string Run(McClient handler, string command, Dictionary? localVars) { return Translations.Get("cmd.list.players", String.Join(", ", handler.GetOnlinePlayers())); diff --git a/MinecraftClient/Commands/Log.cs b/MinecraftClient/Commands/Log.cs index 412e401b..896e560a 100644 --- a/MinecraftClient/Commands/Log.cs +++ b/MinecraftClient/Commands/Log.cs @@ -1,4 +1,5 @@ using System.Collections.Generic; +using Brigadier.NET; namespace MinecraftClient.Commands { @@ -8,6 +9,10 @@ namespace MinecraftClient.Commands public override string CmdUsage { get { return "log "; } } public override string CmdDesc { get { return "cmd.log.desc"; } } + public override void RegisterCommand(McClient handler, CommandDispatcher dispatcher) + { + } + public override string Run(McClient handler, string command, Dictionary? localVars) { if (HasArg(command)) diff --git a/MinecraftClient/Commands/Look.cs b/MinecraftClient/Commands/Look.cs index 11065f62..0dcc7642 100644 --- a/MinecraftClient/Commands/Look.cs +++ b/MinecraftClient/Commands/Look.cs @@ -1,6 +1,7 @@ using System; using System.Collections.Generic; using System.Globalization; +using Brigadier.NET; using MinecraftClient.Mapping; namespace MinecraftClient.Commands @@ -11,6 +12,10 @@ namespace MinecraftClient.Commands public override string CmdUsage { get { return "look "; } } public override string CmdDesc { get { return "cmd.look.desc"; } } + public override void RegisterCommand(McClient handler, CommandDispatcher dispatcher) + { + } + public override string Run(McClient handler, string command, Dictionary? localVars) { if (handler.GetTerrainEnabled()) diff --git a/MinecraftClient/Commands/Move.cs b/MinecraftClient/Commands/Move.cs index bbebcded..026dac82 100644 --- a/MinecraftClient/Commands/Move.cs +++ b/MinecraftClient/Commands/Move.cs @@ -1,6 +1,7 @@ using System; using System.Collections.Generic; using System.Linq; +using Brigadier.NET; using MinecraftClient.Mapping; namespace MinecraftClient.Commands @@ -11,6 +12,10 @@ namespace MinecraftClient.Commands public override string CmdUsage { get { return "move [-f]"; } } public override string CmdDesc { get { return "walk or start walking. \"-f\": force unsafe movements like falling or touching fire"; } } + public override void RegisterCommand(McClient handler, CommandDispatcher dispatcher) + { + } + public override string Run(McClient handler, string command, Dictionary? localVars) { List args = GetArgs(command.ToLower()).ToList(); diff --git a/MinecraftClient/Commands/Reco.cs b/MinecraftClient/Commands/Reco.cs index 5b5b0e0f..206280e3 100644 --- a/MinecraftClient/Commands/Reco.cs +++ b/MinecraftClient/Commands/Reco.cs @@ -1,4 +1,5 @@ using System.Collections.Generic; +using Brigadier.NET; namespace MinecraftClient.Commands { @@ -8,6 +9,10 @@ namespace MinecraftClient.Commands public override string CmdUsage { get { return "reco [account]"; } } public override string CmdDesc { get { return "cmd.reco.desc"; } } + public override void RegisterCommand(McClient handler, CommandDispatcher dispatcher) + { + } + public override string Run(McClient? handler, string command, Dictionary? localVars) { string[] args = GetArgs(command); diff --git a/MinecraftClient/Commands/Reload.cs b/MinecraftClient/Commands/Reload.cs index f4063db7..52bd1ab2 100644 --- a/MinecraftClient/Commands/Reload.cs +++ b/MinecraftClient/Commands/Reload.cs @@ -1,4 +1,5 @@ using System.Collections.Generic; +using Brigadier.NET; namespace MinecraftClient.Commands { @@ -8,6 +9,10 @@ namespace MinecraftClient.Commands public override string CmdUsage { get { return "reload"; } } public override string CmdDesc { get { return "cmd.reload.desc"; } } + public override void RegisterCommand(McClient handler, CommandDispatcher dispatcher) + { + } + public override string Run(McClient handler, string command, Dictionary? localVars) { handler.Log.Info(Translations.TryGet("cmd.reload.started")); diff --git a/MinecraftClient/Commands/Respawn.cs b/MinecraftClient/Commands/Respawn.cs index 3ff718fd..fd39bc0a 100644 --- a/MinecraftClient/Commands/Respawn.cs +++ b/MinecraftClient/Commands/Respawn.cs @@ -1,4 +1,5 @@ using System.Collections.Generic; +using Brigadier.NET; namespace MinecraftClient.Commands { @@ -8,6 +9,10 @@ namespace MinecraftClient.Commands public override string CmdUsage { get { return "respawn"; } } public override string CmdDesc { get { return "cmd.respawn.desc"; } } + public override void RegisterCommand(McClient handler, CommandDispatcher dispatcher) + { + } + public override string Run(McClient handler, string command, Dictionary? localVars) { handler.SendRespawnPacket(); diff --git a/MinecraftClient/Commands/Script.cs b/MinecraftClient/Commands/Script.cs index e7e233c5..d345b2e2 100644 --- a/MinecraftClient/Commands/Script.cs +++ b/MinecraftClient/Commands/Script.cs @@ -1,4 +1,5 @@ using System.Collections.Generic; +using Brigadier.NET; namespace MinecraftClient.Commands { @@ -8,6 +9,10 @@ namespace MinecraftClient.Commands public override string CmdUsage { get { return "script "; } } public override string CmdDesc { get { return "cmd.script.desc"; } } + public override void RegisterCommand(McClient handler, CommandDispatcher dispatcher) + { + } + public override string Run(McClient handler, string command, Dictionary? localVars) { if (HasArg(command)) diff --git a/MinecraftClient/Commands/Send.cs b/MinecraftClient/Commands/Send.cs index 55977740..92ef28d4 100644 --- a/MinecraftClient/Commands/Send.cs +++ b/MinecraftClient/Commands/Send.cs @@ -1,4 +1,5 @@ using System.Collections.Generic; +using Brigadier.NET; namespace MinecraftClient.Commands { @@ -8,6 +9,10 @@ namespace MinecraftClient.Commands public override string CmdUsage { get { return "send "; } } public override string CmdDesc { get { return "cmd.send.desc"; } } + public override void RegisterCommand(McClient handler, CommandDispatcher dispatcher) + { + } + public override string Run(McClient handler, string command, Dictionary? localVars) { if (HasArg(command)) diff --git a/MinecraftClient/Commands/Set.cs b/MinecraftClient/Commands/Set.cs index 9f8cfe5d..46f1d716 100644 --- a/MinecraftClient/Commands/Set.cs +++ b/MinecraftClient/Commands/Set.cs @@ -1,4 +1,5 @@ using System.Collections.Generic; +using Brigadier.NET; namespace MinecraftClient.Commands { @@ -8,6 +9,10 @@ namespace MinecraftClient.Commands public override string CmdUsage { get { return "set varname=value"; } } public override string CmdDesc { get { return "cmd.set.desc"; } } + public override void RegisterCommand(McClient handler, CommandDispatcher dispatcher) + { + } + public override string Run(McClient handler, string command, Dictionary? localVars) { if (HasArg(command)) diff --git a/MinecraftClient/Commands/SetRnd.cs b/MinecraftClient/Commands/SetRnd.cs index 6e0d00b2..f489bcca 100644 --- a/MinecraftClient/Commands/SetRnd.cs +++ b/MinecraftClient/Commands/SetRnd.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using Brigadier.NET; namespace MinecraftClient.Commands { @@ -10,6 +11,10 @@ namespace MinecraftClient.Commands public override string CmdDesc { get { return "cmd.setrnd.desc"; } } private static readonly Random rand = new(); + public override void RegisterCommand(McClient handler, CommandDispatcher dispatcher) + { + } + public override string Run(McClient handler, string command, Dictionary? localVars) { if (HasArg(command)) diff --git a/MinecraftClient/Commands/Sneak.cs b/MinecraftClient/Commands/Sneak.cs index 1c8dbf17..2f520b48 100644 --- a/MinecraftClient/Commands/Sneak.cs +++ b/MinecraftClient/Commands/Sneak.cs @@ -1,4 +1,5 @@ using System.Collections.Generic; +using Brigadier.NET; namespace MinecraftClient.Commands { @@ -9,6 +10,10 @@ namespace MinecraftClient.Commands public override string CmdUsage { get { return "Sneak"; } } public override string CmdDesc { get { return "cmd.sneak.desc"; } } + public override void RegisterCommand(McClient handler, CommandDispatcher dispatcher) + { + } + public override string Run(McClient handler, string command, Dictionary? localVars) { if (sneaking) diff --git a/MinecraftClient/Commands/Tps.cs b/MinecraftClient/Commands/Tps.cs index 9aaafc5c..cd0f51c1 100644 --- a/MinecraftClient/Commands/Tps.cs +++ b/MinecraftClient/Commands/Tps.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using Brigadier.NET; namespace MinecraftClient.Commands { @@ -9,6 +10,10 @@ namespace MinecraftClient.Commands public override string CmdUsage { get { return "tps"; } } public override string CmdDesc { get { return "cmd.tps.desc"; } } + public override void RegisterCommand(McClient handler, CommandDispatcher dispatcher) + { + } + public override string Run(McClient handler, string command, Dictionary? localVars) { var tps = Math.Round(handler.GetServerTPS(), 2); diff --git a/MinecraftClient/Commands/UseItem.cs b/MinecraftClient/Commands/UseItem.cs index 497633d2..be7eb2b2 100644 --- a/MinecraftClient/Commands/UseItem.cs +++ b/MinecraftClient/Commands/UseItem.cs @@ -1,4 +1,5 @@ using System.Collections.Generic; +using Brigadier.NET; namespace MinecraftClient.Commands { @@ -8,6 +9,10 @@ namespace MinecraftClient.Commands public override string CmdUsage { get { return "useitem"; } } public override string CmdDesc { get { return "cmd.useitem.desc"; } } + public override void RegisterCommand(McClient handler, CommandDispatcher dispatcher) + { + } + public override string Run(McClient handler, string command, Dictionary? localVars) { if (handler.GetInventoryEnabled()) diff --git a/MinecraftClient/Commands/Useblock.cs b/MinecraftClient/Commands/Useblock.cs index 5d8bf32e..3669794a 100644 --- a/MinecraftClient/Commands/Useblock.cs +++ b/MinecraftClient/Commands/Useblock.cs @@ -1,4 +1,5 @@ using System.Collections.Generic; +using Brigadier.NET; using MinecraftClient.Mapping; namespace MinecraftClient.Commands @@ -9,6 +10,10 @@ namespace MinecraftClient.Commands public override string CmdUsage { get { return "useblock "; } } public override string CmdDesc { get { return "cmd.useblock.desc"; } } + public override void RegisterCommand(McClient handler, CommandDispatcher dispatcher) + { + } + public override string Run(McClient handler, string command, Dictionary? localVars) { if (!handler.GetTerrainEnabled()) diff --git a/MinecraftClient/ConsoleIO.cs b/MinecraftClient/ConsoleIO.cs index 13b55989..022ccaf4 100644 --- a/MinecraftClient/ConsoleIO.cs +++ b/MinecraftClient/ConsoleIO.cs @@ -183,6 +183,7 @@ namespace MinecraftClient return; var buffer = ConsoleInteractive.ConsoleReader.GetBufferContent(); + ConsoleIO.WriteLine("AutoComplete " + buffer); autocomplete_engine.AutoComplete(buffer.Text[..buffer.CursorPosition]); } } diff --git a/MinecraftClient/McClient.cs b/MinecraftClient/McClient.cs index 16bb2e9d..e60919c7 100644 --- a/MinecraftClient/McClient.cs +++ b/MinecraftClient/McClient.cs @@ -2,9 +2,13 @@ using System; using System.Collections.Generic; using System.Linq; using System.Net.Sockets; +using System.Security.Cryptography.Pkcs; using System.Text; using System.Threading; +using Brigadier.NET; +using Brigadier.NET.Exceptions; using MinecraftClient.ChatBots; +using MinecraftClient.Commands; using MinecraftClient.Inventory; using MinecraftClient.Logger; using MinecraftClient.Mapping; @@ -25,8 +29,10 @@ namespace MinecraftClient { public static int ReconnectionAttemptsLeft = 0; - private static readonly List cmd_names = new(); - private static readonly Dictionary cmds = new(); + public static readonly CommandSource cmd_source = new(); + public static readonly CommandDispatcher dispatcher = new(); + //private static readonly List cmd_names = new(); + //private static readonly Dictionary cmds = new(); private readonly Dictionary onlinePlayers = new(); private static bool commandsLoaded = false; @@ -584,17 +590,18 @@ namespace MinecraftClient /// True if successfully registered public bool RegisterCommand(string cmdName, string cmdDesc, string cmdUsage, ChatBot.CommandRunner callback) { - if (cmds.ContainsKey(cmdName.ToLower())) - { - return false; - } - else - { - Command cmd = new ChatBot.ChatBotCommand(cmdName, cmdDesc, cmdUsage, callback); - cmds.Add(cmdName.ToLower(), cmd); - cmd_names.Add(cmdName.ToLower()); - return true; - } + // if (cmds.ContainsKey(cmdName.ToLower())) + // { + // return false; + // } + // else + // { + // Command cmd = new ChatBot.ChatBotCommand(cmdName, cmdDesc, cmdUsage, callback); + // cmds.Add(cmdName.ToLower(), cmd); + // cmd_names.Add(cmdName.ToLower()); + // return true; + // } + return true; } /// @@ -608,13 +615,14 @@ namespace MinecraftClient /// public bool UnregisterCommand(string cmdName) { - if (cmds.ContainsKey(cmdName.ToLower())) - { - cmds.Remove(cmdName.ToLower()); - cmd_names.Remove(cmdName.ToLower()); - return true; - } - else return false; + // if (cmds.ContainsKey(cmdName.ToLower())) + // { + // cmds.Remove(cmdName.ToLower()); + // cmd_names.Remove(cmdName.ToLower()); + // return true; + // } + // else return false; + return true; } /// @@ -627,52 +635,79 @@ namespace MinecraftClient public bool PerformInternalCommand(string command, ref string? response_msg, Dictionary? localVars = null) { /* Process the provided command */ - - string command_name = command.Split(' ')[0].ToLower(); - if (command_name == "help") + ParseResults parse; + try { - if (Command.HasArg(command)) - { - string help_cmdname = Command.GetArgs(command)[0].ToLower(); - if (help_cmdname == "help") - { - response_msg = Translations.Get("icmd.help"); - } - else if (cmds.ContainsKey(help_cmdname)) - { - response_msg = cmds[help_cmdname].GetCmdDescTranslated(); - } - else response_msg = Translations.Get("icmd.unknown", command_name); - } - else response_msg = Translations.Get("icmd.list", String.Join(", ", cmd_names.ToArray()), Config.Main.Advanced.InternalCmdChar.ToChar()); + parse = dispatcher.Parse(command, cmd_source); } - else if (cmds.ContainsKey(command_name)) + catch (Exception e) { - response_msg = cmds[command_name].Run(this, command, localVars); - - foreach (ChatBot bot in bots.ToArray()) - { - try - { - bot.OnInternalCommand(command_name, string.Join(" ", Command.GetArgs(command)), response_msg); - } - catch (Exception e) - { - if (e is not ThreadAbortException) - { - Log.Warn(Translations.Get("icmd.error", bot.ToString() ?? string.Empty, e.ToString())); - } - else throw; //ThreadAbortException should not be caught - } - } - } - else - { - response_msg = Translations.Get("icmd.unknown", command_name); - return false; + Log.Error(e.GetFullMessage()); + return true; } - return true; + try + { + int res = dispatcher.Execute(parse); + Log.Info("res = " + res); + return true; + } + catch (CommandSyntaxException e) + { + Log.Warn(e.GetFullMessage()); + return true; + } + catch (Exception e) + { + Log.Error(e.GetFullMessage()); + return true; + } + + //string command_name = command.Split(' ')[0].ToLower(); + //if (command_name == "help") + //{ + // if (Command.HasArg(command)) + // { + // string help_cmdname = Command.GetArgs(command)[0].ToLower(); + // if (help_cmdname == "help") + // { + // response_msg = Translations.Get("icmd.help"); + // } + // else if (cmds.ContainsKey(help_cmdname)) + // { + // response_msg = cmds[help_cmdname].GetCmdDescTranslated(); + // } + // else response_msg = Translations.Get("icmd.unknown", command_name); + // } + // else response_msg = Translations.Get("icmd.list", String.Join(", ", cmd_names.ToArray()), Config.Main.Advanced.InternalCmdChar.ToChar()); + //} + //else if (cmds.ContainsKey(command_name)) + //{ + // response_msg = cmds[command_name].Run(this, command, localVars); + + // foreach (ChatBot bot in bots.ToArray()) + // { + // try + // { + // bot.OnInternalCommand(command_name, string.Join(" ", Command.GetArgs(command)), response_msg); + // } + // catch (Exception e) + // { + // if (e is not ThreadAbortException) + // { + // Log.Warn(Translations.Get("icmd.error", bot.ToString() ?? string.Empty, e.ToString())); + // } + // else throw; //ThreadAbortException should not be caught + // } + // } + //} + //else + //{ + // response_msg = Translations.Get("icmd.unknown", command_name); + // return false; + //} + + //return true; } public void LoadCommands() @@ -689,10 +724,11 @@ namespace MinecraftClient try { Command cmd = (Command)Activator.CreateInstance(type)!; - cmds[Settings.ToLowerIfNeed(cmd.CmdName)] = cmd; - cmd_names.Add(Settings.ToLowerIfNeed(cmd.CmdName)); - foreach (string alias in cmd.GetCMDAliases()) - cmds[Settings.ToLowerIfNeed(alias)] = cmd; + cmd.RegisterCommand(this, dispatcher); + // cmds[Settings.ToLowerIfNeed(cmd.CmdName)] = cmd; + // cmd_names.Add(Settings.ToLowerIfNeed(cmd.CmdName)); + // foreach (string alias in cmd.GetCMDAliases()) + // cmds[Settings.ToLowerIfNeed(alias)] = cmd; } catch (Exception e) { diff --git a/MinecraftClient/MinecraftClient.csproj b/MinecraftClient/MinecraftClient.csproj index 013e6801..5f0061cf 100644 --- a/MinecraftClient/MinecraftClient.csproj +++ b/MinecraftClient/MinecraftClient.csproj @@ -33,6 +33,7 @@ + diff --git a/MinecraftClient/Protocol/Handlers/Protocol18.cs b/MinecraftClient/Protocol/Handlers/Protocol18.cs index 70670e4b..4856cb6f 100644 --- a/MinecraftClient/Protocol/Handlers/Protocol18.cs +++ b/MinecraftClient/Protocol/Handlers/Protocol18.cs @@ -2082,6 +2082,13 @@ namespace MinecraftClient.Protocol.Handlers /// Completed text IEnumerable IAutoComplete.AutoComplete(string BehindCursor) { + var sug = McClient.dispatcher.GetCompletionSuggestions(McClient.dispatcher.Parse(BehindCursor[1..], McClient.cmd_source)); + sug.Wait(); + foreach (var hint in sug.Result.List) + { + log.Info(hint); + } + //log.Info(McClient.dispatcher.GetSmartUsage(McClient.dispatcher.Parse(BehindCursor, McClient.cmd_source), McClient.cmd_source)); if (String.IsNullOrEmpty(BehindCursor)) return Array.Empty(); diff --git a/MinecraftClient/Scripting/ChatBot.cs b/MinecraftClient/Scripting/ChatBot.cs index 1c8272b1..d8d6a9fb 100644 --- a/MinecraftClient/Scripting/ChatBot.cs +++ b/MinecraftClient/Scripting/ChatBot.cs @@ -4,6 +4,8 @@ using System.IO; using System.Linq; using System.Text; using System.Text.RegularExpressions; +using Brigadier.NET; +using MinecraftClient.Commands; using MinecraftClient.Inventory; using MinecraftClient.Mapping; using static MinecraftClient.Settings; @@ -1676,6 +1678,10 @@ namespace MinecraftClient public override string CmdUsage { get { return _cmdUsage; } } public override string CmdDesc { get { return _cmdDesc; } } + public override void RegisterCommand(McClient handler, CommandDispatcher dispatcher) + { + } + public override string Run(McClient handler, string command, Dictionary? localVars) { return Runner(command, GetArgs(command));