Guest User

tileentitywoodenfurnace

a guest
Oct 26th, 2016
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 17.90 KB | None | 0 0
  1. package dem.tutorialmod.init.blocks.furnace;
  2.  
  3. import javax.annotation.Nullable;
  4.  
  5. import dem.tutorialmod.init.blocks.furnaces.WoodenFurnace;
  6. import net.minecraft.block.Block;
  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.ItemStackHelper;
  17. import net.minecraft.inventory.SlotFurnaceFuel;
  18. import net.minecraft.item.Item;
  19. import net.minecraft.item.ItemBlock;
  20. import net.minecraft.item.ItemHoe;
  21. import net.minecraft.item.ItemStack;
  22. import net.minecraft.item.ItemSword;
  23. import net.minecraft.item.ItemTool;
  24. import net.minecraft.item.crafting.FurnaceRecipes;
  25. import net.minecraft.nbt.NBTTagCompound;
  26. import net.minecraft.nbt.NBTTagList;
  27. import net.minecraft.tileentity.TileEntityLockable;
  28. import net.minecraft.util.EnumFacing;
  29. import net.minecraft.util.ITickable;
  30. import net.minecraft.util.datafix.DataFixer;
  31. import net.minecraft.util.datafix.FixTypes;
  32. import net.minecraft.util.datafix.walkers.ItemStackDataLists;
  33. import net.minecraft.util.math.MathHelper;
  34. import net.minecraftforge.fml.relauncher.Side;
  35. import net.minecraftforge.fml.relauncher.SideOnly;
  36.  
  37. public class TileEntityWoodenFurnace extends TileEntityLockable implements ITickable, ISidedInventory {
  38.  
  39. private static final int[] SLOTS_TOP = new int[] {0};
  40. private static final int[] SLOTS_BOTTOM = new int[] {2, 1};
  41. private static final int[] SLOTS_SIDES = new int[] {1};
  42. /** The ItemStacks that hold the items currently being used in the furnace */
  43. private ItemStack[] furnaceItemStacks = new ItemStack[3];
  44. /** The number of ticks that the furnace will keep burning */
  45. private int furnaceBurnTime;
  46. /** The number of ticks that a fresh copy of the currently-burning item would keep the furnace burning for */
  47. private int currentItemBurnTime;
  48. private int cookTime;
  49. private int totalCookTime;
  50. private String furnaceCustomName;
  51.  
  52. /**
  53. * Returns the number of slots in the inventory.
  54. */
  55. public int getSizeInventory()
  56. {
  57. return this.furnaceItemStacks.length;
  58. }
  59.  
  60. /**
  61. * Returns the stack in the given slot.
  62. */
  63. @Nullable
  64. public ItemStack getStackInSlot(int index)
  65. {
  66. return this.furnaceItemStacks[index];
  67. }
  68.  
  69. /**
  70. * Removes up to a specified number of items from an inventory slot and returns them in a new stack.
  71. */
  72. @Nullable
  73. public ItemStack decrStackSize(int index, int count)
  74. {
  75. return ItemStackHelper.getAndSplit(this.furnaceItemStacks, index, count);
  76. }
  77.  
  78. /**
  79. * Removes a stack from the given slot and returns it.
  80. */
  81. @Nullable
  82. public ItemStack removeStackFromSlot(int index)
  83. {
  84. return ItemStackHelper.getAndRemove(this.furnaceItemStacks, index);
  85. }
  86.  
  87. /**
  88. * Sets the given item stack to the specified slot in the inventory (can be crafting or armor sections).
  89. */
  90. public void setInventorySlotContents(int index, @Nullable ItemStack stack)
  91. {
  92. boolean flag = stack != null && stack.isItemEqual(this.furnaceItemStacks[index]) && ItemStack.areItemStackTagsEqual(stack, this.furnaceItemStacks[index]);
  93. this.furnaceItemStacks[index] = stack;
  94.  
  95. if (stack != null && stack.stackSize > this.getInventoryStackLimit())
  96. {
  97. stack.stackSize = this.getInventoryStackLimit();
  98. }
  99.  
  100. if (index == 0 && !flag)
  101. {
  102. this.totalCookTime = this.getCookTime(stack);
  103. this.cookTime = 0;
  104. this.markDirty();
  105. }
  106. }
  107.  
  108. /**
  109. * Get the name of this object. For players this returns their username
  110. */
  111. public String getName()
  112. {
  113. return this.hasCustomName() ? this.furnaceCustomName : "container.wooden_furnace";
  114. }
  115.  
  116. /**
  117. * Returns true if this thing is named
  118. */
  119. public boolean hasCustomName()
  120. {
  121. return this.furnaceCustomName != null && !this.furnaceCustomName.isEmpty();
  122. }
  123.  
  124. public void setCustomInventoryName(String p_145951_1_)
  125. {
  126. this.furnaceCustomName = p_145951_1_;
  127. }
  128.  
  129. public static void func_189676_a(DataFixer p_189676_0_)
  130. {
  131. p_189676_0_.registerWalker(FixTypes.BLOCK_ENTITY, new ItemStackDataLists("Furnace", new String[] {"Items"}));
  132. }
  133.  
  134. public void readFromNBT(NBTTagCompound compound)
  135. {
  136. super.readFromNBT(compound);
  137. NBTTagList nbttaglist = compound.getTagList("Items", 10);
  138. this.furnaceItemStacks = new ItemStack[this.getSizeInventory()];
  139.  
  140. for (int i = 0; i < nbttaglist.tagCount(); ++i)
  141. {
  142. NBTTagCompound nbttagcompound = nbttaglist.getCompoundTagAt(i);
  143. int j = nbttagcompound.getByte("Slot");
  144.  
  145. if (j >= 0 && j < this.furnaceItemStacks.length)
  146. {
  147. this.furnaceItemStacks[j] = ItemStack.loadItemStackFromNBT(nbttagcompound);
  148. }
  149. }
  150.  
  151. this.furnaceBurnTime = compound.getInteger("BurnTime");
  152. this.cookTime = compound.getInteger("CookTime");
  153. this.totalCookTime = compound.getInteger("CookTimeTotal");
  154. this.currentItemBurnTime = getItemBurnTime(this.furnaceItemStacks[1]);
  155.  
  156. if (compound.hasKey("CustomName", 8))
  157. {
  158. this.furnaceCustomName = compound.getString("CustomName");
  159. }
  160. }
  161.  
  162. public NBTTagCompound writeToNBT(NBTTagCompound compound)
  163. {
  164. super.writeToNBT(compound);
  165. compound.setInteger("BurnTime", this.furnaceBurnTime);
  166. compound.setInteger("CookTime", this.cookTime);
  167. compound.setInteger("CookTimeTotal", this.totalCookTime);
  168. NBTTagList nbttaglist = new NBTTagList();
  169.  
  170. for (int i = 0; i < this.furnaceItemStacks.length; ++i)
  171. {
  172. if (this.furnaceItemStacks[i] != null)
  173. {
  174. NBTTagCompound nbttagcompound = new NBTTagCompound();
  175. nbttagcompound.setByte("Slot", (byte)i);
  176. this.furnaceItemStacks[i].writeToNBT(nbttagcompound);
  177. nbttaglist.appendTag(nbttagcompound);
  178. }
  179. }
  180.  
  181. compound.setTag("Items", nbttaglist);
  182.  
  183. if (this.hasCustomName())
  184. {
  185. compound.setString("CustomName", this.furnaceCustomName);
  186. }
  187.  
  188. return compound;
  189. }
  190.  
  191. /**
  192. * Returns the maximum stack size for a inventory slot. Seems to always be 64, possibly will be extended.
  193. */
  194. public int getInventoryStackLimit()
  195. {
  196. return 64;
  197. }
  198.  
  199. /**
  200. * Furnace isBurning
  201. */
  202. public boolean isBurning()
  203. {
  204. return this.furnaceBurnTime > 0;
  205. }
  206.  
  207. @SideOnly(Side.CLIENT)
  208. public static boolean isBurning(IInventory inventory)
  209. {
  210. return inventory.getField(0) > 0;
  211. }
  212.  
  213. /**
  214. * Like the old updateEntity(), except more generic.
  215. */
  216. public void update()
  217. {
  218. boolean flag = this.isBurning();
  219. boolean flag1 = false;
  220.  
  221. if (this.isBurning())
  222. {
  223. --this.furnaceBurnTime;
  224. }
  225.  
  226. if (!this.worldObj.isRemote)
  227. {
  228. if (this.isBurning() || this.furnaceItemStacks[1] != null && this.furnaceItemStacks[0] != null)
  229. {
  230. if (!this.isBurning() && this.canSmelt())
  231. {
  232. this.furnaceBurnTime = getItemBurnTime(this.furnaceItemStacks[1]);
  233. this.currentItemBurnTime = this.furnaceBurnTime;
  234.  
  235. if (this.isBurning())
  236. {
  237. flag1 = true;
  238.  
  239. if (this.furnaceItemStacks[1] != null)
  240. {
  241. --this.furnaceItemStacks[1].stackSize;
  242.  
  243. if (this.furnaceItemStacks[1].stackSize == 0)
  244. {
  245. this.furnaceItemStacks[1] = furnaceItemStacks[1].getItem().getContainerItem(furnaceItemStacks[1]);
  246. }
  247. }
  248. }
  249. }
  250.  
  251. if (this.isBurning() && this.canSmelt())
  252. {
  253. ++this.cookTime;
  254.  
  255. if (this.cookTime == this.totalCookTime)
  256. {
  257. this.cookTime = 0;
  258. this.totalCookTime = this.getCookTime(this.furnaceItemStacks[0]);
  259. this.smeltItem();
  260. flag1 = true;
  261. }
  262. }
  263. else
  264. {
  265. this.cookTime = 0;
  266. }
  267. }
  268. else if (!this.isBurning() && this.cookTime > 0)
  269. {
  270. this.cookTime = MathHelper.clamp_int(this.cookTime - 2, 0, this.totalCookTime);
  271. }
  272.  
  273. if (flag != this.isBurning())
  274. {
  275. flag1 = true;
  276. WoodenFurnace.setState(this.isBurning(), this.worldObj, this.pos);
  277. }
  278. }
  279.  
  280. if (flag1)
  281. {
  282. this.markDirty();
  283. }
  284. }
  285.  
  286. //READ ME
  287. //
  288. //TIME TO COOK SOMETHING
  289. //LOWER = FASTER
  290. //DEFAULT = 200
  291.  
  292.  
  293. public int getCookTime(@Nullable ItemStack stack)
  294. {
  295. return 500;
  296. }
  297.  
  298. /**
  299. * Returns true if the furnace can smelt an item, i.e. has a source item, destination stack isn't full, etc.
  300. */
  301. private boolean canSmelt()
  302. {
  303. if (this.furnaceItemStacks[0] == null)
  304. {
  305. return false;
  306. }
  307. else
  308. {
  309. ItemStack itemstack = FurnaceRecipes.instance().getSmeltingResult(this.furnaceItemStacks[0]);
  310. if (itemstack == null) return false;
  311. if (this.furnaceItemStacks[2] == null) return true;
  312. if (!this.furnaceItemStacks[2].isItemEqual(itemstack)) return false;
  313. int result = furnaceItemStacks[2].stackSize + itemstack.stackSize;
  314. return result <= getInventoryStackLimit() && result <= this.furnaceItemStacks[2].getMaxStackSize(); //Forge BugFix: Make it respect stack sizes properly.
  315. }
  316. }
  317.  
  318. /**
  319. * Turn one item from the furnace source stack into the appropriate smelted item in the furnace result stack
  320. */
  321. public void smeltItem()
  322. {
  323. if (this.canSmelt())
  324. {
  325. ItemStack itemstack = FurnaceRecipes.instance().getSmeltingResult(this.furnaceItemStacks[0]);
  326.  
  327. if (this.furnaceItemStacks[2] == null)
  328. {
  329. this.furnaceItemStacks[2] = itemstack.copy();
  330. }
  331. else if (this.furnaceItemStacks[2].getItem() == itemstack.getItem())
  332. {
  333. this.furnaceItemStacks[2].stackSize += itemstack.stackSize; // Forge BugFix: Results may have multiple items
  334. }
  335.  
  336. if (this.furnaceItemStacks[0].getItem() == Item.getItemFromBlock(Blocks.SPONGE) && this.furnaceItemStacks[0].getMetadata() == 1 && this.furnaceItemStacks[1] != null && this.furnaceItemStacks[1].getItem() == Items.BUCKET)
  337. {
  338. this.furnaceItemStacks[1] = new ItemStack(Items.WATER_BUCKET);
  339. }
  340.  
  341. --this.furnaceItemStacks[0].stackSize;
  342.  
  343. if (this.furnaceItemStacks[0].stackSize <= 0)
  344. {
  345. this.furnaceItemStacks[0] = null;
  346. }
  347. }
  348. }
  349.  
  350. /**
  351. * Returns the number of ticks that the supplied fuel item will keep the furnace burning, or 0 if the item isn't
  352. * fuel
  353. */
  354. public static int getItemBurnTime(ItemStack stack)
  355. {
  356. if (stack == null)
  357. {
  358. return 0;
  359. }
  360. else
  361. {
  362. Item item = stack.getItem();
  363.  
  364. if (item instanceof ItemBlock && Block.getBlockFromItem(item) != Blocks.AIR)
  365. {
  366. Block block = Block.getBlockFromItem(item);
  367.  
  368. if (block == Blocks.WOODEN_SLAB)
  369. {
  370. return 150;
  371. }
  372.  
  373. if (block.getDefaultState().getMaterial() == Material.WOOD)
  374. {
  375. return 300;
  376. }
  377.  
  378. if (block == Blocks.COAL_BLOCK)
  379. {
  380. return 16000;
  381. }
  382. }
  383.  
  384. if (item instanceof ItemTool && "WOOD".equals(((ItemTool)item).getToolMaterialName())) return 200;
  385. if (item instanceof ItemSword && "WOOD".equals(((ItemSword)item).getToolMaterialName())) return 200;
  386. if (item instanceof ItemHoe && "WOOD".equals(((ItemHoe)item).getMaterialName())) return 200;
  387. if (item == Items.STICK) return 100;
  388. if (item == Items.COAL) return 1600;
  389. if (item == Items.LAVA_BUCKET) return 20000;
  390. if (item == Item.getItemFromBlock(Blocks.SAPLING)) return 100;
  391. if (item == Items.BLAZE_ROD) return 2400;
  392. return net.minecraftforge.fml.common.registry.GameRegistry.getFuelValue(stack);
  393. }
  394. }
  395.  
  396. public static boolean isItemFuel(ItemStack stack)
  397. {
  398. /**
  399. * Returns the number of ticks that the supplied fuel item will keep the furnace burning, or 0 if the item isn't
  400. * fuel
  401. */
  402. return getItemBurnTime(stack) > 0;
  403. }
  404.  
  405. /**
  406. * Do not make give this method the name canInteractWith because it clashes with Container
  407. */
  408. public boolean isUseableByPlayer(EntityPlayer player)
  409. {
  410. 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;
  411. }
  412.  
  413. public void openInventory(EntityPlayer player)
  414. {
  415. }
  416.  
  417. public void closeInventory(EntityPlayer player)
  418. {
  419. }
  420.  
  421. /**
  422. * Returns true if automation is allowed to insert the given stack (ignoring stack size) into the given slot.
  423. */
  424. public boolean isItemValidForSlot(int index, ItemStack stack)
  425. {
  426. if (index == 2)
  427. {
  428. return false;
  429. }
  430. else if (index != 1)
  431. {
  432. return true;
  433. }
  434. else
  435. {
  436. ItemStack itemstack = this.furnaceItemStacks[1];
  437. return isItemFuel(stack) || SlotFurnaceFuel.isBucket(stack) && (itemstack == null || itemstack.getItem() != Items.BUCKET);
  438. }
  439. }
  440.  
  441. public int[] getSlotsForFace(EnumFacing side)
  442. {
  443. return side == EnumFacing.DOWN ? SLOTS_BOTTOM : (side == EnumFacing.UP ? SLOTS_TOP : SLOTS_SIDES);
  444. }
  445.  
  446. /**
  447. * Returns true if automation can insert the given item in the given slot from the given side.
  448. */
  449. public boolean canInsertItem(int index, ItemStack itemStackIn, EnumFacing direction)
  450. {
  451. return this.isItemValidForSlot(index, itemStackIn);
  452. }
  453.  
  454. /**
  455. * Returns true if automation can extract the given item in the given slot from the given side.
  456. */
  457. public boolean canExtractItem(int index, ItemStack stack, EnumFacing direction)
  458. {
  459. if (direction == EnumFacing.DOWN && index == 1)
  460. {
  461. Item item = stack.getItem();
  462.  
  463. if (item != Items.WATER_BUCKET && item != Items.BUCKET)
  464. {
  465. return false;
  466. }
  467. }
  468.  
  469. return true;
  470. }
  471.  
  472. public String getGuiID()
  473. {
  474. return "minecraft:furnace";
  475. }
  476.  
  477. public Container createContainer(InventoryPlayer playerInventory, EntityPlayer playerIn)
  478. {
  479. return new ContainerFurnace(playerInventory, this);
  480. }
  481.  
  482. public int getField(int id)
  483. {
  484. switch (id)
  485. {
  486. case 0:
  487. return this.furnaceBurnTime;
  488. case 1:
  489. return this.currentItemBurnTime;
  490. case 2:
  491. return this.cookTime;
  492. case 3:
  493. return this.totalCookTime;
  494. default:
  495. return 0;
  496. }
  497. }
  498.  
  499. public void setField(int id, int value)
  500. {
  501. switch (id)
  502. {
  503. case 0:
  504. this.furnaceBurnTime = value;
  505. break;
  506. case 1:
  507. this.currentItemBurnTime = value;
  508. break;
  509. case 2:
  510. this.cookTime = value;
  511. break;
  512. case 3:
  513. this.totalCookTime = value;
  514. }
  515. }
  516.  
  517. public int getFieldCount()
  518. {
  519. return 4;
  520. }
  521.  
  522. public void clear()
  523. {
  524. for (int i = 0; i < this.furnaceItemStacks.length; ++i)
  525. {
  526. this.furnaceItemStacks[i] = null;
  527. }
  528. }
  529.  
  530. net.minecraftforge.items.IItemHandler handlerTop = new net.minecraftforge.items.wrapper.SidedInvWrapper(this, net.minecraft.util.EnumFacing.UP);
  531. net.minecraftforge.items.IItemHandler handlerBottom = new net.minecraftforge.items.wrapper.SidedInvWrapper(this, net.minecraft.util.EnumFacing.DOWN);
  532. net.minecraftforge.items.IItemHandler handlerSide = new net.minecraftforge.items.wrapper.SidedInvWrapper(this, net.minecraft.util.EnumFacing.WEST);
  533.  
  534. @SuppressWarnings("unchecked")
  535. @Override
  536. public <T> T getCapability(net.minecraftforge.common.capabilities.Capability<T> capability, net.minecraft.util.EnumFacing facing)
  537. {
  538. if (facing != null && capability == net.minecraftforge.items.CapabilityItemHandler.ITEM_HANDLER_CAPABILITY)
  539. if (facing == EnumFacing.DOWN)
  540. return (T) handlerBottom;
  541. else if (facing == EnumFacing.UP)
  542. return (T) handlerTop;
  543. else
  544. return (T) handlerSide;
  545. return super.getCapability(capability, facing);
  546. }
  547.  
  548. }
Add Comment
Please, Sign In to add comment