Add ability to save user data on player death

feat/data-edit-commands
William 2 years ago
parent acd97a1cb0
commit 89d8b79ae3

@ -131,9 +131,16 @@ public class BukkitEventListener extends EventListener implements Listener {
@EventHandler(ignoreCancelled = true) @EventHandler(ignoreCancelled = true)
public void onPlayerDeath(PlayerDeathEvent event) { public void onPlayerDeath(PlayerDeathEvent event) {
if (cancelPlayerEvent(BukkitPlayer.adapt(event.getEntity()))) { final OnlineUser user = BukkitPlayer.adapt(event.getEntity());
// If the player is locked or the plugin disabling, clear their death drops and return
if (cancelPlayerEvent(user)) {
event.getDrops().clear(); event.getDrops().clear();
return;
} }
// Handle their death, e.g. if we need to save their inventory
super.handlePlayerDeath(user);
} }
} }

@ -26,6 +26,12 @@ public enum DataSaveCause {
* @since 2.0 * @since 2.0
*/ */
WORLD_SAVE, WORLD_SAVE,
/**
* Indicates data saved when the user died
*
* @since 2.1
*/
DEATH,
/** /**
* Indicates data saved when the server shut down * Indicates data saved when the server shut down
* *

@ -176,6 +176,20 @@ public abstract class EventListener {
userData -> plugin.getDatabase().setUserData(user, userData, DataSaveCause.WORLD_SAVE).join())); userData -> plugin.getDatabase().setUserData(user, userData, DataSaveCause.WORLD_SAVE).join()));
} }
/**
* Asynchronously handles a player death
*
* @param user The user who died
*/
protected void handlePlayerDeath(@NotNull OnlineUser user) {
if (disabling || !plugin.getSettings().saveOnDeath) {
return;
}
user.getUserData(plugin.getLoggingAdapter(), plugin.getSettings())
.thenAccept(data -> data.ifPresent(userData -> plugin.getDatabase()
.setUserData(user, userData, DataSaveCause.DEATH)));
}
/** /**
* Handle an inventory menu closing * Handle an inventory menu closing
* *

Loading…
Cancel
Save