    001package net.minecraft.inventory;
    002
    003import cpw.mods.fml.common.registry.GameRegistry;
    004import net.minecraft.block.Block;
    005import net.minecraft.entity.player.EntityPlayer;
    006import net.minecraft.item.Item;
    007import net.minecraft.item.ItemStack;
    008import net.minecraft.stats.AchievementList;
    009
    010import net.minecraftforge.common.ForgeHooks;
    011import net.minecraftforge.common.MinecraftForge;
    012import net.minecraftforge.event.entity.player.PlayerDestroyItemEvent;
    013
    014public class SlotCrafting extends Slot
    015{
    016    /** The craft matrix inventory linked to this result slot. */
    017    private final IInventory craftMatrix;
    018
    019    /** The player that is using the GUI where this slot resides. */
    020    private EntityPlayer thePlayer;
    021
    022    /**
    023     * The number of items that have been crafted so far. Gets passed to ItemStack.onCrafting before being reset.
    024     */
    025    private int amountCrafted;
    026
    027    public SlotCrafting(EntityPlayer par1EntityPlayer, IInventory par2IInventory, IInventory par3IInventory, int par4, int par5, int par6)
    028    {
    029        super(par3IInventory, par4, par5, par6);
    030        this.thePlayer = par1EntityPlayer;
    031        this.craftMatrix = par2IInventory;
    032    }
    033
    034    /**
    035     * Check if the stack is a valid item for this slot. Always true beside for the armor slots.
    036     */
    037    public boolean isItemValid(ItemStack par1ItemStack)
    038    {
    039        return false;
    040    }
    041
    042    /**
    043     * Decrease the size of the stack in slot (first int arg) by the amount of the second int arg. Returns the new
    044     * stack.
    045     */
    046    public ItemStack decrStackSize(int par1)
    047    {
    048        if (this.getHasStack())
    049        {
    050            this.amountCrafted += Math.min(par1, this.getStack().stackSize);
    051        }
    052
    053        return super.decrStackSize(par1);
    054    }
    055
    056    /**
    057     * the itemStack passed in is the output - ie, iron ingots, and pickaxes, not ore and wood. Typically increases an
    058     * internal count then calls onCrafting(item).
    059     */
    060    protected void onCrafting(ItemStack par1ItemStack, int par2)
    061    {
    062        this.amountCrafted += par2;
    063        this.onCrafting(par1ItemStack);
    064    }
    065
    066    /**
    067     * the itemStack passed in is the output - ie, iron ingots, and pickaxes, not ore and wood.
    068     */
    069    protected void onCrafting(ItemStack par1ItemStack)
    070    {
    071        par1ItemStack.onCrafting(this.thePlayer.worldObj, this.thePlayer, this.amountCrafted);
    072        this.amountCrafted = 0;
    073
    074        if (par1ItemStack.itemID == Block.workbench.blockID)
    075        {
    076            this.thePlayer.addStat(AchievementList.buildWorkBench, 1);
    077        }
    078        else if (par1ItemStack.itemID == Item.pickaxeWood.itemID)
    079        {
    080            this.thePlayer.addStat(AchievementList.buildPickaxe, 1);
    081        }
    082        else if (par1ItemStack.itemID == Block.stoneOvenIdle.blockID)
    083        {
    084            this.thePlayer.addStat(AchievementList.buildFurnace, 1);
    085        }
    086        else if (par1ItemStack.itemID == Item.hoeWood.itemID)
    087        {
    088            this.thePlayer.addStat(AchievementList.buildHoe, 1);
    089        }
    090        else if (par1ItemStack.itemID == Item.bread.itemID)
    091        {
    092            this.thePlayer.addStat(AchievementList.makeBread, 1);
    093        }
    094        else if (par1ItemStack.itemID == Item.cake.itemID)
    095        {
    096            this.thePlayer.addStat(AchievementList.bakeCake, 1);
    097        }
    098        else if (par1ItemStack.itemID == Item.pickaxeStone.itemID)
    099        {
    100            this.thePlayer.addStat(AchievementList.buildBetterPickaxe, 1);
    101        }
    102        else if (par1ItemStack.itemID == Item.swordWood.itemID)
    103        {
    104            this.thePlayer.addStat(AchievementList.buildSword, 1);
    105        }
    106        else if (par1ItemStack.itemID == Block.enchantmentTable.blockID)
    107        {
    108            this.thePlayer.addStat(AchievementList.enchantments, 1);
    109        }
    110        else if (par1ItemStack.itemID == Block.bookShelf.blockID)
    111        {
    112            this.thePlayer.addStat(AchievementList.bookcase, 1);
    113        }
    114    }
    115
    116    public void onPickupFromSlot(EntityPlayer par1EntityPlayer, ItemStack par2ItemStack)
    117    {
    118        GameRegistry.onItemCrafted(par1EntityPlayer, par2ItemStack, craftMatrix);
    119        this.onCrafting(par2ItemStack);
    120
    121        for (int var3 = 0; var3 < this.craftMatrix.getSizeInventory(); ++var3)
    122        {
    123            ItemStack var4 = this.craftMatrix.getStackInSlot(var3);
    124
    125            if (var4 != null)
    126            {
    127                this.craftMatrix.decrStackSize(var3, 1);
    128
    129                if (var4.getItem().hasContainerItem())
    130                {
    131                    ItemStack var5 = var4.getItem().getContainerItemStack(var4);
    132
    133                    if (var5.isItemStackDamageable() && var5.getItemDamage() > var5.getMaxDamage())
    134                    {
    135                        MinecraftForge.EVENT_BUS.post(new PlayerDestroyItemEvent(thePlayer, var5));
    136                        var5 = null;
    137                    }
    138
    139                    if (var5 != null && (!var4.getItem().doesContainerItemLeaveCraftingGrid(var4) || !this.thePlayer.inventory.addItemStackToInventory(var5)))
    140                    {
    141                        if (this.craftMatrix.getStackInSlot(var3) == null)
    142                        {
    143                            this.craftMatrix.setInventorySlotContents(var3, var5);
    144                        }
    145                        else
    146                        {
    147                            this.thePlayer.dropPlayerItem(var5);
    148                        }
    149                    }
    150                }
    151            }
    152        }
    153    }
    154}