Skip to content

The Scheduler

SimplyCmd edited this page Dec 27, 2021 · 1 revision

The scheduler can be used to delay the execution of code by a set amount of ticks on either the client or the server.

Usage

Simply call the method schedule() in either ServerScheduler or ClientScheduler, like so:

ServerScheduler.schedule(/* Put your delay here */, (id) -> /* Put your single-line code here */);
ClientScheduler.schedule(/* Put your delay here */, (id) -> {
    // Put your multi-line code here
});

Example

This is an item that delays a task by 5 seconds when right-clicked with.

public class SchedulerExampleItem extends Item {
    public SchedulerExampleItem(Settings settings) {
        super(settings);
    }

    @Override
    public TypedActionResult<ItemStack> use(World world, PlayerEntity user, Hand hand) {
        ServerScheduler.schedule(100, (id) -> System.out.println("Task with ID: " + id + " printed 100 ticks or 5 seconds later"));
        return TypedActionResult.consume(user.getStackInHand(hand));
    }
}

Clone this wiki locally