package com.soravoid.sparkon.inventory.containers; import com.soravoid.sparkon.capabilities.CapabilityAligner; import net.minecraft.entity.player.PlayerEntity; import net.minecraft.entity.player.PlayerInventory; import net.minecraft.inventory.container.Container; import net.minecraft.inventory.container.Slot; import net.minecraft.item.ItemStack; import net.minecraftforge.items.IItemHandler; import net.minecraftforge.items.SlotItemHandler; public class AlignerContainer extends Container { private IItemHandler handler; private int ARMOR_START, ARMOR_END, INV_START, INV_END, HOTBAR_START, HOTBAR_END; public AlignerContainer(int windowId, PlayerInventory inv) { this(windowId, inv, inv.player); } public AlignerContainer(int windowId, PlayerInventory inv, PlayerEntity player) { super(SparkContainerType.ALIGNERCONTAINER, windowId); int i; IItemHandler handler = player.getCapability(CapabilityAligner.ALIGNER).orElse(null); this.addSlot(new SlotItemHandler(handler, 0, 80, 23)); this.handler = handler; ARMOR_START = handler.getSlots(); ARMOR_END = ARMOR_START+3; INV_START = ARMOR_END+1; INV_END = INV_START+26; HOTBAR_START = INV_END+1; HOTBAR_END = HOTBAR_START+8; for (i = 0; i < 3; ++i) { for (int j = 0; j < 9; ++j) { this.addSlot(new Slot(inv, j + i * 9 + 9, 8 + j * 18, 84 + i * 18)); } } for (i = 0; i < 9; ++i) { this.addSlot(new Slot(inv, i, 8 + i * 18, 142)); } } @Override public boolean canInteractWith(PlayerEntity playerIn) { return true; } @Override public ItemStack transferStackInSlot(PlayerEntity playerIn, int index) { ItemStack itemstack = null; Slot slot = this.inventorySlots.get(index); if (slot != null && slot.getHasStack()) { ItemStack itemstack1 = slot.getStack(); itemstack = itemstack1.copy(); if (index < INV_START) { if (!this.mergeItemStack(itemstack1, INV_START, 37, true)) { return ItemStack.EMPTY; } slot.onSlotChange(itemstack1, itemstack); } else { if(index == 0) { if (!this.mergeItemStack(itemstack1, INV_START, HOTBAR_END + 1, true)) { return ItemStack.EMPTY; } slot.onSlotChange(itemstack1, itemstack); } else if (slot.isItemValid(itemstack1)) { if (!this.mergeItemStack(itemstack1, 0, handler.getSlots(), false)) { return ItemStack.EMPTY; } } else if (index >= INV_START && index < HOTBAR_START) { if (!this.mergeItemStack(itemstack1, HOTBAR_START, HOTBAR_START + 1, false)) { return ItemStack.EMPTY; } } else if (index >= HOTBAR_START && index < HOTBAR_END + 1) { if (!this.mergeItemStack(itemstack1, INV_START, INV_END + 1, false)) { return ItemStack.EMPTY; } } } if (itemstack1.getCount() == 0) { slot.putStack(ItemStack.EMPTY); } else { slot.onSlotChanged(); } if (itemstack1.getCount() == itemstack.getCount()) { return ItemStack.EMPTY; } slot.onTake(playerIn, itemstack1); } return itemstack; } }