Описание
Grieflogger Rollback Addon
==========================
This NeoForge addon reads [GriefLogger](https://github.com/DAQEM/GriefLogger) data from SQLite (default) or MySQL/MariaDB and reverts block actions in-game via command. It targets NeoForge 21.1.213 / Minecraft 1.21.1 and is server-side only: any logged block placements or breaks can be undone selectively. Its like CoreProtect, but for Modded Server.
Features
--------
- Rollback of block changes and container/inventory changes from the GriefLogger database (`blocks` + `containers` tables with joins to `materials`, `users`, `levels`).
- Filters by time window, optional player name, and optional radius around the command executor.
- Ensures target chunks are loaded before changing blocks.
- Progress logging and batch processing per tick to keep the server responsive.
- Permission nodes for all commands and notifications (LuckPerms/permission mods supported, default op-level fallback 3).
- Broadcast notifications for rollbacks started in-game, rollbacks triggered via web UI/API, and blocked/unauthorized web access attempts.
- Automatically disables itself on startup if no database connection is available.
- Highly configurable via `griefloggerrollbackaddon-common.toml`: database backend/credentials, web UI/API bind/port/token, rollback batch size, progress interval, and unauthorized-access logging options.
Requirements
------------
- NeoForge 21.1.213 / Minecraft 1.21.1.
- A GriefLogger database (either the SQLite DB file GriefLogger writes, or a MySQL/MariaDB database with the GriefLogger schema).
- Database credentials with read access (for MySQL/MariaDB).
- Config file `config/grieflogger/griefloggerrollbackaddon-common.toml` generated by first server start (set DB type/connection, web options, and batch sizes).
- If the web UI/API is enabled: a free TCP port (default `8765`) on the bind address (`webApiBindAddress`, default `0.0.0.0`) and, if exposed beyond localhost, a configured token (`webApiToken`) and appropriate firewall/forwarding.
- Permission mod (e.g., LuckPerms) optional; without one, only operators (op-level 3) can run commands/receive notifications.
Installation
------------
1. Place the mod JAR and the JDBC driver for your backend (SQLite, MySQL, or MariaDB) into the server `mods/` folder.
2. Start the server once to generate `config/grieflogger/griefloggerrollbackaddon-common.toml`.
3. Choose the backend via `dbType` (case-insensitive, default `SQLITE`). For SQLite set `dbFile`; for MySQL/MariaDB set host/port/name/user/password.
4. Check the log for `[griefloggerrollbackaddon] Database connection succeeded`. If it fails, the addon remains disabled until the connection works.
Configuration (`config/grieflogger/griefloggerrollbackaddon-common.toml`)
------------------------------------------------------------------------
- `dbType` (Enum, default `SQLITE`): Choose `SQLITE`, `MYSQL`, or `MARIADB` (case-insensitive).
- `dbFile` (String, default `config/grieflogger/grieflogger.sqlite`): SQLite database file path when `dbType=SQLITE`.
- `dbHost` (String, default `localhost`): MySQL/MariaDB host.
- `dbPort` (Int, default `3306`): MySQL/MariaDB port.
- `dbName` (String, default `grieflogger`): Database name for MySQL/MariaDB.
- `dbUser` (String, default `root`): Database user for MySQL/MariaDB.
- `dbPassword` (String, default empty): Database password for MySQL/MariaDB.
- `rollbackBatchSize` (Int, default `200`): Number of actions processed per tick.
- `progressTickInterval` (Int, default `20`): How many ticks between progress log messages.
- `webApiEnabled` (Boolean, default `false`): Start a small HTTP server with a web UI to trigger rollbacks.
- `requireApiToken` (Boolean, default `false`): If true and `webApiToken` is empty, the web UI/API is disabled; otherwise every request must include the token.
- `webApiBindAddress` (String, default `0.0.0.0`): Bind address for the web UI/API.
- `webApiPort` (Int, default `8765`): Port for the web UI/API.
- `webApiToken` (String, default empty): Optional shared secret required for web requests. Leave empty only on localhost.
- `logUnauthorizedWebAccess.enabled` (Boolean, default `false`): Log unauthorized web requests to DB table `glra_web_unauthorized` (keeps the latest 1000 entries).
- `logUnauthorizedWebAccess.logHeaders` (Boolean, default `false`): Store request headers for unauthorized web requests.
- `logUnauthorizedWebAccess.logBody` (Boolean, default `false`): Store request body for unauthorized web requests (may include tokens).
- `logUnauthorizedWebAccess.logQuery` (Boolean, default `true`): Store query string for unauthorized web requests.
Command: `/gl rollback`
-----------------------
Syntax: `/gl rollback t: [u:] [r:] [i|b]`
- `t:` Required time window to roll back. Supported units: `s`, `m`, `h`, `d`, `M` (30 days), `y`. Examples: `t:30m`, `t:12h`, `t:90s`.
- `u:` Optional exact player name (matches the `users` table).
- `r:` Optional radius. Default uses blocks (`r:25`). Prefix `c` switches to chunks (`r:c4` = radius of 4 chunks). Prefix `b` forces blocks (`r:b40`).
- `i` Optional flag: only roll back inventory/container changes (items).
- `b` Optional flag: only roll back block changes. If neither `i` nor `b` is given, both are rolled back.
Examples
- `/gl rollback t:2h` - Roll back all actions from the last 2 hours.
- `/gl rollback t:1d u:Griefer123` - Only that player's actions in the last 24h.
- `/gl rollback t:30m r:c2` - Within 2 chunks around the executor.
- `/gl rollback t:10m u:User r:20` - Player filter plus 20-block radius.
- `/gl rollback t:45m i` - Only inventory/container changes from the last 45 minutes.
- `/gl rollback t:10m b r:15` - Only block changes within 15 blocks from the last 10 minutes.
Additional Commands
-------------------
- `/gl web token add ` — Create/replace a web API token for a player (copyable in chat).
- `/gl web token remove ` — Delete a player's web API token.
- `/gl web token list [page]` — List tokens with copy-to-clipboard hints.
- `/gl web start` / `/gl web stop` — Start/stop the built-in web UI/API (honors config toggles and token requirements).
- `/gl config reload` — Reload the addon config (`griefloggerrollbackaddon-common.toml`) without restarting.
Web UI / HTTP API
-----------------
- Enable in config with `webApiEnabled=true`. Defaults to `0.0.0.0:8765`; change `webApiBindAddress`/`webApiPort` as needed.
- Optional security: set `webApiToken` and pass it as header `X-Auth-Token` or form field `token`.
- Open `http://:/` for a minimal form: time window (e.g., `30m`), optional player, checkboxes for blocks/items, optional radius plus center X/Z/Y and dimension. The form POSTs to `/api/rollback`.
- The API endpoint `/api/rollback` accepts `application/x-www-form-urlencoded` with the same fields and returns a small JSON status.
- Unauthorized requests can be logged to the database (configurable) and will notify eligible players/ops with a readable message.
Permissions
-----------
- Default fallback: op-level 3 if no permission mod is present.
- Nodes (namespace `griefloggerrollbackaddon`):
- `command.rollback`
- `command.web.token`
- `command.web.server`
- `command.config.reload`
- `notify.rollback` (in-game rollback command)
- `notify.web.rollback` (web-triggered rollback)
- `notify.web.unauthorized` (blocked web requests)
Use your permission mod (e.g., LuckPerms) to grant/deny; otherwise only operators (level 3) can run commands and receive notifications.
How it works
------------
1. Server start: immediately tests a DB connection. On failure, the addon disables itself (no commands/events).
2. Command: `/gl rollback ...` starts a job:
- Loads matching entries from `blocks` and `containers` since the given time in a background thread, optionally filtered by player and/or radius.
- Reconstructs the previous block state per coordinate (`oldMaterialName`) so placements and breaks can be inverted correctly.
- Enqueues all actions (blocks + inventory operations) for processing, newest first so the latest change is undone first.
3. Server ticks: up to `rollbackBatchSize` actions are processed each tick:
- `BREAK` logs restore the broken block from the DB entry.
- `PLACE` logs restore the previous block state.
- Container logs remove items that were inserted and add back items (including stored NBT) that were taken. If a container is full, overflow is dropped at the container position.
- Unknown/other codes fall back to restoring the previous state (or air).
- The target chunk is loaded before setting the block.
4. Progress: every `progressTickInterval` ticks, the queue size and processed count are logged. When the queue is empty, the job finishes.
Notes and limitations
---------------------
- If a container has no space while items are restored, the overflow is dropped as item entities next to the container.
- Dimension mapping uses `levels.name` (ResourceLocation) or falls back for IDs 1/2/3 (Overworld/End/Nether).
- Invalid or unknown block names default to `minecraft:air` with a warning.
- The JDBC driver is not bundled in the mod JAR; it must be provided separately.
- Large time windows can produce big queues. Adjust `rollbackBatchSize` and radius to control server load.
Development/Building
--------------------
- Java 21, Gradle wrapper included. Build locally with `./gradlew build` (or `gradlew.bat build` on Windows).
- MariaDB, MySQL, and SQLite drivers are declared as `localRuntime`; provide the driver JAR separately for production.