You are an expert Minecraft plugin developer specializing in Paper/Spigot 1.21+ plugins using Java 21.
Create a complete, production-ready Minecraft plugin called **EpicData** with the following full specification:
---
## PLUGIN OVERVIEW
- Name: EpicData
- Target: Paper API 1.21+
- Java: 21 (use records, sealed classes, text blocks where appropriate)
- Build: Maven with maven-shade-plugin (shade MySQL connector)
- Must be lag-free: all database operations must be ASYNC (only Bukkit API calls on main thread)
---
## FEATURES TO IMPLEMENT
### 1. MySQL Database Storage
- Connect to MySQL using config.yml credentials
- Auto-reconnect on disconnect
- Create these tables on startup:
**epicdata_players** — stores per-player stats:
- uuid (VARCHAR 36, PRIMARY KEY)
- username (VARCHAR 16)
- first_join (BIGINT)
- last_seen (BIGINT)
- playtime_seconds (BIGINT)
- last_death_time (BIGINT)
- last_death_location (TEXT) — format: "world,x,y,z"
- last_death_cause (VARCHAR 64)
- total_deaths (INT)
- last_location (TEXT)
- health (DOUBLE)
- food_level (INT)
- experience (FLOAT)
- exp_level (INT)
- game_mode (VARCHAR 16)
- ip_address (VARCHAR 45)
**epicdata_inventories** — inventory snapshots:
- id (INT AUTO_INCREMENT PRIMARY KEY)
- uuid (VARCHAR 36)
- snapshot_time (BIGINT)
- snapshot_type (VARCHAR 16) — values: JOIN, QUIT, DEATH, MANUAL
- inventory_data (LONGTEXT) — Base64 serialized ItemStack array
- armor_data (TEXT)
- offhand_data (TEXT)
- Keep only the last 15 snapshots per player (delete older ones after insert)
### 2. Player Data Lifecycle
- On JOIN: load player data from DB into memory cache (async), then save a JOIN inventory snapshot
- On QUIT: save QUIT inventory snapshot, save all player stats to DB, remove from cache after 3 seconds
- On DEATH: save DEATH inventory snapshot BEFORE items drop, update death count/location/cause/time
- Playtime tracking: track session start time on join, add elapsed seconds on quit/save
- Auto-save all online players every 5 minutes (configurable)
### 3. Inventory Serialization
- Use BukkitObjectOutputStream + Base64Coder to serialize/deserialize ItemStack arrays
- Serialize: main inventory (36 slots), armor (4 slots), offhand (1 slot) separately
- Handle null items gracefully
### 4. Built-in HTTP API Server
- Use com.sun.net.httpserver.HttpServer on a configurable port (default: 7890)
- Use 4 threads via Executors.newFixedThreadPool(4)
- All responses: Content-Type application/json + CORS headers (Access-Control-Allow-Origin: *)
- Handle OPTIONS preflight requests (return 204)
- Auth: X-API-Key header must match config value, return 401 JSON if missing/wrong
- Endpoints:
GET /api/online
→ Returns: { online: int, max: int, players: [{ uuid, username, health, food, gamemode, world, x, y, z, playtime, deaths }] }
→ Live data from online players directly
GET /api/players
→ Returns array of last 100 players from DB ordered by last_seen DESC
→ Fields: uuid, username, last_seen, playtime_seconds, total_deaths, online (bool)
GET /api/player?uuid=XXX or ?name=XXX
→ Returns full player data including snapshots array (id, time, type only — not the full base64 data)
→ If player is online, return live health/food/location/gamemode
→ snapshot_count field
POST /api/restore
→ Body: { "uuid": "...", "snapshot_id": "123" }
→ Player must be online
→ Restore inventory/armor/offhand on main thread using scheduler
→ Send player a message: "§a[EpicData] §fYour inventory has been restored by an admin."
→ Return: { success: true } or { success: false, error: "..." }
GET /api/stats
→ Returns: { total_players, online_players, total_playtime_seconds, total_deaths }
- Parse JSON body manually (no external JSON library) using simple string extraction
- Parse query parameters manually
### 5. Admin Commands
Command: /epicdata (alias: /ed)
Permission: epicdata.admin (default: op)
Tab completion for all subcommands and online player names
Subcommands:
- /epicdata info — show formatted stats in chat (uuid, playtime, deaths, health, food, level, location, last death)
- /epicdata restore — restore inventory (player must be online)
- /epicdata save [player] — force save one or all online players + take MANUAL snapshot
- /epicdata reload — reload config.yml (note: DB/API changes need restart)
- /epicdata help — show formatted help
Use §a/§7/§f/§c colors. Use decorative separator lines with §8§m.
### 6. config.yml (with saveDefaultConfig)
```yaml
mysql:
host: localhost
port: 3306
database: epicdata
username: root
password: YourPasswordHere
api:
port: 7890
key: "CHANGE_THIS_TO_A_RANDOM_SECRET_KEY"
settings:
max_snapshots: 15
auto_save_minutes: 5
```
### 7. plugin.yml
```yaml
name: EpicData
version: '1.0.0'
main: dev.epicdata.EpicData
api-version: '1.21'
description: Advanced SMP player data tracking with inventory backup and web API
author: YourName
commands:
epicdata:
description: EpicData admin commands
usage: /epicdata
permission: epicdata.admin
aliases: [ed]
permissions:
epicdata.admin:
description: Full access to EpicData
default: op
```
---
## CODE STRUCTURE
Organize into these packages:
- dev.epicdata — Main class (EpicData.java)
- dev.epicdata.managers — DatabaseManager.java, PlayerDataManager.java
- dev.epicdata.listeners — PlayerListener.java
- dev.epicdata.api — HTTPServer.java
- dev.epicdata.models — PlayerData.java
- dev.epicdata.commands — EpicDataCommand.java
---
## IMPORTANT RULES
- NEVER do database/file I/O on the main thread — use getServer().getScheduler().runTaskAsynchronously()
- ALWAYS use getServer().getScheduler().runTask() to call Bukkit API from async context
- Handle all SQLExceptions with proper logging (plugin.getLogger().severe())
- Auto-reconnect database if connection drops (check isClosed() before queries)
- Use try-with-resources for all PreparedStatements and ResultSets
- Plugin must enable/disable cleanly (save all data on disable, stop HTTP server, close DB)
- Use ConcurrentHashMap for the player cache (thread-safe)
- Shade MySQL connector under dev.epicdata.libs.mysql to avoid conflicts
---
## DELIVERABLES
Output the complete source code for every file listed in the package structure above, plus pom.xml. Each file should be complete and compilable. Do not skip or truncate any file.