Advertisement
Guest User

Untitled

a guest
Feb 18th, 2015
288
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 15.05 KB | None | 0 0
  1. package com.scriph.luckybox.blocks;
  2.  
  3. import net.minecraft.block.Block;
  4. import net.minecraft.block.BlockFurnace;
  5. import net.minecraft.block.material.Material;
  6. import net.minecraft.entity.player.EntityPlayer;
  7. import net.minecraft.init.Blocks;
  8. import net.minecraft.init.Items;
  9. import net.minecraft.inventory.ISidedInventory;
  10. import net.minecraft.item.Item;
  11. import net.minecraft.item.ItemBlock;
  12. import net.minecraft.item.ItemHoe;
  13. import net.minecraft.item.ItemStack;
  14. import net.minecraft.item.ItemSword;
  15. import net.minecraft.item.ItemTool;
  16. import net.minecraft.item.crafting.FurnaceRecipes;
  17. import net.minecraft.nbt.NBTTagCompound;
  18. import net.minecraft.nbt.NBTTagList;
  19. import net.minecraft.tileentity.TileEntity;
  20. import cpw.mods.fml.common.registry.GameRegistry;
  21. import cpw.mods.fml.relauncher.Side;
  22. import cpw.mods.fml.relauncher.SideOnly;
  23.  
  24. public class TileEntityFurnace extends TileEntity implements ISidedInventory
  25. {
  26. private static final int[] slotsTop = new int[] {0};
  27. private static final int[] slotsBottom = new int[] {2, 1};
  28. private static final int[] slotsSides = new int[] {1};
  29. /** The ItemStacks that hold the items currently being used in the furnace */
  30. private ItemStack[] furnaceItemStacks = new ItemStack[3];
  31. /** The number of ticks that the furnace will keep burning */
  32. public int furnaceBurnTime;
  33. /** The number of ticks that a fresh copy of the currently-burning item would keep the furnace burning for */
  34. public int currentItemBurnTime;
  35. /** The number of ticks that the current item has been cooking for */
  36. public int furnaceCookTime;
  37. private String field_145958_o;
  38. private static final String __OBFID = "CL_00000357";
  39.  
  40. /**
  41. * Returns the number of slots in the inventory.
  42. */
  43. public int getSizeInventory()
  44. {
  45. return this.furnaceItemStacks.length;
  46. }
  47.  
  48. /**
  49. * Returns the stack in slot i
  50. */
  51. public ItemStack getStackInSlot(int p_70301_1_)
  52. {
  53. return this.furnaceItemStacks[p_70301_1_];
  54. }
  55.  
  56. /**
  57. * Removes from an inventory slot (first arg) up to a specified number (second arg) of items and returns them in a
  58. * new stack.
  59. */
  60. public ItemStack decrStackSize(int p_70298_1_, int p_70298_2_)
  61. {
  62. if (this.furnaceItemStacks[p_70298_1_] != null)
  63. {
  64. ItemStack itemstack;
  65.  
  66. if (this.furnaceItemStacks[p_70298_1_].stackSize <= p_70298_2_)
  67. {
  68. itemstack = this.furnaceItemStacks[p_70298_1_];
  69. this.furnaceItemStacks[p_70298_1_] = null;
  70. return itemstack;
  71. }
  72. else
  73. {
  74. itemstack = this.furnaceItemStacks[p_70298_1_].splitStack(p_70298_2_);
  75.  
  76. if (this.furnaceItemStacks[p_70298_1_].stackSize == 0)
  77. {
  78. this.furnaceItemStacks[p_70298_1_] = null;
  79. }
  80.  
  81. return itemstack;
  82. }
  83. }
  84. else
  85. {
  86. return null;
  87. }
  88. }
  89.  
  90. /**
  91. * When some containers are closed they call this on each slot, then drop whatever it returns as an EntityItem -
  92. * like when you close a workbench GUI.
  93. */
  94. public ItemStack getStackInSlotOnClosing(int p_70304_1_)
  95. {
  96. if (this.furnaceItemStacks[p_70304_1_] != null)
  97. {
  98. ItemStack itemstack = this.furnaceItemStacks[p_70304_1_];
  99. this.furnaceItemStacks[p_70304_1_] = null;
  100. return itemstack;
  101. }
  102. else
  103. {
  104. return null;
  105. }
  106. }
  107.  
  108. /**
  109. * Sets the given item stack to the specified slot in the inventory (can be crafting or armor sections).
  110. */
  111. public void setInventorySlotContents(int p_70299_1_, ItemStack p_70299_2_)
  112. {
  113. this.furnaceItemStacks[p_70299_1_] = p_70299_2_;
  114.  
  115. if (p_70299_2_ != null && p_70299_2_.stackSize > this.getInventoryStackLimit())
  116. {
  117. p_70299_2_.stackSize = this.getInventoryStackLimit();
  118. }
  119. }
  120.  
  121. /**
  122. * Returns the name of the inventory
  123. */
  124. public String getInventoryName()
  125. {
  126. return this.hasCustomInventoryName() ? this.field_145958_o : "container.furnace";
  127. }
  128.  
  129. /**
  130. * Returns if the inventory is named
  131. */
  132. public boolean hasCustomInventoryName()
  133. {
  134. return this.field_145958_o != null && this.field_145958_o.length() > 0;
  135. }
  136.  
  137. public void func_145951_a(String p_145951_1_)
  138. {
  139. this.field_145958_o = p_145951_1_;
  140. }
  141.  
  142. public void readFromNBT(NBTTagCompound p_145839_1_)
  143. {
  144. super.readFromNBT(p_145839_1_);
  145. NBTTagList nbttaglist = p_145839_1_.getTagList("Items", 10);
  146. this.furnaceItemStacks = new ItemStack[this.getSizeInventory()];
  147.  
  148. for (int i = 0; i < nbttaglist.tagCount(); ++i)
  149. {
  150. NBTTagCompound nbttagcompound1 = nbttaglist.getCompoundTagAt(i);
  151. byte b0 = nbttagcompound1.getByte("Slot");
  152.  
  153. if (b0 >= 0 && b0 < this.furnaceItemStacks.length)
  154. {
  155. this.furnaceItemStacks[b0] = ItemStack.loadItemStackFromNBT(nbttagcompound1);
  156. }
  157. }
  158.  
  159. this.furnaceBurnTime = p_145839_1_.getShort("BurnTime");
  160. this.furnaceCookTime = p_145839_1_.getShort("CookTime");
  161. this.currentItemBurnTime = getItemBurnTime(this.furnaceItemStacks[1]);
  162.  
  163. if (p_145839_1_.hasKey("CustomName", 8))
  164. {
  165. this.field_145958_o = p_145839_1_.getString("CustomName");
  166. }
  167. }
  168.  
  169. public void writeToNBT(NBTTagCompound p_145841_1_)
  170. {
  171. super.writeToNBT(p_145841_1_);
  172. p_145841_1_.setShort("BurnTime", (short)this.furnaceBurnTime);
  173. p_145841_1_.setShort("CookTime", (short)this.furnaceCookTime);
  174. NBTTagList nbttaglist = new NBTTagList();
  175.  
  176. for (int i = 0; i < this.furnaceItemStacks.length; ++i)
  177. {
  178. if (this.furnaceItemStacks[i] != null)
  179. {
  180. NBTTagCompound nbttagcompound1 = new NBTTagCompound();
  181. nbttagcompound1.setByte("Slot", (byte)i);
  182. this.furnaceItemStacks[i].writeToNBT(nbttagcompound1);
  183. nbttaglist.appendTag(nbttagcompound1);
  184. }
  185. }
  186.  
  187. p_145841_1_.setTag("Items", nbttaglist);
  188.  
  189. if (this.hasCustomInventoryName())
  190. {
  191. p_145841_1_.setString("CustomName", this.field_145958_o);
  192. }
  193. }
  194.  
  195. /**
  196. * Returns the maximum stack size for a inventory slot.
  197. */
  198. public int getInventoryStackLimit()
  199. {
  200. return 64;
  201. }
  202.  
  203. /**
  204. * Returns an integer between 0 and the passed value representing how close the current item is to being completely
  205. * cooked
  206. */
  207. @SideOnly(Side.CLIENT)
  208. public int getCookProgressScaled(int p_145953_1_)
  209. {
  210. return this.furnaceCookTime * p_145953_1_ / 200;
  211. }
  212.  
  213. /**
  214. * Returns an integer between 0 and the passed value representing how much burn time is left on the current fuel
  215. * item, where 0 means that the item is exhausted and the passed value means that the item is fresh
  216. */
  217. @SideOnly(Side.CLIENT)
  218. public int getBurnTimeRemainingScaled(int p_145955_1_)
  219. {
  220. if (this.currentItemBurnTime == 0)
  221. {
  222. this.currentItemBurnTime = 200;
  223. }
  224.  
  225. return this.furnaceBurnTime * p_145955_1_ / this.currentItemBurnTime;
  226. }
  227.  
  228. /**
  229. * Furnace isBurning
  230. */
  231. public boolean isBurning()
  232. {
  233. return this.furnaceBurnTime > 0;
  234. }
  235.  
  236. public void updateEntity()
  237. {
  238. boolean flag = this.furnaceBurnTime > 0;
  239. boolean flag1 = false;
  240.  
  241. if (this.furnaceBurnTime > 0)
  242. {
  243. --this.furnaceBurnTime;
  244. }
  245.  
  246. if (!this.worldObj.isRemote)
  247. {
  248. if (this.furnaceBurnTime != 0 || this.furnaceItemStacks[1] != null && this.furnaceItemStacks[0] != null)
  249. {
  250. if (this.furnaceBurnTime == 0 && this.canSmelt())
  251. {
  252. this.currentItemBurnTime = this.furnaceBurnTime = getItemBurnTime(this.furnaceItemStacks[1]);
  253.  
  254. if (this.furnaceBurnTime > 0)
  255. {
  256. flag1 = true;
  257.  
  258. if (this.furnaceItemStacks[1] != null)
  259. {
  260. --this.furnaceItemStacks[1].stackSize;
  261.  
  262. if (this.furnaceItemStacks[1].stackSize == 0)
  263. {
  264. this.furnaceItemStacks[1] = furnaceItemStacks[1].getItem().getContainerItem(furnaceItemStacks[1]);
  265. }
  266. }
  267. }
  268. }
  269.  
  270. if (this.isBurning() && this.canSmelt())
  271. {
  272. ++this.furnaceCookTime;
  273. ++this.furnaceCookTime;
  274. ++this.furnaceCookTime;
  275.  
  276. if (this.furnaceCookTime == 200)
  277. {
  278. this.furnaceCookTime = 0;
  279. this.smeltItem();
  280. flag1 = true;
  281. }
  282. }
  283. else
  284. {
  285. this.furnaceCookTime = 0;
  286. }
  287. }
  288.  
  289. if (flag != this.furnaceBurnTime > 0)
  290. {
  291. flag1 = true;
  292. BlockFurnace.updateFurnaceBlockState(this.furnaceBurnTime > 0, this.worldObj, this.xCoord, this.yCoord, this.zCoord);
  293. }
  294. }
  295.  
  296. if (flag1)
  297. {
  298. this.markDirty();
  299. }
  300. }
  301.  
  302. /**
  303. * Returns true if the furnace can smelt an item, i.e. has a source item, destination stack isn't full, etc.
  304. */
  305. private boolean canSmelt()
  306. {
  307. if (this.furnaceItemStacks[0] == null)
  308. {
  309. return false;
  310. }
  311. else
  312. {
  313. ItemStack itemstack = FurnaceRecipes.smelting().getSmeltingResult(this.furnaceItemStacks[0]);
  314. if (itemstack == null) return false;
  315. if (this.furnaceItemStacks[2] == null) return true;
  316. if (!this.furnaceItemStacks[2].isItemEqual(itemstack)) return false;
  317. int result = furnaceItemStacks[2].stackSize + itemstack.stackSize;
  318. return result <= getInventoryStackLimit() && result <= this.furnaceItemStacks[2].getMaxStackSize(); //Forge BugFix: Make it respect stack sizes properly.
  319. }
  320. }
  321.  
  322. /**
  323. * Turn one item from the furnace source stack into the appropriate smelted item in the furnace result stack
  324. */
  325. public void smeltItem()
  326. {
  327. if (this.canSmelt())
  328. {
  329. ItemStack itemstack = FurnaceRecipes.smelting().getSmeltingResult(this.furnaceItemStacks[0]);
  330.  
  331. if (this.furnaceItemStacks[2] == null)
  332. {
  333. this.furnaceItemStacks[2] = itemstack.copy();
  334. }
  335. else if (this.furnaceItemStacks[2].getItem() == itemstack.getItem())
  336. {
  337. this.furnaceItemStacks[2].stackSize += itemstack.stackSize; // Forge BugFix: Results may have multiple items
  338. }
  339.  
  340. --this.furnaceItemStacks[0].stackSize;
  341.  
  342. if (this.furnaceItemStacks[0].stackSize <= 0)
  343. {
  344. this.furnaceItemStacks[0] = null;
  345. }
  346. }
  347. }
  348.  
  349. /**
  350. * Returns the number of ticks that the supplied fuel item will keep the furnace burning, or 0 if the item isn't
  351. * fuel
  352. */
  353. public static int getItemBurnTime(ItemStack p_145952_0_)
  354. {
  355. if (p_145952_0_ == null)
  356. {
  357. return 0;
  358. }
  359. else
  360. {
  361. Item item = p_145952_0_.getItem();
  362.  
  363. if (item instanceof ItemBlock && Block.getBlockFromItem(item) != Blocks.air)
  364. {
  365. Block block = Block.getBlockFromItem(item);
  366.  
  367. if (block == Blocks.wooden_slab)
  368. {
  369. return 150;
  370. }
  371.  
  372. if (block.getMaterial() == Material.wood)
  373. {
  374. return 300;
  375. }
  376.  
  377. if (block == Blocks.coal_block)
  378. {
  379. return 16000;
  380. }
  381. }
  382.  
  383. if (item instanceof ItemTool && ((ItemTool)item).getToolMaterialName().equals("WOOD")) return 200;
  384. if (item instanceof ItemSword && ((ItemSword)item).getToolMaterialName().equals("WOOD")) return 200;
  385. if (item instanceof ItemHoe && ((ItemHoe)item).getToolMaterialName().equals("WOOD")) return 200;
  386. if (item == Items.stick) return 100;
  387. if (item == Items.coal) return 1600;
  388. if (item == Items.lava_bucket) return 20000;
  389. if (item == Item.getItemFromBlock(Blocks.sapling)) return 100;
  390. if (item == Items.blaze_rod) return 2400;
  391. return GameRegistry.getFuelValue(p_145952_0_);
  392. }
  393. }
  394.  
  395. public static boolean isItemFuel(ItemStack p_145954_0_)
  396. {
  397. /**
  398. * Returns the number of ticks that the supplied fuel item will keep the furnace burning, or 0 if the item isn't
  399. * fuel
  400. */
  401. return getItemBurnTime(p_145954_0_) > 0;
  402. }
  403.  
  404. /**
  405. * Do not make give this method the name canInteractWith because it clashes with Container
  406. */
  407. public boolean isUseableByPlayer(EntityPlayer p_70300_1_)
  408. {
  409. return this.worldObj.getTileEntity(this.xCoord, this.yCoord, this.zCoord) != this ? false : p_70300_1_.getDistanceSq((double)this.xCoord + 0.5D, (double)this.yCoord + 0.5D, (double)this.zCoord + 0.5D) <= 64.0D;
  410. }
  411.  
  412. public void openInventory() {}
  413.  
  414. public void closeInventory() {}
  415.  
  416. /**
  417. * Returns true if automation is allowed to insert the given stack (ignoring stack size) into the given slot.
  418. */
  419. public boolean isItemValidForSlot(int p_94041_1_, ItemStack p_94041_2_)
  420. {
  421. return p_94041_1_ == 2 ? false : (p_94041_1_ == 1 ? isItemFuel(p_94041_2_) : true);
  422. }
  423.  
  424. /**
  425. * Returns an array containing the indices of the slots that can be accessed by automation on the given side of this
  426. * block.
  427. */
  428. public int[] getAccessibleSlotsFromSide(int p_94128_1_)
  429. {
  430. return p_94128_1_ == 0 ? slotsBottom : (p_94128_1_ == 1 ? slotsTop : slotsSides);
  431. }
  432.  
  433. /**
  434. * Returns true if automation can insert the given item in the given slot from the given side. Args: Slot, item,
  435. * side
  436. */
  437. public boolean canInsertItem(int p_102007_1_, ItemStack p_102007_2_, int p_102007_3_)
  438. {
  439. return this.isItemValidForSlot(p_102007_1_, p_102007_2_);
  440. }
  441.  
  442. /**
  443. * Returns true if automation can extract the given item in the given slot from the given side. Args: Slot, item,
  444. * side
  445. */
  446. public boolean canExtractItem(int p_102008_1_, ItemStack p_102008_2_, int p_102008_3_)
  447. {
  448. return p_102008_3_ != 0 || p_102008_1_ != 1 || p_102008_2_.getItem() == Items.bucket;
  449. }
  450. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement