Package pneumaticCraft.common.inventory

Source Code of pneumaticCraft.common.inventory.ContainerProgrammer

package pneumaticCraft.common.inventory;

import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.entity.player.InventoryPlayer;
import net.minecraft.inventory.Container;
import net.minecraft.inventory.IInventory;
import net.minecraft.inventory.Slot;
import net.minecraft.item.ItemStack;
import pneumaticCraft.api.item.IProgrammable;
import pneumaticCraft.common.tileentity.TileEntityBase;

public class ContainerProgrammer extends Container{
    IInventory te;

    public ContainerProgrammer(InventoryPlayer inventoryPlayer, IInventory te){
        this.te = te;
        ((TileEntityBase)te).openGUI();
        // add the upgrade slots
        addSlotToContainer(new Slot(te, 0, 152, 15){
            @Override
            public boolean isItemValid(ItemStack stack){
                return isProgrammableItem(stack);
            }
        });

        // Add the player's inventory slots to the container
        for(int inventoryRowIndex = 0; inventoryRowIndex < 3; ++inventoryRowIndex) {
            for(int inventoryColumnIndex = 0; inventoryColumnIndex < 9; ++inventoryColumnIndex) {
                addSlotToContainer(new Slot(inventoryPlayer, inventoryColumnIndex + inventoryRowIndex * 9 + 9, 8 + inventoryColumnIndex * 18, 174 + inventoryRowIndex * 18));
            }
        }

        // Add the player's action bar slots to the container
        for(int actionBarSlotIndex = 0; actionBarSlotIndex < 9; ++actionBarSlotIndex) {
            addSlotToContainer(new Slot(inventoryPlayer, actionBarSlotIndex, 8 + actionBarSlotIndex * 18, 232));
        }
    }

    private static boolean isProgrammableItem(ItemStack stack){
        if(stack == null) return false;
        return stack.getItem() instanceof IProgrammable && ((IProgrammable)stack.getItem()).canProgram(stack);
    }

    @Override
    public boolean canInteractWith(EntityPlayer entityplayer){
        return true;//TODO do properly
    }

    @Override
    public ItemStack transferStackInSlot(EntityPlayer par1EntityPlayer, int par2){

        ItemStack var3 = null;
        Slot var4 = (Slot)inventorySlots.get(par2);

        if(var4 != null && var4.getHasStack()) {
            ItemStack var5 = var4.getStack();
            var3 = var5.copy();

            if(par2 == 0) {
                if(!mergeItemStack(var5, 1, 36, false)) return null;
                var4.onSlotChange(var5, var3);
            } else if(isProgrammableItem(var3)) {
                if(!mergeItemStack(var5, 0, 1, false)) return null;
                var4.onSlotChange(var5, var3);
            }
            if(var5.stackSize == 0) {
                var4.putStack((ItemStack)null);
            } else {
                var4.onSlotChanged();
            }

            if(var5.stackSize == var3.stackSize) return null;

            var4.onPickupFromSlot(par1EntityPlayer, var5);
        }

        return var3;
    }

    @Override
    public void onContainerClosed(EntityPlayer par1EntityPlayer){
        super.onContainerClosed(par1EntityPlayer);
        ((TileEntityBase)te).closeGUI();
    }
}
TOP

Related Classes of pneumaticCraft.common.inventory.ContainerProgrammer

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.