From f4550898a2ff53b8f043e905e05c3a0c3ffeb0d8 Mon Sep 17 00:00:00 2001 From: jotty Date: Fri, 21 Aug 2020 13:35:10 +0200 Subject: [PATCH] backpack further steps --- .../container/BackpackScreen.java | 10 +++--- .../container/BackpackScreenHandler.java | 36 ++++++++----------- 2 files changed, 18 insertions(+), 28 deletions(-) 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 78a7087..bd265bd 100644 --- a/src/main/java/de/jottyfan/minecraft/quickiefabric/container/BackpackScreen.java +++ b/src/main/java/de/jottyfan/minecraft/quickiefabric/container/BackpackScreen.java @@ -21,13 +21,11 @@ public class BackpackScreen extends HandledScreen "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; public BackpackScreen(BackpackScreenHandler handler, PlayerInventory inventory, Text text) { super(handler, inventory, new TranslatableText("container.quickiefabric.backpack")); - slots = ItemBackpack.SLOTSIZE; } @Override @@ -38,14 +36,14 @@ public class BackpackScreen extends HandledScreen private void drawSlots(MatrixStack matrices, int guiX, int guiY) { this.client.getTextureManager().bindTexture(SLOT_TEXTURE); - for (int y = 0; y < (slots / 9); y++) + for (int y = 0; y < (ItemBackpack.SLOTSIZE / 9); y++) for (int x = 0; x < 9; x++) { this.drawTexture(matrices, guiX + 7 + (x * 18), guiY + 17 + (y * 18), 0, 0, 18, 18); } - if ((slots % 9) != 0) - for (int x = 0; x < (slots % 9); x++) { - this.drawTexture(matrices, guiX + 7 + (x * 18), guiY + 17 + (slots / 9 * 18), 0, 0, 18, 18); + if ((ItemBackpack.SLOTSIZE % 9) != 0) + for (int x = 0; x < (ItemBackpack.SLOTSIZE % 9); x++) { + this.drawTexture(matrices, guiX + 7 + (x * 18), guiY + 17 + (ItemBackpack.SLOTSIZE / 9 * 18), 0, 0, 18, 18); } } 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 1ef0ad5..66231a2 100644 --- a/src/main/java/de/jottyfan/minecraft/quickiefabric/container/BackpackScreenHandler.java +++ b/src/main/java/de/jottyfan/minecraft/quickiefabric/container/BackpackScreenHandler.java @@ -1,7 +1,6 @@ package de.jottyfan.minecraft.quickiefabric.container; 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.item.ItemStack; @@ -15,16 +14,13 @@ 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; - public BackpackScreenHandler(int syncId, PlayerInventory playerInventory, PacketByteBuf buf) { + public BackpackScreenHandler(int syncId, PlayerInventory playerInventory, PacketByteBuf buf) { super(RegistryManager.BACKPACK_SCREEN_HANDLER, syncId); this.player = playerInventory.player; - this.stack = buf.readItemStack(); + ItemStack stack = buf.readItemStack(); this.hand = buf.readByte() > 0 ? Hand.MAIN_HAND : Hand.OFF_HAND; this.backpackInventory = BackpackInventory.getInventory(this, player, stack); - backpackSlotSize = ItemBackpack.SLOTSIZE; int spacing = 112; @@ -49,8 +45,8 @@ public class BackpackScreenHandler extends ScreenHandler { } @Override - public boolean canUse(PlayerEntity player) { - return true; + public boolean canUse(PlayerEntity playerEntity) { + return backpackInventory.canPlayerUse(playerEntity); } @Override @@ -63,27 +59,23 @@ public class BackpackScreenHandler extends ScreenHandler { @Override public ItemStack transferSlot(PlayerEntity player, int slotNum) { ItemStack copy = ItemStack.EMPTY; - Slot clickedSlot = this.slots.get(slotNum); - if (clickedSlot != null && clickedSlot.hasStack()) { - ItemStack clickedStack = clickedSlot.getStack(); - copy = clickedStack.copy(); - if (slotNum < backpackSlotSize) { - if (!this.insertItem(clickedStack, backpackSlotSize, this.slots.size(), true)) { + Slot chosenSlot = this.slots.get(slotNum); + if (chosenSlot != null && chosenSlot.hasStack()) { + ItemStack chosenStack = chosenSlot.getStack(); + copy = chosenStack.copy(); + if (slotNum < backpackInventory.size()) { + if (!this.insertItem(chosenStack, backpackInventory.size(), this.slots.size(), true)) { return ItemStack.EMPTY; } - } else if (!this.insertItem(clickedStack, 0, backpackSlotSize, false)) { + } else if (!this.insertItem(chosenStack, 0, backpackInventory.size(), false)) { return ItemStack.EMPTY; } - if (clickedStack.isEmpty()) { - clickedSlot.setStack(ItemStack.EMPTY); + if (chosenStack.isEmpty()) { + chosenSlot.setStack(ItemStack.EMPTY); } else { - clickedSlot.markDirty(); + chosenSlot.markDirty(); } } return copy; } - - public ItemStack getStack() { - return stack; - } }