Guest User

Untitled

a guest
Nov 9th, 2014
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 13.07 KB | None | 0 0
  1. package tutorial.generic;
  2.  
  3. import cpw.mods.fml.common.registry.GameRegistry;
  4. import cpw.mods.fml.relauncher.Side;
  5. import cpw.mods.fml.relauncher.SideOnly;
  6. import net.minecraft.block.Block;
  7. import net.minecraft.block.BlockFurnace;
  8. import net.minecraft.block.material.Material;
  9. import net.minecraft.entity.player.EntityPlayer;
  10. import net.minecraft.init.Blocks;
  11. import net.minecraft.init.Items;
  12. import net.minecraft.inventory.ISidedInventory;
  13. import net.minecraft.item.Item;
  14. import net.minecraft.item.ItemBlock;
  15. import net.minecraft.item.ItemHoe;
  16. import net.minecraft.item.ItemStack;
  17. import net.minecraft.item.ItemSword;
  18. import net.minecraft.item.ItemTool;
  19. import net.minecraft.item.crafting.FurnaceRecipes;
  20. import net.minecraft.nbt.NBTTagCompound;
  21. import net.minecraft.nbt.NBTTagList;
  22. import net.minecraft.tileentity.TileEntity;
  23. import net.minecraft.tileentity.TileEntityFurnace;
  24.  
  25. public class TileEntityEnderFurnace extends TileEntity implements
  26. ISidedInventory {
  27. private static final int[] slotsTop = new int[] { 0 };
  28. private static final int[] slotsBottom = new int[] { 2, 1 };
  29. private static final int[] slotsSides = new int[] { 1 };
  30. /** The ItemStacks that hold the items currently being used in the furnace */
  31. private ItemStack[] furnaceItemStacks = new ItemStack[3];
  32. /** The number of ticks that the furnace will keep burning */
  33. public int furnaceBurnTime;
  34. /**
  35. * The number of ticks that a fresh copy of the currently-burning item would
  36. * keep the furnace burning for
  37. */
  38. public int currentItemBurnTime;
  39. /** The number of ticks that the current item has been cooking for */
  40. public int furnaceCookTime;
  41. private String field_145958_o;
  42. private static final String __OBFID = "CL_00000358";
  43.  
  44. /**
  45. * Returns the number of slots in the inventory.
  46. */
  47. public int getSizeInventory() {
  48. return this.furnaceItemStacks.length;
  49. }
  50.  
  51. /**
  52. * Returns the stack in slot i
  53. */
  54. public ItemStack getStackInSlot(int p_70301_1_) {
  55. return this.furnaceItemStacks[p_70301_1_];
  56. }
  57.  
  58. /**
  59. * Removes from an inventory slot (first arg) up to a specified number
  60. * (second arg) of items and returns them in a new stack.
  61. */
  62. public ItemStack decrStackSize(int p_70298_1_, int p_70298_2_) {
  63. if (this.furnaceItemStacks[p_70298_1_] != null) {
  64. ItemStack itemstack;
  65.  
  66. if (this.furnaceItemStacks[p_70298_1_].stackSize <= p_70298_2_) {
  67. itemstack = this.furnaceItemStacks[p_70298_1_];
  68. this.furnaceItemStacks[p_70298_1_] = null;
  69. return itemstack;
  70. } else {
  71. itemstack = this.furnaceItemStacks[p_70298_1_]
  72. .splitStack(p_70298_2_);
  73.  
  74. if (this.furnaceItemStacks[p_70298_1_].stackSize == 0) {
  75. this.furnaceItemStacks[p_70298_1_] = null;
  76. }
  77.  
  78. return itemstack;
  79. }
  80. } else {
  81. return null;
  82. }
  83. }
  84.  
  85. /**
  86. * When some containers are closed they call this on each slot, then drop
  87. * whatever it returns as an EntityItem - like when you close a workbench
  88. * GUI.
  89. */
  90. public ItemStack getStackInSlotOnClosing(int p_70304_1_) {
  91. if (this.furnaceItemStacks[p_70304_1_] != null) {
  92. ItemStack itemstack = this.furnaceItemStacks[p_70304_1_];
  93. this.furnaceItemStacks[p_70304_1_] = null;
  94. return itemstack;
  95. } else {
  96. return null;
  97. }
  98. }
  99.  
  100. /**
  101. * Sets the given item stack to the specified slot in the inventory (can be
  102. * crafting or armor sections).
  103. */
  104. public void setInventorySlotContents(int p_70299_1_, ItemStack p_70299_2_) {
  105. this.furnaceItemStacks[p_70299_1_] = p_70299_2_;
  106.  
  107. if (p_70299_2_ != null
  108. && p_70299_2_.stackSize > this.getInventoryStackLimit()) {
  109. p_70299_2_.stackSize = this.getInventoryStackLimit();
  110. }
  111. }
  112.  
  113. /**
  114. * Returns the name of the inventory
  115. */
  116. public String getInventoryName() {
  117. return this.hasCustomInventoryName() ? this.field_145958_o
  118. : "container.enderfurnace"; // container.furnace
  119. }
  120.  
  121. /**
  122. * Returns if the inventory is named
  123. */
  124. public boolean hasCustomInventoryName() {
  125. return this.field_145958_o != null && this.field_145958_o.length() > 0;
  126. }
  127.  
  128. public void func_145951_a(String p_145951_1_) {
  129. this.field_145958_o = p_145951_1_;
  130. }
  131.  
  132. public void readFromNBT(NBTTagCompound p_145839_1_) {
  133. super.readFromNBT(p_145839_1_);
  134. NBTTagList nbttaglist = p_145839_1_.getTagList("Items", 10);
  135. this.furnaceItemStacks = new ItemStack[this.getSizeInventory()];
  136.  
  137. for (int i = 0; i < nbttaglist.tagCount(); ++i) {
  138. NBTTagCompound nbttagcompound1 = nbttaglist.getCompoundTagAt(i);
  139. byte b0 = nbttagcompound1.getByte("Slot");
  140.  
  141. if (b0 >= 0 && b0 < this.furnaceItemStacks.length) {
  142. this.furnaceItemStacks[b0] = ItemStack
  143. .loadItemStackFromNBT(nbttagcompound1);
  144. }
  145. }
  146.  
  147. this.furnaceBurnTime = p_145839_1_.getShort("BurnTime");
  148. this.furnaceCookTime = p_145839_1_.getShort("CookTime");
  149. this.currentItemBurnTime = getItemBurnTime(this.furnaceItemStacks[1]);
  150.  
  151. if (p_145839_1_.hasKey("CustomName", 8)) {
  152. this.field_145958_o = p_145839_1_.getString("CustomName");
  153. }
  154. }
  155.  
  156. public void writeToNBT(NBTTagCompound p_145841_1_) {
  157. super.writeToNBT(p_145841_1_);
  158. p_145841_1_.setShort("BurnTime", (short) this.furnaceBurnTime);
  159. p_145841_1_.setShort("CookTime", (short) this.furnaceCookTime);
  160. NBTTagList nbttaglist = new NBTTagList();
  161.  
  162. for (int i = 0; i < this.furnaceItemStacks.length; ++i) {
  163. if (this.furnaceItemStacks[i] != null) {
  164. NBTTagCompound nbttagcompound1 = new NBTTagCompound();
  165. nbttagcompound1.setByte("Slot", (byte) i);
  166. this.furnaceItemStacks[i].writeToNBT(nbttagcompound1);
  167. nbttaglist.appendTag(nbttagcompound1);
  168. }
  169. }
  170.  
  171. p_145841_1_.setTag("Items", nbttaglist);
  172.  
  173. if (this.hasCustomInventoryName()) {
  174. p_145841_1_.setString("CustomName", this.field_145958_o);
  175. }
  176. }
  177.  
  178. /**
  179. * Returns the maximum stack size for a inventory slot.
  180. */
  181. public int getInventoryStackLimit() {
  182. return 64;
  183. }
  184.  
  185. /**
  186. * Returns an integer between 0 and the passed value representing how close
  187. * the current item is to being completely cooked
  188. */
  189. @SideOnly(Side.CLIENT)
  190. public int getCookProgressScaled(int p_145953_1_) {
  191. return this.furnaceCookTime * p_145953_1_ / 200; // /////// WAS 200
  192. }
  193.  
  194. /**
  195. * Returns an integer between 0 and the passed value representing how much
  196. * burn time is left on the current fuel item, where 0 means that the item
  197. * is exhausted and the passed value means that the item is fresh
  198. */
  199. @SideOnly(Side.CLIENT)
  200. public int getBurnTimeRemainingScaled(int p_145955_1_) {
  201. if (this.currentItemBurnTime == 0) {
  202. this.currentItemBurnTime = 200; // /////// WAS 200
  203. }
  204.  
  205. return this.furnaceBurnTime * p_145955_1_ / this.currentItemBurnTime;
  206. }
  207.  
  208. /**
  209. * Furnace isBurning
  210. */
  211. public boolean isBurning() {
  212. return this.furnaceBurnTime > 0;
  213. }
  214.  
  215. public void updateEntity() {
  216. boolean flag = this.furnaceBurnTime > 0;
  217. boolean flag1 = false;
  218.  
  219. if (this.furnaceBurnTime > 0) {
  220. --this.furnaceBurnTime;
  221. }
  222.  
  223. if (!this.worldObj.isRemote) {
  224. if (this.furnaceBurnTime != 0 || this.furnaceItemStacks[1] != null
  225. && this.furnaceItemStacks[0] != null) {
  226. if (this.furnaceBurnTime == 0 && this.canSmelt()) {
  227. this.currentItemBurnTime = this.furnaceBurnTime = getItemBurnTime(this.furnaceItemStacks[1]);
  228.  
  229. if (this.furnaceBurnTime > 0) {
  230. flag1 = true;
  231.  
  232. if (this.furnaceItemStacks[1] != null) {
  233. --this.furnaceItemStacks[1].stackSize;
  234.  
  235. if (this.furnaceItemStacks[1].stackSize == 0) {
  236. this.furnaceItemStacks[1] = furnaceItemStacks[1]
  237. .getItem().getContainerItem(
  238. furnaceItemStacks[1]);
  239. }
  240. }
  241. }
  242. }
  243.  
  244. if (this.isBurning() && this.canSmelt()) {
  245. ++this.furnaceCookTime;
  246.  
  247. if (this.furnaceCookTime == 200) // //////// WAS 200
  248. {
  249. this.furnaceCookTime = 0;
  250. this.smeltItem();
  251. flag1 = true;
  252. }
  253. } else {
  254. this.furnaceCookTime = 0;
  255. }
  256. }
  257.  
  258. if (flag != this.furnaceBurnTime > 0) {
  259. flag1 = true;
  260. BlockFurnace.updateFurnaceBlockState(this.furnaceBurnTime > 0,
  261. this.worldObj, this.xCoord, this.yCoord, this.zCoord);
  262. }
  263. }
  264.  
  265. if (flag1) {
  266. this.markDirty();
  267. }
  268. }
  269.  
  270. /**
  271. * Returns true if the furnace can smelt an item, i.e. has a source item,
  272. * destination stack isn't full, etc.
  273. */
  274. private boolean canSmelt() {
  275. if (this.furnaceItemStacks[0] == null) {
  276. return false;
  277. } else {
  278. ItemStack itemstack = FurnaceRecipes.smelting().getSmeltingResult(
  279. this.furnaceItemStacks[0]);
  280. if (itemstack == null)
  281. return false;
  282. if (this.furnaceItemStacks[2] == null)
  283. return true;
  284. if (!this.furnaceItemStacks[2].isItemEqual(itemstack))
  285. return false;
  286. int result = furnaceItemStacks[2].stackSize + itemstack.stackSize;
  287. return result <= getInventoryStackLimit()
  288. && result <= this.furnaceItemStacks[2].getMaxStackSize(); // Forge
  289. // BugFix:
  290. // Make
  291. // it
  292. // respect
  293. // stack
  294. // sizes
  295. // properly.
  296. }
  297. }
  298.  
  299. /**
  300. * Turn one item from the furnace source stack into the appropriate smelted
  301. * item in the furnace result stack
  302. */
  303. public void smeltItem() {
  304. if (this.canSmelt()) {
  305. ItemStack itemstack = FurnaceRecipes.smelting().getSmeltingResult(
  306. this.furnaceItemStacks[0]);
  307.  
  308. if (this.furnaceItemStacks[2] == null) {
  309. this.furnaceItemStacks[2] = itemstack.copy();
  310. } else if (this.furnaceItemStacks[2].getItem() == itemstack
  311. .getItem()) {
  312. this.furnaceItemStacks[2].stackSize += itemstack.stackSize; // Forge
  313. // BugFix:
  314. // Results
  315. // may
  316. // have
  317. // multiple
  318. // items
  319. }
  320.  
  321. --this.furnaceItemStacks[0].stackSize;
  322.  
  323. if (this.furnaceItemStacks[0].stackSize <= 0) {
  324. this.furnaceItemStacks[0] = null;
  325. }
  326. }
  327. }
  328.  
  329. /**
  330. * Returns the number of ticks that the supplied fuel item will keep the
  331. * furnace burning, or 0 if the item isn't fuel
  332. */
  333. public static int getItemBurnTime(ItemStack p_145952_0_) {
  334. if (p_145952_0_ == null) {
  335. return 0;
  336. } else {
  337. Item item = p_145952_0_.getItem();
  338.  
  339. if (item instanceof ItemBlock
  340. && Block.getBlockFromItem(item) != Blocks.air) {
  341. Block block = Block.getBlockFromItem(item);
  342.  
  343. if (block == Blocks.wooden_slab) {
  344. return 150;
  345. }
  346.  
  347. if (block.getMaterial() == Material.wood) {
  348. return 300;
  349. }
  350.  
  351. if (block == Blocks.coal_block) {
  352. return 16000;
  353. }
  354. }
  355.  
  356. if (item instanceof ItemTool
  357. && ((ItemTool) item).getToolMaterialName().equals("WOOD"))
  358. return 200;
  359. if (item instanceof ItemSword
  360. && ((ItemSword) item).getToolMaterialName().equals("WOOD"))
  361. return 200;
  362. if (item instanceof ItemHoe
  363. && ((ItemHoe) item).getToolMaterialName().equals("WOOD"))
  364. return 200;
  365. if (item == Items.stick)
  366. return 100;
  367. if (item == Items.coal)
  368. return 1600;
  369. if (item == Items.lava_bucket)
  370. return 20000;
  371. if (item == Item.getItemFromBlock(Blocks.sapling))
  372. return 100;
  373. if (item == Items.blaze_rod)
  374. return 2400;
  375. return GameRegistry.getFuelValue(p_145952_0_);
  376. }
  377. }
  378.  
  379. public static boolean isItemFuel(ItemStack p_145954_0_) {
  380. /**
  381. * Returns the number of ticks that the supplied fuel item will keep the
  382. * furnace burning, or 0 if the item isn't fuel
  383. */
  384. return getItemBurnTime(p_145954_0_) > 0;
  385. }
  386.  
  387. /**
  388. * Do not make give this method the name canInteractWith because it clashes
  389. * with Container
  390. */
  391. public boolean isUseableByPlayer(EntityPlayer p_70300_1_) {
  392. return this.worldObj.getTileEntity(this.xCoord, this.yCoord,
  393. this.zCoord) != this ? false : p_70300_1_.getDistanceSq(
  394. (double) this.xCoord + 0.5D, (double) this.yCoord + 0.5D,
  395. (double) this.zCoord + 0.5D) <= 64.0D;
  396. }
  397.  
  398. public void openInventory() {
  399. }
  400.  
  401. public void closeInventory() {
  402. }
  403.  
  404. /**
  405. * Returns true if automation is allowed to insert the given stack (ignoring
  406. * stack size) into the given slot.
  407. */
  408. public boolean isItemValidForSlot(int p_94041_1_, ItemStack p_94041_2_) {
  409. return p_94041_1_ == 2 ? false
  410. : (p_94041_1_ == 1 ? isItemFuel(p_94041_2_) : true);
  411. }
  412.  
  413. /**
  414. * Returns an array containing the indices of the slots that can be accessed
  415. * by automation on the given side of this block.
  416. */
  417. public int[] getAccessibleSlotsFromSide(int p_94128_1_) {
  418. return p_94128_1_ == 0 ? slotsBottom : (p_94128_1_ == 1 ? slotsTop
  419. : slotsSides);
  420. }
  421.  
  422. /**
  423. * Returns true if automation can insert the given item in the given slot
  424. * from the given side. Args: Slot, item, side
  425. */
  426. public boolean canInsertItem(int p_102007_1_, ItemStack p_102007_2_,
  427. int p_102007_3_) {
  428. return this.isItemValidForSlot(p_102007_1_, p_102007_2_);
  429. }
  430.  
  431. /**
  432. * Returns true if automation can extract the given item in the given slot
  433. * from the given side. Args: Slot, item, side
  434. */
  435. public boolean canExtractItem(int p_102008_1_, ItemStack p_102008_2_,
  436. int p_102008_3_) {
  437. return p_102008_3_ != 0 || p_102008_1_ != 1
  438. || p_102008_2_.getItem() == Items.bucket;
  439. }
  440. }
Advertisement
Add Comment
Please, Sign In to add comment