Advertisement
Guest User

TileEntity

a guest
Aug 29th, 2014
314
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.72 KB | None | 0 0
  1. package net.sciencecraft.tileentity;
  2.  
  3. import cpw.mods.fml.common.registry.GameRegistry;
  4. import net.minecraft.block.Block;
  5. import net.minecraft.block.material.Material;
  6. import net.minecraft.entity.player.EntityPlayer;
  7. import net.minecraft.init.Items;
  8. import net.minecraft.inventory.ISidedInventory;
  9. import net.minecraft.item.Item;
  10. import net.minecraft.item.ItemBlock;
  11. import net.minecraft.item.ItemStack;
  12. import net.minecraft.item.ItemSword;
  13. import net.minecraft.item.ItemTool;
  14. import net.minecraft.item.crafting.FurnaceRecipes;
  15. import net.minecraft.nbt.NBTTagCompound;
  16. import net.minecraft.nbt.NBTTagList;
  17. import net.minecraft.tileentity.TileEntity;
  18. import net.sciencecraft.machines.Grinder;
  19. import net.sciencecraft.main.ScienceCraft;
  20. import net.sciencecraft.recipes.ScienceCraft_recipes;
  21.  
  22. public class TileEntityGrinder extends TileEntity implements ISidedInventory
  23. {
  24. private String localizedName;
  25.  
  26. private static final int[] slots_top = new int[] {0};
  27. private static final int[] slots_bottom = new int[] {2};
  28. private static final int[] slots_sides = new int[] {1};
  29.  
  30. private ItemStack[] slots = new ItemStack[3];
  31.  
  32. /** Furnace speed */
  33. public int furnaceSpeed = 450; //200 is normaal, hoe kleiner, hoe sneller
  34.  
  35. /** How long this furnace will continue to burn for (fuel) */
  36. public int power;
  37. public final int maxPower = 10000;
  38.  
  39. /** How long time left before cooked */
  40. public int cookTime;
  41.  
  42. public int getSizeInventory()
  43. {
  44. return this.slots.length;
  45. }
  46.  
  47. public String getInventoryName()
  48. {
  49. return this.hasCustomInventoryName() ? this.localizedName : "container.Grinder";
  50. }
  51.  
  52. public boolean hasCustomInventoryName()
  53. {
  54. return this.localizedName != null && this.localizedName.length() > 0;
  55. }
  56.  
  57. public void setGuiDisplayName(String displayName)
  58. {
  59. this.localizedName = displayName;
  60. }
  61.  
  62. public ItemStack getStackInSlot(int i)
  63. {
  64. return this.slots[i];
  65. }
  66.  
  67. public ItemStack decrStackSize(int i, int j)
  68. {
  69. if (this.slots[i] != null)
  70. {
  71. ItemStack itemstack;
  72.  
  73. if (this.slots[i].stackSize <= j)
  74. {
  75. itemstack = this.slots[i];
  76. this.slots[i] = null;
  77. return itemstack;
  78. }
  79. else
  80. {
  81. itemstack = this.slots[i].splitStack(j);
  82.  
  83. if (this.slots[i].stackSize == 0)
  84. {
  85. this.slots[i] = null;
  86. }
  87.  
  88. return itemstack;
  89. }
  90. }
  91.  
  92. return null;
  93. }
  94.  
  95. public ItemStack getStackInSlotOnClosing(int i)
  96. {
  97. if (this.slots[i] != null)
  98. {
  99. ItemStack itemstack = this.slots[i];
  100. this.slots[i] = null;
  101. return itemstack;
  102. }
  103.  
  104. return null;
  105. }
  106.  
  107. public void setInventorySlotContents(int i, ItemStack itemstack)
  108. {
  109. this.slots[i] = itemstack;
  110.  
  111. if (itemstack != null && itemstack.stackSize > this.getInventoryStackLimit())
  112. {
  113. itemstack.stackSize = this.getInventoryStackLimit();
  114. }
  115. }
  116.  
  117. public int getInventoryStackLimit()
  118. {
  119. return 64;
  120. }
  121.  
  122. public void readFromNBT(NBTTagCompound nbt)
  123. {
  124. super.readFromNBT(nbt);
  125. NBTTagList list = nbt.getTagList("Items", 10);
  126. this.slots = new ItemStack[this.getSizeInventory()];
  127.  
  128. for (int i = 0; i < list.tagCount(); i++)
  129. {
  130. NBTTagCompound compound = (NBTTagCompound) list.getCompoundTagAt(i);
  131. byte b = compound.getByte("Slot");
  132.  
  133. if (b >= 0 && b < this.slots.length)
  134. {
  135. this.slots[b] = ItemStack.loadItemStackFromNBT(compound);
  136. }
  137. }
  138.  
  139. this.power = nbt.getShort("BurnTime");
  140. this.cookTime = nbt.getShort("CookTime");
  141.  
  142. if (nbt.hasKey("CustomName"))
  143. {
  144. this.localizedName = nbt.getString("CustomName");
  145. }
  146. }
  147.  
  148. public void writeToNBT(NBTTagCompound nbt)
  149. {
  150. super.writeToNBT(nbt);
  151. nbt.setShort("BurnTime", (short)this.power);
  152. nbt.setShort("CookTime", (short)this.cookTime);
  153. NBTTagList list = new NBTTagList();
  154.  
  155. for (int i = 0; i < this.slots.length; i++)
  156. {
  157. if (this.slots[i] != null)
  158. {
  159. NBTTagCompound compound = new NBTTagCompound();
  160. compound.setByte("Slot", (byte)i);
  161. this.slots[i].writeToNBT(compound);
  162. list.appendTag(compound);
  163. }
  164. }
  165.  
  166. nbt.setTag("Items", list);
  167.  
  168. if (this.hasCustomInventoryName())
  169. {
  170. nbt.setString("CustomName", this.localizedName);
  171. }
  172. }
  173.  
  174. public boolean isUseableByPlayer(EntityPlayer entityplayer)
  175. {
  176. return this.worldObj.getTileEntity(this.xCoord, this.yCoord, this.zCoord) != this ? false : entityplayer.getDistanceSq((double)this.xCoord + 0.5D, (double)this.yCoord + 0.5D, (double)this.zCoord + 0.5D) <= 64.0D;
  177. }
  178.  
  179. public void openInventory() {}
  180. public void closeInventory() {}
  181.  
  182. public boolean hasPower()
  183. {
  184. return this.power > 0;
  185. }
  186.  
  187. public void updateEntity()
  188. {
  189. boolean flag = this.hasPower();
  190. boolean flag1 = false;
  191.  
  192. if (this.hasPower() && this.isMacerating())
  193. {
  194. this.power--;
  195. }
  196.  
  197. if (!this.worldObj.isRemote)
  198. {
  199. if (this.hasItemPower(this.slots[1]) && this.power < (this.maxPower - this.getItemPower(this.slots[1])))
  200. {
  201. this.power += getItemPower(this.slots[1]);
  202.  
  203. if (this.slots[1] != null)
  204. {
  205. flag1 = true;
  206. this.slots[1].stackSize--;
  207.  
  208. if (this.slots[1].stackSize == 0)
  209. {
  210. this.slots[1] = this.slots[1].getItem().getContainerItem(this.slots[1]);
  211. }
  212. }
  213. }
  214.  
  215. if (this.hasPower() && this.canSmelt())
  216. {
  217. this.cookTime++;
  218.  
  219. if (this.cookTime == this.furnaceSpeed)
  220. {
  221. this.cookTime = 0;
  222. this.smeltItem();
  223. flag1 = true;
  224. }
  225. }
  226. else
  227. {
  228. this.cookTime = 0;
  229. }
  230.  
  231. if (flag != this.hasPower())
  232. {
  233. flag1 = true;
  234. Grinder.updateGrinderBlockState(this.hasPower(), this.worldObj, this.xCoord, this.yCoord, this.zCoord);
  235. }
  236. }
  237.  
  238. if (flag1)
  239. {
  240. this.markDirty();
  241. }
  242. }
  243.  
  244. private boolean canSmelt()
  245. {
  246. if (this.slots[0] == null)
  247. {
  248. return false;
  249. }
  250. else
  251. {
  252. ItemStack itemstack = ScienceCraft_recipes.smelting().getSmeltingResult(this.slots[0]);
  253.  
  254. if (itemstack == null)
  255. {
  256. return false;
  257. }
  258.  
  259. if (this.slots[2] == null)
  260. {
  261. return true;
  262. }
  263.  
  264. if (!this.slots[2].isItemEqual(itemstack))
  265. {
  266. return false;
  267. }
  268.  
  269. int result = this.slots[2].stackSize + 2;
  270. return (result <= getInventoryStackLimit() && result <= itemstack.getMaxStackSize());
  271. }
  272. }
  273.  
  274. public void smeltItem()
  275. {
  276. if (this.canSmelt())
  277. {
  278. ItemStack itemstack = ScienceCraft_recipes.smelting().getSmeltingResult(this.slots[0]);
  279. itemstack.stackSize = 2;
  280.  
  281. if (this.slots[2] == null)
  282. {
  283. this.slots[2] = itemstack.copy();
  284. }
  285. else if (this.slots[2].isItemEqual(itemstack))
  286. {
  287. this.slots[2].stackSize += itemstack.stackSize;
  288. }
  289.  
  290. this.slots[0].stackSize--;
  291.  
  292. if (this.slots[0].stackSize <= 0)
  293. {
  294. this.slots[0] = null;
  295. }
  296. }
  297. }
  298.  
  299. public static int getItemPower(ItemStack itemstack)
  300. {
  301. if (itemstack == null)
  302. {
  303. return 0;
  304. }
  305. else
  306. {
  307. Item i = itemstack.getItem();
  308. //if(item instanceof ItemSword && ((ItemSword) item).getToolMaterialName().equals("WOOD")) return 200;
  309.  
  310. if (i == ScienceCraft.EnergyCrystal)
  311. {
  312. return 500; //200 is 1 item
  313. }
  314.  
  315. return 0;
  316. }
  317. }
  318.  
  319. public boolean isMacerating()
  320. {
  321. return this.cookTime > 0;
  322. }
  323.  
  324. public static boolean hasItemPower(ItemStack itemstack)
  325. {
  326. return getItemPower(itemstack) > 0;
  327. }
  328.  
  329. public boolean isItemValidForSlot(int i, ItemStack itemstack)
  330. {
  331. return i == 2 ? false : (i == 1 ? hasItemPower(itemstack) : true);
  332. }
  333.  
  334. public int[] getAccessibleSlotsFromSide(int var1)
  335. {
  336. return var1 == 0 ? slots_bottom : (var1 == 1 ? slots_top : slots_sides);
  337. }
  338.  
  339. public boolean canInsertItem(int i, ItemStack itemstack, int j)
  340. {
  341. return this.isItemValidForSlot(i, itemstack);
  342. }
  343.  
  344. public boolean canExtractItem(int i, ItemStack itemstack, int j)
  345. {
  346. return j != 0 || i != 1 || itemstack.getItem() == Items.bucket;
  347. }
  348.  
  349. public int getPowerRemainingScaled(int i)
  350. {
  351. return this.power * i / this.maxPower;
  352. }
  353.  
  354. public int getMacerationProgressScaled(int i)
  355. {
  356. return this.cookTime * i / this.furnaceSpeed;
  357. }
  358. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement