diff --git a/src/main/java/de/jottyfan/minecraft/quickiefabric/container/BackpackInventory.java b/src/main/java/de/jottyfan/minecraft/quickiefabric/container/BackpackInventory.java index b8d51fb..6d7a56f 100644 --- a/src/main/java/de/jottyfan/minecraft/quickiefabric/container/BackpackInventory.java +++ b/src/main/java/de/jottyfan/minecraft/quickiefabric/container/BackpackInventory.java @@ -5,6 +5,7 @@ import net.minecraft.entity.player.PlayerEntity; import net.minecraft.inventory.SimpleInventory; import net.minecraft.item.ItemStack; import net.minecraft.nbt.NbtCompound; +import net.minecraft.nbt.NbtElement; import net.minecraft.nbt.NbtList; import net.minecraft.sound.SoundCategory; import net.minecraft.sound.SoundEvents; @@ -16,6 +17,10 @@ import net.minecraft.util.Hand; * */ public class BackpackInventory extends SimpleInventory { + private static final String NBT_BACKPACK = "backpack"; + private static final String NBT_SLOT = "slot"; + private static final String NBT_ITEMS = "items"; + private Hand hand; private BackpackInventory(NbtCompound tag, BackpackScreenHandler handler) { @@ -23,22 +28,19 @@ public class BackpackInventory extends SimpleInventory { readItemsFromTag(super.size(), tag); } + public BackpackInventory(ItemStack stack) { + this(init(stack).getOrCreateNbt().getCompound(NBT_BACKPACK), null); + } + public static final BackpackInventory getInventory(BackpackScreenHandler handler, PlayerEntity player, ItemStack stack) { - return new BackpackInventory(init(stack).getNbt().getCompound("backpack"), handler); + return new BackpackInventory(init(stack).getOrCreateNbt().getCompound(NBT_BACKPACK), handler); } - - public BackpackInventory(ItemStack stack) { - this(init(stack).getNbt().getCompound("backpack"), null); - } - + private final static ItemStack init(ItemStack stack) { if (stack != null) { - if (!stack.hasNbt()) { - stack.setNbt(new NbtCompound()); - } - if (!stack.getNbt().contains("backpack")) { - stack.getNbt().put("backpack", new NbtCompound()); + if (!stack.getOrCreateNbt().contains(NBT_BACKPACK)) { + stack.getOrCreateNbt().put(NBT_BACKPACK, new NbtCompound()); } } return stack; @@ -55,43 +57,43 @@ public class BackpackInventory extends SimpleInventory { super.onClose(player); ItemStack stack = player.getStackInHand(hand); if (stack != null) { - if (!stack.hasNbt()) { - stack.setNbt(new NbtCompound()); - } - stack.getNbt().put("backpack", writeItemsToTag()); + stack.getOrCreateNbt().put(NBT_BACKPACK, writeItemsToTag(super.size())); } player.getStackInHand(hand).setNbt(stack.getNbt()); player.playSound(SoundEvents.BLOCK_WOOL_PLACE, SoundCategory.PLAYERS, 1f, 1f); } private void readItemsFromTag(Integer size, NbtCompound tag) { - NbtList listTag = tag.getList("items", 10); + NbtList listTag = tag.getList(NBT_ITEMS, NbtElement.COMPOUND_TYPE); for (int i = 0; i < listTag.size(); ++i) { NbtCompound compoundTag = listTag.getCompound(i); - int slot = compoundTag.getInt("slot"); - + int slot = compoundTag.getInt(NBT_SLOT); if (slot >= 0 && slot < size) { super.setStack(slot, ItemStack.fromNbt(compoundTag)); } } } - private NbtCompound writeItemsToTag() { + private NbtCompound writeItemsToTag(int slotsize) { NbtList listTag = new NbtList(); - for (int i = 0; i < super.size(); ++i) { - ItemStack itemStack = (ItemStack) super.getStack(i); + for (int slot = 0; slot < slotsize; ++slot) { + ItemStack itemStack = super.getStack(slot); if (!(itemStack == null) && !itemStack.isEmpty()) { - NbtCompound compoundTag = new NbtCompound(); - compoundTag.putInt("slot", i); - itemStack.setNbt(compoundTag); - listTag.add(compoundTag); + listTag.add(prepareCompoundTag(slot, itemStack)); } } NbtCompound tag = new NbtCompound(); - tag.put("items", listTag); + tag.put(NBT_ITEMS, listTag); return tag; } - + + private NbtCompound prepareCompoundTag(Integer slot, ItemStack stack) { + NbtCompound compoundTag = new NbtCompound(); + compoundTag.putInt(NBT_SLOT, slot); + stack.writeNbt(compoundTag); + return compoundTag; + } + public void setHand(Hand hand) { this.hand = hand; } diff --git a/src/main/java/de/jottyfan/minecraft/quickiefabric/container/BackpackScreenHandler.java b/src/main/java/de/jottyfan/minecraft/quickiefabric/container/BackpackScreenHandler.java index 29ebddf..6a381a1 100644 --- a/src/main/java/de/jottyfan/minecraft/quickiefabric/container/BackpackScreenHandler.java +++ b/src/main/java/de/jottyfan/minecraft/quickiefabric/container/BackpackScreenHandler.java @@ -63,9 +63,9 @@ public class BackpackScreenHandler extends ScreenHandler { @Override public void close(PlayerEntity player) { - super.close(player); this.backpackInventory.setHand(hand); this.backpackInventory.onClose(player); + super.close(player); } @Override