Advertisement
Zarbi4734

Untitled

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