# Architecture

Package boundaries, startup order, the command router and how to add a module.

GlacierStaff is one Maven project that produces one JAR, with the same module system, storage engine, command framework and API style as GlacierHub.

## Project layout

```text
src/main/java/dev/glacierlabs/glacierstaff/
  GlacierStaff.java       Bukkit entry point
  api/                    public API
  runtime/
    GlacierStaffRuntime   composition root: startup and shutdown order
    bridge/               API service, GlacierHub event bridge, main-thread guard
    commands/             command framework
    config/               YAML files, module states, global settings
    lifecycle/            module manager, module context, built-in module list
    modules/<module>/     one package per feature
    platform/             Paper/Folia schedulers and version-safe names
    redis/                Redis pub/sub
    security/             staff-auth guard and vanish visibility levels
    storage/              SQLite, MySQL, MongoDB and the write-behind store
    util/                 text, durations, commands, safe file writes, menus
```

Modules never import each other. They talk through the API package, Bukkit events or a focused internal service. `ArchitectureBoundaryTest` enforces this in the build.

Two shared services in `security` matter to every feature: `StaffAccess` is the single staff-auth guard for commands, menus and events, and `VanishVisibility` is the shared vanish-level check.

## Startup order

`GlacierStaffRuntime` starts, in order:

1. the menu framework
2. the staff-auth command guard
3. global settings and text
4. the Redis connection
5. storage
6. saved module states
7. the command router
8. the module manager with every built-in module
9. the Bukkit API service

Shutdown runs the other way. The API is invalidated and unregistered first, then open menus close, command routes clear, Redis shuts down and tasks are cancelled. Storage closes last so writes from disabling modules are flushed.

## The command router

Commands are declared once in `plugin.yml`. `CommandRouter` installs a gateway for each at startup, and modules mount their handlers onto it. A disabled module's command answers with the shared "module disabled" message instead of disappearing from Bukkit's command map, so tab completion and help stay stable.

## Module lifecycle

Each module instance lives as long as the plugin. Every `enable` gets a fresh `ModuleContext`:

```java
context.listen(listener);
context.command(command);
context.repeat(service::refresh, 0L, 100L);
context.onClose(service::flush);
```

On stop, the context releases everything in reverse order. The same cleanup runs if `enable` fails partway. Reloading closes the context, builds a new one and enables the same instance without changing the saved enabled flag.

## Player state and recovery

Anything that temporarily replaces a player's state writes it to disk first. Staff Mode journals the original inventory to `recovery/staffmode/` before handing out tools and restores it after a crash. Inventory restores are records with IDs, so a restore is never applied twice and tagged copies can be revoked. Recovery files go through `AtomicTextFile`.

## Threads and Folia

| Call | Runs on |
| --- | --- |
| `ModuleContext.repeat` / `later` | The global thread |
| `ModuleContext.laterFor` / `forPlayer` | The player's own thread |
| `ModuleContext.async` | Background, in order with storage writes |
| `Platform.forEachOnline` | Each online player's thread |
| `Platform.teleport` / `teleportResult` | `teleportAsync` on Folia |
| `Platform.runGlobal` | Console commands |

Replays teleports synchronously only on Bukkit/Paper, where it needs the result in the same tick. On Folia, shared state lives in concurrent collections or behind `synchronized` methods.

## Storage and configuration

`StorageEngine` hands modules a `DataStore` through `ModuleContext.getStorage()`. Writes queue on one background thread with retries. If the backend is down at startup, storage calls throw `StorageUnavailableException` and only modules that need storage fail to enable.

`YamlFile` loads each file with bundled defaults and reports version mismatches. Feature code reads YAML once in `enable`. Player-facing text lives in `lang/<locale>/<module>.yml`; `ConfigDefaultsTest` checks that Java fallbacks match the bundled English.

## Adding a module

1. Add a public interface under `api` only if other plugins need it, and expose it from `GlacierStaffApi`.
2. Create a shallow package under `runtime.modules` with a module facade and clearly named services, listeners, commands or menus.
3. Add an ID to `ModuleIds`, register one instance in `BuiltInModules` and add its default state to `modules.yml`.
4. Register Bukkit resources through `ModuleContext` and make cleanup idempotent.
5. Add `modules/<id>.yml` and `lang/en/<id>.yml` with a `schema-version`.
6. Declare commands and permissions in `plugin.yml` and add the permissions to `glacierstaff.admin`.
7. Test enable, disable and enable again before shipping.
