backpack further steps

This commit is contained in:
Jörg Henke 2020-08-21 13:35:10 +02:00
parent 2a6e71b914
commit f4550898a2
2 changed files with 18 additions and 28 deletions

View File

@ -21,13 +21,11 @@ public class BackpackScreen extends HandledScreen<BackpackScreenHandler>
"textures/gui/backpack.png"); "textures/gui/backpack.png");
private final static Identifier SLOT_TEXTURE = new Identifier(RegistryManager.QUICKIEFABRIC, private final static Identifier SLOT_TEXTURE = new Identifier(RegistryManager.QUICKIEFABRIC,
"textures/gui/slot.png"); "textures/gui/slot.png");
private int slots;
private final Integer containerHeight = 222; private final Integer containerHeight = 222;
private final Integer containerWidth = 176; private final Integer containerWidth = 176;
public BackpackScreen(BackpackScreenHandler handler, PlayerInventory inventory, Text text) { public BackpackScreen(BackpackScreenHandler handler, PlayerInventory inventory, Text text) {
super(handler, inventory, new TranslatableText("container.quickiefabric.backpack")); super(handler, inventory, new TranslatableText("container.quickiefabric.backpack"));
slots = ItemBackpack.SLOTSIZE;
} }
@Override @Override
@ -38,14 +36,14 @@ public class BackpackScreen extends HandledScreen<BackpackScreenHandler>
private void drawSlots(MatrixStack matrices, int guiX, int guiY) { private void drawSlots(MatrixStack matrices, int guiX, int guiY) {
this.client.getTextureManager().bindTexture(SLOT_TEXTURE); 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++) { for (int x = 0; x < 9; x++) {
this.drawTexture(matrices, guiX + 7 + (x * 18), guiY + 17 + (y * 18), 0, 0, 18, 18); this.drawTexture(matrices, guiX + 7 + (x * 18), guiY + 17 + (y * 18), 0, 0, 18, 18);
} }
if ((slots % 9) != 0) if ((ItemBackpack.SLOTSIZE % 9) != 0)
for (int x = 0; x < (slots % 9); x++) { for (int x = 0; x < (ItemBackpack.SLOTSIZE % 9); x++) {
this.drawTexture(matrices, guiX + 7 + (x * 18), guiY + 17 + (slots / 9 * 18), 0, 0, 18, 18); this.drawTexture(matrices, guiX + 7 + (x * 18), guiY + 17 + (ItemBackpack.SLOTSIZE / 9 * 18), 0, 0, 18, 18);
} }
} }

View File

@ -1,7 +1,6 @@
package de.jottyfan.minecraft.quickiefabric.container; package de.jottyfan.minecraft.quickiefabric.container;
import de.jottyfan.minecraft.quickiefabric.init.RegistryManager; 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.PlayerEntity;
import net.minecraft.entity.player.PlayerInventory; import net.minecraft.entity.player.PlayerInventory;
import net.minecraft.item.ItemStack; import net.minecraft.item.ItemStack;
@ -15,16 +14,13 @@ public class BackpackScreenHandler extends ScreenHandler {
private final BackpackInventory backpackInventory; private final BackpackInventory backpackInventory;
private final PlayerEntity player; private final PlayerEntity player;
private final Hand hand; 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); super(RegistryManager.BACKPACK_SCREEN_HANDLER, syncId);
this.player = playerInventory.player; this.player = playerInventory.player;
this.stack = buf.readItemStack(); ItemStack stack = buf.readItemStack();
this.hand = buf.readByte() > 0 ? Hand.MAIN_HAND : Hand.OFF_HAND; this.hand = buf.readByte() > 0 ? Hand.MAIN_HAND : Hand.OFF_HAND;
this.backpackInventory = BackpackInventory.getInventory(this, player, stack); this.backpackInventory = BackpackInventory.getInventory(this, player, stack);
backpackSlotSize = ItemBackpack.SLOTSIZE;
int spacing = 112; int spacing = 112;
@ -49,8 +45,8 @@ public class BackpackScreenHandler extends ScreenHandler {
} }
@Override @Override
public boolean canUse(PlayerEntity player) { public boolean canUse(PlayerEntity playerEntity) {
return true; return backpackInventory.canPlayerUse(playerEntity);
} }
@Override @Override
@ -63,27 +59,23 @@ public class BackpackScreenHandler extends ScreenHandler {
@Override @Override
public ItemStack transferSlot(PlayerEntity player, int slotNum) { public ItemStack transferSlot(PlayerEntity player, int slotNum) {
ItemStack copy = ItemStack.EMPTY; ItemStack copy = ItemStack.EMPTY;
Slot clickedSlot = this.slots.get(slotNum); Slot chosenSlot = this.slots.get(slotNum);
if (clickedSlot != null && clickedSlot.hasStack()) { if (chosenSlot != null && chosenSlot.hasStack()) {
ItemStack clickedStack = clickedSlot.getStack(); ItemStack chosenStack = chosenSlot.getStack();
copy = clickedStack.copy(); copy = chosenStack.copy();
if (slotNum < backpackSlotSize) { if (slotNum < backpackInventory.size()) {
if (!this.insertItem(clickedStack, backpackSlotSize, this.slots.size(), true)) { if (!this.insertItem(chosenStack, backpackInventory.size(), this.slots.size(), true)) {
return ItemStack.EMPTY; return ItemStack.EMPTY;
} }
} else if (!this.insertItem(clickedStack, 0, backpackSlotSize, false)) { } else if (!this.insertItem(chosenStack, 0, backpackInventory.size(), false)) {
return ItemStack.EMPTY; return ItemStack.EMPTY;
} }
if (clickedStack.isEmpty()) { if (chosenStack.isEmpty()) {
clickedSlot.setStack(ItemStack.EMPTY); chosenSlot.setStack(ItemStack.EMPTY);
} else { } else {
clickedSlot.markDirty(); chosenSlot.markDirty();
} }
} }
return copy; return copy;
} }
public ItemStack getStack() {
return stack;
}
} }