From 2a6e71b91451e3c1a09080874681b420511ec69c Mon Sep 17 00:00:00 2001 From: jotty Date: Thu, 20 Aug 2020 22:18:26 +0200 Subject: [PATCH] keep backpack content by tags in compound --- .../container/BackpackInventory.java | 83 +++++++++++-------- .../container/BackpackScreen.java | 18 ++-- .../container/BackpackScreenHandler.java | 32 +------ .../quickiefabric/items/ItemBackpack.java | 4 +- 4 files changed, 67 insertions(+), 70 deletions(-) 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 87b8092..8d7190d 100644 --- a/src/main/java/de/jottyfan/minecraft/quickiefabric/container/BackpackInventory.java +++ b/src/main/java/de/jottyfan/minecraft/quickiefabric/container/BackpackInventory.java @@ -1,5 +1,8 @@ package de.jottyfan.minecraft.quickiefabric.container; +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; + import de.jottyfan.minecraft.quickiefabric.items.ItemBackpack; import net.minecraft.entity.player.PlayerEntity; import net.minecraft.inventory.Inventories; @@ -9,16 +12,23 @@ import net.minecraft.nbt.CompoundTag; import net.minecraft.nbt.ListTag; import net.minecraft.sound.SoundCategory; import net.minecraft.sound.SoundEvents; +import net.minecraft.util.Hand; import net.minecraft.util.collection.DefaultedList; +/** + * + * @author jotty + * + */ public class BackpackInventory implements Inventory { - public static final int SECTION_SIZE = 9; + private final static Logger LOGGER = LogManager.getLogger(BackpackInventory.class); private final DefaultedList stacks; private final BackpackScreenHandler handler; + private Hand hand; private BackpackInventory(CompoundTag tag, BackpackScreenHandler handler) { this.handler = handler; - this.stacks = DefaultedList.ofSize(SECTION_SIZE * ItemBackpack.SLOTSIZE, ItemStack.EMPTY); + this.stacks = DefaultedList.ofSize(ItemBackpack.SLOTSIZE, ItemStack.EMPTY); readItemsFromTag(this.stacks, tag); } @@ -35,35 +45,6 @@ public class BackpackInventory implements Inventory { return new BackpackInventory(stack.getTag().getCompound("backpack"), handler); } - private void readItemsFromTag(DefaultedList inventory, CompoundTag tag) { - ListTag listTag = tag.getList("items", 10); - for (int i = 0; i < listTag.size(); ++i) { - CompoundTag compoundTag = listTag.getCompound(i); - int j = compoundTag.getInt("slot"); - - if (j >= 0 && j < inventory.size()) { - inventory.set(j, ItemStack.fromTag(compoundTag)); - } - } - } - - private CompoundTag writeItemsToTag() { - DefaultedList inventory = stacks; - ListTag listTag = new ListTag(); - for (int i = 0; i < inventory.size(); ++i) { - ItemStack itemStack = (ItemStack) inventory.get(i); - if (!itemStack.isEmpty()) { - CompoundTag compoundTag = new CompoundTag(); - compoundTag.putInt("slot", i); - itemStack.toTag(compoundTag); - listTag.add(compoundTag); - } - } - CompoundTag tag = new CompoundTag(); - tag.put("items", listTag); - return tag; - } - public DefaultedList getList() { return stacks; } @@ -110,6 +91,7 @@ public class BackpackInventory implements Inventory { @Override public void setStack(int slot, ItemStack stack) { +// LOGGER.info("set slot {} item stack {}x {}", slot, stack.getCount(), stack.getItem().getName()); this.stacks.set(slot, stack); markDirty(); } @@ -121,7 +103,8 @@ public class BackpackInventory implements Inventory { @Override public void markDirty() { - handler.onContentChanged(this); +// LOGGER.info("dirty marked"); +// handler.onContentChanged(this); } @Override @@ -133,13 +116,47 @@ public class BackpackInventory implements Inventory { @Override public void onClose(PlayerEntity player) { Inventory.super.onClose(player); - ItemStack stack = handler.getStack(); + ItemStack stack = player.getStackInHand(hand); if (stack != null) { if (!stack.hasTag()) { stack.setTag(new CompoundTag()); } stack.getTag().put("backpack", writeItemsToTag()); } + player.getStackInHand(hand).setTag(stack.getTag()); player.playSound(SoundEvents.BLOCK_WOOL_PLACE, SoundCategory.PLAYERS, 1f, 1f); } + + private void readItemsFromTag(DefaultedList inventory, CompoundTag tag) { + ListTag listTag = tag.getList("items", 10); + for (int i = 0; i < listTag.size(); ++i) { + CompoundTag compoundTag = listTag.getCompound(i); + int j = compoundTag.getInt("slot"); + + if (j >= 0 && j < inventory.size()) { + inventory.set(j, ItemStack.fromTag(compoundTag)); + } + } + } + + private CompoundTag writeItemsToTag() { + DefaultedList inventory = stacks; + ListTag listTag = new ListTag(); + for (int i = 0; i < inventory.size(); ++i) { + ItemStack itemStack = (ItemStack) inventory.get(i); + if (!itemStack.isEmpty()) { + CompoundTag compoundTag = new CompoundTag(); + compoundTag.putInt("slot", i); + compoundTag = itemStack.toTag(compoundTag); + listTag.add(compoundTag); + } + } + CompoundTag tag = new CompoundTag(); + tag.put("items", listTag); + return tag; + } + + public void setHand(Hand hand) { + this.hand = hand; + } } diff --git a/src/main/java/de/jottyfan/minecraft/quickiefabric/container/BackpackScreen.java b/src/main/java/de/jottyfan/minecraft/quickiefabric/container/BackpackScreen.java index 97cdca4..78a7087 100644 --- a/src/main/java/de/jottyfan/minecraft/quickiefabric/container/BackpackScreen.java +++ b/src/main/java/de/jottyfan/minecraft/quickiefabric/container/BackpackScreen.java @@ -15,9 +15,12 @@ import net.minecraft.text.TranslatableText; import net.minecraft.util.Identifier; @Environment(EnvType.CLIENT) -public class BackpackScreen extends HandledScreen implements ScreenHandlerProvider { - private final static Identifier TEXTURE = new Identifier(RegistryManager.QUICKIEFABRIC, "textures/gui/backpack.png"); - private final static Identifier SLOT_TEXTURE = new Identifier(RegistryManager.QUICKIEFABRIC, "textures/gui/slot.png"); +public class BackpackScreen extends HandledScreen + implements ScreenHandlerProvider { + private final static Identifier TEXTURE = new Identifier(RegistryManager.QUICKIEFABRIC, + "textures/gui/backpack.png"); + private final static Identifier SLOT_TEXTURE = new Identifier(RegistryManager.QUICKIEFABRIC, + "textures/gui/slot.png"); private int slots; private final Integer containerHeight = 222; private final Integer containerWidth = 176; @@ -65,9 +68,8 @@ public class BackpackScreen extends HandledScreen impleme } @Override - protected void drawForeground(MatrixStack matrixStack, int i, int j) - { - this.textRenderer.draw(matrixStack, this.title, 8.0F, -20.0F, 4210752); - this.textRenderer.draw(matrixStack, this.playerInventory.getDisplayName(), 8.0F, 101f, 4210752); - } + protected void drawForeground(MatrixStack matrixStack, int i, int j) { + this.textRenderer.draw(matrixStack, this.title, 8.0F, -20.0F, 4210752); + this.textRenderer.draw(matrixStack, this.playerInventory.getDisplayName(), 8.0F, 101f, 4210752); + } } 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 49b1bfb..1ef0ad5 100644 --- a/src/main/java/de/jottyfan/minecraft/quickiefabric/container/BackpackScreenHandler.java +++ b/src/main/java/de/jottyfan/minecraft/quickiefabric/container/BackpackScreenHandler.java @@ -4,17 +4,17 @@ import de.jottyfan.minecraft.quickiefabric.init.RegistryManager; import de.jottyfan.minecraft.quickiefabric.items.ItemBackpack; import net.minecraft.entity.player.PlayerEntity; import net.minecraft.entity.player.PlayerInventory; -import net.minecraft.inventory.Inventory; import net.minecraft.item.ItemStack; import net.minecraft.network.PacketByteBuf; import net.minecraft.screen.ScreenHandler; import net.minecraft.screen.slot.Slot; -import net.minecraft.util.collection.DefaultedList; +import net.minecraft.util.Hand; public class BackpackScreenHandler extends ScreenHandler { private final BackpackInventory backpackInventory; private final PlayerEntity player; + private final Hand hand; private final ItemStack stack; // the stack of the backpack item private int backpackSlotSize; @@ -22,6 +22,7 @@ public class BackpackScreenHandler extends ScreenHandler { super(RegistryManager.BACKPACK_SCREEN_HANDLER, syncId); this.player = playerInventory.player; this.stack = buf.readItemStack(); + this.hand = buf.readByte() > 0 ? Hand.MAIN_HAND : Hand.OFF_HAND; this.backpackInventory = BackpackInventory.getInventory(this, player, stack); backpackSlotSize = ItemBackpack.SLOTSIZE; @@ -41,20 +42,6 @@ public class BackpackScreenHandler extends ScreenHandler { for (int x = 0; x < 9; ++x) { this.addSlot(new Slot(playerInventory, x, 8 + x * 18, 58 + spacing)); } - playerInventory.onOpen(player); - - DefaultedList backpackSlots = backpackInventory.getList(); - if (backpackSlots.size() == 0) { - return; - } - for (int x = 0; x < 9; x++) { - for (int y = 0; y < backpackSlotSize / 9; y++) { - this.getSlot(x + y * 9).setStack(backpackSlots.get(x + y * 9)); - } - } - for (int x = 0; x < (backpackSlotSize % 9); x++) { - this.getSlot(x + (backpackSlotSize / 9) * 9).setStack(backpackSlots.get(x + (backpackSlotSize / 9) * 9)); - } } public PlayerEntity getPlayer() { @@ -66,24 +53,13 @@ public class BackpackScreenHandler extends ScreenHandler { return true; } - @Override - public void onContentChanged(Inventory inv) { - super.onContentChanged(inv); - if (inv == this.backpackInventory) { - this.updateInv(); - } - } - @Override public void close(PlayerEntity player) { super.close(player); + this.backpackInventory.setHand(hand); this.backpackInventory.onClose(player); } - public void updateInv() { - this.sendContentUpdates(); - } - @Override public ItemStack transferSlot(PlayerEntity player, int slotNum) { ItemStack copy = ItemStack.EMPTY; diff --git a/src/main/java/de/jottyfan/minecraft/quickiefabric/items/ItemBackpack.java b/src/main/java/de/jottyfan/minecraft/quickiefabric/items/ItemBackpack.java index 7541a36..63921c6 100644 --- a/src/main/java/de/jottyfan/minecraft/quickiefabric/items/ItemBackpack.java +++ b/src/main/java/de/jottyfan/minecraft/quickiefabric/items/ItemBackpack.java @@ -30,7 +30,7 @@ public class ItemBackpack extends Item implements DyeableItem { public ItemBackpack() { super(new Item.Settings().group(RegistryManager.QUICKIEFABRIC_GROUP).maxCount(1)); } - + @Override public TypedActionResult use(World world, PlayerEntity player, Hand hand) { ItemStack itemStack = player.getStackInHand(hand); @@ -39,6 +39,7 @@ public class ItemBackpack extends Item implements DyeableItem { @Override public void writeScreenOpeningData(ServerPlayerEntity serverPlayerEntity, PacketByteBuf packetByteBuf) { packetByteBuf.writeItemStack(itemStack); + packetByteBuf.writeByte(hand == Hand.MAIN_HAND ? 1 : 0); } @Override @@ -50,6 +51,7 @@ public class ItemBackpack extends Item implements DyeableItem { public ScreenHandler createMenu(int syncId, PlayerInventory inv, PlayerEntity player) { PacketByteBuf buf = new PacketByteBuf(Unpooled.buffer()); buf.writeItemStack(itemStack); + buf.writeByte(hand == Hand.MAIN_HAND ? 1 : 0); return new BackpackScreenHandler(syncId, inv, buf); } });