Minestom wiki
Search…
Schedulers
Schedulers are fairly straightforward, they allow you to perform an action based on time.
Be aware that scheduled tasks are executed in their own thread pool, synchronization can be required depending on what you do.
There are 4 types of schedulers, each one of them can be accessed by calling the SchedulerManager.
1
SchedulerManager schedulerManager = MinecraftServer.getSchedulerManager();
2
3
// First scheduler to repeat the task each time
4
schedulerManager.buildTask(new Runnable() {
5
@Override
6
public void run() {
7
// This method is gonna be called every 5 ticks
8
}
9
}).repeat(5 /*Time in tick*/, TimeUnit.SERVER_TICK).schedule();
10
11
// Second scheduler that delays a task for a certain time
12
schedulerManager.buildTask(new Runnable() {
13
@Override
14
public void run() {
15
// This method is gonna be called after 1 tick
16
}
17
}).delay(1 /*Time in tick*/, TimeUnit.SERVER_TICK).schedule();
18
19
// Third scheduler, is a combination of a delayed task and a repeated task
20
schedulerManager.buildTask(new Runnable() {
21
@Override
22
public void run() {
23
// This method is called with a 5 second delay and then called every 1 second.
24
}
25
}).delay( 5/*Time in seconds*/, TimeUnit.SECOND).repeat(1 /*Time in seconds*/, TimeUnit.SECOND).schedule();
26
27
// The fourth and last task is the shutdown task
28
schedulerManager.buildShutdownTask(new Runnable() {
29
@Override
30
public void run() {
31
// This method is called when the server is shut down
32
// Can be used to save cached data, for example
33
}
34
}).schedule();
Copied!
Last modified 28d ago
Copy link