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