grossik

Untitled

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