package net.kanawo.cemodern.gui; import net.kanawo.cemodern.tile.TileEMCBuilder; import net.minecraft.entity.player.EntityPlayer; import net.minecraft.entity.player.InventoryPlayer; import net.minecraft.inventory.IInventory; import net.minecraft.inventory.Slot; import net.minecraft.item.ItemStack; import buildcraft.builders.gui.ContainerBuilder; import buildcraft.core.lib.gui.widgets.ScrollbarWidget; /** * The Builder container plus an EMC consume slot. Extending {@link ContainerBuilder} keeps BuildCraft's requirement * tooltips and GUI watcher handling. */ public class ContainerEMCBuilder extends ContainerBuilder { /** Matches the slot frame at (132, 26) in emcbuilder.png. */ public static final int CONSUME_SLOT_X = 133; public static final int CONSUME_SLOT_Y = 27; private static final int PLAYER_SLOTS = 9 * 4; /** Index of the consume slot; the builder and player slots are all before it. */ private final int consumeSlot; public ContainerEMCBuilder(IInventory playerInventory, TileEMCBuilder builder) { super(playerInventory, builder); // A builder made from a blueprint has no owner yet; whoever opens it first takes it. if (!builder.getWorldObj().isRemote && playerInventory instanceof InventoryPlayer) { builder.claimOwner(((InventoryPlayer) playerInventory).player); } consumeSlot = inventorySlots.size(); addSlotToContainer(new SlotEMCConsume(builder, CONSUME_SLOT_X, CONSUME_SLOT_Y)); } public ScrollbarWidget getScrollbar() { return scrollbarWidget; } /** * Same behaviour as BuildCraftContainer, which assumes the player inventory is the last 36 slots; the consume slot * comes after it here. Shift-clicking never moves items into the consume slot. */ @Override public ItemStack transferStackInSlot(EntityPlayer player, int slotIndex) { if (slotIndex == consumeSlot) { return null; } Slot slot = (Slot) inventorySlots.get(slotIndex); if (slot == null || !slot.getHasStack()) { return null; } int playerStart = consumeSlot - PLAYER_SLOTS; int hotbarStart = consumeSlot - 9; ItemStack stack = slot.getStack(); ItemStack original = stack.copy(); if (slotIndex >= playerStart) { if (!shiftIntoBuilder(stack, playerStart)) { boolean fromHotbar = slotIndex >= hotbarStart; if (!shiftItemStack( stack, fromHotbar ? playerStart : hotbarStart, fromHotbar ? hotbarStart : consumeSlot)) { return null; } } } else if (!shiftItemStack(stack, playerStart, consumeSlot)) { return null; } slot.onSlotChange(stack, original); if (stack.stackSize <= 0) { slot.putStack(null); } else { slot.onSlotChanged(); } if (stack.stackSize == original.stackSize) { return null; } slot.onPickupFromSlot(player, stack); return original; } private boolean shiftIntoBuilder(ItemStack stack, int builderSlots) { for (int i = 0; i < builderSlots; i++) { Slot slot = (Slot) inventorySlots.get(i); if (slot.isItemValid(stack) && shiftItemStack(stack, i, i + 1)) { return true; } } return false; } }