Act as an expert Minecraft Spigot/Paper Plugin Developer. Fix the data persistence and rejoin bug in my custom PerWorldLifesteal plugin for Paper 1.20+.
Bug Description:
When players leave and rejoin the server while inside a Whitelisted Lifesteal world, their max health resets back to default 20.0 HP (10 Hearts) instead of restoring their saved hearts from data.yml.
Requirements & Fixes to Implement:
- Data Management (data.yml):
- Use UUIDs as keys (players.<UUID>) in data.yml to store max health values (e.g., 30.0 for 15 hearts). - Ensure a robust saveData() method that writes to disk immediately whenever health changes, on PlayerQuit, and on Server Shutdown (onDisable).
- Delayed Health Restoration on Join (CRITICAL FIX):
- In PlayerJoinEvent, do NOT set health immediately on the main thread frame. - Use Bukkit.getScheduler().runTaskLater(plugin, () -> { ... }, 1L) (1-tick delay) to allow Paper/Spigot to fully process the player join and world loading. - Inside the delayed task, check if the player's current world is in enabled-worlds from config.yml. - If YES: Fetch saved HP from data.yml (default 20.0 if new) and set Attribute.GENERIC_MAX_HEALTH base value. - If NO: Set Attribute.GENERIC_MAX_HEALTH base value to 20.0 HP (10 Hearts).
- Instant Save on Quit & World Change:
- In PlayerQuitEvent: If the player is leaving from a Whitelisted Lifesteal world, save their current Attribute.GENERIC_MAX_HEALTH base value to data.yml and call saveData() immediately. - In PlayerChangedWorldEvent: * When moving FROM Lifesteal world TO Non-Lifesteal world: Save current Lifesteal HP to data.yml, call saveData(), then set current max health to 20.0 HP. * When moving FROM Non-Lifesteal world TO Lifesteal world: Load saved HP from data.yml and set max health.
- Lifesteal Combat Mechanics:
- Handle PlayerDeathEvent: When a player is killed by another player inside a whitelisted world, deduct 2.0 HP (1 heart) from the victim and add 2.0 HP (1 heart) to the killer (respecting min-hearts and max-hearts from config.yml). - Immediately update both players' values in data.yml and call saveData().
Output Deliverables:
Provide the complete, fully written Java source code for all classes (PerWorldLifestealPlugin, LifestealListener, LifestealManager, HealthDataStore), along with full imports and no pseudocode or placeholders, so it can be re-compiled straight into a working .jar file.