From 9df255dd29d48c772e9382026e1233821726402c Mon Sep 17 00:00:00 2001 From: ORelio Date: Sun, 18 Oct 2020 12:05:42 +0200 Subject: [PATCH] Add sample script with task (#1281) This script shows how to run code periodically without using a thread. --- .../config/sample-script-with-task.cs | 32 +++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 MinecraftClient/config/sample-script-with-task.cs diff --git a/MinecraftClient/config/sample-script-with-task.cs b/MinecraftClient/config/sample-script-with-task.cs new file mode 100644 index 00000000..8feb6c6c --- /dev/null +++ b/MinecraftClient/config/sample-script-with-task.cs @@ -0,0 +1,32 @@ +//MCCScript 1.0 + +MCC.LoadBot(new PeriodicTask()); + +//MCCScript Extensions + +/// +/// The ChatBot API is not thread-safe so tasks must occur on the main thread. +/// This bot shows an example of running a task periodically without using threads. +/// +public class PeriodicTask : ChatBot +{ + private DateTime nextTaskRun = DateTime.Now; + + /// + /// Called on each MCC tick, around 10 times per second + /// + public override void Update() + { + DateTime dateNow = DateTime.Now; + if (nextTaskRun < dateNow) + { + LogDebugToConsole("Running task @ " + dateNow); + + // Your task here + SendText("/ping"); + + // Schedule next run + nextTaskRun = dateNow.AddSeconds(60); + } + } +} \ No newline at end of file