001package net.minecraft.inventory;
002
003import cpw.mods.fml.relauncher.Side;
004import cpw.mods.fml.relauncher.SideOnly;
005import net.minecraft.entity.player.EntityPlayer;
006import net.minecraft.item.ItemStack;
007import net.minecraft.util.Icon;
008
009public class Slot
010{
011    /** The index of the slot in the inventory. */
012    private final int slotIndex;
013
014    /** The inventory we want to extract a slot from. */
015    public final IInventory inventory;
016
017    /** the id of the slot(also the index in the inventory arraylist) */
018    public int slotNumber;
019
020    /** display position of the inventory slot on the screen x axis */
021    public int xDisplayPosition;
022
023    /** display position of the inventory slot on the screen y axis */
024    public int yDisplayPosition;
025
026    /** Position within background texture file, normally -1 which causes no background to be drawn. */
027    protected Icon backgroundIcon = null;
028
029    /** Background texture file assigned to this slot, if any. Vanilla "/gui/items.png" is used if this is null. */
030    protected String texture = "/gui/items.png";
031
032    public Slot(IInventory par1IInventory, int par2, int par3, int par4)
033    {
034        this.inventory = par1IInventory;
035        this.slotIndex = par2;
036        this.xDisplayPosition = par3;
037        this.yDisplayPosition = par4;
038    }
039
040    /**
041     * if par2 has more items than par1, onCrafting(item,countIncrease) is called
042     */
043    public void onSlotChange(ItemStack par1ItemStack, ItemStack par2ItemStack)
044    {
045        if (par1ItemStack != null && par2ItemStack != null)
046        {
047            if (par1ItemStack.itemID == par2ItemStack.itemID)
048            {
049                int i = par2ItemStack.stackSize - par1ItemStack.stackSize;
050
051                if (i > 0)
052                {
053                    this.onCrafting(par1ItemStack, i);
054                }
055            }
056        }
057    }
058
059    /**
060     * the itemStack passed in is the output - ie, iron ingots, and pickaxes, not ore and wood. Typically increases an
061     * internal count then calls onCrafting(item).
062     */
063    protected void onCrafting(ItemStack par1ItemStack, int par2) {}
064
065    /**
066     * the itemStack passed in is the output - ie, iron ingots, and pickaxes, not ore and wood.
067     */
068    protected void onCrafting(ItemStack par1ItemStack) {}
069
070    public void onPickupFromSlot(EntityPlayer par1EntityPlayer, ItemStack par2ItemStack)
071    {
072        this.onSlotChanged();
073    }
074
075    /**
076     * Check if the stack is a valid item for this slot. Always true beside for the armor slots.
077     */
078    public boolean isItemValid(ItemStack par1ItemStack)
079    {
080        return true;
081    }
082
083    /**
084     * Helper fnct to get the stack in the slot.
085     */
086    public ItemStack getStack()
087    {
088        return this.inventory.getStackInSlot(this.slotIndex);
089    }
090
091    /**
092     * Returns if this slot contains a stack.
093     */
094    public boolean getHasStack()
095    {
096        return this.getStack() != null;
097    }
098
099    /**
100     * Helper method to put a stack in the slot.
101     */
102    public void putStack(ItemStack par1ItemStack)
103    {
104        this.inventory.setInventorySlotContents(this.slotIndex, par1ItemStack);
105        this.onSlotChanged();
106    }
107
108    /**
109     * Called when the stack in a Slot changes
110     */
111    public void onSlotChanged()
112    {
113        this.inventory.onInventoryChanged();
114    }
115
116    /**
117     * Returns the maximum stack size for a given slot (usually the same as getInventoryStackLimit(), but 1 in the case
118     * of armor slots)
119     */
120    public int getSlotStackLimit()
121    {
122        return this.inventory.getInventoryStackLimit();
123    }
124
125    /**
126     * Decrease the size of the stack in slot (first int arg) by the amount of the second int arg. Returns the new
127     * stack.
128     */
129    public ItemStack decrStackSize(int par1)
130    {
131        return this.inventory.decrStackSize(this.slotIndex, par1);
132    }
133
134    /**
135     * returns true if this slot is in par2 of par1
136     */
137    public boolean isSlotInInventory(IInventory par1IInventory, int par2)
138    {
139        return par1IInventory == this.inventory && par2 == this.slotIndex;
140    }
141
142    /**
143     * Return whether this slot's stack can be taken from this slot.
144     */
145    public boolean canTakeStack(EntityPlayer par1EntityPlayer)
146    {
147        return true;
148    }
149
150    @SideOnly(Side.CLIENT)
151
152    /**
153     * Returns the icon index on items.png that is used as background image of the slot.
154     */
155    public Icon getBackgroundIconIndex()
156    {
157        return backgroundIcon;
158    }
159
160    /**
161     * Gets the path of the texture file to use for the background image of this slot when drawing the GUI.
162     * @return String: The texture file that will be used in GuiContainer.drawSlotInventory for the slot background.  
163     */
164    public String getBackgroundIconTexture()
165    {
166        return (texture == null ? "/gui/items.png" : texture);
167    }
168
169    /**
170     * Sets which icon index to use as the background image of the slot when it's empty.
171     * @param icon The icon to use, null for none  
172     */
173    public void setBackgroundIconIndex(Icon icon)
174    {
175        backgroundIcon = icon;
176    }
177
178    /**
179     * Sets the texture file to use for the background image of the slot when it's empty.
180     * @param textureFilename String: Path of texture file to use, or null to use "/gui/items.png"
181     */
182    public void setBackgroundIconTexture(String textureFilename)
183    {
184        texture = textureFilename;
185    }
186
187    /**
188     * Retrieves the index in the inventory for this slot, this value should typically not 
189     * be used, but can be useful for some occasions.
190     * 
191     * @return Index in associated inventory for this slot.
192     */
193    public int getSlotIndex()
194    {
195        return slotIndex;
196    }
197
198}