Guest User

KNO3

a guest
Jul 10th, 2016
150
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. public static class TileEntityForge extends TileEntityLockable implements ITickable, ISidedInventory{
  2. public enum slotEnum {
  3. INPUT_SLOT, OUTPUT_SLOT , FUEL_SLOT , MOLD_SLOT }
  4.  
  5. private static final int[] slotsTop = new int[] {
  6. slotEnum.INPUT_SLOT.ordinal(), slotEnum.FUEL_SLOT.ordinal() };
  7. private static final int[] slotsBottom = new int[] {
  8. slotEnum.OUTPUT_SLOT.ordinal()};
  9. private static final int[] slotsSides = new int[] {
  10. slotEnum.MOLD_SLOT.ordinal()};
  11. private ItemStack[] ForgeItemStacks = new ItemStack[7];
  12. /** The number of ticks that the Forge will keep burning */ private int ForgeBurnTime;
  13. /** The number of ticks that a fresh copy of the currently-burning item would keep the Forge burning for */ private int currentItemBurnTime;
  14. private int cookTime;
  15. private int totalCookTime;
  16. private String ForgeCustomName;
  17. /** * Returns the number of slots in the inventory. */ public int getSizeInventory() {
  18. return this.ForgeItemStacks.length;
  19. }
  20.  
  21. /** * Returns the stack in the given slot. * * @param index The slot to retrieve from. */ public ItemStack getStackInSlot(int index) {
  22. return this.ForgeItemStacks[index];
  23. }
  24.  
  25. /** * Removes up to a specified number of items from an inventory slot and returns them in a new stack. * * @param index The slot to remove from. * @param count The maximum amount of items to remove. */ public ItemStack decrStackSize(int index, int count) {
  26. if (this.ForgeItemStacks[index] != null) {
  27. if (this.ForgeItemStacks[index].stackSize <= count) {
  28. ItemStack itemstack1 = this.ForgeItemStacks[index];
  29. this.ForgeItemStacks[index] = null;
  30. return itemstack1;
  31. }
  32.  
  33. else {
  34. ItemStack itemstack = this.ForgeItemStacks[index].splitStack(count);
  35. if (this.ForgeItemStacks[index].stackSize == 0) {
  36. this.ForgeItemStacks[index] = null;
  37. }
  38.  
  39. return itemstack;
  40. }
  41.  
  42. }
  43.  
  44. else {
  45. return null;
  46. }
  47.  
  48. }
  49.  
  50. /** * Removes a stack from the given slot and returns it. * * @param index The slot to remove a stack from. */ public ItemStack getStackInSlotOnClosing(int index) {
  51. if (this.ForgeItemStacks[index] != null) {
  52. ItemStack itemstack = this.ForgeItemStacks[index];
  53. this.ForgeItemStacks[index] = null;
  54. return itemstack;
  55. }
  56.  
  57. else {
  58. return null;
  59. }
  60.  
  61. }
  62.  
  63. /** * Sets the given item stack to the specified slot in the inventory (can be crafting or armor sections). */ public void setInventorySlotContents(int index, ItemStack stack) {
  64. boolean flag = stack != null && stack.isItemEqual(this.ForgeItemStacks[index]) && ItemStack.areItemStackTagsEqual(stack, this.ForgeItemStacks[index]);
  65. this.ForgeItemStacks[index] = stack;
  66. if (stack != null && stack.stackSize > this.getInventoryStackLimit()) {
  67. stack.stackSize = this.getInventoryStackLimit();
  68. }
  69.  
  70. if (index == 0 && !flag) {
  71. this.totalCookTime = this.getCookTime(stack);
  72. this.cookTime = 0;
  73. this.markDirty();
  74. }
  75.  
  76. }
  77.  
  78. /** * Gets the name of this command sender (usually username, but possibly "Rcon") */ public String getCommandSenderName() {
  79. return this.hasCustomName() ? this.ForgeCustomName : "container.forge";
  80. }
  81.  
  82. /** * Returns true if this thing is named */ public boolean hasCustomName() {
  83. return this.ForgeCustomName != null && this.ForgeCustomName.length() > 0;
  84. }
  85.  
  86. public void setCustomInventoryName(String p_145951_1_) {
  87. this.ForgeCustomName = p_145951_1_;
  88. }
  89.  
  90. public void readFromNBT(NBTTagCompound compound) {
  91. super.readFromNBT(compound);
  92. NBTTagList nbttaglist = compound.getTagList("Items", 10);
  93. this.ForgeItemStacks = new ItemStack[this.getSizeInventory()];
  94. for (int i = 0;
  95. i < nbttaglist.tagCount();
  96. ++i) {
  97. NBTTagCompound nbttagcompound = nbttaglist.getCompoundTagAt(i);
  98. int j = nbttagcompound.getByte("Slot");
  99. if (j >= 0 && j < this.ForgeItemStacks.length) {
  100. this.ForgeItemStacks[j] = ItemStack.loadItemStackFromNBT(nbttagcompound);
  101. }
  102.  
  103. }
  104.  
  105. this.ForgeBurnTime = compound.getShort("BurnTime");
  106. this.cookTime = compound.getShort("CookTime");
  107. this.totalCookTime = compound.getShort("CookTimeTotal");
  108. this.currentItemBurnTime = getItemBurnTime(this.ForgeItemStacks[slotEnum.FUEL_SLOT.ordinal()]);
  109. if (compound.hasKey("CustomName", 8)) {
  110. this.ForgeCustomName = compound.getString("CustomName");
  111. }
  112.  
  113. }
  114.  
  115. public void writeToNBT(NBTTagCompound compound) {
  116. super.writeToNBT(compound);
  117. compound.setShort("BurnTime", (short)this.ForgeBurnTime);
  118. compound.setShort("CookTime", (short)this.cookTime);
  119. compound.setShort("CookTimeTotal", (short)this.totalCookTime);
  120. NBTTagList nbttaglist = new NBTTagList();
  121. for (int i = 0;
  122. i < this.ForgeItemStacks.length;
  123. ++i) {
  124. if (this.ForgeItemStacks[i] != null) {
  125. NBTTagCompound nbttagcompound = new NBTTagCompound();
  126. nbttagcompound.setByte("Slot", (byte)i);
  127. this.ForgeItemStacks[i].writeToNBT(nbttagcompound);
  128. nbttaglist.appendTag(nbttagcompound);
  129. }
  130.  
  131. }
  132.  
  133. compound.setTag("Items", nbttaglist);
  134. if (this.hasCustomName()) {
  135. compound.setString("CustomName", this.ForgeCustomName);
  136. }
  137.  
  138. }
  139.  
  140. /** * Returns the maximum stack size for a inventory slot. Seems to always be 64, possibly will be extended. */ public int getInventoryStackLimit() {
  141. return 64;
  142. }
  143.  
  144. /** * Forge isBurning */ public boolean isBurning() {
  145. return this.ForgeBurnTime > 0;
  146. }
  147.  
  148. @SideOnly(Side.CLIENT) public static boolean isBurning(IInventory p_174903_0_) {
  149. return p_174903_0_.getField(0) > 0;
  150. }
  151.  
  152. /** * Like the old updateEntity(), except more generic. */ public void update() {
  153. boolean flag = this.isBurning();
  154. boolean flag1 = false;
  155. if (this.isBurning()) {
  156. --this.ForgeBurnTime;
  157. }
  158.  
  159. if (!this.worldObj.isRemote) {
  160. if (this.isBurning() || this.ForgeItemStacks[slotEnum.FUEL_SLOT.ordinal()] != null && ForgeItemStacks[slotEnum.INPUT_SLOT.ordinal()] != null && ForgeItemStacks[slotEnum.MOLD_SLOT.ordinal()] != null) {
  161.  
  162.  
  163. if (!this.isBurning() && this.canSmelt()) {
  164. this.currentItemBurnTime = this.ForgeBurnTime = getItemBurnTime(this.ForgeItemStacks[slotEnum.FUEL_SLOT.ordinal()]);
  165. if (this.isBurning()) {
  166. flag1 = true;
  167. if (this.ForgeItemStacks[slotEnum.FUEL_SLOT.ordinal()] != null ) {
  168. --this.ForgeItemStacks[slotEnum.FUEL_SLOT.ordinal()].stackSize;
  169. if (this.ForgeItemStacks[slotEnum.FUEL_SLOT.ordinal()].stackSize == 0) {
  170. this.ForgeItemStacks[slotEnum.FUEL_SLOT.ordinal()] = ForgeItemStacks[slotEnum.FUEL_SLOT.ordinal()].getItem().getContainerItem(ForgeItemStacks[slotEnum.FUEL_SLOT.ordinal()]);
  171. }
  172.  
  173. }
  174.  
  175. }
  176.  
  177. }
  178.  
  179. if (this.isBurning() && this.canSmelt()) {
  180. ++this.cookTime;
  181. if (this.cookTime == this.totalCookTime) {
  182. this.cookTime = 0;
  183. this.totalCookTime = this.getCookTime(ForgeItemStacks[slotEnum.INPUT_SLOT.ordinal()]);
  184. this.smeltItem();
  185. flag1 = true;
  186. }
  187.  
  188. }
  189.  
  190. else {
  191. this.cookTime = 0;
  192. }
  193.  
  194. }
  195.  
  196. else if (!this.isBurning() && this.cookTime > 0) {
  197. this.cookTime = MathHelper.clamp_int(this.cookTime - 2, 0, this.totalCookTime);
  198. }
  199.  
  200. if (flag != this.isBurning()) {
  201. flag1 = true;
  202. BlockForge.setState(this.isBurning(), this.worldObj, this.pos);
  203.  
  204. }
  205.  
  206. }
  207.  
  208. if (flag1) {
  209. this.markDirty();
  210. }
  211.  
  212. }
  213.  
  214. public int getCookTime(ItemStack stack) {
  215. return 350;
  216. }
  217.  
  218. private boolean canSmelt() {
  219. if (ForgeItemStacks[slotEnum.INPUT_SLOT.ordinal()] == null) {
  220. return false;
  221. }
  222.  
  223. if (ForgeItemStacks[slotEnum.MOLD_SLOT.ordinal()] == null) {
  224. return false;
  225. }
  226.  
  227.  
  228.  
  229. if(ItemStack.areItemsEqual(this.ForgeItemStacks[slotEnum.MOLD_SLOT.ordinal()] , ForgeRecipes.instance().getSmeltingMOLD(ForgeItemStacks[slotEnum.INPUT_SLOT.ordinal()]))){
  230.  
  231. ItemStack itemstack = ForgeRecipes.instance().getSmeltingResult(ForgeItemStacks[slotEnum.INPUT_SLOT.ordinal()]);
  232. ItemStack MOLD = ForgeRecipes.instance().getSmeltingMOLD(ForgeItemStacks[slotEnum.INPUT_SLOT.ordinal()]);
  233. ItemStack MOLDSlot = this.ForgeItemStacks[slotEnum.MOLD_SLOT.ordinal()];
  234. if (itemstack == null || MOLD == null) return false;
  235. if (this.ForgeItemStacks[slotEnum.OUTPUT_SLOT.ordinal()] == null){
  236. return true;
  237. }
  238.  
  239. if (!this.ForgeItemStacks[slotEnum.OUTPUT_SLOT.ordinal()].isItemEqual(itemstack)){
  240. return false;
  241. }
  242.  
  243. if (!this.ForgeItemStacks[slotEnum.MOLD_SLOT.ordinal()].isItemEqual(MOLD)){
  244. return false;
  245. }
  246.  
  247. int result = ForgeItemStacks[slotEnum.OUTPUT_SLOT.ordinal()].stackSize + itemstack.stackSize;
  248.  
  249. return result <= getInventoryStackLimit() && result <= this.ForgeItemStacks[slotEnum.OUTPUT_SLOT.ordinal()].getMaxStackSize();
  250.  
  251. }
  252. return false;
  253. }
  254.  
  255.  
  256.  
  257.  
  258.  
  259. /** * Turn one item from the Forge source stack into the appropriate smelted item in the Forge result stack */
  260. public void smeltItem(){
  261. if (this.canSmelt()) {
  262. if(ItemStack.areItemsEqual(this.ForgeItemStacks[slotEnum.MOLD_SLOT.ordinal()] , ForgeRecipes.instance().getSmeltingMOLD(ForgeItemStacks[slotEnum.INPUT_SLOT.ordinal()]))){
  263. ItemStack itemstack = ForgeRecipes.instance().getSmeltingResult(ForgeItemStacks[slotEnum.INPUT_SLOT.ordinal()]);
  264.  
  265. if (this.ForgeItemStacks[slotEnum.OUTPUT_SLOT.ordinal()] == null) {
  266. this.ForgeItemStacks[slotEnum.OUTPUT_SLOT.ordinal()] = itemstack.copy();
  267. }
  268.  
  269. else if (this.ForgeItemStacks[slotEnum.OUTPUT_SLOT.ordinal()].getItem() == itemstack.getItem()) {
  270. this.ForgeItemStacks[slotEnum.OUTPUT_SLOT.ordinal()].stackSize += itemstack.stackSize;
  271. --ForgeItemStacks[slotEnum.INPUT_SLOT.ordinal()].stackSize;
  272. if (ForgeItemStacks[slotEnum.INPUT_SLOT.ordinal()].stackSize <= 0) {
  273. ForgeItemStacks[slotEnum.INPUT_SLOT.ordinal()] = null;
  274. }
  275. }
  276.  
  277. }
  278.  
  279. }
  280. }
  281.  
  282.  
  283. public static int getItemBurnTime(ItemStack p_145952_0_){
  284. if (p_145952_0_ == null) {
  285. return 0;
  286. }
  287.  
  288. else {
  289. Item item = p_145952_0_.getItem();
  290. if (item instanceof ItemBlock && Block.getBlockFromItem(item) != Blocks.air) {
  291. Block block = Block.getBlockFromItem(item);
  292. if (block == Blocks.wooden_slab) {
  293. return 150;
  294. }
  295.  
  296. if (block.getMaterial() == Material.wood) {
  297. return 300;
  298. }
  299.  
  300. if (block == Blocks.coal_block) {
  301. return 16000;
  302. }
  303.  
  304. }
  305.  
  306. if (item instanceof ItemTool && ((ItemTool)item).getToolMaterialName().equals("WOOD")) return 200;
  307. if (item instanceof ItemSword && ((ItemSword)item).getToolMaterialName().equals("WOOD")) return 200;
  308. if (item instanceof ItemHoe && ((ItemHoe)item).getMaterialName().equals("WOOD")) return 200;
  309. if (item == Items.stick) return 100;
  310. if (item == Items.coal) return 1600;
  311. if (item == Items.lava_bucket) return 20000;
  312. if (item == Item.getItemFromBlock(Blocks.sapling)) return 100;
  313. if (item == Items.blaze_rod) return 2400;
  314. return net.minecraftforge.fml.common.registry.GameRegistry.getFuelValue(p_145952_0_);
  315. }
  316.  
  317. }
  318.  
  319. public static boolean isItemFuel(ItemStack p_145954_0_) {
  320. /** * Returns the number of ticks that the supplied fuel item will keep the Forge burning, or 0 if the item isn't * fuel */ return getItemBurnTime(p_145954_0_) > 0;
  321. }
  322.  
  323. /** * Do not make give this method the name canInteractWith because it clashes with Container */ public boolean isUseableByPlayer(EntityPlayer player) {
  324. return this.worldObj.getTileEntity(this.pos) != this ? false : player.getDistanceSq((double)this.pos.getX() + 0.5D, (double)this.pos.getY() + 0.5D, (double)this.pos.getZ() + 0.5D) <= 64.0D;
  325. }
  326.  
  327. public void openInventory(EntityPlayer player) {
  328. }
  329.  
  330. public void closeInventory(EntityPlayer player) {
  331. }
  332.  
  333. /** * Returns true if automation is allowed to insert the given stack (ignoring stack size) into the given slot. */ public boolean isItemValidForSlot(int index, ItemStack stack) {
  334. return index == 2 ? false : (index != 1 ? true : isItemFuel(stack) || SlotForgeFuel.isBucket(stack));
  335. }
  336.  
  337. public int[] getSlotsForFace(EnumFacing side) {
  338. return side == EnumFacing.DOWN ? slotsBottom : (side == EnumFacing.UP ? slotsTop : slotsSides);
  339. }
  340.  
  341. /** * Returns true if automation can insert the given item in the given slot from the given side. Args: slot, item, * side */ public boolean canInsertItem(int index, ItemStack itemStackIn, EnumFacing direction) {
  342. return this.isItemValidForSlot(index, itemStackIn);
  343. }
  344.  
  345. /** * Returns true if automation can extract the given item in the given slot from the given side. Args: slot, item, * side */ public boolean canExtractItem(int index, ItemStack stack, EnumFacing direction) {
  346. if (direction == EnumFacing.DOWN && index == 1) {
  347. Item item = stack.getItem();
  348. if (item != Items.water_bucket && item != Items.bucket) {
  349. return false;
  350. }
  351.  
  352. }
  353.  
  354. return true;
  355. }
  356.  
  357. public String getGuiID() {
  358. return "minecraft:Forge";
  359. }
  360.  
  361. public Container createContainer(InventoryPlayer playerInventory, EntityPlayer playerIn) {
  362. return new ContainerForge(playerInventory, this);
  363. }
  364.  
  365. public int getField(int id) {
  366. switch (id) {
  367. case 0: return this.ForgeBurnTime;
  368. case 1: return this.currentItemBurnTime;
  369. case 2: return this.cookTime;
  370. case 3: return this.totalCookTime;
  371. default: return 0;
  372. }
  373.  
  374. }
  375.  
  376. public void setField(int id, int value) {
  377. switch (id) {
  378. case 0: this.ForgeBurnTime = value;
  379. break;
  380. case 1: this.currentItemBurnTime = value;
  381. break;
  382. case 2: this.cookTime = value;
  383. break;
  384. case 3: this.totalCookTime = value;
  385. }
  386.  
  387. }
  388.  
  389. public int getFieldCount() {
  390. return 4;
  391. }
  392.  
  393. public void clear() {
  394. for (int i = 0;
  395. i < this.ForgeItemStacks.length;
  396. ++i) {
  397. this.ForgeItemStacks[i] = null;
  398. }
  399.  
  400. }
  401. }
RAW Paste Data