001 package net.minecraft.src;
002
003 import java.util.Random;
004
005 public class TileEntityDispenser extends TileEntity implements IInventory
006 {
007 private ItemStack[] dispenserContents = new ItemStack[9];
008
009 /**
010 * random number generator for instance. Used in random item stack selection.
011 */
012 private Random dispenserRandom = new Random();
013
014 /**
015 * Returns the number of slots in the inventory.
016 */
017 public int getSizeInventory()
018 {
019 return 9;
020 }
021
022 /**
023 * Returns the stack in slot i
024 */
025 public ItemStack getStackInSlot(int par1)
026 {
027 return this.dispenserContents[par1];
028 }
029
030 /**
031 * Removes from an inventory slot (first arg) up to a specified number (second arg) of items and returns them in a
032 * new stack.
033 */
034 public ItemStack decrStackSize(int par1, int par2)
035 {
036 if (this.dispenserContents[par1] != null)
037 {
038 ItemStack var3;
039
040 if (this.dispenserContents[par1].stackSize <= par2)
041 {
042 var3 = this.dispenserContents[par1];
043 this.dispenserContents[par1] = null;
044 this.onInventoryChanged();
045 return var3;
046 }
047 else
048 {
049 var3 = this.dispenserContents[par1].splitStack(par2);
050
051 if (this.dispenserContents[par1].stackSize == 0)
052 {
053 this.dispenserContents[par1] = null;
054 }
055
056 this.onInventoryChanged();
057 return var3;
058 }
059 }
060 else
061 {
062 return null;
063 }
064 }
065
066 /**
067 * When some containers are closed they call this on each slot, then drop whatever it returns as an EntityItem -
068 * like when you close a workbench GUI.
069 */
070 public ItemStack getStackInSlotOnClosing(int par1)
071 {
072 if (this.dispenserContents[par1] != null)
073 {
074 ItemStack var2 = this.dispenserContents[par1];
075 this.dispenserContents[par1] = null;
076 return var2;
077 }
078 else
079 {
080 return null;
081 }
082 }
083
084 public int getRandomStackFromInventory()
085 {
086 int var1 = -1;
087 int var2 = 1;
088
089 for (int var3 = 0; var3 < this.dispenserContents.length; ++var3)
090 {
091 if (this.dispenserContents[var3] != null && this.dispenserRandom.nextInt(var2++) == 0)
092 {
093 var1 = var3;
094 }
095 }
096
097 return var1;
098 }
099
100 /**
101 * Sets the given item stack to the specified slot in the inventory (can be crafting or armor sections).
102 */
103 public void setInventorySlotContents(int par1, ItemStack par2ItemStack)
104 {
105 this.dispenserContents[par1] = par2ItemStack;
106
107 if (par2ItemStack != null && par2ItemStack.stackSize > this.getInventoryStackLimit())
108 {
109 par2ItemStack.stackSize = this.getInventoryStackLimit();
110 }
111
112 this.onInventoryChanged();
113 }
114
115 public int func_70360_a(ItemStack par1ItemStack)
116 {
117 for (int var2 = 0; var2 < this.dispenserContents.length; ++var2)
118 {
119 if (this.dispenserContents[var2] == null || this.dispenserContents[var2].itemID == 0)
120 {
121 this.dispenserContents[var2] = par1ItemStack;
122 return var2;
123 }
124 }
125
126 return -1;
127 }
128
129 /**
130 * Returns the name of the inventory.
131 */
132 public String getInvName()
133 {
134 return "container.dispenser";
135 }
136
137 /**
138 * Reads a tile entity from NBT.
139 */
140 public void readFromNBT(NBTTagCompound par1NBTTagCompound)
141 {
142 super.readFromNBT(par1NBTTagCompound);
143 NBTTagList var2 = par1NBTTagCompound.getTagList("Items");
144 this.dispenserContents = new ItemStack[this.getSizeInventory()];
145
146 for (int var3 = 0; var3 < var2.tagCount(); ++var3)
147 {
148 NBTTagCompound var4 = (NBTTagCompound)var2.tagAt(var3);
149 int var5 = var4.getByte("Slot") & 255;
150
151 if (var5 >= 0 && var5 < this.dispenserContents.length)
152 {
153 this.dispenserContents[var5] = ItemStack.loadItemStackFromNBT(var4);
154 }
155 }
156 }
157
158 /**
159 * Writes a tile entity to NBT.
160 */
161 public void writeToNBT(NBTTagCompound par1NBTTagCompound)
162 {
163 super.writeToNBT(par1NBTTagCompound);
164 NBTTagList var2 = new NBTTagList();
165
166 for (int var3 = 0; var3 < this.dispenserContents.length; ++var3)
167 {
168 if (this.dispenserContents[var3] != null)
169 {
170 NBTTagCompound var4 = new NBTTagCompound();
171 var4.setByte("Slot", (byte)var3);
172 this.dispenserContents[var3].writeToNBT(var4);
173 var2.appendTag(var4);
174 }
175 }
176
177 par1NBTTagCompound.setTag("Items", var2);
178 }
179
180 /**
181 * Returns the maximum stack size for a inventory slot. Seems to always be 64, possibly will be extended. *Isn't
182 * this more of a set than a get?*
183 */
184 public int getInventoryStackLimit()
185 {
186 return 64;
187 }
188
189 /**
190 * Do not make give this method the name canInteractWith because it clashes with Container
191 */
192 public boolean isUseableByPlayer(EntityPlayer par1EntityPlayer)
193 {
194 return this.worldObj.getBlockTileEntity(this.xCoord, this.yCoord, this.zCoord) != this ? false : par1EntityPlayer.getDistanceSq((double)this.xCoord + 0.5D, (double)this.yCoord + 0.5D, (double)this.zCoord + 0.5D) <= 64.0D;
195 }
196
197 public void openChest() {}
198
199 public void closeChest() {}
200 }