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")
public class BukkitInventoryMap {
public static final int INVENTORY_SLOT_COUNT = 41;
private ItemStack[] contents;
/**

@ -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)));
}
}

@ -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);
}));
}
/**

Loading…
Cancel
Save