Scheduler in Kore
This document explains how to schedule and run tasks at specific times or intervals using Kore's built-in scheduler. Schedulers help automate recurring actions, delayed tasks, and cleanup when tasks are no longer needed.
Overview
A "Scheduler" in Kore lets you:
- Schedule a one-time execution of a function or a block of code.
- Schedule repeating tasks with a fixed period.
- Persist references to scheduled tasks so they can be modified or canceled.
All scheduling logic revolves around three core classes:
- Scheduler – Represents a single scheduled task (with optional delay and period).
- UnScheduler – Cancels, or clears, repeating tasks.
- SchedulerManager – Maintains a list of schedulers for a given DataPack and offers convenience methods to add or remove them.
Note: Schedulers are saved and loaded from a scheduler_setup
function that is added to the minecraft/load.json
tag.
Basic Usage
Use the DataPack extension function schedulerManager to get or create a SchedulerManager for your datapack:
Inside the schedulerManager block, you can add schedulers with different behaviors by calling addScheduler. Common calls are:
addScheduler(delay)
– Executes once after a given delay.addScheduler(delay, period)
– Executes once after delay, then repeats every period.addScheduler(block)
– Executes right away if no delay or period is specified.
In many cases, you'll pass a function block (Function.() -> Command) so you can include DSL commands:
One-Time Execution
If you only want to run a function once after a delay:
Recurrent Execution
For periodic tasks:
Schedule by reference
You can also schedule a task by reference:
Canceling a Repeating Task
If you have a repeating scheduler, you can unschedule it when you no longer need it:
- Pass a named function or store the return value of addScheduler.
- Call unSchedule or removeScheduler.
Example removing by reference:
Or remove by function name:
Canceling all tasks
You can cancel all tasks by calling clearSchedulers:
Complex Example
Below is a more advanced scenario showing how to:
- Run a periodic task (initial delay + repeating period).
- Store custom data in a named storage using the /data command.
- Use execute to conditionally run commands based on stored data.
In this example, we keep a running "counter" in a storage and increment it every time the repeating task runs. We also demonstrate how to read from that storage in subsequent commands.
Full Example
Explanation:
- We create a custom storage named "kore_example:counter_storage" to maintain a key called "counter".
- We set up a repeating task (delayed by 2 seconds, repeats every 4 seconds). Inside that repeating schedule:
- We store the counter in a score
- We increment the counter in storage.
- We show the updated counter using data get and say commands.
- We run a condition (ifData … >= 5) to check if "counter" has reached 5 or more, then summon a lightning bolt.
- We also add a single-run scheduler at 10 seconds that calls a function to reset the counter and prints a final message.
Conclusion
- Schedulers let you automate tasks in your datapack with fixed delays or repetition.
- Use a SchedulerManager on your DataPack via
schedulerManager { … }
. - Add, remove, or clear schedulers by referencing either the assigned function or the function name.
- Combine schedulers with any usual commands for fully automated or repeated logic (debugging, storing data, advanced "execute" conditions, etc.).
This powerful system helps keep your datapack logic neatly organized and easy to maintain when you need repeated or delayed operations.