diff --git a/bukkit/src/main/java/net/william278/husksync/listener/BukkitEventListener.java b/bukkit/src/main/java/net/william278/husksync/listener/BukkitEventListener.java index ea5e26b1..c397a310 100644 --- a/bukkit/src/main/java/net/william278/husksync/listener/BukkitEventListener.java +++ b/bukkit/src/main/java/net/william278/husksync/listener/BukkitEventListener.java @@ -131,9 +131,16 @@ public class BukkitEventListener extends EventListener implements Listener { @EventHandler(ignoreCancelled = true) 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(); + return; } + + // Handle their death, e.g. if we need to save their inventory + super.handlePlayerDeath(user); } } diff --git a/common/src/main/java/net/william278/husksync/data/DataSaveCause.java b/common/src/main/java/net/william278/husksync/data/DataSaveCause.java index 2a99fe82..86502db2 100644 --- a/common/src/main/java/net/william278/husksync/data/DataSaveCause.java +++ b/common/src/main/java/net/william278/husksync/data/DataSaveCause.java @@ -26,6 +26,12 @@ public enum DataSaveCause { * @since 2.0 */ WORLD_SAVE, + /** + * Indicates data saved when the user died + * + * @since 2.1 + */ + DEATH, /** * Indicates data saved when the server shut down * diff --git a/common/src/main/java/net/william278/husksync/listener/EventListener.java b/common/src/main/java/net/william278/husksync/listener/EventListener.java index 7ec6ff18..2f75c09b 100644 --- a/common/src/main/java/net/william278/husksync/listener/EventListener.java +++ b/common/src/main/java/net/william278/husksync/listener/EventListener.java @@ -176,6 +176,20 @@ public abstract class EventListener { 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 *