Advertisement
lillobby6

Untitled

Apr 19th, 2015
143
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.58 KB | None | 0 0
  1. package com.camp.tileEntity;
  2.  
  3. import net.minecraft.entity.player.EntityPlayer;
  4. import net.minecraft.init.Blocks;
  5. import net.minecraft.init.Items;
  6. import net.minecraft.inventory.ISidedInventory;
  7. import net.minecraft.item.Item;
  8. import net.minecraft.item.ItemStack;
  9. import net.minecraft.item.crafting.FurnaceRecipes;
  10. import net.minecraft.nbt.NBTTagCompound;
  11. import net.minecraft.nbt.NBTTagList;
  12. import net.minecraft.tileentity.TileEntity;
  13. import net.minecraftforge.oredict.OreDictionary;
  14.  
  15. import com.camp.block.Macerator;
  16.  
  17. import cpw.mods.fml.relauncher.Side;
  18. import cpw.mods.fml.relauncher.SideOnly;
  19.  
  20. public class TileEntityMacerator extends TileEntity implements ISidedInventory{
  21.  
  22. private String localizedName;
  23.  
  24. private static final int[] slots_top = new int[]{0};
  25. private static final int[] slots_bottom = new int[]{2, 1};
  26. private static final int[] slots_sides = new int[]{1};
  27.  
  28. private ItemStack[] slots = new ItemStack[3];
  29.  
  30. /**Furnace Speed*/
  31. public int maceratingSpeed = 70;
  32.  
  33.  
  34. /**How long this furnace will continue to burn.*/
  35. public static int power;
  36.  
  37. public int maxPower = 10000;
  38.  
  39. /**How much time left before item 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 : "Macerator";
  50. }
  51.  
  52.  
  53. public boolean hasCustomInventoryName()
  54. {
  55. return this.localizedName != null && this.localizedName.length() > 0;
  56. }
  57.  
  58. public void setGuiDisplayName(String displayName) {
  59. this.localizedName = displayName;
  60.  
  61. }
  62.  
  63. @Override
  64. public ItemStack getStackInSlot(int i) {
  65.  
  66. return this.slots[i];
  67. }
  68.  
  69. @Override
  70. public ItemStack decrStackSize(int i, int j) {
  71.  
  72. if (this.slots[i] != null)
  73. {
  74. ItemStack itemstack;
  75.  
  76. if (this.slots[i].stackSize <= j)
  77. {
  78. itemstack = this.slots[i];
  79. this.slots[i] = null;
  80. return itemstack;
  81. }
  82. else
  83. {
  84. itemstack = this.slots[i].splitStack(j);
  85.  
  86. if (this.slots[i].stackSize == 0)
  87. {
  88. this.slots[i] = null;
  89. }
  90.  
  91. return itemstack;
  92. }
  93. }
  94. else
  95. {
  96. return null;
  97. }
  98. }
  99.  
  100. @Override
  101. public ItemStack getStackInSlotOnClosing(int i) {
  102.  
  103. if (this.slots[i] != null)
  104. {
  105. ItemStack itemstack = this.slots[i];
  106. this.slots[i] = null;
  107. return itemstack;
  108. }
  109. else
  110. {
  111. return null;
  112. }
  113. }
  114.  
  115. @Override
  116. public void setInventorySlotContents(int i, ItemStack itemstack) {
  117. this.slots[i] = itemstack;
  118.  
  119. if (itemstack != null && itemstack.stackSize > this.getInventoryStackLimit())
  120. {
  121. itemstack.stackSize = this.getInventoryStackLimit();
  122. }
  123.  
  124. }
  125.  
  126. @Override
  127. public int getInventoryStackLimit() {
  128.  
  129. return 64;
  130. }
  131.  
  132. @Override
  133. public void readFromNBT(NBTTagCompound nbt)
  134. {
  135. super.readFromNBT(nbt);
  136.  
  137. NBTTagList list = nbt.getTagList("items", 10);
  138. this.slots = new ItemStack[this.getSizeInventory()];
  139.  
  140. for(int i = 0; i < list.tagCount(); i++)
  141. {
  142. NBTTagCompound compound = (NBTTagCompound) list.getCompoundTagAt(i);
  143. byte b = compound.getByte("Slot");
  144.  
  145. if(b >= 0 && b < this.slots.length)
  146. {
  147. this.slots[b] = ItemStack.loadItemStackFromNBT(compound);
  148. }
  149.  
  150. }
  151.  
  152. this.power = nbt.getShort("Power");
  153. this.cookTime = nbt.getShort("CookTime");
  154. this.maxPower = nbt.getShort("MaxPower");
  155.  
  156. if(nbt.hasKey("MaceratorCont"))
  157. {
  158. this.localizedName = nbt.getString("MaceratorCont");
  159. }
  160.  
  161. }
  162.  
  163. @Override
  164. public void writeToNBT(NBTTagCompound nbt)
  165. {
  166. super.writeToNBT(nbt);
  167.  
  168. nbt.setShort("Power", (short)this.power);
  169. nbt.setShort("CookTime", (short)this.cookTime);
  170. nbt.setShort("MaxPower", (short)this.maxPower);
  171.  
  172. NBTTagList list = new NBTTagList();
  173.  
  174. for(int i = 0; i < this.slots.length; i++)
  175. {
  176. if(this.slots[i] != null)
  177. {
  178. NBTTagCompound compound = new NBTTagCompound();
  179. compound.setByte("Slot", (byte) i );
  180. this.slots[i].writeToNBT(compound);
  181. list.appendTag(compound);
  182. }
  183. }
  184.  
  185. nbt.setTag("items", list);
  186.  
  187. if(this.hasCustomInventoryName())
  188. {
  189. nbt.setString("MaceratorCont", this.localizedName);
  190. }
  191. }
  192.  
  193. @Override
  194. public boolean isUseableByPlayer(EntityPlayer entityplayer) {
  195.  
  196. 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;
  197. }
  198.  
  199. @Override
  200. public void openInventory() {}
  201.  
  202. @Override
  203. public void closeInventory() {}
  204.  
  205. public boolean hasPower()
  206. {
  207. return this.power > 0;
  208. }
  209.  
  210.  
  211. @Override
  212. public void updateEntity()
  213. {
  214. boolean flag = this.power > 0;
  215. boolean flag1 = false;
  216.  
  217. if (this.hasPower() && this.isMacerating())
  218. {
  219. this.power--;
  220. }
  221.  
  222. if (!this.worldObj.isRemote)
  223. {
  224. if (this.hasItemPower(this.slots[1]) && this.power <= (this.maxPower - this.getPower(this.slots[1])))
  225. {
  226. this.power += getPower(this.slots[1]);
  227.  
  228. flag1 = true;
  229.  
  230. if(this.slots[1] != null)
  231. {
  232. this.slots[1].stackSize--;
  233.  
  234. if(this.slots[1].stackSize == 0)
  235. {
  236. this.slots[1] = this.slots[1].getItem().getContainerItem(this.slots[1]);
  237. }
  238. }
  239. }
  240. if(this.hasPower() && this.canMacerate())
  241. {
  242. this.cookTime++;
  243.  
  244. if(this.cookTime == this.maceratingSpeed)
  245. {
  246. this.cookTime = 0;
  247. this.macerateItem();
  248. flag1 = true;
  249. }
  250. }
  251. else
  252. {
  253. this.cookTime = 0;
  254. }
  255.  
  256. if(flag != this.hasPower())
  257. {
  258. flag1 = true;
  259. Macerator.updateMaceratorBlockState(this.hasPower(), this.worldObj, this.xCoord, this.yCoord, this.zCoord);
  260. }
  261.  
  262. }
  263.  
  264. if (flag1)
  265. {
  266. this.markDirty();
  267. }
  268. }
  269.  
  270. String[] ores = OreDictionary.getOreNames();
  271.  
  272. private boolean isOre(ItemStack itemStack) {
  273.  
  274.  
  275. if(itemStack.getDisplayName().contains("Ore"))
  276. {
  277. return true;
  278. }
  279. else
  280. {
  281. return false;
  282. }
  283. }
  284.  
  285. private boolean canMacerate()
  286. {
  287.  
  288. if (this.slots[0] == null)
  289. {
  290. return false;
  291. }
  292. else
  293. {
  294. ItemStack itemstack = FurnaceRecipes.smelting().getSmeltingResult(this.slots[0]);
  295. if (itemstack == null) return false;
  296. if (!this.isOre(this.slots[0])) return false;
  297. if (this.slots[2] == null) return true;
  298. if (!this.slots[2].isItemEqual(itemstack)) return false;
  299. int result = slots[2].stackSize + itemstack.stackSize * 2;
  300. return result <= getInventoryStackLimit() && result <= this.slots[2].getMaxStackSize(); //Forge BugFix: Make it respect stack sizes properly.
  301. }
  302. }
  303.  
  304.  
  305.  
  306. public void macerateItem()
  307. {
  308.  
  309. if (this.canMacerate())
  310. {
  311. ItemStack itemstack = FurnaceRecipes.smelting().getSmeltingResult(this.slots[0]);
  312.  
  313. if (this.slots[2] == null)
  314. {
  315. this.slots[2] = itemstack.copy();
  316. this.slots[2].stackSize*=2;
  317. }
  318. else if (this.slots[2].getItem() == itemstack.getItem())
  319. {
  320. this.slots[2].stackSize += itemstack.stackSize*2; // Forge BugFix: Results may have multiple items
  321. }
  322.  
  323. --this.slots[0].stackSize;
  324.  
  325. if (this.slots[0].stackSize <= 0)
  326. {
  327. this.slots[0] = null;
  328. }
  329. }
  330.  
  331. }
  332.  
  333. /**Gets the time in tick in which the item burns.*/
  334. public static int getPower(ItemStack itemstack)
  335. {
  336. if (itemstack == null)
  337. {
  338. return 0;
  339. }
  340. else
  341. {
  342. Item item = itemstack.getItem();
  343.  
  344. if (item == Items.redstone) return 5;
  345. if (item == Item.getItemFromBlock(Blocks.redstone_block)) return 45;
  346. return 0;
  347. }
  348. }
  349.  
  350. public boolean isMacerating()
  351. {
  352. return this.cookTime > 0;
  353. }
  354.  
  355. public static boolean hasItemPower(ItemStack itemstack)
  356. {
  357. return getPower(itemstack) > 0;
  358. }
  359.  
  360. @Override
  361. public boolean isItemValidForSlot(int i, ItemStack itemstack) {
  362. return i == 2 ? false : (i == 1 ? hasItemPower(itemstack) : true);
  363. }
  364.  
  365. @Override
  366. public int[] getAccessibleSlotsFromSide(int var1) {
  367. return var1 == 0 ? slots_bottom : (var1 == 1 ? slots_top : slots_sides);
  368. }
  369.  
  370. @Override
  371. public boolean canInsertItem(int i, ItemStack itemstack,
  372. int j) {
  373.  
  374. return this.isItemValidForSlot(i, itemstack);
  375. }
  376.  
  377. @Override
  378. public boolean canExtractItem(int i, ItemStack itemstack,
  379. int j) {
  380.  
  381. return j != 0 || i != 1 || itemstack.getItem() == Items.bucket;
  382. }
  383.  
  384. @SideOnly(Side.CLIENT)
  385. public int getPowerRemainingScaled(int i) {
  386. if(this.maxPower == 0)
  387. {
  388. this.maxPower = this.maceratingSpeed;
  389. }
  390. return this.power * i / this.maxPower;
  391. }
  392.  
  393. @SideOnly(Side.CLIENT)
  394. public int getMacerationProgressScaled(int i)
  395. {
  396. return this.cookTime * i / this.maceratingSpeed;
  397. }
  398. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement