repaired backpack storage
This commit is contained in:
parent
d1a237102b
commit
633484f22d
@ -5,6 +5,7 @@ import net.minecraft.entity.player.PlayerEntity;
|
|||||||
import net.minecraft.inventory.SimpleInventory;
|
import net.minecraft.inventory.SimpleInventory;
|
||||||
import net.minecraft.item.ItemStack;
|
import net.minecraft.item.ItemStack;
|
||||||
import net.minecraft.nbt.NbtCompound;
|
import net.minecraft.nbt.NbtCompound;
|
||||||
|
import net.minecraft.nbt.NbtElement;
|
||||||
import net.minecraft.nbt.NbtList;
|
import net.minecraft.nbt.NbtList;
|
||||||
import net.minecraft.sound.SoundCategory;
|
import net.minecraft.sound.SoundCategory;
|
||||||
import net.minecraft.sound.SoundEvents;
|
import net.minecraft.sound.SoundEvents;
|
||||||
@ -16,6 +17,10 @@ import net.minecraft.util.Hand;
|
|||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
public class BackpackInventory extends SimpleInventory {
|
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 Hand hand;
|
||||||
|
|
||||||
private BackpackInventory(NbtCompound tag, BackpackScreenHandler handler) {
|
private BackpackInventory(NbtCompound tag, BackpackScreenHandler handler) {
|
||||||
@ -23,22 +28,19 @@ public class BackpackInventory extends SimpleInventory {
|
|||||||
readItemsFromTag(super.size(), tag);
|
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,
|
public static final BackpackInventory getInventory(BackpackScreenHandler handler, PlayerEntity player,
|
||||||
ItemStack stack) {
|
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) {
|
private final static ItemStack init(ItemStack stack) {
|
||||||
if (stack != null) {
|
if (stack != null) {
|
||||||
if (!stack.hasNbt()) {
|
if (!stack.getOrCreateNbt().contains(NBT_BACKPACK)) {
|
||||||
stack.setNbt(new NbtCompound());
|
stack.getOrCreateNbt().put(NBT_BACKPACK, new NbtCompound());
|
||||||
}
|
|
||||||
if (!stack.getNbt().contains("backpack")) {
|
|
||||||
stack.getNbt().put("backpack", new NbtCompound());
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return stack;
|
return stack;
|
||||||
@ -55,43 +57,43 @@ public class BackpackInventory extends SimpleInventory {
|
|||||||
super.onClose(player);
|
super.onClose(player);
|
||||||
ItemStack stack = player.getStackInHand(hand);
|
ItemStack stack = player.getStackInHand(hand);
|
||||||
if (stack != null) {
|
if (stack != null) {
|
||||||
if (!stack.hasNbt()) {
|
stack.getOrCreateNbt().put(NBT_BACKPACK, writeItemsToTag(super.size()));
|
||||||
stack.setNbt(new NbtCompound());
|
|
||||||
}
|
|
||||||
stack.getNbt().put("backpack", writeItemsToTag());
|
|
||||||
}
|
}
|
||||||
player.getStackInHand(hand).setNbt(stack.getNbt());
|
player.getStackInHand(hand).setNbt(stack.getNbt());
|
||||||
player.playSound(SoundEvents.BLOCK_WOOL_PLACE, SoundCategory.PLAYERS, 1f, 1f);
|
player.playSound(SoundEvents.BLOCK_WOOL_PLACE, SoundCategory.PLAYERS, 1f, 1f);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void readItemsFromTag(Integer size, NbtCompound tag) {
|
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) {
|
for (int i = 0; i < listTag.size(); ++i) {
|
||||||
NbtCompound compoundTag = listTag.getCompound(i);
|
NbtCompound compoundTag = listTag.getCompound(i);
|
||||||
int slot = compoundTag.getInt("slot");
|
int slot = compoundTag.getInt(NBT_SLOT);
|
||||||
|
|
||||||
if (slot >= 0 && slot < size) {
|
if (slot >= 0 && slot < size) {
|
||||||
super.setStack(slot, ItemStack.fromNbt(compoundTag));
|
super.setStack(slot, ItemStack.fromNbt(compoundTag));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private NbtCompound writeItemsToTag() {
|
private NbtCompound writeItemsToTag(int slotsize) {
|
||||||
NbtList listTag = new NbtList();
|
NbtList listTag = new NbtList();
|
||||||
for (int i = 0; i < super.size(); ++i) {
|
for (int slot = 0; slot < slotsize; ++slot) {
|
||||||
ItemStack itemStack = (ItemStack) super.getStack(i);
|
ItemStack itemStack = super.getStack(slot);
|
||||||
if (!(itemStack == null) && !itemStack.isEmpty()) {
|
if (!(itemStack == null) && !itemStack.isEmpty()) {
|
||||||
NbtCompound compoundTag = new NbtCompound();
|
listTag.add(prepareCompoundTag(slot, itemStack));
|
||||||
compoundTag.putInt("slot", i);
|
|
||||||
itemStack.setNbt(compoundTag);
|
|
||||||
listTag.add(compoundTag);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
NbtCompound tag = new NbtCompound();
|
NbtCompound tag = new NbtCompound();
|
||||||
tag.put("items", listTag);
|
tag.put(NBT_ITEMS, listTag);
|
||||||
return tag;
|
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) {
|
public void setHand(Hand hand) {
|
||||||
this.hand = hand;
|
this.hand = hand;
|
||||||
}
|
}
|
||||||
|
@ -63,9 +63,9 @@ public class BackpackScreenHandler extends ScreenHandler {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void close(PlayerEntity player) {
|
public void close(PlayerEntity player) {
|
||||||
super.close(player);
|
|
||||||
this.backpackInventory.setHand(hand);
|
this.backpackInventory.setHand(hand);
|
||||||
this.backpackInventory.onClose(player);
|
this.backpackInventory.onClose(player);
|
||||||
|
super.close(player);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
Loading…
x
Reference in New Issue
Block a user