# Architecture

How GlacierHub is structured, and how to add a module of your own.

GlacierHub is one Maven project that produces one JAR. The API and the implementation are separated by package, not by a second download.

## Project layout

```text
src/main/java/dev/glacierlabs/glacierhub/
|-- GlacierHub.java        Bukkit entry point
|-- api/                   public API
`-- runtime/
    |-- GlacierHubRuntime  startup and shutdown
    |-- bridge/            API implementation, main-thread guard
    |-- commands/          command routing
    |-- config/            YAML loading
    |-- hotbar/            shared hotbar item slots
    |-- leaderboard/       leaderboard sources
    |-- lifecycle/         module enable/disable, BuiltInModules
    |-- modules/<id>/      one package per module
    |-- platform/          Bukkit/Folia scheduling and version differences
    |-- player/            shared player state
    |-- queue/             queue gateway used by the navigator
    |-- recovery/          crash recovery
    |-- storage/           SQLite/MySQL/MongoDB
    `-- util/              text, durations, menus
```

A feature package may use the public API and small shared internal services. It must never import another feature's implementation. Shared behaviour goes through a public API interface or a focused internal one, such as the queue gateway the navigator uses or the leaderboard source that Points, Parkour and Vote Rewards implement.

## Startup and shutdown

`GlacierHubRuntime` is the composition root. It creates configuration, text, commands, module lifecycle, recovery services, built-in modules and finally the Bukkit API service, in that order. Inventory recovery runs from broadest to narrowest: PvP restores its full snapshot before hotbar slots are reconciled.

On shutdown the public API is invalidated before any module stops.

`BuiltInModules` lists every module explicitly. There's no classpath scanning and no dependency container, so you can see every feature and every cross-module dependency in one file.

## Module lifecycle

Each module instance lives as long as the plugin, which keeps cached API handles valid across disable and re-enable. Every `enable` gets a fresh `ModuleContext` that owns the module's Bukkit resources:

```java
context.listen(listener);
context.command(command);
context.repeat(service::refresh, 0L, 100L);
context.outgoingChannel("BungeeCord");
```

When the module stops, the context releases everything in reverse order. The module's `disable` only restores feature state and clears references. The same cleanup runs when `enable` fails halfway.

Reload is the same operation: close the context, build a new one, enable the same instance. There's no second, partial reload path to maintain.

## Threads and Folia

Module code never calls the Bukkit scheduler or teleports directly:

| Call | Runs on |
| --- | --- |
| `ModuleContext.repeat` / `later` | The global thread |
| `ModuleContext.repeatFor` / `laterFor` / `forPlayer` | The player's thread (their region on Folia) |
| `Platform.forEachOnline` | Each online player's own thread |
| `Platform.teleport` | `teleportAsync` on Folia |
| `ConfiguredCommands` | Console commands, on the global thread |

On Folia, events for different players run at the same time on different threads, so shared state lives in concurrent collections or behind `synchronized` service methods. `Names` resolves sounds, particles and enchantments by name across versions, and `ItemTags` uses the 1.13 tag API or the persistent data API, whichever the server has.

## Storage

`StorageEngine` opens the backend from `storage.yml` and hands modules a `DataStore` through `ModuleContext.getStorage()`. Writes queue on one background thread, in order, with retries. Reads wait for queued writes first. Modules load their data on enable and keep it in memory, so nothing touches the database during gameplay.

If the backend is unreachable at startup, storage calls throw `StorageUnavailableException`. Modules that need storage fail to enable with a short warning and the rest keep running. Use `ModuleContext.async` for other work that must stay in order with saves.

## Configuration

`YamlFile` loads the server copy, fills in bundled defaults and understands the older `FILE-VERSION` key. Feature code reads its YAML once during `enable` and passes typed values to services and listeners. Compatibility lookups and defaults belong at that boundary, not scattered through event handlers.

Player-facing text lives in `lang/<locale>/<module>.yml` and is loaded through `ModuleContext.getMessages()`. Java fallbacks should match the bundled English file.

## Adding a module

1. Add a public interface under `api` only if other plugins need to call it.
2. Create a shallow package under `runtime.modules` with a class implementing `HubModule`, plus clearly named services, listeners, commands or menus.
3. Add an ID to `ModuleIds` and register one instance in `BuiltInModules`.
4. Register listeners, commands and tasks through `ModuleContext`, and make cleanup idempotent.
5. Add `modules/<id>.yml` and `lang/en/<id>.yml`, both with a `schema-version`.
6. Declare commands and permissions in `plugin.yml`, and add the permissions to `glacierhub.admin`.
7. Test enable, disable, enable again, and a server shutdown before you ship.

See [building from source](/docs/network/building) for the build commands.
