# The runtime in the Minekube plugin

> The Minekube plugin's JavaScript runtime runs bundles in GraalJS inside the server's own process - it loads a new bundle on a thread of its own, swaps it in on the server thread, and never replaces a working bundle with a broken one.

The runtime is the half of the Minekube plugin that runs your code. It watches a folder of [bundles](https://minekube.com/games/concepts/bundle), gives each one its own GraalJS context inside the server's Java process, and connects it to the platform's events, scheduler and main thread. The connector is the plugin's other half.

The same runtime core runs on Paper and Velocity, as the Minekube plugin, and in the Minecraft client, as a Fabric mod.

## Inside the server, on its thread

- **Same process.** A game calls Paper's own classes directly, in the same tick, with no network in between.
- **One thread.** A GraalJS context runs on one thread at a time, so your code runs only on the platform's main thread. Calls from Java on other threads are brought onto it. The server thread itself never waits, so there is no deadlock.
- **One context per bundle.** Bundles do not share state by accident, and one can be replaced without touching the others.

On a GraalVM JDK, the Truffle compiler compiles your code; on other JDKs, GraalJS only interprets it. Run servers on GraalVM.

## Live swap

Live swap replaces a running bundle with its new content while the server keeps running and players stay connected. The runtime notices a changed file in its folder and:

1. **Loads the new bundle on a thread of its own.** Evaluating a large bundle takes long on a small CPU, so it happens off the server thread, and the server keeps ticking.
2. **Keeps the old one if the new one fails.** A bundle that fails to evaluate is rejected, and the running one stays: a typo costs a message in chat, not the game.
3. **Swaps on the server thread, in one task.** The old code stops and its scope closes, then the new code starts, so no event reaches the game between the two.

[Performance](https://minekube.com/games/platform/performance#live-swap) has the measured pause on the server thread.

## Revisions

Each content of a bundle that the runtime loads is a revision, named by the first digits of its SHA-256: `Reloaded my-game (5d2e7b18)`. The same name appears in the server's messages, in the recording's file name and in the devtools, so a session always says which code it ran. A file rewritten with the same content is the same revision, and nothing reloads.

A revision on one server is one bundle. A [version](https://minekube.com/games/concepts/versions-and-deployments) is what a network deploys: a set of bundles, one revision of each.

## What survives a swap

| Survives | Does not survive |
| --- | --- |
| Players and their connections | The game's Model: the new revision starts from `init` |
| The world: blocks and entities | Listeners, timers, fibers and running Commands of the old revision |
| Other bundles on the server | Server commands the old revision registered, until the new one registers them |

The old revision's messages stay in its [recording](https://minekube.com/games/concepts/recording).

## Why swap instead of restart

A restart throws every player out and takes long enough to break the flow of an idea. A swap changes the rules under the players: you try a change where you play, and your friend keeps building. The same move serves production, where a network's servers swap in a newly deployed version the way `mk dev` swaps in a save.

## What the runtime is not

- **Not a plugin loader.** It runs JavaScript bundles against the platform's public API. It adds no Java classes, and features that need Java belong in the Minekube plugin itself.
- **Not a reload of the plugin.** The Minekube plugin and its native libraries load once per server start; only bundles swap.

## Built and planned

Built on Paper, Velocity and the Fabric client. Planned: a game can hold a swap until a safe moment, such as the end of a round, and running the same game code outside the server, in a Worker or a Durable Object, through a second implementation of the platform's service.

## Related

- [Live swap and lifetime](https://minekube.com/games/local-development/live-swap): what a swap means for your game's state.
- [Architecture](https://minekube.com/games/platform/architecture): the runtime's parts, from a save to running code.
- [Runtime messages](https://minekube.com/games/reference/runtime-messages): what the runtime says when it loads, rejects or restarts a bundle.
