Guest User

Untitled

a guest
Jan 22nd, 2018
171
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package forgingaura.forgeyourworld.ffactory.tileentity;
  2.  
  3. import forgingaura.forgeyourworld.ffactory.Container.ContainerSteampunkFurnace;
  4. import forgingaura.forgeyourworld.ffactory.block.SteampunkFurnace;
  5. import forgingaura.forgeyourworld.ffactory.gui.GuiSteampunkFurnace;
  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.*;
  13. import net.minecraft.item.*;
  14. import net.minecraft.item.crafting.FurnaceRecipes;
  15. import net.minecraft.nbt.NBTTagCompound;
  16. import net.minecraft.tileentity.TileEntityLockable;
  17. import net.minecraft.util.EnumFacing;
  18. import net.minecraft.util.ITickable;
  19. import net.minecraft.util.NonNullList;
  20. import net.minecraft.util.datafix.DataFixer;
  21. import net.minecraft.util.datafix.FixTypes;
  22. import net.minecraft.util.datafix.walkers.ItemStackDataLists;
  23. import net.minecraft.util.math.MathHelper;
  24. import net.minecraftforge.fml.relauncher.Side;
  25. import net.minecraftforge.fml.relauncher.SideOnly;
  26.  
  27. public class TileEntitySteampunkFurnace extends TileEntityLockable implements ITickable, ISidedInventory {
  28. private static final int[] SLOTS_TOP = new int[] {0};
  29. private static final int[] SLOTS_BOTTOM = new int[] {2, 1};
  30. private static final int[] SLOTS_SIDES = new int[] {1};
  31. /** The ItemStacks that hold the items currently being used in the furnace */
  32. private NonNullList<ItemStack> furnaceItemStacks = NonNullList.<ItemStack>withSize(3, ItemStack.EMPTY);
  33. /** The number of ticks that the furnace will keep burning */
  34. private int furnaceBurnTime;
  35. /** The number of ticks that a fresh copy of the currently-burning item would keep the furnace burning for */
  36. private int currentItemBurnTime;
  37. private int cookTime;
  38. private int totalCookTime;
  39. private String furnaceCustomName;
  40.  
  41. /**
  42. * Returns the number of slots in the inventory.
  43. */
  44. public int getSizeInventory()
  45. {
  46. return this.furnaceItemStacks.size();
  47. }
  48.  
  49. public boolean isEmpty()
  50. {
  51. for (ItemStack itemstack : this.furnaceItemStacks)
  52. {
  53. if (!itemstack.isEmpty())
  54. {
  55. return false;
  56. }
  57. }
  58.  
  59. return true;
  60. }
  61.  
  62. /**
  63. * Returns the stack in the given slot.
  64. */
  65. public ItemStack getStackInSlot(int index)
  66. {
  67. return this.furnaceItemStacks.get(index);
  68. }
  69.  
  70. /**
  71. * Removes up to a specified number of items from an inventory slot and returns them in a new stack.
  72. */
  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. public ItemStack removeStackFromSlot(int index)
  82. {
  83. return ItemStackHelper.getAndRemove(this.furnaceItemStacks, index);
  84. }
  85.  
  86. /**
  87. * Sets the given item stack to the specified slot in the inventory (can be crafting or armor sections).
  88. */
  89. public void setInventorySlotContents(int index, ItemStack stack)
  90. {
  91. ItemStack itemstack = this.furnaceItemStacks.get(index);
  92. boolean flag = !stack.isEmpty() && stack.isItemEqual(itemstack) && ItemStack.areItemStackTagsEqual(stack, itemstack);
  93. this.furnaceItemStacks.set(index, stack);
  94.  
  95. if (stack.getCount() > this.getInventoryStackLimit())
  96. {
  97. stack.setCount(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.steampunkfurnace";
  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 registerFixesFurnace(DataFixer fixer)
  130. {
  131. fixer.registerWalker(FixTypes.BLOCK_ENTITY, new ItemStackDataLists(TileEntitySteampunkFurnace.class, new String[] {"Items"}));
  132. }
  133.  
  134. public void readFromNBT(NBTTagCompound compound)
  135. {
  136. super.readFromNBT(compound);
  137. this.furnaceItemStacks = NonNullList.<ItemStack>withSize(this.getSizeInventory(), ItemStack.EMPTY);
  138. ItemStackHelper.loadAllItems(compound, this.furnaceItemStacks);
  139. this.furnaceBurnTime = compound.getInteger("BurnTime");
  140. this.cookTime = compound.getInteger("CookTime");
  141. this.totalCookTime = compound.getInteger("CookTimeTotal");
  142. this.currentItemBurnTime = getItemBurnTime(this.furnaceItemStacks.get(1));
  143.  
  144. if (compound.hasKey("CustomName", 8))
  145. {
  146. this.furnaceCustomName = compound.getString("CustomName");
  147. }
  148. }
  149.  
  150. public NBTTagCompound writeToNBT(NBTTagCompound compound)
  151. {
  152. super.writeToNBT(compound);
  153. compound.setInteger("BurnTime", (short)this.furnaceBurnTime);
  154. compound.setInteger("CookTime", (short)this.cookTime);
  155. compound.setInteger("CookTimeTotal", (short)this.totalCookTime);
  156. ItemStackHelper.saveAllItems(compound, this.furnaceItemStacks);
  157.  
  158. if (this.hasCustomName())
  159. {
  160. compound.setString("CustomName", this.furnaceCustomName);
  161. }
  162.  
  163. return compound;
  164. }
  165.  
  166. /**
  167. * Returns the maximum stack size for a inventory slot. Seems to always be 64, possibly will be extended.
  168. */
  169. public int getInventoryStackLimit()
  170. {
  171. return 64;
  172. }
  173.  
  174. /**
  175. * Furnace isBurning
  176. */
  177. public boolean isBurning()
  178. {
  179. return this.furnaceBurnTime > 0;
  180. }
  181.  
  182. @SideOnly(Side.CLIENT)
  183. public static boolean isBurning(IInventory inventory)
  184. {
  185. return inventory.getField(0) > 0;
  186. }
  187.  
  188. /**
  189. * Like the old updateEntity(), except more generic.
  190. */
  191. public void update()
  192. {
  193. boolean flag = this.isBurning();
  194. boolean flag1 = false;
  195.  
  196. if (this.isBurning())
  197. {
  198. --this.furnaceBurnTime;
  199. }
  200.  
  201. if (!this.world.isRemote)
  202. {
  203. ItemStack itemstack = this.furnaceItemStacks.get(1);
  204.  
  205. if (this.isBurning() || !itemstack.isEmpty() && !((ItemStack)this.furnaceItemStacks.get(0)).isEmpty())
  206. {
  207. if (!this.isBurning() && this.canSmelt())
  208. {
  209. this.furnaceBurnTime = getItemBurnTime(itemstack);
  210. this.currentItemBurnTime = this.furnaceBurnTime;
  211.  
  212. if (this.isBurning())
  213. {
  214. flag1 = true;
  215.  
  216. if (!itemstack.isEmpty())
  217. {
  218. Item item = itemstack.getItem();
  219. itemstack.shrink(1);
  220.  
  221. if (itemstack.isEmpty())
  222. {
  223. ItemStack item1 = item.getContainerItem(itemstack);
  224. this.furnaceItemStacks.set(1, item1);
  225. }
  226. }
  227. }
  228. }
  229.  
  230. if (this.isBurning() && this.canSmelt())
  231. {
  232. ++this.cookTime;
  233.  
  234. if (this.cookTime == this.totalCookTime)
  235. {
  236. this.cookTime = 0;
  237. this.totalCookTime = (int)(this.getCookTime(this.furnaceItemStacks.get(0)) / 2);
  238. this.smeltItem();
  239. flag1 = true;
  240. }
  241. }
  242. else
  243. {
  244. this.cookTime = 0;
  245. }
  246. }
  247. else if (!this.isBurning() && this.cookTime > 0)
  248. {
  249. this.cookTime = MathHelper.clamp(this.cookTime - 2, 0, this.totalCookTime);
  250. }
  251.  
  252. if (flag != this.isBurning())
  253. {
  254. flag1 = true;
  255. SteampunkFurnace.setState(this.isBurning(), this.world, this.pos);
  256. }
  257. }
  258.  
  259. if (flag1)
  260. {
  261. this.markDirty();
  262. }
  263. }
  264.  
  265. public int getCookTime(ItemStack stack)
  266. {
  267. return 200;
  268. }
  269.  
  270. /**
  271. * Returns true if the furnace can smelt an item, i.e. has a source item, destination stack isn't full, etc.
  272. */
  273. private boolean canSmelt()
  274. {
  275. if (((ItemStack)this.furnaceItemStacks.get(0)).isEmpty())
  276. {
  277. return false;
  278. }
  279. else
  280. {
  281. ItemStack itemstack = FurnaceRecipes.instance().getSmeltingResult(this.furnaceItemStacks.get(0));
  282.  
  283. if (itemstack.isEmpty())
  284. {
  285. return false;
  286. }
  287. else
  288. {
  289. ItemStack itemstack1 = this.furnaceItemStacks.get(2);
  290.  
  291. if (itemstack1.isEmpty())
  292. {
  293. return true;
  294. }
  295. else if (!itemstack1.isItemEqual(itemstack))
  296. {
  297. return false;
  298. }
  299. else if (itemstack1.getCount() + itemstack.getCount() <= this.getInventoryStackLimit() && itemstack1.getCount() + itemstack.getCount() <= itemstack1.getMaxStackSize()) // Forge fix: make furnace respect stack sizes in furnace recipes
  300. {
  301. return true;
  302. }
  303. else
  304. {
  305. return itemstack1.getCount() + itemstack.getCount() <= itemstack.getMaxStackSize(); // Forge fix: make furnace respect stack sizes in furnace recipes
  306. }
  307. }
  308. }
  309. }
  310.  
  311. /**
  312. * Turn one item from the furnace source stack into the appropriate smelted item in the furnace result stack
  313. */
  314. public void smeltItem()
  315. {
  316. if (this.canSmelt())
  317. {
  318. ItemStack itemstack = this.furnaceItemStacks.get(0);
  319. ItemStack itemstack1 = FurnaceRecipes.instance().getSmeltingResult(itemstack);
  320. ItemStack itemstack2 = this.furnaceItemStacks.get(2);
  321.  
  322. if (itemstack2.isEmpty())
  323. {
  324. this.furnaceItemStacks.set(2, itemstack1.copy());
  325. }
  326. else if (itemstack2.getItem() == itemstack1.getItem())
  327. {
  328. itemstack2.grow(itemstack1.getCount());
  329. }
  330.  
  331. if (itemstack.getItem() == Item.getItemFromBlock(Blocks.SPONGE) && itemstack.getMetadata() == 1 && !((ItemStack)this.furnaceItemStacks.get(1)).isEmpty() && ((ItemStack)this.furnaceItemStacks.get(1)).getItem() == Items.BUCKET)
  332. {
  333. this.furnaceItemStacks.set(1, new ItemStack(Items.WATER_BUCKET));
  334. }
  335.  
  336. itemstack.shrink(1);
  337. }
  338. }
  339.  
  340. /**
  341. * Returns the number of ticks that the supplied fuel item will keep the furnace burning, or 0 if the item isn't
  342. * fuel
  343. */
  344. public static int getItemBurnTime(ItemStack stack)
  345. {
  346. if (stack.isEmpty())
  347. {
  348. return 0;
  349. }
  350. else
  351. {
  352. int burnTime = net.minecraftforge.event.ForgeEventFactory.getItemBurnTime(stack);
  353. if (burnTime >= 0) return burnTime;
  354. Item item = stack.getItem();
  355.  
  356. if (item == Item.getItemFromBlock(Blocks.WOODEN_SLAB))
  357. {
  358. return 150;
  359. }
  360. else if (item == Item.getItemFromBlock(Blocks.WOOL))
  361. {
  362. return 100;
  363. }
  364. else if (item == Item.getItemFromBlock(Blocks.CARPET))
  365. {
  366. return 67;
  367. }
  368. else if (item == Item.getItemFromBlock(Blocks.LADDER))
  369. {
  370. return 300;
  371. }
  372. else if (item == Item.getItemFromBlock(Blocks.WOODEN_BUTTON))
  373. {
  374. return 100;
  375. }
  376. else if (Block.getBlockFromItem(item).getDefaultState().getMaterial() == Material.WOOD)
  377. {
  378. return 300;
  379. }
  380. else if (item == Item.getItemFromBlock(Blocks.COAL_BLOCK))
  381. {
  382. return 16000;
  383. }
  384. else if (item instanceof ItemTool && "WOOD".equals(((ItemTool)item).getToolMaterialName()))
  385. {
  386. return 200;
  387. }
  388. else if (item instanceof ItemSword && "WOOD".equals(((ItemSword)item).getToolMaterialName()))
  389. {
  390. return 200;
  391. }
  392. else if (item instanceof ItemHoe && "WOOD".equals(((ItemHoe)item).getMaterialName()))
  393. {
  394. return 200;
  395. }
  396. else if (item == Items.STICK)
  397. {
  398. return 100;
  399. }
  400. else if (item != Items.BOW && item != Items.FISHING_ROD)
  401. {
  402. if (item == Items.SIGN)
  403. {
  404. return 200;
  405. }
  406. else if (item == Items.COAL)
  407. {
  408. return 1600;
  409. }
  410. else if (item == Items.LAVA_BUCKET)
  411. {
  412. return 20000;
  413. }
  414. else if (item != Item.getItemFromBlock(Blocks.SAPLING) && item != Items.BOWL)
  415. {
  416. if (item == Items.BLAZE_ROD)
  417. {
  418. return 2400;
  419. }
  420. else if (item instanceof ItemDoor && item != Items.IRON_DOOR)
  421. {
  422. return 200;
  423. }
  424. else
  425. {
  426. return item instanceof ItemBoat ? 400 : 0;
  427. }
  428. }
  429. else
  430. {
  431. return 100;
  432. }
  433. }
  434. else
  435. {
  436. return 300;
  437. }
  438. }
  439. }
  440.  
  441. public static boolean isItemFuel(ItemStack stack)
  442. {
  443. return getItemBurnTime(stack) > 0;
  444. }
  445.  
  446. /**
  447. * Don't rename this method to canInteractWith due to conflicts with Container
  448. */
  449. public boolean isUsableByPlayer(EntityPlayer player)
  450. {
  451. if (this.world.getTileEntity(this.pos) != this)
  452. {
  453. return false;
  454. }
  455. else
  456. {
  457. return player.getDistanceSq((double)this.pos.getX() + 0.5D, (double)this.pos.getY() + 0.5D, (double)this.pos.getZ() + 0.5D) <= 64.0D;
  458. }
  459. }
  460.  
  461. public void openInventory(EntityPlayer player)
  462. {
  463. }
  464.  
  465. public void closeInventory(EntityPlayer player)
  466. {
  467. }
  468.  
  469. /**
  470. * Returns true if automation is allowed to insert the given stack (ignoring stack size) into the given slot. For
  471. * guis use Slot.isItemValid
  472. */
  473. public boolean isItemValidForSlot(int index, ItemStack stack)
  474. {
  475. if (index == 2)
  476. {
  477. return false;
  478. }
  479. else if (index != 1)
  480. {
  481. return true;
  482. }
  483. else
  484. {
  485. ItemStack itemstack = this.furnaceItemStacks.get(1);
  486. return isItemFuel(stack) || SlotFurnaceFuel.isBucket(stack) && itemstack.getItem() != Items.BUCKET;
  487. }
  488. }
  489.  
  490. public int[] getSlotsForFace(EnumFacing side)
  491. {
  492. if (side == EnumFacing.DOWN)
  493. {
  494. return SLOTS_BOTTOM;
  495. }
  496. else
  497. {
  498. return side == EnumFacing.UP ? SLOTS_TOP : SLOTS_SIDES;
  499. }
  500. }
  501.  
  502. /**
  503. * Returns true if automation can insert the given item in the given slot from the given side.
  504. */
  505. public boolean canInsertItem(int index, ItemStack itemStackIn, EnumFacing direction)
  506. {
  507. return this.isItemValidForSlot(index, itemStackIn);
  508. }
  509.  
  510. /**
  511. * Returns true if automation can extract the given item in the given slot from the given side.
  512. */
  513. public boolean canExtractItem(int index, ItemStack stack, EnumFacing direction)
  514. {
  515. if (direction == EnumFacing.DOWN && index == 1)
  516. {
  517. Item item = stack.getItem();
  518.  
  519. if (item != Items.WATER_BUCKET && item != Items.BUCKET)
  520. {
  521. return false;
  522. }
  523. }
  524.  
  525. return true;
  526. }
  527.  
  528. public String getGuiID()
  529. {
  530. return "minecraft:furnace";
  531. }
  532.  
  533. public Container createContainer(InventoryPlayer playerInventory, EntityPlayer playerIn)
  534. {
  535. return new ContainerSteampunkFurnace(playerInventory, this);
  536. }
  537.  
  538. public int getField(int id)
  539. {
  540. switch (id)
  541. {
  542. case 0:
  543. return this.furnaceBurnTime;
  544. case 1:
  545. return this.currentItemBurnTime;
  546. case 2:
  547. return this.cookTime;
  548. case 3:
  549. return this.totalCookTime;
  550. default:
  551. return 0;
  552. }
  553. }
  554.  
  555. public void setField(int id, int value)
  556. {
  557. switch (id)
  558. {
  559. case 0:
  560. this.furnaceBurnTime = value;
  561. break;
  562. case 1:
  563. this.currentItemBurnTime = value;
  564. break;
  565. case 2:
  566. this.cookTime = value;
  567. break;
  568. case 3:
  569. this.totalCookTime = value;
  570. }
  571. }
  572.  
  573. public int getFieldCount()
  574. {
  575. return 4;
  576. }
  577.  
  578. public void clear()
  579. {
  580. this.furnaceItemStacks.clear();
  581. }
  582.  
  583. net.minecraftforge.items.IItemHandler handlerTop = new net.minecraftforge.items.wrapper.SidedInvWrapper(this, net.minecraft.util.EnumFacing.UP);
  584. net.minecraftforge.items.IItemHandler handlerBottom = new net.minecraftforge.items.wrapper.SidedInvWrapper(this, net.minecraft.util.EnumFacing.DOWN);
  585. net.minecraftforge.items.IItemHandler handlerSide = new net.minecraftforge.items.wrapper.SidedInvWrapper(this, net.minecraft.util.EnumFacing.WEST);
  586.  
  587. @SuppressWarnings("unchecked")
  588. @Override
  589. public <T> T getCapability(net.minecraftforge.common.capabilities.Capability<T> capability, @javax.annotation.Nullable net.minecraft.util.EnumFacing facing)
  590. {
  591. if (facing != null && capability == net.minecraftforge.items.CapabilityItemHandler.ITEM_HANDLER_CAPABILITY)
  592. if (facing == EnumFacing.DOWN)
  593. return (T) handlerBottom;
  594. else if (facing == EnumFacing.UP)
  595. return (T) handlerTop;
  596. else
  597. return (T) handlerSide;
  598. return super.getCapability(capability, facing);
  599. }
  600. }
RAW Paste Data