mirror of
https://github.com/MCCTeam/Minecraft-Console-Client
synced 2026-08-15 13:04:36 +00:00
Fix command registration for external MCC scripts
- Update Roslyn compiler LanguageVersion from CSharp9 to Latest - Implement ChatBotCommand.RegisterCommand() (was empty) - Add RegisterChatBotCommand() helper to ChatBot base class - Add automatic command cleanup in BotUnLoad via UnregisterChatBotCommands() - Fix external scripts using Handler.dispatcher (CS0176 static via instance) - Fix AutoTree.cs wrong Initialize/OnUnload signatures (CS0115) - EntityCount.cs RegisterChatBotCommand() now works with new helper Co-authored-by: milutinke <441903+milutinke@users.noreply.github.com> Agent-Logs-Url: https://github.com/MCCTeam/Minecraft-Console-Client/sessions/27fc44b8-50d8-4194-8057-019a29675d4a
This commit is contained in:
parent
34bc6c5400
commit
687d1746ed
7 changed files with 62 additions and 15 deletions
|
|
@ -6,7 +6,9 @@ using System.Text;
|
|||
using System.Text.RegularExpressions;
|
||||
using System.Threading;
|
||||
using Brigadier.NET;
|
||||
using Brigadier.NET.Builder;
|
||||
using MinecraftClient.CommandHandler;
|
||||
using MinecraftClient.CommandHandler.Patch;
|
||||
using MinecraftClient.Inventory;
|
||||
using MinecraftClient.Mapping;
|
||||
using static MinecraftClient.Settings;
|
||||
|
|
@ -43,6 +45,7 @@ namespace MinecraftClient.Scripting
|
|||
private McClient? _handler = null;
|
||||
private ChatBot? master = null;
|
||||
private readonly List<string> registeredPluginChannels = new();
|
||||
private readonly List<string> registeredChatBotCommands = new();
|
||||
private readonly Lock delayTasksLock = new();
|
||||
private readonly List<TaskWithDelay> delayedTasks = new();
|
||||
protected McClient Handler
|
||||
|
|
@ -1710,6 +1713,34 @@ namespace MinecraftClient.Scripting
|
|||
/// <returns>Command result to display to the user</returns>
|
||||
public delegate string CommandRunner(string command, string[] args);
|
||||
|
||||
/// <summary>
|
||||
/// Register a simple ChatBot command with the Brigadier dispatcher.
|
||||
/// The command is automatically unregistered when the bot is unloaded.
|
||||
/// </summary>
|
||||
/// <param name="cmdName">Name of the command (used as the literal command name)</param>
|
||||
/// <param name="cmdDesc">Description of the command</param>
|
||||
/// <param name="cmdUsage">Usage string for the command</param>
|
||||
/// <param name="callback">Method to handle the command execution</param>
|
||||
protected void RegisterChatBotCommand(string cmdName, string cmdDesc, string cmdUsage, CommandRunner callback)
|
||||
{
|
||||
var command = new ChatBotCommand(cmdName, cmdDesc, cmdUsage, callback);
|
||||
command.RegisterCommand(McClient.dispatcher);
|
||||
registeredChatBotCommands.Add(cmdName);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Unregisters all commands that were registered via RegisterChatBotCommand.
|
||||
/// Called automatically during bot unload.
|
||||
/// </summary>
|
||||
internal void UnregisterChatBotCommands()
|
||||
{
|
||||
foreach (var cmdName in registeredChatBotCommands)
|
||||
{
|
||||
McClient.dispatcher.Unregister(cmdName);
|
||||
}
|
||||
registeredChatBotCommands.Clear();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Command class with constructor for creating command for ChatBots.
|
||||
/// </summary>
|
||||
|
|
@ -1727,7 +1758,22 @@ namespace MinecraftClient.Scripting
|
|||
|
||||
public override void RegisterCommand(CommandDispatcher<CmdResult> dispatcher)
|
||||
{
|
||||
|
||||
dispatcher.Register(l => l.Literal(_cmdName)
|
||||
.Then(l => l.Argument("args", Arguments.GreedyString())
|
||||
.Executes(r =>
|
||||
{
|
||||
string fullArgs = Arguments.GetString(r, "args");
|
||||
string result = Runner(
|
||||
$"{_cmdName} {fullArgs}",
|
||||
fullArgs.Split(' ', StringSplitOptions.RemoveEmptyEntries));
|
||||
return r.Source.SetAndReturn(CmdResult.Status.Done, result);
|
||||
}))
|
||||
.Executes(r =>
|
||||
{
|
||||
string result = Runner(_cmdName, Array.Empty<string>());
|
||||
return r.Source.SetAndReturn(CmdResult.Status.Done, result);
|
||||
})
|
||||
);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
|
|
|||
|
|
@ -49,7 +49,7 @@ namespace MinecraftClient.Scripting.DynamicRun.Builder
|
|||
private static CSharpCompilation GenerateCode(string sourceCode, string fileName, List<string> additionalAssemblies)
|
||||
{
|
||||
var codeString = SourceText.From(sourceCode);
|
||||
var options = CSharpParseOptions.Default.WithLanguageVersion(LanguageVersion.CSharp9);
|
||||
var options = CSharpParseOptions.Default.WithLanguageVersion(LanguageVersion.Latest);
|
||||
|
||||
var parsedSyntaxTree = SyntaxFactory.ParseSyntaxTree(codeString, options);
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue