Skip to content

Scheduling

This page contains information on how to schedule actions to be run in the future.

Using Minecraft's built-in tick scheduler.

The schedule method provided by Minecraft only allows you to schedule your actions to be run the next tick.

kotlin
client.schedule {
    // Do something
}

Creating your own scheduler

This is a code snippet for a basic scheduler that allows you to use the Kotlin durations library and Java Executors to schedule actions to happen in the future.

WARNING

This runs actions on a separate thread and not the main thread.

kotlin
object Example {
    private val executor = Executors.newSingleThreadScheduledExecutor { Thread(it, "Executor").apply { isDaemon = true } }

    fun schedule(delay: Duration, action: () -> Unit) {
        executor.schedule(action, delay.inWholeMilliseconds, TimeUnit.MILLISECONDS)
    }
}

Example usage

kotlin
Example.schedule(5.seconds) {
    println("This runs after 5 seconds!")
}

Running actions on the main thread

You can use the client.execute() method to ensure that the specified action runs on the main thread.

Not affiliated with Mojang, Microsoft, or Hypixel.