Guest User

Untitled

a guest
Aug 8th, 2014
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.71 KB | None | 0 0
  1. package com.jack.jackmod.tileentity;
  2.  
  3. import net.minecraft.block.Block;
  4. import net.minecraft.entity.player.EntityPlayer;
  5. import net.minecraft.init.Blocks;
  6. import net.minecraft.init.Items;
  7. import net.minecraft.inventory.ISidedInventory;
  8. import net.minecraft.item.Item;
  9. import net.minecraft.item.ItemBlock;
  10. import net.minecraft.item.ItemStack;
  11. import net.minecraft.item.crafting.FurnaceRecipes;
  12. import net.minecraft.nbt.NBTTagCompound;
  13. import net.minecraft.nbt.NBTTagList;
  14. import net.minecraft.tileentity.TileEntity;
  15. import com.jack.jackmod.block.BlockHydrogenSeparator;
  16. import cpw.mods.fml.common.registry.GameRegistry;
  17.  
  18. public class TileEntityHydrogenSeparator extends TileEntity implements ISidedInventory {
  19.  
  20. private String localizedName;
  21.  
  22. private static final int[] slots_top = new int[]{0};
  23. private static final int[] slots_bottom = new int[]{2, 1};
  24. private static final int[] slots_side = new int[]{1};
  25.  
  26. private ItemStack[] slots = new ItemStack [3];
  27.  
  28. public int furnaceSpeed = 150;
  29. /**
  30. * The number of ticks that the furnace will keep burning
  31. */
  32. public int burnTime;
  33. /**
  34. * The number of ticks that a fresh copy of the currently-burning item would keep the furnace burning for
  35. */
  36. public int currentItemBurnTime;
  37. /**
  38. * The number of ticks that the current item has been cooking for
  39. */
  40. public int cookTime;
  41.  
  42.  
  43.  
  44. public void setGuiDisplayName(String displayName) {
  45. this.localizedName = displayName;
  46. }
  47.  
  48. public String getInventoryName() {
  49. return this.hasCustomInventoryName() ? this.localizedName : "container.alabasterOven";
  50. }
  51.  
  52. public boolean hasCustomInventoryName() {
  53. return this.localizedName != null && this.localizedName.length() > 0;
  54. }
  55.  
  56. public int getSizeInventory() {
  57. return this.slots.length;
  58. }
  59.  
  60. @Override
  61. public ItemStack getStackInSlot(int var1) {
  62. return this.slots[var1];
  63. }
  64.  
  65. @Override
  66. public ItemStack decrStackSize(int var1, int var2) {
  67. if(this.slots[var1] != null){
  68. ItemStack itemstack;
  69.  
  70. if(this.slots[var1].stackSize <= var2 ){
  71. itemstack = this.slots[var1];
  72. this.slots[var1] = null;
  73. return itemstack;
  74. }else{
  75. itemstack = this.slots[var1].splitStack(var2);
  76.  
  77. if(this.slots[var1].stackSize == 0) {
  78. this.slots[var1] = null;
  79. }
  80.  
  81. return itemstack;
  82. }
  83. }else{
  84. return null;
  85. }
  86. }
  87.  
  88.  
  89. @Override
  90. public ItemStack getStackInSlotOnClosing(int i) {
  91. if(this.slots[i]!= null) {
  92. ItemStack itemstack = this.slots[i];
  93. this.slots[i] = null;
  94. return itemstack;
  95. }
  96. return null;
  97. }
  98.  
  99. @Override
  100. public void setInventorySlotContents(int i, ItemStack itemstack) {
  101. this.slots[i] = itemstack;
  102.  
  103. if(itemstack != null && itemstack.stackSize > this.getInventoryStackLimit()) {
  104. itemstack.stackSize = this.getInventoryStackLimit();
  105. }
  106.  
  107. }
  108.  
  109. @Override
  110. public int getInventoryStackLimit() {
  111. return 64;
  112. }
  113.  
  114. @Override
  115. public boolean isUseableByPlayer(EntityPlayer entityplayer) {
  116. 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;
  117. }
  118.  
  119. public void openInventory() {}
  120. public void closeInventory() {}
  121.  
  122. @Override
  123. public boolean isItemValidForSlot(int i, ItemStack itemstack) {
  124. return i == 2 ? false : (i == 1 ? isItemFuel(itemstack) : true);
  125. }
  126.  
  127. public static boolean isItemFuel (ItemStack itemstack) {
  128. return getItemBurnTime(itemstack) > 0;
  129. }
  130.  
  131. private static int getItemBurnTime(ItemStack itemstack) {
  132. if(itemstack == null){
  133. return 0;
  134. }else{
  135. Item item = itemstack.getItem();
  136.  
  137. if(item instanceof ItemBlock && Block.getBlockFromItem(item) != Blocks.air) {
  138. Block block = Block.getBlockFromItem(item);
  139.  
  140. if(block == Blocks.sapling) return 100;
  141. if(block == Blocks.coal_block) return 14400;
  142.  
  143. }
  144.  
  145. if(item == Items.coal) return 1600;
  146. if(item == Items.stick) return 100;
  147. if(item == Items.lava_bucket) return 20000;
  148.  
  149. if(item == Items.blaze_rod) return 2400;
  150. }
  151. return GameRegistry.getFuelValue(itemstack);
  152. }
  153.  
  154. public boolean isBurning() {
  155. return this.burnTime > 0;
  156. }
  157.  
  158. public void updateEntity() {
  159. boolean flag = this.burnTime > 0;
  160. boolean flag1 = false;
  161.  
  162. if(this.isBurning()) {
  163. this.burnTime--;
  164. }
  165. if(!this.worldObj.isRemote) {
  166. if(this.burnTime == 0 && this.canSmelt()) {
  167. this.currentItemBurnTime = this.burnTime = getItemBurnTime(this.slots[1]);
  168.  
  169. if(this.isBurning()) {
  170. flag1 = true;
  171.  
  172. if(this.slots[1] != null) {
  173. this.slots[1].stackSize--;
  174.  
  175. if(this.slots[1].stackSize == 0) {
  176. this.slots[1] = this.slots[1].getItem().getContainerItem(this.slots[1]);
  177. }
  178. }
  179. }
  180. }
  181. if(this.isBurning() && this.canSmelt()) {
  182. this.cookTime++;
  183.  
  184. if(this.cookTime == this.furnaceSpeed) {
  185. this.cookTime = 0;
  186. this.smeltItem();
  187. flag1 = true;
  188. }
  189. }else{
  190. this.cookTime = 0;
  191. }
  192.  
  193. if(flag != this.isBurning()) {
  194. flag1 = true;
  195. BlockHydrogenSeparator.updateHydrogenSeparatorBlockState(this.burnTime > 0, this.worldObj, this.xCoord, this.yCoord, this.zCoord);
  196. }
  197. }
  198. if(flag1) {
  199. this.markDirty();
  200. }
  201. }
  202.  
  203. public boolean canSmelt() {
  204. if (this.slots[0] == null) {
  205. return false;
  206. }else{
  207. ItemStack itemstack = FurnaceRecipes.smelting().getSmeltingResult(this.slots[0]);
  208.  
  209. if(itemstack == null) return false;
  210. if(this.slots[2] == null) return true;
  211. if(!this.slots[2].isItemEqual(itemstack)) return false;
  212.  
  213. int result = this.slots[2].stackSize + itemstack.stackSize;
  214.  
  215. return (result <= getInventoryStackLimit() && result <= itemstack.getMaxStackSize());
  216. }
  217. }
  218.  
  219. public void smeltItem() {
  220. if(this.canSmelt()) {
  221. ItemStack itemstack = FurnaceRecipes.smelting().getSmeltingResult(this.slots[0]);
  222.  
  223. if(this.slots[2] == null) {
  224. this.slots[2] = itemstack.copy();
  225. }else if(this.slots[2].isItemEqual(itemstack)) {
  226. this.slots[2].stackSize += itemstack.stackSize;
  227. }
  228.  
  229. this.slots[0].stackSize--;
  230.  
  231. if(this.slots[0].stackSize <= 0) {
  232. this.slots[0] = null;
  233. }
  234. }
  235. }
  236.  
  237. @Override
  238. public int[] getAccessibleSlotsFromSide(int var1) {
  239. return var1 == 0 ? slots_bottom : (var1 == 1 ? slots_top : slots_side);
  240. }
  241.  
  242. @Override
  243. public boolean canInsertItem(int i, ItemStack itemstack, int j) {
  244. return this.isItemValidForSlot(i, itemstack);
  245. }
  246.  
  247. @Override
  248. public boolean canExtractItem(int i, ItemStack itemstack, int j) {
  249. return j != 0 || i != 1 || itemstack.getItem() == Items.bucket;
  250. }
  251.  
  252. public int getBurnTimeRemainingScaled(int i) {
  253. if(this.currentItemBurnTime ==0) {
  254. this.currentItemBurnTime = this.furnaceSpeed;
  255. }
  256. return this.burnTime * i / this.currentItemBurnTime;
  257. }
  258.  
  259. public int getCookProgressScaled(int i) {
  260. return this.cookTime * i / this.furnaceSpeed;
  261. }
  262.  
  263. public void readFromNBT(NBTTagCompound nbt) {
  264. super.readFromNBT(nbt);
  265.  
  266. NBTTagList list = nbt.getTagList("Items", 10);
  267. this.slots = new ItemStack[this.getSizeInventory()];
  268.  
  269. for(int i = 0; i < list.tagCount(); i++) {
  270. NBTTagCompound compound = (NBTTagCompound) list.getCompoundTagAt(i);
  271. byte b = compound.getByte("Slot");
  272.  
  273. if(b >= 0 && b < this.slots.length) {
  274. this.slots[b] = ItemStack.loadItemStackFromNBT(compound);
  275. }
  276. }
  277.  
  278. this.burnTime = (int)nbt.getShort("BurnTime");
  279. this.cookTime = (int)nbt.getShort("CookTime");
  280. this.currentItemBurnTime = (int)nbt.getShort("CurrentBurnTime");
  281.  
  282. if(nbt.hasKey("CustomName")) {
  283. this.localizedName = nbt.getString("CustomName");
  284. }
  285. }
  286.  
  287. public void writeToNBT(NBTTagCompound nbt) {
  288. super.writeToNBT(nbt);
  289.  
  290. nbt.setShort("BurnTime", (short)this.burnTime);
  291. nbt.setShort("CookTime", (short)this.cookTime);
  292. nbt.setShort("CurrentBurnTime", (short)this.currentItemBurnTime);
  293.  
  294. NBTTagList list = new NBTTagList();
  295.  
  296. for (int i = 0; i < this.slots.length; i++) {
  297. if(this.slots[i] != null) {
  298. NBTTagCompound compound = new NBTTagCompound();
  299. compound.setByte("Slot", (byte)i);
  300. this.slots[i].writeToNBT(compound);
  301. list.appendTag(compound);
  302. }
  303. }
  304.  
  305. nbt.setTag("Items", list);
  306.  
  307. if (this.hasCustomInventoryName()) {
  308. nbt.setString("CustomName", this.localizedName);
  309. }
  310. }
  311. }
Advertisement
Add Comment
Please, Sign In to add comment