Guest User

TileEntityCookingFurnace

a guest
Feb 21st, 2015
382
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 14.20 KB | None | 0 0
  1. package com.chef.mod.tileentity;
  2.  
  3. import com.chef.mod.container.ContainerCookingFurnace;
  4.  
  5. import net.minecraft.block.Block;
  6. import net.minecraft.block.BlockFurnace;
  7. import net.minecraft.block.material.Material;
  8. import net.minecraft.entity.player.EntityPlayer;
  9. import net.minecraft.entity.player.InventoryPlayer;
  10. import net.minecraft.init.Blocks;
  11. import net.minecraft.init.Items;
  12. import net.minecraft.inventory.Container;
  13. import net.minecraft.inventory.ContainerFurnace;
  14. import net.minecraft.inventory.IInventory;
  15. import net.minecraft.inventory.ISidedInventory;
  16. import net.minecraft.inventory.SlotFurnaceFuel;
  17. import net.minecraft.item.Item;
  18. import net.minecraft.item.ItemBlock;
  19. import net.minecraft.item.ItemHoe;
  20. import net.minecraft.item.ItemStack;
  21. import net.minecraft.item.ItemSword;
  22. import net.minecraft.item.ItemTool;
  23. import net.minecraft.item.crafting.FurnaceRecipes;
  24. import net.minecraft.nbt.NBTTagCompound;
  25. import net.minecraft.nbt.NBTTagList;
  26. import net.minecraft.server.gui.IUpdatePlayerListBox;
  27. import net.minecraft.tileentity.TileEntityLockable;
  28. import net.minecraft.util.EnumFacing;
  29. import net.minecraft.util.MathHelper;
  30. import net.minecraftforge.fml.relauncher.Side;
  31. import net.minecraftforge.fml.relauncher.SideOnly;
  32.  
  33. public class TileEntityCookingFurnace extends TileEntityLockable implements IUpdatePlayerListBox, ISidedInventory
  34. {
  35. private static final int[] slotsTop = new int[] {0};
  36. private static final int[] slotsBottom = new int[] {2, 1};
  37. private static final int[] slotsSides = new int[] {1};
  38. private ItemStack[] slots = new ItemStack[4];
  39. private int furnaceBurnTime;
  40. private int currentItemBurnTime;
  41. private int cookTime;
  42. private int totalCookTime;
  43. private String furnaceCustomName;
  44. private static final String __OBFID = "CL_00000357";
  45.  
  46. public int getSizeInventory()
  47. {
  48. return this.slots.length;
  49. }
  50.  
  51. public ItemStack getStackInSlot(int index)
  52. {
  53. return this.slots[index];
  54. }
  55.  
  56. public ItemStack decrStackSize(int index, int count)
  57. {
  58. if (this.slots[index] != null)
  59. {
  60. ItemStack itemstack;
  61.  
  62. if (this.slots[index].stackSize <= count)
  63. {
  64. itemstack = this.slots[index];
  65. this.slots[index] = null;
  66. return itemstack;
  67. }
  68. else
  69. {
  70. itemstack = this.slots[index].splitStack(count);
  71.  
  72. if (this.slots[index].stackSize == 0)
  73. {
  74. this.slots[index] = null;
  75. }
  76.  
  77. return itemstack;
  78. }
  79. }
  80. else
  81. {
  82. return null;
  83. }
  84. }
  85.  
  86. public ItemStack getStackInSlotOnClosing(int index)
  87. {
  88. if (this.slots[index] != null)
  89. {
  90. ItemStack itemstack = this.slots[index];
  91. this.slots[index] = null;
  92. return itemstack;
  93. }
  94. else
  95. {
  96. return null;
  97. }
  98. }
  99.  
  100. public void setInventorySlotContents(int index, ItemStack stack)
  101. {
  102. boolean flag = stack != null && stack.isItemEqual(this.slots[index]) && ItemStack.areItemStackTagsEqual(stack, this.slots[index]);
  103. this.slots[index] = stack;
  104.  
  105. if (stack != null && stack.stackSize > this.getInventoryStackLimit())
  106. {
  107. stack.stackSize = this.getInventoryStackLimit();
  108. }
  109.  
  110. if (index == 0 && !flag)
  111. {
  112. this.totalCookTime = this.func_174904_a(stack);
  113. this.cookTime = 0;
  114. this.markDirty();
  115. }
  116. }
  117.  
  118. public String getName()
  119. {
  120. return this.hasCustomName() ? this.furnaceCustomName : "container.cookFurnaceFurnace";
  121. }
  122.  
  123. public boolean hasCustomName()
  124. {
  125. return this.furnaceCustomName != null && this.furnaceCustomName.length() > 0;
  126. }
  127.  
  128. public void setCustomInventoryName(String p_145951_1_)
  129. {
  130. this.furnaceCustomName = p_145951_1_;
  131. }
  132.  
  133. public void readFromNBT(NBTTagCompound compound)
  134. {
  135. super.readFromNBT(compound);
  136. NBTTagList nbttaglist = compound.getTagList("Items", 10);
  137. this.slots = new ItemStack[this.getSizeInventory()];
  138.  
  139. for (int i = 0; i < nbttaglist.tagCount(); ++i)
  140. {
  141. NBTTagCompound nbttagcompound1 = nbttaglist.getCompoundTagAt(i);
  142. byte b0 = nbttagcompound1.getByte("Slot");
  143.  
  144. if (b0 >= 0 && b0 < this.slots.length)
  145. {
  146. this.slots[b0] = ItemStack.loadItemStackFromNBT(nbttagcompound1);
  147. }
  148. }
  149.  
  150. this.furnaceBurnTime = compound.getShort("BurnTime");
  151. this.cookTime = compound.getShort("CookTime");
  152. this.totalCookTime = compound.getShort("CookTimeTotal");
  153. this.currentItemBurnTime = getItemBurnTime(this.slots[1]);
  154.  
  155. if (compound.hasKey("CustomName", 8))
  156. {
  157. this.furnaceCustomName = compound.getString("CustomName");
  158. }
  159. }
  160.  
  161. public void writeToNBT(NBTTagCompound compound)
  162. {
  163. super.writeToNBT(compound);
  164. compound.setShort("BurnTime", (short)this.furnaceBurnTime);
  165. compound.setShort("CookTime", (short)this.cookTime);
  166. compound.setShort("CookTimeTotal", (short)this.totalCookTime);
  167. NBTTagList nbttaglist = new NBTTagList();
  168.  
  169. for (int i = 0; i < this.slots.length; ++i)
  170. {
  171. if (this.slots[i] != null)
  172. {
  173. NBTTagCompound nbttagcompound1 = new NBTTagCompound();
  174. nbttagcompound1.setByte("Slot", (byte)i);
  175. this.slots[i].writeToNBT(nbttagcompound1);
  176. nbttaglist.appendTag(nbttagcompound1);
  177. }
  178. }
  179.  
  180. compound.setTag("Items", nbttaglist);
  181.  
  182. if (this.hasCustomName())
  183. {
  184. compound.setString("CustomName", this.furnaceCustomName);
  185. }
  186. }
  187.  
  188. public int getInventoryStackLimit()
  189. {
  190. return 64;
  191. }
  192.  
  193. public boolean isBurning()
  194. {
  195. return this.furnaceBurnTime > 0;
  196. }
  197.  
  198. @SideOnly(Side.CLIENT)
  199. public static boolean isBurning(IInventory p_174903_0_)
  200. {
  201. return p_174903_0_.getField(0) > 0;
  202. }
  203.  
  204. public void update()
  205. {
  206. boolean flag = this.isBurning();
  207. boolean flag1 = false;
  208.  
  209. if (this.isBurning())
  210. {
  211. --this.furnaceBurnTime;
  212. }
  213.  
  214. if (!this.worldObj.isRemote)
  215. {
  216. if (!this.isBurning() && (this.slots[1] == null || this.slots[0] == null))
  217. {
  218. if (!this.isBurning() && this.cookTime > 0)
  219. {
  220. this.cookTime = MathHelper.clamp_int(this.cookTime - 2, 0, this.totalCookTime);
  221. }
  222. }
  223. else
  224. {
  225. if (!this.isBurning() && this.canSmelt())
  226. {
  227. this.currentItemBurnTime = this.furnaceBurnTime = getItemBurnTime(this.slots[1]);
  228.  
  229. if (this.isBurning())
  230. {
  231. flag1 = true;
  232.  
  233. if (this.slots[1] != null)
  234. {
  235. --this.slots[1].stackSize;
  236.  
  237. if (this.slots[1].stackSize == 0)
  238. {
  239. this.slots[1] = slots[1].getItem().getContainerItem(slots[1]);
  240. }
  241. }
  242. }
  243. }
  244.  
  245. if (this.isBurning() && this.canSmelt())
  246. {
  247. ++this.cookTime;
  248.  
  249. if (this.cookTime == this.totalCookTime)
  250. {
  251. this.cookTime = 0;
  252. this.totalCookTime = this.func_174904_a(this.slots[0]);
  253. this.smeltItem();
  254. flag1 = true;
  255. }
  256. }
  257. else
  258. {
  259. this.cookTime = 0;
  260. }
  261. }
  262.  
  263. if (flag != this.isBurning())
  264. {
  265. flag1 = true;
  266. BlockFurnace.setState(this.isBurning(), this.worldObj, this.pos);
  267. }
  268. }
  269.  
  270. if (flag1)
  271. {
  272. this.markDirty();
  273. }
  274. }
  275.  
  276. public int func_174904_a(ItemStack p_174904_1_)
  277. {
  278. return 200;
  279. }
  280.  
  281. private boolean canSmelt()
  282. {
  283. if (this.slots[0] == null)
  284. {
  285. return false;
  286. }
  287. else
  288. {
  289. ItemStack itemstack = FurnaceRecipes.instance().getSmeltingResult(this.slots[0]);
  290. if (itemstack == null) return false;
  291. if (this.slots[2] == null) return true;
  292. if (!this.slots[2].isItemEqual(itemstack)) return false;
  293. int result = slots[2].stackSize + itemstack.stackSize;
  294. return result <= getInventoryStackLimit() && result <= this.slots[2].getMaxStackSize(); //Forge BugFix: Make it respect stack sizes properly.
  295. }
  296. }
  297.  
  298. public void smeltItem()
  299. {
  300. if (this.canSmelt())
  301. {
  302. ItemStack itemstack = FurnaceRecipes.instance().getSmeltingResult(this.slots[0]);
  303.  
  304. if (this.slots[2] == null)
  305. {
  306. this.slots[2] = itemstack.copy();
  307. }
  308. else if (this.slots[2].getItem() == itemstack.getItem())
  309. {
  310. this.slots[2].stackSize += itemstack.stackSize; // Forge BugFix: Results may have multiple items
  311. }
  312.  
  313. if (this.slots[0].getItem() == Item.getItemFromBlock(Blocks.sponge) && this.slots[0].getMetadata() == 1 && this.slots[1] != null && this.slots[1].getItem() == Items.bucket)
  314. {
  315. this.slots[1] = new ItemStack(Items.water_bucket);
  316. }
  317.  
  318. --this.slots[0].stackSize;
  319.  
  320. if (this.slots[0].stackSize <= 0)
  321. {
  322. this.slots[0] = null;
  323. }
  324. }
  325. }
  326.  
  327. public static int getItemBurnTime(ItemStack p_145952_0_)
  328. {
  329. if (p_145952_0_ == null)
  330. {
  331. return 0;
  332. }
  333. else
  334. {
  335. Item item = p_145952_0_.getItem();
  336.  
  337. if (item instanceof ItemBlock && Block.getBlockFromItem(item) != Blocks.air)
  338. {
  339. Block block = Block.getBlockFromItem(item);
  340.  
  341. if (block == Blocks.wooden_slab)
  342. {
  343. return 150;
  344. }
  345.  
  346. if (block.getMaterial() == Material.wood)
  347. {
  348. return 300;
  349. }
  350.  
  351. if (block == Blocks.coal_block)
  352. {
  353. return 16000;
  354. }
  355. }
  356.  
  357. if (item instanceof ItemTool && ((ItemTool)item).getToolMaterialName().equals("WOOD")) return 200;
  358. if (item instanceof ItemSword && ((ItemSword)item).getToolMaterialName().equals("WOOD")) return 200;
  359. if (item instanceof ItemHoe && ((ItemHoe)item).getMaterialName().equals("WOOD")) return 200;
  360. if (item == Items.stick) return 100;
  361. if (item == Items.coal) return 1600;
  362. if (item == Items.lava_bucket) return 20000;
  363. if (item == Item.getItemFromBlock(Blocks.sapling)) return 100;
  364. if (item == Items.blaze_rod) return 2400;
  365. return net.minecraftforge.fml.common.registry.GameRegistry.getFuelValue(p_145952_0_);
  366. }
  367. }
  368.  
  369. public static boolean isItemFuel(ItemStack p_145954_0_)
  370. {
  371. return getItemBurnTime(p_145954_0_) > 0;
  372. }
  373.  
  374. public boolean isUseableByPlayer(EntityPlayer player)
  375. {
  376. return this.worldObj.getTileEntity(this.pos) != this ? false : player.getDistanceSq((double)this.pos.getX() + 0.5D, (double)this.pos.getY() + 0.5D, (double)this.pos.getZ() + 0.5D) <= 64.0D;
  377. }
  378.  
  379. public void openInventory(EntityPlayer player) {}
  380.  
  381. public void closeInventory(EntityPlayer player) {}
  382.  
  383. public boolean isItemValidForSlot(int index, ItemStack stack)
  384. {
  385. return index == 2 ? false : (index != 1 ? true : isItemFuel(stack) || SlotFurnaceFuel.isBucket(stack));
  386. }
  387.  
  388. public int[] getSlotsForFace(EnumFacing side)
  389. {
  390. return side == EnumFacing.DOWN ? slotsBottom : (side == EnumFacing.UP ? slotsTop : slotsSides);
  391. }
  392.  
  393. public boolean canInsertItem(int index, ItemStack itemStackIn, EnumFacing direction)
  394. {
  395. return this.isItemValidForSlot(index, itemStackIn);
  396. }
  397.  
  398. public boolean canExtractItem(int index, ItemStack stack, EnumFacing direction)
  399. {
  400. if (direction == EnumFacing.DOWN && index == 1)
  401. {
  402. Item item = stack.getItem();
  403.  
  404. if (item != Items.water_bucket && item != Items.bucket)
  405. {
  406. return false;
  407. }
  408. }
  409.  
  410. return true;
  411. }
  412.  
  413. public String getGuiID()
  414. {
  415. return "chef:cookFurnace";
  416. }
  417.  
  418. public Container createContainer(InventoryPlayer playerInventory, EntityPlayer playerIn)
  419. {
  420. return new ContainerCookingFurnace(playerInventory, this);
  421. }
  422.  
  423. public int getField(int id)
  424. {
  425. switch (id)
  426. {
  427. case 0:
  428. return this.furnaceBurnTime;
  429. case 1:
  430. return this.currentItemBurnTime;
  431. case 2:
  432. return this.cookTime;
  433. case 3:
  434. return this.totalCookTime;
  435. default:
  436. return 0;
  437. }
  438. }
  439.  
  440. public void setField(int id, int value)
  441. {
  442. switch (id)
  443. {
  444. case 0:
  445. this.furnaceBurnTime = value;
  446. break;
  447. case 1:
  448. this.currentItemBurnTime = value;
  449. break;
  450. case 2:
  451. this.cookTime = value;
  452. break;
  453. case 3:
  454. this.totalCookTime = value;
  455. }
  456. }
  457.  
  458. public int getFieldCount()
  459. {
  460. return 4;
  461. }
  462.  
  463. public void clear()
  464. {
  465. for (int i = 0; i < this.slots.length; ++i)
  466. {
  467. this.slots[i] = null;
  468. }
  469. }
  470. }
Advertisement
Add Comment
Please, Sign In to add comment