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