> For the complete documentation index, see [llms.txt](https://docs.aethercube.fr/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.aethercube.fr/developers/plugins/lifecycle-and-threads.md). Agents can also search and read this documentation through its [MCP server](https://docs.aethercube.fr/~gitbook/mcp).

# Lifecycle and threads

This page explains when your plugin code runs and on which thread. Read it before you write anything beyond a hello world, because the thread rule is the one that bites.

## States

`/plugins` shows the state of each plugin.

| State          | Meaning                                                   |
| -------------- | --------------------------------------------------------- |
| `DISCOVERED`   | The jar was found.                                        |
| `VALIDATED`    | `plugin.yml` is valid.                                    |
| `RESOLVED`     | Dependencies are satisfied and the order is known.        |
| `LOADED`       | The class was instantiated and `onLoad` ran.              |
| `ENABLED`      | `onEnable` ran. The plugin works.                         |
| `DISABLING`    | The plugin is shutting down.                              |
| `DISABLED`     | Stopped, by `/plugin disable` or at shutdown.             |
| `ERROR`        | A lifecycle call threw, or the dependencies form a cycle. |
| `BLOCKED`      | A required dependency is missing or failed.               |
| `INCOMPATIBLE` | The jar is invalid, or redefines a reserved package.      |

An exception in one plugin never stops the server or another plugin. The plugin moves to `ERROR`, the server removes what it had registered and logs the failure.

## Order

1. The server reads the `plugins/` folder and validates every jar.
2. It sorts the plugins so that each comes after its dependencies.
3. `onLoad` runs for every plugin, each in its own class loader.
4. `STARTUP` plugins are enabled. The world loads.
5. `POSTWORLD` plugins are enabled.
6. At shutdown, dependents are disabled before their dependencies.

When a plugin is disabled, the server first disables the plugins that depend on it. It then stops routing calls to the plugin and removes its listeners, commands and scheduled tasks. Then `onDisable` runs: close your files there and save your data. Last, the server closes the plugin's class loader. Spawn entries added with `biomes().addSpawn(this, ...)` are removed too.

## Hooks

```java
public final class Example extends JavaPlugin {
    @Override public void onLoad()    { /* no server state yet; register generators here */ }
    @Override public void onEnable()  { /* register content, listeners and commands */ }
    @Override public void onDisable() { /* save and close */ }
}
```

All three have an empty default.

* Register chunk generators in `onLoad`, or in `onEnable` of a `STARTUP` plugin. See [World, biomes and generation](https://docs.aethercube.fr/developers/plugins/world-biomes-and-generation.md).
* Register items and creature types, and content packs, in `onEnable`. The registrations close when the network starts.

## Threads

The server runs the simulation on one thread at 20 ticks per second.

| Code                                            | Thread                                              |
| ----------------------------------------------- | --------------------------------------------------- |
| Event handlers                                  | Simulation thread.                                  |
| Command handlers                                | Simulation thread.                                  |
| `scheduler().runLater` and `runRepeating` tasks | Simulation thread, in the plugin phase of the tick. |
| `scheduler().runAsync` tasks                    | A worker thread.                                    |
| `ChunkGenerator.generate`                       | Worker threads, concurrently.                       |

The rule: touch game state only on the simulation thread. Game state means worlds, entities, players, inventories and registries that gameplay reads. I keep the simulation single-threaded so that nothing in the game needs a lock, and the price is that you must not break that. From an async task, hand the result back with `runLater`:

```java
getServer().scheduler().runAsync(this, () -> {
    String result = slowLookup();               // worker thread: no game state here
    getServer().scheduler().runLater(this, 0, () -> {
        getServer().broadcast(result);          // simulation thread
    });
});
```

Some calls throw when used off the simulation thread. `Creatures.spawn` throws `IllegalStateException`. `ScreenEffects.show` and `clear` are safe from any thread. Each registry page states its rule.

A handler that blocks stalls the whole server. Do not read files, call the network or wait on locks in a handler. The server measures plugin CPU time and reports it in the metrics. See [Performance and metrics](https://docs.aethercube.fr/developers/running-a-server/performance-and-metrics.md).

## No hot reload

`/plugin reload` is always refused. Restart the server to load a new jar. `/plugin disable <name>` stops a plugin until the next start.


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://docs.aethercube.fr/developers/plugins/lifecycle-and-threads.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
