Fix script not showing execution results.

This commit is contained in:
BruceChen 2023-01-15 19:59:57 +08:00
parent 8db0467f69
commit 338f534239
5 changed files with 88 additions and 7 deletions

View file

@ -5,6 +5,7 @@ using System.IO;
using System.Reflection; using System.Reflection;
using System.Text; using System.Text;
using System.Threading; using System.Threading;
using MinecraftClient.CommandHandler;
using MinecraftClient.Scripting; using MinecraftClient.Scripting;
namespace MinecraftClient.ChatBots namespace MinecraftClient.ChatBots
@ -85,7 +86,7 @@ namespace MinecraftClient.ChatBots
public static bool LookForScript(ref string filename) public static bool LookForScript(ref string filename)
{ {
//Automatically look in subfolders and try to add ".txt" file extension //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[] string[] files = new string[]
{ {
filename, filename,
@ -210,11 +211,22 @@ namespace MinecraftClient.ChatBots
sleepticks = ticks; sleepticks = ticks;
break; break;
default: 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 Update(); //Unknown command : process next line immediately
} }
else if (instruction_name.ToLower() != "log") { LogToConsole(instruction_line); }
break; break;
} }
} }

View file

@ -1,4 +1,5 @@
using System; using System;
using MinecraftClient.CommandHandler;
using MinecraftClient.Scripting; using MinecraftClient.Scripting;
using Tomlet.Attributes; using Tomlet.Attributes;
using static MinecraftClient.ChatBots.ScriptScheduler.Configs; using static MinecraftClient.ChatBots.ScriptScheduler.Configs;
@ -191,7 +192,10 @@ namespace MinecraftClient.ChatBots
{ {
task.Trigger_On_Time_Already_Triggered = true; task.Trigger_On_Time_Already_Triggered = true;
LogDebugToConsole(string.Format(Translations.bot_scriptScheduler_running_time, task.Action)); 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)) if (task.Trigger_On_Login || (firstlogin_done == false && task.Trigger_On_First_Login))
{ {
LogDebugToConsole(string.Format(Translations.bot_scriptScheduler_running_login, task.Action)); 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) Settings.DoubleToTick(task.Trigger_On_Interval.MinTime), Settings.DoubleToTick(task.Trigger_On_Interval.MaxTime)
); );
LogDebugToConsole(string.Format(Translations.bot_scriptScheduler_running_inverval, task.Action)); 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--; else task.Trigger_On_Interval_Countdown--;
} }

View file

@ -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<string>
{
public override string Parse(IStringReader reader)
{
string remaining = reader.Remaining;
reader.Cursor += reader.RemainingLength;
return remaining;
}
public override Task<Suggestions> ListSuggestions<TSource>(CommandContext<TSource> 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();
}
}
}

View file

@ -110,5 +110,10 @@ namespace MinecraftClient.CommandHandler
{ {
return new HotbarSlotArgumentType(); return new HotbarSlotArgumentType();
} }
public static ScriptNameArgumentType ScriptName()
{
return new ScriptNameArgumentType();
}
} }
} }

View file

@ -20,7 +20,7 @@ namespace MinecraftClient.Commands
); );
dispatcher.Register(l => l.Literal(CmdName) 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))) .Executes(r => DoExecuteScript(r.Source, Arguments.GetString(r, "Script"), null)))
.Then(l => l.Literal("_help") .Then(l => l.Literal("_help")
.Executes(r => GetUsage(r.Source, string.Empty)) .Executes(r => GetUsage(r.Source, string.Empty))