001package net.minecraft.tileentity; 002 003import cpw.mods.fml.common.registry.GameRegistry; 004import cpw.mods.fml.relauncher.Side; 005import cpw.mods.fml.relauncher.SideOnly; 006import net.minecraft.block.Block; 007import net.minecraft.block.BlockFurnace; 008import net.minecraft.block.material.Material; 009import net.minecraft.entity.player.EntityPlayer; 010import net.minecraft.inventory.ISidedInventory; 011import net.minecraft.item.Item; 012import net.minecraft.item.ItemBlock; 013import net.minecraft.item.ItemHoe; 014import net.minecraft.item.ItemStack; 015import net.minecraft.item.ItemSword; 016import net.minecraft.item.ItemTool; 017import net.minecraft.item.crafting.FurnaceRecipes; 018import net.minecraft.nbt.NBTTagCompound; 019import net.minecraft.nbt.NBTTagList; 020import net.minecraftforge.common.ForgeDirection; 021import net.minecraftforge.common.ForgeDummyContainer; 022 023public class TileEntityFurnace extends TileEntity implements ISidedInventory, net.minecraftforge.common.ISidedInventory 024{ 025 /** 026 * The ItemStacks that hold the items currently being used in the furnace 027 */ 028 private ItemStack[] furnaceItemStacks = new ItemStack[3]; 029 030 /** The number of ticks that the furnace will keep burning */ 031 public int furnaceBurnTime = 0; 032 033 /** 034 * The number of ticks that a fresh copy of the currently-burning item would keep the furnace burning for 035 */ 036 public int currentItemBurnTime = 0; 037 038 /** The number of ticks that the current item has been cooking for */ 039 public int furnaceCookTime = 0; 040 private String field_94130_e; 041 042 /** 043 * Returns the number of slots in the inventory. 044 */ 045 public int getSizeInventory() 046 { 047 return this.furnaceItemStacks.length; 048 } 049 050 /** 051 * Returns the stack in slot i 052 */ 053 public ItemStack getStackInSlot(int par1) 054 { 055 return this.furnaceItemStacks[par1]; 056 } 057 058 /** 059 * Removes from an inventory slot (first arg) up to a specified number (second arg) of items and returns them in a 060 * new stack. 061 */ 062 public ItemStack decrStackSize(int par1, int par2) 063 { 064 if (this.furnaceItemStacks[par1] != null) 065 { 066 ItemStack itemstack; 067 068 if (this.furnaceItemStacks[par1].stackSize <= par2) 069 { 070 itemstack = this.furnaceItemStacks[par1]; 071 this.furnaceItemStacks[par1] = null; 072 return itemstack; 073 } 074 else 075 { 076 itemstack = this.furnaceItemStacks[par1].splitStack(par2); 077 078 if (this.furnaceItemStacks[par1].stackSize == 0) 079 { 080 this.furnaceItemStacks[par1] = null; 081 } 082 083 return itemstack; 084 } 085 } 086 else 087 { 088 return null; 089 } 090 } 091 092 /** 093 * When some containers are closed they call this on each slot, then drop whatever it returns as an EntityItem - 094 * like when you close a workbench GUI. 095 */ 096 public ItemStack getStackInSlotOnClosing(int par1) 097 { 098 if (this.furnaceItemStacks[par1] != null) 099 { 100 ItemStack itemstack = this.furnaceItemStacks[par1]; 101 this.furnaceItemStacks[par1] = null; 102 return itemstack; 103 } 104 else 105 { 106 return null; 107 } 108 } 109 110 /** 111 * Sets the given item stack to the specified slot in the inventory (can be crafting or armor sections). 112 */ 113 public void setInventorySlotContents(int par1, ItemStack par2ItemStack) 114 { 115 this.furnaceItemStacks[par1] = par2ItemStack; 116 117 if (par2ItemStack != null && par2ItemStack.stackSize > this.getInventoryStackLimit()) 118 { 119 par2ItemStack.stackSize = this.getInventoryStackLimit(); 120 } 121 } 122 123 /** 124 * Returns the name of the inventory. 125 */ 126 public String getInvName() 127 { 128 return this.func_94042_c() ? this.field_94130_e : "container.furnace"; 129 } 130 131 public boolean func_94042_c() 132 { 133 return this.field_94130_e != null && this.field_94130_e.length() > 0; 134 } 135 136 public void func_94129_a(String par1Str) 137 { 138 this.field_94130_e = par1Str; 139 } 140 141 /** 142 * Reads a tile entity from NBT. 143 */ 144 public void readFromNBT(NBTTagCompound par1NBTTagCompound) 145 { 146 super.readFromNBT(par1NBTTagCompound); 147 NBTTagList nbttaglist = par1NBTTagCompound.getTagList("Items"); 148 this.furnaceItemStacks = new ItemStack[this.getSizeInventory()]; 149 150 for (int i = 0; i < nbttaglist.tagCount(); ++i) 151 { 152 NBTTagCompound nbttagcompound1 = (NBTTagCompound)nbttaglist.tagAt(i); 153 byte b0 = nbttagcompound1.getByte("Slot"); 154 155 if (b0 >= 0 && b0 < this.furnaceItemStacks.length) 156 { 157 this.furnaceItemStacks[b0] = ItemStack.loadItemStackFromNBT(nbttagcompound1); 158 } 159 } 160 161 this.furnaceBurnTime = par1NBTTagCompound.getShort("BurnTime"); 162 this.furnaceCookTime = par1NBTTagCompound.getShort("CookTime"); 163 this.currentItemBurnTime = getItemBurnTime(this.furnaceItemStacks[1]); 164 165 if (par1NBTTagCompound.hasKey("CustomName")) 166 { 167 this.field_94130_e = par1NBTTagCompound.getString("CustomName"); 168 } 169 } 170 171 /** 172 * Writes a tile entity to NBT. 173 */ 174 public void writeToNBT(NBTTagCompound par1NBTTagCompound) 175 { 176 super.writeToNBT(par1NBTTagCompound); 177 par1NBTTagCompound.setShort("BurnTime", (short)this.furnaceBurnTime); 178 par1NBTTagCompound.setShort("CookTime", (short)this.furnaceCookTime); 179 NBTTagList nbttaglist = new NBTTagList(); 180 181 for (int i = 0; i < this.furnaceItemStacks.length; ++i) 182 { 183 if (this.furnaceItemStacks[i] != null) 184 { 185 NBTTagCompound nbttagcompound1 = new NBTTagCompound(); 186 nbttagcompound1.setByte("Slot", (byte)i); 187 this.furnaceItemStacks[i].writeToNBT(nbttagcompound1); 188 nbttaglist.appendTag(nbttagcompound1); 189 } 190 } 191 192 par1NBTTagCompound.setTag("Items", nbttaglist); 193 194 if (this.func_94042_c()) 195 { 196 par1NBTTagCompound.setString("CustomName", this.field_94130_e); 197 } 198 } 199 200 /** 201 * Returns the maximum stack size for a inventory slot. Seems to always be 64, possibly will be extended. *Isn't 202 * this more of a set than a get?* 203 */ 204 public int getInventoryStackLimit() 205 { 206 return 64; 207 } 208 209 @SideOnly(Side.CLIENT) 210 211 /** 212 * Returns an integer between 0 and the passed value representing how close the current item is to being completely 213 * cooked 214 */ 215 public int getCookProgressScaled(int par1) 216 { 217 return this.furnaceCookTime * par1 / 200; 218 } 219 220 @SideOnly(Side.CLIENT) 221 222 /** 223 * Returns an integer between 0 and the passed value representing how much burn time is left on the current fuel 224 * item, where 0 means that the item is exhausted and the passed value means that the item is fresh 225 */ 226 public int getBurnTimeRemainingScaled(int par1) 227 { 228 if (this.currentItemBurnTime == 0) 229 { 230 this.currentItemBurnTime = 200; 231 } 232 233 return this.furnaceBurnTime * par1 / this.currentItemBurnTime; 234 } 235 236 /** 237 * Returns true if the furnace is currently burning 238 */ 239 public boolean isBurning() 240 { 241 return this.furnaceBurnTime > 0; 242 } 243 244 /** 245 * Allows the entity to update its state. Overridden in most subclasses, e.g. the mob spawner uses this to count 246 * ticks and creates a new spawn inside its implementation. 247 */ 248 public void updateEntity() 249 { 250 boolean flag = this.furnaceBurnTime > 0; 251 boolean flag1 = false; 252 253 if (this.furnaceBurnTime > 0) 254 { 255 --this.furnaceBurnTime; 256 } 257 258 if (!this.worldObj.isRemote) 259 { 260 if (this.furnaceBurnTime == 0 && this.canSmelt()) 261 { 262 this.currentItemBurnTime = this.furnaceBurnTime = getItemBurnTime(this.furnaceItemStacks[1]); 263 264 if (this.furnaceBurnTime > 0) 265 { 266 flag1 = true; 267 268 if (this.furnaceItemStacks[1] != null) 269 { 270 --this.furnaceItemStacks[1].stackSize; 271 272 if (this.furnaceItemStacks[1].stackSize == 0) 273 { 274 this.furnaceItemStacks[1] = this.furnaceItemStacks[1].getItem().getContainerItemStack(furnaceItemStacks[1]); 275 } 276 } 277 } 278 } 279 280 if (this.isBurning() && this.canSmelt()) 281 { 282 ++this.furnaceCookTime; 283 284 if (this.furnaceCookTime == 200) 285 { 286 this.furnaceCookTime = 0; 287 this.smeltItem(); 288 flag1 = true; 289 } 290 } 291 else 292 { 293 this.furnaceCookTime = 0; 294 } 295 296 if (flag != this.furnaceBurnTime > 0) 297 { 298 flag1 = true; 299 BlockFurnace.updateFurnaceBlockState(this.furnaceBurnTime > 0, this.worldObj, this.xCoord, this.yCoord, this.zCoord); 300 } 301 } 302 303 if (flag1) 304 { 305 this.onInventoryChanged(); 306 } 307 } 308 309 /** 310 * Returns true if the furnace can smelt an item, i.e. has a source item, destination stack isn't full, etc. 311 */ 312 private boolean canSmelt() 313 { 314 if (this.furnaceItemStacks[0] == null) 315 { 316 return false; 317 } 318 else 319 { 320 ItemStack itemstack = FurnaceRecipes.smelting().getSmeltingResult(this.furnaceItemStacks[0]); 321 if (itemstack == null) return false; 322 if (this.furnaceItemStacks[2] == null) return true; 323 if (!this.furnaceItemStacks[2].isItemEqual(itemstack)) return false; 324 int result = furnaceItemStacks[2].stackSize + itemstack.stackSize; 325 return (result <= getInventoryStackLimit() && result <= itemstack.getMaxStackSize()); 326 } 327 } 328 329 /** 330 * Turn one item from the furnace source stack into the appropriate smelted item in the furnace result stack 331 */ 332 public void smeltItem() 333 { 334 if (this.canSmelt()) 335 { 336 ItemStack itemstack = FurnaceRecipes.smelting().getSmeltingResult(this.furnaceItemStacks[0]); 337 338 if (this.furnaceItemStacks[2] == null) 339 { 340 this.furnaceItemStacks[2] = itemstack.copy(); 341 } 342 else if (this.furnaceItemStacks[2].isItemEqual(itemstack)) 343 { 344 furnaceItemStacks[2].stackSize += itemstack.stackSize; 345 } 346 347 --this.furnaceItemStacks[0].stackSize; 348 349 if (this.furnaceItemStacks[0].stackSize <= 0) 350 { 351 this.furnaceItemStacks[0] = null; 352 } 353 } 354 } 355 356 /** 357 * Returns the number of ticks that the supplied fuel item will keep the furnace burning, or 0 if the item isn't 358 * fuel 359 */ 360 public static int getItemBurnTime(ItemStack par0ItemStack) 361 { 362 if (par0ItemStack == null) 363 { 364 return 0; 365 } 366 else 367 { 368 int i = par0ItemStack.getItem().itemID; 369 Item item = par0ItemStack.getItem(); 370 371 if (par0ItemStack.getItem() instanceof ItemBlock && Block.blocksList[i] != null) 372 { 373 Block block = Block.blocksList[i]; 374 375 if (block == Block.woodSingleSlab) 376 { 377 return 150; 378 } 379 380 if (block.blockMaterial == Material.wood) 381 { 382 return 300; 383 } 384 } 385 386 if (item instanceof ItemTool && ((ItemTool) item).getToolMaterialName().equals("WOOD")) return 200; 387 if (item instanceof ItemSword && ((ItemSword) item).getToolMaterialName().equals("WOOD")) return 200; 388 if (item instanceof ItemHoe && ((ItemHoe) item).func_77842_f().equals("WOOD")) return 200; 389 if (i == Item.stick.itemID) return 100; 390 if (i == Item.coal.itemID) return 1600; 391 if (i == Item.bucketLava.itemID) return 20000; 392 if (i == Block.sapling.blockID) return 100; 393 if (i == Item.blazeRod.itemID) return 2400; 394 return GameRegistry.getFuelValue(par0ItemStack); 395 } 396 } 397 398 /** 399 * Return true if item is a fuel source (getItemBurnTime() > 0). 400 */ 401 public static boolean isItemFuel(ItemStack par0ItemStack) 402 { 403 return getItemBurnTime(par0ItemStack) > 0; 404 } 405 406 /** 407 * Do not make give this method the name canInteractWith because it clashes with Container 408 */ 409 public boolean isUseableByPlayer(EntityPlayer par1EntityPlayer) 410 { 411 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; 412 } 413 414 public void openChest() {} 415 416 public void closeChest() {} 417 418 public boolean func_94041_b(int par1, ItemStack par2ItemStack) 419 { 420 return par1 == 2 ? false : (par1 == 1 ? isItemFuel(par2ItemStack) : true); 421 } 422 423 public int func_94127_c(int par1) 424 { 425 return par1 == 0 ? 2 : (par1 == 1 ? 0 : 1); 426 } 427 428 public int func_94128_d(int par1) 429 { 430 return 1; 431 } 432 433 /*********************************************************************************** 434 * This function is here for compatibilities sake, Modders should Check for 435 * Sided before ContainerWorldly, Vanilla Minecraft does not follow the sided standard 436 * that Modding has for a while. 437 * 438 * In vanilla: 439 * 440 * Top: Ores 441 * Sides: Fuel 442 * Bottom: Output 443 * 444 * Standard Modding: 445 * Top: Ores 446 * Sides: Output 447 * Bottom: Fuel 448 * 449 * The Modding one is designed after the GUI, the vanilla one is designed because its 450 * intended use is for the hopper, which logically would take things in from the top. 451 * 452 * This will possibly be removed in future updates, and make vanilla the definitive 453 * standard. 454 */ 455 456 @Override 457 public int getStartInventorySide(ForgeDirection side) 458 { 459 if (ForgeDummyContainer.legacyFurnaceSides) 460 { 461 if (side == ForgeDirection.DOWN) return 1; 462 if (side == ForgeDirection.UP) return 0; 463 return 2; 464 } 465 else 466 { 467 if (side == ForgeDirection.DOWN) return 2; 468 if (side == ForgeDirection.UP) return 0; 469 return 1; 470 } 471 } 472 473 @Override 474 public int getSizeInventorySide(ForgeDirection side) 475 { 476 return 1; 477 } 478}