repaired backpack storage

This commit is contained in:
Jörg Henke 2021-08-05 21:51:01 +02:00
parent d1a237102b
commit 633484f22d
2 changed files with 30 additions and 28 deletions

View File

@ -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 static final BackpackInventory getInventory(BackpackScreenHandler handler, PlayerEntity player,
ItemStack stack) {
return new BackpackInventory(init(stack).getNbt().getCompound("backpack"), handler);
public BackpackInventory(ItemStack stack) {
this(init(stack).getOrCreateNbt().getCompound(NBT_BACKPACK), null);
}
public BackpackInventory(ItemStack stack) {
this(init(stack).getNbt().getCompound("backpack"), null);
public static final BackpackInventory getInventory(BackpackScreenHandler handler, PlayerEntity player,
ItemStack stack) {
return new BackpackInventory(init(stack).getOrCreateNbt().getCompound(NBT_BACKPACK), handler);
}
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;
}

View File

@ -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