Guest User

Untitled

a guest
Aug 19th, 2018
68
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package com.lezink.lezinkmod.common;
  2.  
  3. import net.minecraft.entity.player.EntityPlayer;
  4.  
  5. import net.minecraft.inventory.IInventory;
  6. import net.minecraft.item.ItemStack;
  7. import net.minecraft.nbt.NBTTagCompound;
  8. import net.minecraft.nbt.NBTTagList;
  9. import net.minecraft.tileentity.TileEntity;
  10. import net.minecraft.world.World;
  11.  
  12. public class TileEntityCompactor extends TileEntity implements IInventory {
  13.  
  14. private ItemStack[] contents = new ItemStack[2];
  15.  
  16. private int workingTime = 0;
  17. private int workingTimeNeeded = 200 ;
  18.  
  19. @Override
  20. public void writeToNBT(NBTTagCompound compound)
  21. {
  22. super.writeToNBT(compound);
  23.  
  24. NBTTagList nbttaglist = new NBTTagList();
  25.  
  26. for(int i = 0; i < this.contents.length; ++i)
  27. {
  28. if(this.contents != null)
  29. {
  30. NBTTagCompound nbttagcompound1 = new NBTTagCompound();
  31. nbttagcompound1.setByte("Slot", (byte)i);
  32. this.contents[i].writeToNBT(nbttagcompound1);
  33. nbttaglist.appendTag(nbttagcompound1);
  34. }
  35. }
  36.  
  37. compound.setTag("Items", nbttaglist);
  38. compound.setShort("workingTime", (short)this.workingTime);
  39. compound.setShort("workingTimeNeeded", (short)this.workingTimeNeeded);
  40. }
  41.  
  42. @Override
  43. public void readFromNBT(NBTTagCompound compound)
  44. {
  45. super.readFromNBT(compound);
  46.  
  47. NBTTagList nbttaglist = compound.getTagList("Items", 10);
  48. this.contents = new ItemStack[this.getSizeInventory()];
  49.  
  50. for(int i = 0; i < nbttaglist.tagCount(); ++i)
  51. {
  52. NBTTagCompound nbttagcompound1 = nbttaglist.getCompoundTagAt(i);
  53. int j = nbttagcompound1.getByte("Slot") & 255;
  54.  
  55. if(j >= 0 && j < this.contents.length)
  56. {
  57. this.contents[j] = ItemStack.loadItemStackFromNBT(nbttagcompound1);
  58. }
  59. }
  60.  
  61. this.workingTime = compound.getShort("workingTime");
  62. this.workingTimeNeeded = compound.getShort("workingTimeNeeded");
  63. }
  64.  
  65. @Override
  66. public int getSizeInventory()
  67. {
  68. return this.contents.length;
  69. }
  70.  
  71. @Override
  72. public ItemStack getStackInSlot(int slotIndex)
  73. {
  74. return this.contents[slotIndex];
  75. }
  76.  
  77. @Override
  78. public ItemStack decrStackSize(int slotIndex, int amount)
  79. {
  80. if (this.contents[slotIndex] != null)
  81. {
  82. ItemStack itemstack;
  83.  
  84. if (this.contents[slotIndex].stackSize <= amount)
  85. {
  86. itemstack = this.contents[slotIndex];
  87. this.contents[slotIndex] = null;
  88. this.markDirty();
  89.  
  90. return itemstack;
  91. }
  92. else
  93. {
  94. itemstack = this.contents[slotIndex].splitStack(amount);
  95.  
  96. if (this.contents[slotIndex].stackSize == 0)
  97. {
  98. this.contents[slotIndex] = null;
  99. }
  100.  
  101. this.markDirty();
  102.  
  103. return itemstack;
  104. }
  105. }
  106. else
  107. {
  108. return null;
  109. }
  110. }
  111.  
  112. @Override
  113. public ItemStack getStackInSlotOnClosing(int slotIndex)
  114. {
  115. if (this.contents[slotIndex] != null)
  116. {
  117. ItemStack itemstack = this.contents[slotIndex];
  118. this.contents[slotIndex] = null;
  119. return itemstack;
  120. }
  121. else
  122. {
  123. return null;
  124. }
  125. }
  126.  
  127. @Override
  128. public void setInventorySlotContents(int slotIndex, ItemStack stack)
  129. {
  130. this.contents[slotIndex] = stack;
  131.  
  132. if (stack != null && stack.stackSize > this.getInventoryStackLimit())
  133. {
  134. stack.stackSize = this.getInventoryStackLimit();
  135. }
  136.  
  137. this.markDirty();
  138. }
  139.  
  140. @Override
  141. public String getInventoryName()
  142. {
  143. return "tile.compactor";
  144. }
  145.  
  146. @Override
  147. public boolean hasCustomInventoryName()
  148. {
  149. return false;
  150. }
  151.  
  152. @Override
  153. public int getInventoryStackLimit()
  154. {
  155. return 64;
  156. }
  157.  
  158. @Override
  159. public boolean isUseableByPlayer(EntityPlayer player)
  160. {
  161. return this.worldObj.getTileEntity(this.xCoord, this.yCoord, this.zCoord) != this ? false : player.getDistanceSq((double)this.xCoord + 0.5D, (double)this.yCoord + 0.5D, (double)this.zCoord + 0.5D) <= 64.0D;
  162. }
  163.  
  164. @Override
  165. public void openInventory()
  166. {
  167.  
  168. }
  169.  
  170. @Override
  171. public void closeInventory()
  172. {
  173.  
  174. }
  175.  
  176. @Override
  177. public boolean isItemValidForSlot(int slot, ItemStack stack)
  178. {
  179. return slot == 1 ? false : true;
  180. }
  181.  
  182. public boolean isBurning()
  183. {
  184. return this.workingTime > 0;
  185. }
  186.  
  187. private boolean canSmelt()
  188. {
  189. if (this.contents[0] == null || this.contents[1] == null)
  190. {
  191. return false;
  192. }
  193. else
  194. {
  195. ItemStack itemstack = TransformatorRecipes.smelting().getSmeltingResult(new ItemStack[]{this.contents[0], this.contents[1]});
  196. if (itemstack == null) return false;
  197. if (this.contents[1] == null) return true;
  198. if (!this.contents[1].isItemEqual(itemstack)) return false;
  199. int result = contents[1].stackSize + itemstack.stackSize;
  200. return result <= getInventoryStackLimit() && result <= this.contents[1].getMaxStackSize();
  201. }
  202. }
  203.  
  204. @Override
  205. public void updateEntity()
  206. {
  207. if(this.isBurning() && this.canSmelt())
  208. {
  209. ++this.workingTime;
  210. }
  211. if(this.canSmelt() && !this.isBurning())
  212. {
  213. this.workingTime = 1;
  214. }
  215. if(this.canSmelt() && this.workingTime == this.workingTimeNeeded)
  216. {
  217. this.smeltItem();
  218. this.workingTime = 0;
  219. }
  220. if(!this.canSmelt())
  221. {
  222. this.workingTime= 0;
  223. }
  224. }
  225.  
  226. public void smeltItem()
  227. {
  228. if (this.canSmelt())
  229. {
  230. ItemStack itemstack = TransformatorRecipes.smelting().getSmeltingResult(new ItemStack[]{this.contents[0], this.contents[1]});
  231. if (this.contents[1] == null)
  232. {
  233. this.contents[1] = itemstack.copy();
  234. }
  235. else if (this.contents[1].getItem() == itemstack.getItem())
  236. {
  237. this.contents[1].stackSize += itemstack.stackSize;
  238. }
  239.  
  240. --this.contents[0].stackSize;
  241. --this.contents[1].stackSize;
  242.  
  243.  
  244. if (this.contents[0].stackSize <= 0)
  245. {
  246. this.contents[0] = null;
  247. }
  248. if (this.contents[1].stackSize <= 0)
  249. {
  250. this.contents[1] = null;
  251. }
  252.  
  253. }
  254. }
  255.  
  256. public void setCustomName(String displayName) {
  257.  
  258. }
  259. public int getCookProgressScaled(int i) {
  260. return this.workingTime * i / 200;
  261.  
  262. }
  263. public boolean onBlockActivated(World world, int x, int y, int z, EntityPlayer player, int side, float hitx, float hity, float hitz)
  264. {
  265. if (world.isRemote)
  266. {
  267. return true;
  268. }
  269. else
  270. {
  271. player.openGui(lezinkmod.instance, 1, world, x, y, z);
  272. return true;
  273. }
  274. }
  275.  
  276. }
RAW Paste Data