Small improvements to offline player data handling.
This commit is contained in:
@@ -4,6 +4,7 @@ import com.google.common.base.Preconditions;
|
||||
import fr.pandacube.lib.paper.reflect.wrapper.craftbukkit.CraftItemStack;
|
||||
import fr.pandacube.lib.paper.reflect.wrapper.minecraft.nbt.CompoundTag;
|
||||
import fr.pandacube.lib.paper.reflect.wrapper.minecraft.nbt.ListTag;
|
||||
import fr.pandacube.lib.paper.reflect.wrapper.minecraft.nbt.Tag;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.Material;
|
||||
import org.bukkit.entity.HumanEntity;
|
||||
@@ -23,13 +24,10 @@ import java.util.function.IntUnaryOperator;
|
||||
|
||||
/**
|
||||
* A wrapper to easily manipulate vanilla player data.
|
||||
*
|
||||
* @param data The data as they are stored in the player file.
|
||||
*/
|
||||
public class PlayerDataWrapper {
|
||||
|
||||
/**
|
||||
* The data as they are stored in the player file.
|
||||
*/
|
||||
public final CompoundTag data;
|
||||
public record PlayerDataWrapper(CompoundTag data) {
|
||||
|
||||
/**
|
||||
* Creates a new wrapper for the provided player data.
|
||||
@@ -86,8 +84,6 @@ public class PlayerDataWrapper {
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
public Inventory getEnderChest() {
|
||||
return getBukkitInventory("EnderItems", InventoryType.ENDER_CHEST, IntUnaryOperator.identity());
|
||||
}
|
||||
@@ -97,7 +93,6 @@ public class PlayerDataWrapper {
|
||||
}
|
||||
|
||||
|
||||
|
||||
private Inventory getBukkitInventory(String nbtKey, InventoryType bukkitType, IntUnaryOperator nbtToBukkitSlotConverter) {
|
||||
Map<Integer, ItemStack> stacks = getRawInventoryContent(nbtKey);
|
||||
Inventory inv = Bukkit.createInventory(null, bukkitType);
|
||||
@@ -110,9 +105,9 @@ public class PlayerDataWrapper {
|
||||
}
|
||||
|
||||
private Map<Integer, ItemStack> getRawInventoryContent(String key) {
|
||||
if (!data.contains(key, 9)) // type 9 is list
|
||||
if (!data.contains(key, Tag.TAG_LIST()))
|
||||
return Map.of();
|
||||
ListTag list = data.getList(key, 10); // type of list element 10 is CompoundTag
|
||||
ListTag list = data.getList(key, Tag.TAG_COMPOUND());
|
||||
if (list == null)
|
||||
return Map.of();
|
||||
|
||||
@@ -129,9 +124,6 @@ public class PlayerDataWrapper {
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
private void setBukkitInventory(String nbtKey, Inventory inv, IntUnaryOperator bukkitToNBTSlotConverter) {
|
||||
Map<Integer, ItemStack> stacks = new TreeMap<>();
|
||||
if (inv == null) {
|
||||
@@ -168,12 +160,6 @@ public class PlayerDataWrapper {
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
private int getHeldItemSlot() {
|
||||
if (!data.contains("SelectedItemSlot"))
|
||||
return 0;
|
||||
@@ -185,9 +171,6 @@ public class PlayerDataWrapper {
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
public int getScore() {
|
||||
if (!data.contains("Score"))
|
||||
return 0;
|
||||
@@ -200,7 +183,6 @@ public class PlayerDataWrapper {
|
||||
}
|
||||
|
||||
|
||||
|
||||
public int getTotalExperience() {
|
||||
if (!data.contains("XpTotal"))
|
||||
return 0;
|
||||
@@ -217,12 +199,6 @@ public class PlayerDataWrapper {
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
private static class DummyPlayerInventory extends InventoryWrapper implements PlayerInventory {
|
||||
|
||||
private int heldItemSlot;
|
||||
|
Reference in New Issue
Block a user