grossik

Untitled

Nov 16th, 2016
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.17 KB | None | 0 0
  1. package cz.grossik.farmcraft2.pan;
  2.  
  3. import javax.annotation.Nullable;
  4.  
  5. import net.minecraft.entity.player.EntityPlayer;
  6. import net.minecraft.entity.player.InventoryPlayer;
  7. import net.minecraft.init.Items;
  8. import net.minecraft.inventory.Container;
  9. import net.minecraft.inventory.IInventory;
  10. import net.minecraft.inventory.ISidedInventory;
  11. import net.minecraft.inventory.ItemStackHelper;
  12. import net.minecraft.item.Item;
  13. import net.minecraft.item.ItemStack;
  14. import net.minecraft.nbt.NBTTagCompound;
  15. import net.minecraft.nbt.NBTTagList;
  16. import net.minecraft.tileentity.TileEntityLockable;
  17. import net.minecraft.util.EnumFacing;
  18. import net.minecraft.util.ITickable;
  19. import net.minecraftforge.fml.relauncher.Side;
  20. import net.minecraftforge.fml.relauncher.SideOnly;
  21.  
  22. public class TileEntityPan extends TileEntityLockable implements ITickable, ISidedInventory
  23. {
  24. //0 je nalevo, 2 napravo
  25. private static final int[] slotsTop = new int[] {0};
  26. private static final int[] slotsBottom = new int[] {2};
  27. private static final int[] slotsSides = new int[] {0};
  28. private ItemStack[] furnaceItemStacks = new ItemStack[3];
  29. private int furnaceBurnTime;
  30. private int cookTime;
  31. private int totalCookTime;
  32. private String furnaceCustomName;
  33.  
  34. public int getSizeInventory()
  35. {
  36. return this.furnaceItemStacks.length;
  37. }
  38.  
  39. public ItemStack getStackInSlot(int index)
  40. {
  41. return this.furnaceItemStacks[index];
  42. }
  43.  
  44. @Nullable
  45. public ItemStack decrStackSize(int index, int count)
  46. {
  47. return ItemStackHelper.getAndSplit(this.furnaceItemStacks, index, count);
  48. }
  49.  
  50. @Nullable
  51. public ItemStack removeStackFromSlot(int index)
  52. {
  53. return ItemStackHelper.getAndRemove(this.furnaceItemStacks, index);
  54. }
  55.  
  56. public boolean isBurning()
  57. {
  58. return this.totalCookTime > 0;
  59. }
  60.  
  61. public void setInventorySlotContents(int index, ItemStack stack)
  62. {
  63. boolean flag = stack != null && stack.isItemEqual(this.furnaceItemStacks[index]) && ItemStack.areItemStackTagsEqual(stack, this.furnaceItemStacks[index]);
  64. this.furnaceItemStacks[index] = stack;
  65.  
  66. if (stack != null && stack.stackSize > this.getInventoryStackLimit())
  67. {
  68. stack.stackSize = this.getInventoryStackLimit();
  69. }
  70.  
  71. if (index == 0 && !flag)
  72. {
  73. this.totalCookTime = 120;
  74. this.cookTime = 0;
  75. this.markDirty();
  76. }
  77. }
  78.  
  79. public String getName()
  80. {
  81. return this.hasCustomName() ? this.furnaceCustomName : "Pan";
  82. }
  83.  
  84. public boolean hasCustomName()
  85. {
  86. return this.furnaceCustomName != null && !this.furnaceCustomName.isEmpty();
  87. }
  88.  
  89. public void setCustomInventoryName(String p_145951_1_)
  90. {
  91. this.furnaceCustomName = p_145951_1_;
  92. }
  93.  
  94. public void readFromNBT(NBTTagCompound compound)
  95. {
  96. super.readFromNBT(compound);
  97. NBTTagList nbttaglist = compound.getTagList("Items", 10);
  98. this.furnaceItemStacks = new ItemStack[this.getSizeInventory()];
  99.  
  100. for (int i = 0; i < nbttaglist.tagCount(); ++i)
  101. {
  102. NBTTagCompound nbttagcompound = nbttaglist.getCompoundTagAt(i);
  103. int j = nbttagcompound.getByte("Slot");
  104.  
  105. if (j >= 0 && j < this.furnaceItemStacks.length)
  106. {
  107. this.furnaceItemStacks[j] = ItemStack.loadItemStackFromNBT(nbttagcompound);
  108. }
  109. }
  110.  
  111. this.furnaceBurnTime = compound.getInteger("BurnTime");
  112. this.cookTime = compound.getInteger("CookTime");
  113. this.totalCookTime = compound.getInteger("CookTimeTotal");
  114.  
  115. if (compound.hasKey("CustomName", 8))
  116. {
  117. this.furnaceCustomName = compound.getString("CustomName");
  118. }
  119. }
  120.  
  121. public NBTTagCompound writeToNBT(NBTTagCompound compound)
  122. {
  123. super.writeToNBT(compound);
  124. compound.setInteger("BurnTime", this.furnaceBurnTime);
  125. compound.setInteger("CookTime", this.cookTime);
  126. compound.setInteger("CookTimeTotal", this.totalCookTime);
  127. NBTTagList nbttaglist = new NBTTagList();
  128.  
  129. for (int i = 0; i < this.furnaceItemStacks.length; ++i)
  130. {
  131. if (this.furnaceItemStacks[i] != null)
  132. {
  133. NBTTagCompound nbttagcompound = new NBTTagCompound();
  134. nbttagcompound.setByte("Slot", (byte)i);
  135. this.furnaceItemStacks[i].writeToNBT(nbttagcompound);
  136. nbttaglist.appendTag(nbttagcompound);
  137. }
  138. }
  139.  
  140. compound.setTag("Items", nbttaglist);
  141.  
  142. if (this.hasCustomName())
  143. {
  144. compound.setString("CustomName", this.furnaceCustomName);
  145. }
  146. return compound;
  147. }
  148.  
  149. public int getInventoryStackLimit()
  150. {
  151. return 1;
  152. }
  153.  
  154. @SideOnly(Side.CLIENT)
  155. public static boolean isBurning(IInventory p_174903_0_)
  156. {
  157. return p_174903_0_.getField(3) > 0;
  158. }
  159.  
  160. public void update()
  161. {
  162. boolean flag = this.isBurning();
  163. boolean flag1 = false;
  164.  
  165. if (this.isBurning() && this.canSmelt())
  166. {
  167. ++this.cookTime;
  168.  
  169. if (this.cookTime == this.totalCookTime)
  170. {
  171. this.cookTime = 0;
  172. this.totalCookTime = this.getCookTime(this.furnaceItemStacks[2]);
  173. this.smeltItem();
  174. flag1 = true;
  175. }
  176. }
  177. else
  178. {
  179. this.cookTime = 0;
  180. this.totalCookTime = 0;
  181. }
  182. }
  183.  
  184. public int getCookTime(ItemStack stack)
  185. {
  186. return 0;
  187. }
  188.  
  189. private boolean canSmelt()
  190. {
  191. if (this.furnaceItemStacks[0] == null)
  192. {
  193. return false;
  194. }
  195. else
  196. {
  197. ItemStack itemstack = PanRecipes.instance().getSmeltingResult(this.furnaceItemStacks[0]);
  198. if (itemstack == null) return false;
  199. if (this.furnaceItemStacks[2] == null) return true;
  200. if (!this.furnaceItemStacks[2].isItemEqual(itemstack)) return false;
  201. int result = furnaceItemStacks[2].stackSize + itemstack.stackSize;
  202. return result <= getInventoryStackLimit() && result <= this.furnaceItemStacks[2].getMaxStackSize(); //Forge BugFix: Make it respect stack sizes properly.
  203. }
  204. }
  205.  
  206. /**
  207. * Turn one item from the furnace source stack into the appropriate smelted item in the furnace result stack
  208. */
  209. public void smeltItem()
  210. {
  211. if (this.canSmelt())
  212. {
  213. ItemStack itemstack = PanRecipes.instance().getSmeltingResult(this.furnaceItemStacks[0]);
  214.  
  215. if (this.furnaceItemStacks[2] == null)
  216. {
  217. this.furnaceItemStacks[2] = itemstack.copy();
  218. }
  219. else if (this.furnaceItemStacks[2].getItem() == itemstack.getItem())
  220. {
  221. this.furnaceItemStacks[2].stackSize += itemstack.stackSize; // Forge BugFix: Results may have multiple items
  222. }
  223.  
  224. --this.furnaceItemStacks[0].stackSize;
  225.  
  226. if (this.furnaceItemStacks[0].stackSize <= 0)
  227. {
  228. this.furnaceItemStacks[0] = null;
  229. }
  230. }
  231. }
  232.  
  233. public boolean isItemFuel(ItemStack p_145954_0_)
  234. {
  235. return this.totalCookTime > 0;
  236. }
  237.  
  238. /**
  239. * Do not make give this method the name canInteractWith because it clashes with Container
  240. */
  241. public boolean isUseableByPlayer(EntityPlayer player)
  242. {
  243. 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;
  244. }
  245.  
  246. public void openInventory(EntityPlayer player)
  247. {
  248. }
  249.  
  250. public void closeInventory(EntityPlayer player)
  251. {
  252. }
  253.  
  254. /**
  255. * Returns true if automation is allowed to insert the given stack (ignoring stack size) into the given slot.
  256. */
  257. public boolean isItemValidForSlot(int index, ItemStack stack)
  258. {
  259. if (index == 2)
  260. {
  261. return false;
  262. }
  263. return true;
  264. }
  265.  
  266. public int[] getSlotsForFace(EnumFacing side)
  267. {
  268. return side == EnumFacing.DOWN ? slotsBottom : (side == EnumFacing.UP ? slotsTop : slotsSides);
  269. }
  270.  
  271. public boolean canInsertItem(int index, ItemStack itemStackIn, EnumFacing direction)
  272. {
  273. return this.isItemValidForSlot(index, itemStackIn);
  274. }
  275.  
  276. public boolean canExtractItem(int index, ItemStack stack, EnumFacing direction)
  277. {
  278. if (direction == EnumFacing.DOWN && index == 1)
  279. {
  280. Item item = stack.getItem();
  281.  
  282. if (item != Items.WATER_BUCKET && item != Items.BUCKET)
  283. {
  284. return false;
  285. }
  286. }
  287.  
  288. return true;
  289. }
  290.  
  291. public String getGuiID()
  292. {
  293. return "10";
  294. }
  295.  
  296. public Container createContainer(InventoryPlayer playerInventory, EntityPlayer playerIn)
  297. {
  298. return new ContainerPan(playerInventory, this);
  299. }
  300.  
  301. public int getField(int id)
  302. {
  303. switch (id)
  304. {
  305. case 0:
  306. return this.furnaceBurnTime;
  307. case 1:
  308. return this.furnaceBurnTime;
  309. case 2:
  310. return this.cookTime;
  311. case 3:
  312. return this.totalCookTime;
  313. default:
  314. return 0;
  315. }
  316. }
  317.  
  318. public void setField(int id, int value)
  319. {
  320. switch (id)
  321. {
  322. case 0:
  323. this.furnaceBurnTime = value;
  324. break;
  325. case 1:
  326.  
  327. break;
  328. case 2:
  329. this.cookTime = value;
  330. break;
  331. case 3:
  332. this.totalCookTime = value;
  333. }
  334. }
  335.  
  336. public int getFieldCount()
  337. {
  338. return 4;
  339. }
  340.  
  341. public void clear()
  342. {
  343. for (int i = 0; i < this.furnaceItemStacks.length; ++i)
  344. {
  345. this.furnaceItemStacks[i] = null;
  346. }
  347. }
  348.  
  349. net.minecraftforge.items.IItemHandler handlerTop = new net.minecraftforge.items.wrapper.SidedInvWrapper(this, net.minecraft.util.EnumFacing.UP);
  350. net.minecraftforge.items.IItemHandler handlerBottom = new net.minecraftforge.items.wrapper.SidedInvWrapper(this, net.minecraft.util.EnumFacing.DOWN);
  351. net.minecraftforge.items.IItemHandler handlerSide = new net.minecraftforge.items.wrapper.SidedInvWrapper(this, net.minecraft.util.EnumFacing.WEST);
  352.  
  353. @SuppressWarnings("unchecked")
  354. @Override
  355. public <T> T getCapability(net.minecraftforge.common.capabilities.Capability<T> capability, net.minecraft.util.EnumFacing facing)
  356. {
  357. if (facing != null && capability == net.minecraftforge.items.CapabilityItemHandler.ITEM_HANDLER_CAPABILITY)
  358. if (facing == EnumFacing.DOWN)
  359. return (T) handlerBottom;
  360. else if (facing == EnumFacing.UP)
  361. return (T) handlerTop;
  362. else
  363. return (T) handlerSide;
  364. return super.getCapability(capability, facing);
  365. }
  366. }
Advertisement
Add Comment
Please, Sign In to add comment