Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.PlayerInventory;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.annotations.NotNull;

import java.util.Arrays;
import java.util.HashMap;
Expand Down Expand Up @@ -44,13 +44,13 @@ public SaveInventory(Player player, LogType logType, DamageCause deathCause, Str

public void snapshotAndSave(PlayerInventory mainInventory, Inventory enderChestInventory, boolean saveAsync) {
PlayerDataSnapshot snapshot = createSnapshot(mainInventory, enderChestInventory);
if (snapshot == null) return;
if (snapshot.isEmptySnapshot()) return;

save(snapshot, saveAsync);
}

public void save(PlayerDataSnapshot snapshot, boolean async) {
if (snapshot == null) return;
if (snapshot.isEmptySnapshot()) return;
UUID uuid = player.getUniqueId();

// Rate limiter
Expand Down Expand Up @@ -103,7 +103,7 @@ public void save(PlayerDataSnapshot snapshot, boolean async) {

}

public @Nullable PlayerDataSnapshot createSnapshot(PlayerInventory mainInventory, Inventory enderChestInventory) {
public @NotNull PlayerDataSnapshot createSnapshot(PlayerInventory mainInventory, Inventory enderChestInventory) {
ItemStack[] mainInvContents = null;
ItemStack[] mainInvArmor = null;
ItemStack[] enderInvContents = null;
Expand All @@ -125,7 +125,7 @@ public void save(PlayerDataSnapshot snapshot, boolean async) {

// Skip saving when inv is empty and config allows skipping empty invs
if (emptyInvAndArmor && !ConfigData.isSaveEmptyInventories()) {
return null;
return PlayerDataSnapshot.EMPTY_SNAPSHOT;
}

ItemStack[] enderContents = enderChestInventory.getContents();
Expand Down Expand Up @@ -203,6 +203,24 @@ public PlayerDataSnapshot(float totalXp, double health, int foodLevel, float sat
this.finalEnderInvContents = finalEnderInvContents;
}

private static final PlayerDataSnapshot EMPTY_SNAPSHOT = new PlayerDataSnapshot(
-1,
-1,
-1,
-1,
null,
-1,
-1,
-1,
null,
null,
null
);

public boolean isEmptySnapshot() {
return this == EMPTY_SNAPSHOT;
}

@Override
public boolean equals(Object obj) {
if (this == obj) return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -208,11 +208,13 @@ public void playerDeathHandle(PlayerDeathEvent event) {
if (preSnapshot == null) {
saveInventory.snapshotAndSave(player.getInventory(), player.getEnderChest(), true);
} else {
// Remove the snapshot from the cache
this.inventoryCache.remove(uuid);
// Ensure we don't save the empty snapshot
if (preSnapshot.isEmptySnapshot()) return;
// Save the snapshot inventory instead of the current one. We apparently had an edit
// during the damage event.
saveInventory.save(preSnapshot, true);
// Remove the snapshot from the cache
this.inventoryCache.remove(uuid);
}
}
}
Expand Down