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 0d8ab53a..27623cb0 100644 --- a/bukkit/src/main/java/net/william278/husksync/listener/BukkitEventListener.java +++ b/bukkit/src/main/java/net/william278/husksync/listener/BukkitEventListener.java @@ -53,8 +53,11 @@ public class BukkitEventListener extends EventListener implements Listener { @EventHandler(ignoreCancelled = true) public void onWorldSave(@NotNull WorldSaveEvent event) { - CompletableFuture.runAsync(() -> super.handleAsyncWorldSave(event.getWorld().getPlayers().stream() - .map(BukkitPlayer::adapt) + // Handle saving player data snapshots when the world saves + if (!plugin.getSettings().saveOnWorldSave) return; + + CompletableFuture.runAsync(() -> super.saveOnWorldSave(event.getWorld().getPlayers() + .stream().map(BukkitPlayer::adapt) .collect(Collectors.toList()))); } @@ -79,6 +82,29 @@ public class BukkitEventListener extends EventListener implements Listener { }); } + @EventHandler(ignoreCancelled = true) + public void onPlayerDeath(PlayerDeathEvent event) { + final OnlineUser user = BukkitPlayer.adapt(event.getEntity()); + + // If the player is locked or the plugin disabling, clear their drops + if (cancelPlayerEvent(user)) { + event.getDrops().clear(); + return; + } + + // Handle saving player data snapshots on death + if (!plugin.getSettings().saveOnDeath) return; + + // Truncate the drops list to the inventory size and save the player's inventory + final int maxInventorySize = BukkitInventoryMap.INVENTORY_SLOT_COUNT; + if (event.getDrops().size() > maxInventorySize) { + event.getDrops().subList(maxInventorySize, event.getDrops().size()).clear(); + } + BukkitSerializer.serializeItemStackArray(event.getDrops().toArray(new ItemStack[0])) + .thenAccept(serializedDrops -> super.saveOnPlayerDeath(user, new ItemData(serializedDrops))); + } + + /* * Events to cancel if the player has not been set yet */ @@ -132,24 +158,4 @@ public class BukkitEventListener extends EventListener implements Listener { } } - @EventHandler(ignoreCancelled = true) - public void onPlayerDeath(PlayerDeathEvent event) { - 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; - } - - // Truncate the drops list to the maximum allowed (44) - if (event.getDrops().size() > BukkitInventoryMap.INVENTORY_SLOT_COUNT) { - event.getDrops().subList(BukkitInventoryMap.INVENTORY_SLOT_COUNT, event.getDrops().size()).clear(); - } - - // Convert the death drops to a serialized item stack array - BukkitSerializer.serializeItemStackArray(event.getDrops().toArray(new ItemStack[0])) - .thenAccept(serializedDrops -> super.handlePlayerDeath(user, new ItemData(serializedDrops))); - } - } 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 b8ecaf6b..751338e0 100644 --- a/common/src/main/java/net/william278/husksync/listener/EventListener.java +++ b/common/src/main/java/net/william278/husksync/listener/EventListener.java @@ -164,11 +164,11 @@ public abstract class EventListener { } /** - * Asynchronously handles a world save event + * Handles the saving of data when the world save event is fired * * @param usersInWorld a list of users in the world that is being saved */ - protected final void handleAsyncWorldSave(@NotNull List usersInWorld) { + protected final void saveOnWorldSave(@NotNull List usersInWorld) { if (disabling || !plugin.getSettings().saveOnWorldSave) { return; } @@ -177,11 +177,12 @@ public abstract class EventListener { } /** - * Asynchronously handles a player death + * Handles the saving of data when a player dies * - * @param user The user who died + * @param user The user who died + * @param drops The items that this user would have dropped */ - protected void handlePlayerDeath(@NotNull OnlineUser user, @NotNull ItemData drops) { + protected void saveOnPlayerDeath(@NotNull OnlineUser user, @NotNull ItemData drops) { if (disabling || !plugin.getSettings().saveOnDeath) { return; }