Ensure players have their inventory contents saved on death

feat/data-edit-commands
William 2 years ago
parent 2351be31e3
commit 2b91154ca2

@ -11,6 +11,8 @@ import java.util.Optional;
@SuppressWarnings("unused") @SuppressWarnings("unused")
public class BukkitInventoryMap { public class BukkitInventoryMap {
public static final int INVENTORY_SLOT_COUNT = 41;
private ItemStack[] contents; private ItemStack[] contents;
/** /**

@ -1,6 +1,7 @@
package net.william278.husksync.listener; package net.william278.husksync.listener;
import net.william278.husksync.BukkitHuskSync; import net.william278.husksync.BukkitHuskSync;
import net.william278.husksync.data.BukkitInventoryMap;
import net.william278.husksync.data.BukkitSerializer; import net.william278.husksync.data.BukkitSerializer;
import net.william278.husksync.data.DataSerializationException; import net.william278.husksync.data.DataSerializationException;
import net.william278.husksync.data.ItemData; 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.PlayerJoinEvent;
import org.bukkit.event.player.PlayerQuitEvent; import org.bukkit.event.player.PlayerQuitEvent;
import org.bukkit.event.world.WorldSaveEvent; import org.bukkit.event.world.WorldSaveEvent;
import org.bukkit.inventory.ItemStack;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import java.util.Arrays; import java.util.Arrays;
@ -52,7 +54,8 @@ public class BukkitEventListener extends EventListener implements Listener {
@EventHandler(ignoreCancelled = true) @EventHandler(ignoreCancelled = true)
public void onWorldSave(@NotNull WorldSaveEvent event) { public void onWorldSave(@NotNull WorldSaveEvent event) {
CompletableFuture.runAsync(() -> super.handleAsyncWorldSave(event.getWorld().getPlayers().stream() CompletableFuture.runAsync(() -> super.handleAsyncWorldSave(event.getWorld().getPlayers().stream()
.map(BukkitPlayer::adapt).collect(Collectors.toList()))); .map(BukkitPlayer::adapt)
.collect(Collectors.toList())));
} }
@EventHandler(ignoreCancelled = true) @EventHandler(ignoreCancelled = true)
@ -139,8 +142,14 @@ public class BukkitEventListener extends EventListener implements Listener {
return; return;
} }
// Handle their death, e.g. if we need to save their inventory // Truncate the drops list to the maximum allowed (44)
super.handlePlayerDeath(user); 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)));
} }
} }

@ -181,13 +181,16 @@ public abstract class EventListener {
* *
* @param user The user who died * @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) { if (disabling || !plugin.getSettings().saveOnDeath) {
return; return;
} }
user.getUserData(plugin.getLoggingAdapter(), plugin.getSettings()) user.getUserData(plugin.getLoggingAdapter(), plugin.getSettings())
.thenAccept(data -> data.ifPresent(userData -> plugin.getDatabase() .thenAccept(data -> data.ifPresent(userData -> {
.setUserData(user, userData, DataSaveCause.DEATH))); userData.getInventory().orElse(ItemData.empty()).serializedItems = drops.serializedItems;
plugin.getDatabase().setUserData(user, userData, DataSaveCause.DEATH);
}));
} }
/** /**

Loading…
Cancel
Save