Add method to schedule main thread task (#1570)

* Add method to schedule main thread task
* ChatBot API: New delay task method
* Add sealed attribute to ChatBot internal method
This commit is contained in:
ReinforceZwei 2021-05-08 21:03:23 +08:00 committed by GitHub
parent b8ce4d16c4
commit a5a8075efb
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 134 additions and 13 deletions

View file

@ -28,11 +28,13 @@ namespace MinecraftClient
private readonly Dictionary<Guid, string> onlinePlayers = new Dictionary<Guid, string>();
private static bool commandsLoaded = false;
private Queue<string> commandQueue = new Queue<string>();
private Queue<string> chatQueue = new Queue<string>();
private static DateTime nextMessageSendTime = DateTime.MinValue;
private Action threadTasks;
private object threadTasksLock = new object();
private readonly List<ChatBot> bots = new List<ChatBot>();
private static readonly List<ChatBot> botsOnHold = new List<ChatBot>();
private static Dictionary<int, Container> inventories = new Dictionary<int, Container>();
@ -310,10 +312,7 @@ namespace MinecraftClient
while (client.Client.Connected)
{
string text = ConsoleIO.ReadLine();
lock (commandQueue)
{
commandQueue.Enqueue(text);
}
ScheduleTask(delegate () { HandleCommandPromptText(text); });
}
}
catch (IOException) { }
@ -579,6 +578,7 @@ namespace MinecraftClient
try
{
bot.Update();
bot.UpdateInternal();
}
catch (Exception e)
{
@ -590,14 +590,6 @@ namespace MinecraftClient
}
}
lock (commandQueue)
{
if (commandQueue.Count > 0)
{
HandleCommandPromptText(commandQueue.Dequeue());
}
}
lock (chatQueue)
{
if (chatQueue.Count > 0 && nextMessageSendTime < DateTime.Now)
@ -648,6 +640,15 @@ namespace MinecraftClient
if (respawnTicks == 0)
SendRespawnPacket();
}
if (threadTasks != null)
{
lock (threadTasksLock)
{
threadTasks();
threadTasks = null;
}
}
}
/// <summary>
@ -692,6 +693,18 @@ namespace MinecraftClient
else return false;
}
/// <summary>
/// Schedule a task to run on the main thread
/// </summary>
/// <param name="task">Task to run</param>
public void ScheduleTask(Action task)
{
lock (threadTasksLock)
{
threadTasks += task;
}
}
#region Management: Load/Unload ChatBots and Enable/Disable settings
/// <summary>