diff --git a/MinecraftClient/ChatBots/Script.cs b/MinecraftClient/ChatBots/Script.cs index 8ed7cb42..9e139db9 100644 --- a/MinecraftClient/ChatBots/Script.cs +++ b/MinecraftClient/ChatBots/Script.cs @@ -5,6 +5,7 @@ using System.IO; using System.Reflection; using System.Text; using System.Threading; +using MinecraftClient.CommandHandler; using MinecraftClient.Scripting; namespace MinecraftClient.ChatBots @@ -85,7 +86,7 @@ namespace MinecraftClient.ChatBots public static bool LookForScript(ref string filename) { //Automatically look in subfolders and try to add ".txt" file extension - char dir_slash = Path.DirectorySeparatorChar; + char dir_slash = Path.DirectorySeparatorChar; string[] files = new string[] { filename, @@ -210,11 +211,22 @@ namespace MinecraftClient.ChatBots sleepticks = ticks; break; default: - if (!PerformInternalCommand(instruction_line)) + CmdResult response = new(); + if (PerformInternalCommand(instruction_line, ref response)) + { + if (instruction_name.ToLower() != "log") + { + LogToConsole(instruction_line); + } + if (response.status != CmdResult.Status.Done || !string.IsNullOrWhiteSpace(response.result)) + { + LogToConsole(response); + } + } + else { Update(); //Unknown command : process next line immediately } - else if (instruction_name.ToLower() != "log") { LogToConsole(instruction_line); } break; } } diff --git a/MinecraftClient/ChatBots/ScriptScheduler.cs b/MinecraftClient/ChatBots/ScriptScheduler.cs index bf8680a4..0e15949d 100644 --- a/MinecraftClient/ChatBots/ScriptScheduler.cs +++ b/MinecraftClient/ChatBots/ScriptScheduler.cs @@ -1,4 +1,5 @@ using System; +using MinecraftClient.CommandHandler; using MinecraftClient.Scripting; using Tomlet.Attributes; using static MinecraftClient.ChatBots.ScriptScheduler.Configs; @@ -191,7 +192,10 @@ namespace MinecraftClient.ChatBots { task.Trigger_On_Time_Already_Triggered = true; LogDebugToConsole(string.Format(Translations.bot_scriptScheduler_running_time, task.Action)); - PerformInternalCommand(task.Action); + CmdResult response = new(); + PerformInternalCommand(task.Action, ref response); + if (response.status != CmdResult.Status.Done || !string.IsNullOrWhiteSpace(response.result)) + LogToConsole(response); } } } @@ -209,7 +213,10 @@ namespace MinecraftClient.ChatBots if (task.Trigger_On_Login || (firstlogin_done == false && task.Trigger_On_First_Login)) { LogDebugToConsole(string.Format(Translations.bot_scriptScheduler_running_login, task.Action)); - PerformInternalCommand(task.Action); + CmdResult response = new(); + PerformInternalCommand(task.Action, ref response); + if (response.status != CmdResult.Status.Done || !string.IsNullOrWhiteSpace(response.result)) + LogToConsole(response); } } @@ -229,7 +236,10 @@ namespace MinecraftClient.ChatBots Settings.DoubleToTick(task.Trigger_On_Interval.MinTime), Settings.DoubleToTick(task.Trigger_On_Interval.MaxTime) ); LogDebugToConsole(string.Format(Translations.bot_scriptScheduler_running_inverval, task.Action)); - PerformInternalCommand(task.Action); + CmdResult response = new(); + PerformInternalCommand(task.Action, ref response); + if (response.status != CmdResult.Status.Done || !string.IsNullOrWhiteSpace(response.result)) + LogToConsole(response); } else task.Trigger_On_Interval_Countdown--; } diff --git a/MinecraftClient/CommandHandler/ArgumentType/ScriptNameArgumentType.cs b/MinecraftClient/CommandHandler/ArgumentType/ScriptNameArgumentType.cs new file mode 100644 index 00000000..fe77a04c --- /dev/null +++ b/MinecraftClient/CommandHandler/ArgumentType/ScriptNameArgumentType.cs @@ -0,0 +1,54 @@ +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Brigadier.NET; +using Brigadier.NET.ArgumentTypes; +using Brigadier.NET.Context; +using Brigadier.NET.Suggestion; + +namespace MinecraftClient.CommandHandler.ArgumentType +{ + public class ScriptNameArgumentType : ArgumentType + { + public override string Parse(IStringReader reader) + { + string remaining = reader.Remaining; + reader.Cursor += reader.RemainingLength; + return remaining; + } + + public override Task ListSuggestions(CommandContext context, SuggestionsBuilder builder) + { + try + { + string? dir = Path.GetDirectoryName(builder.Remaining); + if (!string.IsNullOrEmpty(dir) && Path.Exists(dir)) + { + foreach (string fileName in Directory.GetFiles(dir, "*.cs")) + builder.Suggest(fileName); + foreach (string fileName in Directory.GetFiles(dir, "*.txt")) + builder.Suggest(fileName); + } + } + catch (IOException) { } + catch (ArgumentException) { } + catch (UnauthorizedAccessException) { } + + try + { + foreach (string fileName in Directory.GetFiles("." + Path.DirectorySeparatorChar, "*.cs")) + builder.Suggest(fileName); + foreach (string fileName in Directory.GetFiles("." + Path.DirectorySeparatorChar, "*.txt")) + builder.Suggest(fileName); + } + catch (IOException) { } + catch (ArgumentException) { } + catch (UnauthorizedAccessException) { } + + return builder.BuildFuture(); + } + } +} diff --git a/MinecraftClient/CommandHandler/MccArguments.cs b/MinecraftClient/CommandHandler/MccArguments.cs index c659ee3e..78416bc1 100644 --- a/MinecraftClient/CommandHandler/MccArguments.cs +++ b/MinecraftClient/CommandHandler/MccArguments.cs @@ -110,5 +110,10 @@ namespace MinecraftClient.CommandHandler { return new HotbarSlotArgumentType(); } + + public static ScriptNameArgumentType ScriptName() + { + return new ScriptNameArgumentType(); + } } } diff --git a/MinecraftClient/Commands/Script.cs b/MinecraftClient/Commands/Script.cs index d2f310dd..37a8da80 100644 --- a/MinecraftClient/Commands/Script.cs +++ b/MinecraftClient/Commands/Script.cs @@ -20,7 +20,7 @@ namespace MinecraftClient.Commands ); dispatcher.Register(l => l.Literal(CmdName) - .Then(l => l.Argument("Script", Arguments.GreedyString()) + .Then(l => l.Argument("Script", MccArguments.ScriptName()) .Executes(r => DoExecuteScript(r.Source, Arguments.GetString(r, "Script"), null))) .Then(l => l.Literal("_help") .Executes(r => GetUsage(r.Source, string.Empty))