From 2b91154ca26020994b26c58f42111155bcc04f15 Mon Sep 17 00:00:00 2001 From: William Date: Wed, 12 Oct 2022 01:45:46 +0100 Subject: [PATCH] Ensure players have their inventory contents saved on death --- .../husksync/data/BukkitInventoryMap.java | 2 ++ .../husksync/listener/BukkitEventListener.java | 15 ++++++++++++--- .../husksync/listener/EventListener.java | 9 ++++++--- 3 files changed, 20 insertions(+), 6 deletions(-) diff --git a/bukkit/src/main/java/net/william278/husksync/data/BukkitInventoryMap.java b/bukkit/src/main/java/net/william278/husksync/data/BukkitInventoryMap.java index 6233339b..05abb331 100644 --- a/bukkit/src/main/java/net/william278/husksync/data/BukkitInventoryMap.java +++ b/bukkit/src/main/java/net/william278/husksync/data/BukkitInventoryMap.java @@ -11,6 +11,8 @@ import java.util.Optional; @SuppressWarnings("unused") public class BukkitInventoryMap { + public static final int INVENTORY_SLOT_COUNT = 41; + private ItemStack[] contents; /** 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 c397a310..0d8ab53a 100644 --- a/bukkit/src/main/java/net/william278/husksync/listener/BukkitEventListener.java +++ b/bukkit/src/main/java/net/william278/husksync/listener/BukkitEventListener.java @@ -1,6 +1,7 @@ package net.william278.husksync.listener; import net.william278.husksync.BukkitHuskSync; +import net.william278.husksync.data.BukkitInventoryMap; import net.william278.husksync.data.BukkitSerializer; import net.william278.husksync.data.DataSerializationException; import net.william278.husksync.data.ItemData; @@ -25,6 +26,7 @@ import org.bukkit.event.player.PlayerInteractEvent; import org.bukkit.event.player.PlayerJoinEvent; import org.bukkit.event.player.PlayerQuitEvent; import org.bukkit.event.world.WorldSaveEvent; +import org.bukkit.inventory.ItemStack; import org.jetbrains.annotations.NotNull; import java.util.Arrays; @@ -52,7 +54,8 @@ 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).collect(Collectors.toList()))); + .map(BukkitPlayer::adapt) + .collect(Collectors.toList()))); } @EventHandler(ignoreCancelled = true) @@ -139,8 +142,14 @@ public class BukkitEventListener extends EventListener implements Listener { return; } - // Handle their death, e.g. if we need to save their inventory - super.handlePlayerDeath(user); + // 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 2f75c09b..b8ecaf6b 100644 --- a/common/src/main/java/net/william278/husksync/listener/EventListener.java +++ b/common/src/main/java/net/william278/husksync/listener/EventListener.java @@ -181,13 +181,16 @@ public abstract class EventListener { * * @param user The user who died */ - protected void handlePlayerDeath(@NotNull OnlineUser user) { + protected void handlePlayerDeath(@NotNull OnlineUser user, @NotNull ItemData drops) { if (disabling || !plugin.getSettings().saveOnDeath) { return; } + user.getUserData(plugin.getLoggingAdapter(), plugin.getSettings()) - .thenAccept(data -> data.ifPresent(userData -> plugin.getDatabase() - .setUserData(user, userData, DataSaveCause.DEATH))); + .thenAccept(data -> data.ifPresent(userData -> { + userData.getInventory().orElse(ItemData.empty()).serializedItems = drops.serializedItems; + plugin.getDatabase().setUserData(user, userData, DataSaveCause.DEATH); + })); } /**