Advertisement
Guest User

Untitled

a guest
Feb 19th, 2015
268
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 14.70 KB | None | 0 0
  1. package net.minecraft.tileentity;
  2.  
  3. import net.minecraft.block.Block;
  4. import net.minecraft.block.BlockFurnace;
  5. import net.minecraft.block.material.Material;
  6. import net.minecraft.entity.player.EntityPlayer;
  7. import net.minecraft.entity.player.InventoryPlayer;
  8. import net.minecraft.init.Blocks;
  9. import net.minecraft.init.Items;
  10. import net.minecraft.inventory.Container;
  11. import net.minecraft.inventory.ContainerFurnace;
  12. import net.minecraft.inventory.IInventory;
  13. import net.minecraft.inventory.ISidedInventory;
  14. import net.minecraft.inventory.SlotFurnaceFuel;
  15. import net.minecraft.item.Item;
  16. import net.minecraft.item.ItemBlock;
  17. import net.minecraft.item.ItemHoe;
  18. import net.minecraft.item.ItemStack;
  19. import net.minecraft.item.ItemSword;
  20. import net.minecraft.item.ItemTool;
  21. import net.minecraft.item.crafting.FurnaceRecipes;
  22. import net.minecraft.nbt.NBTTagCompound;
  23. import net.minecraft.nbt.NBTTagList;
  24. import net.minecraft.server.gui.IUpdatePlayerListBox;
  25. import net.minecraft.util.EnumFacing;
  26. import net.minecraft.util.MathHelper;
  27. import net.minecraftforge.fml.relauncher.Side;
  28. import net.minecraftforge.fml.relauncher.SideOnly;
  29.  
  30. public class TileEntityFurnace extends TileEntityLockable implements IUpdatePlayerListBox, ISidedInventory
  31. {
  32.     private static final int[] slotsTop = new int[] {0};
  33.     private static final int[] slotsBottom = new int[] {2, 1};
  34.     private static final int[] slotsSides = new int[] {1};
  35.     private ItemStack[] furnaceItemStacks = new ItemStack[3];
  36.     private int furnaceBurnTime;
  37.     private int currentItemBurnTime;
  38.     private int cookTime;
  39.     private int totalCookTime;
  40.     private String furnaceCustomName;
  41.     private static final String __OBFID = "CL_00000357";
  42.  
  43.     public int getSizeInventory()
  44.     {
  45.         return this.furnaceItemStacks.length;
  46.     }
  47.  
  48.     public ItemStack getStackInSlot(int index)
  49.     {
  50.         return this.furnaceItemStacks[index];
  51.     }
  52.  
  53.     public ItemStack decrStackSize(int index, int count)
  54.     {
  55.         if (this.furnaceItemStacks[index] != null)
  56.         {
  57.             ItemStack itemstack;
  58.  
  59.             if (this.furnaceItemStacks[index].stackSize <= count)
  60.             {
  61.                 itemstack = this.furnaceItemStacks[index];
  62.                 this.furnaceItemStacks[index] = null;
  63.                 return itemstack;
  64.             }
  65.             else
  66.             {
  67.                 itemstack = this.furnaceItemStacks[index].splitStack(count);
  68.  
  69.                 if (this.furnaceItemStacks[index].stackSize == 0)
  70.                 {
  71.                     this.furnaceItemStacks[index] = null;
  72.                 }
  73.  
  74.                 return itemstack;
  75.             }
  76.         }
  77.         else
  78.         {
  79.             return null;
  80.         }
  81.     }
  82.  
  83.     public ItemStack getStackInSlotOnClosing(int index)
  84.     {
  85.         if (this.furnaceItemStacks[index] != null)
  86.         {
  87.             ItemStack itemstack = this.furnaceItemStacks[index];
  88.             this.furnaceItemStacks[index] = null;
  89.             return itemstack;
  90.         }
  91.         else
  92.         {
  93.             return null;
  94.         }
  95.     }
  96.  
  97.     public void setInventorySlotContents(int index, ItemStack stack)
  98.     {
  99.         boolean flag = stack != null && stack.isItemEqual(this.furnaceItemStacks[index]) && ItemStack.areItemStackTagsEqual(stack, this.furnaceItemStacks[index]);
  100.         this.furnaceItemStacks[index] = stack;
  101.  
  102.         if (stack != null && stack.stackSize > this.getInventoryStackLimit())
  103.         {
  104.             stack.stackSize = this.getInventoryStackLimit();
  105.         }
  106.  
  107.         if (index == 0 && !flag)
  108.         {
  109.             this.totalCookTime = this.func_174904_a(stack);
  110.             this.cookTime = 0;
  111.             this.markDirty();
  112.         }
  113.     }
  114.  
  115.     public String getName()
  116.     {
  117.         return this.hasCustomName() ? this.furnaceCustomName : "container.furnace";
  118.     }
  119.  
  120.     public boolean hasCustomName()
  121.     {
  122.         return this.furnaceCustomName != null && this.furnaceCustomName.length() > 0;
  123.     }
  124.  
  125.     public void setCustomInventoryName(String p_145951_1_)
  126.     {
  127.         this.furnaceCustomName = p_145951_1_;
  128.     }
  129.  
  130.     public void readFromNBT(NBTTagCompound compound)
  131.     {
  132.         super.readFromNBT(compound);
  133.         NBTTagList nbttaglist = compound.getTagList("Items", 10);
  134.         this.furnaceItemStacks = new ItemStack[this.getSizeInventory()];
  135.  
  136.         for (int i = 0; i < nbttaglist.tagCount(); ++i)
  137.         {
  138.             NBTTagCompound nbttagcompound1 = nbttaglist.getCompoundTagAt(i);
  139.             byte b0 = nbttagcompound1.getByte("Slot");
  140.  
  141.             if (b0 >= 0 && b0 < this.furnaceItemStacks.length)
  142.             {
  143.                 this.furnaceItemStacks[b0] = ItemStack.loadItemStackFromNBT(nbttagcompound1);
  144.             }
  145.         }
  146.  
  147.         this.furnaceBurnTime = compound.getShort("BurnTime");
  148.         this.cookTime = compound.getShort("CookTime");
  149.         this.totalCookTime = compound.getShort("CookTimeTotal");
  150.         this.currentItemBurnTime = getItemBurnTime(this.furnaceItemStacks[1]);
  151.  
  152.         if (compound.hasKey("CustomName", 8))
  153.         {
  154.             this.furnaceCustomName = compound.getString("CustomName");
  155.         }
  156.     }
  157.  
  158.     public void writeToNBT(NBTTagCompound compound)
  159.     {
  160.         super.writeToNBT(compound);
  161.         compound.setShort("BurnTime", (short)this.furnaceBurnTime);
  162.         compound.setShort("CookTime", (short)this.cookTime);
  163.         compound.setShort("CookTimeTotal", (short)this.totalCookTime);
  164.         NBTTagList nbttaglist = new NBTTagList();
  165.  
  166.         for (int i = 0; i < this.furnaceItemStacks.length; ++i)
  167.         {
  168.             if (this.furnaceItemStacks[i] != null)
  169.             {
  170.                 NBTTagCompound nbttagcompound1 = new NBTTagCompound();
  171.                 nbttagcompound1.setByte("Slot", (byte)i);
  172.                 this.furnaceItemStacks[i].writeToNBT(nbttagcompound1);
  173.                 nbttaglist.appendTag(nbttagcompound1);
  174.             }
  175.         }
  176.  
  177.         compound.setTag("Items", nbttaglist);
  178.  
  179.         if (this.hasCustomName())
  180.         {
  181.             compound.setString("CustomName", this.furnaceCustomName);
  182.         }
  183.     }
  184.  
  185.     public int getInventoryStackLimit()
  186.     {
  187.         return 64;
  188.     }
  189.  
  190.     public boolean isBurning()
  191.     {
  192.         return this.furnaceBurnTime > 0;
  193.     }
  194.  
  195.     @SideOnly(Side.CLIENT)
  196.     public static boolean isBurning(IInventory p_174903_0_)
  197.     {
  198.         return p_174903_0_.getField(0) > 0;
  199.     }
  200.  
  201.     public void update()
  202.     {
  203.         boolean flag = this.isBurning();
  204.         boolean flag1 = false;
  205.  
  206.         if (this.isBurning())
  207.         {
  208.             --this.furnaceBurnTime;
  209.         }
  210.  
  211.         if (!this.worldObj.isRemote)
  212.         {
  213.             if (!this.isBurning() && (this.furnaceItemStacks[1] == null || this.furnaceItemStacks[0] == null))
  214.             {
  215.                 if (!this.isBurning() && this.cookTime > 0)
  216.                 {
  217.                     this.cookTime = MathHelper.clamp_int(this.cookTime - 2, 0, this.totalCookTime);
  218.                 }
  219.             }
  220.             else
  221.             {
  222.                 if (!this.isBurning() && this.canSmelt())
  223.                 {
  224.                     this.currentItemBurnTime = this.furnaceBurnTime = getItemBurnTime(this.furnaceItemStacks[1]);
  225.  
  226.                     if (this.isBurning())
  227.                     {
  228.                         flag1 = true;
  229.  
  230.                         if (this.furnaceItemStacks[1] != null)
  231.                         {
  232.                             --this.furnaceItemStacks[1].stackSize;
  233.  
  234.                             if (this.furnaceItemStacks[1].stackSize == 0)
  235.                             {
  236.                                 this.furnaceItemStacks[1] = furnaceItemStacks[1].getItem().getContainerItem(furnaceItemStacks[1]);
  237.                             }
  238.                         }
  239.                     }
  240.                 }
  241.  
  242.                 if (this.isBurning() && this.canSmelt())
  243.                 {
  244.                     ++this.cookTime;
  245.  
  246.                     if (this.cookTime == this.totalCookTime)
  247.                     {
  248.                         this.cookTime = 0;
  249.                         this.totalCookTime = this.func_174904_a(this.furnaceItemStacks[0]);
  250.                         this.smeltItem();
  251.                         flag1 = true;
  252.                     }
  253.                 }
  254.                 else
  255.                 {
  256.                     this.cookTime = 0;
  257.                 }
  258.             }
  259.  
  260.             if (flag != this.isBurning())
  261.             {
  262.                 flag1 = true;
  263.                 BlockFurnace.setState(this.isBurning(), this.worldObj, this.pos);
  264.             }
  265.         }
  266.  
  267.         if (flag1)
  268.         {
  269.             this.markDirty();
  270.         }
  271.     }
  272.  
  273.     public int func_174904_a(ItemStack p_174904_1_)
  274.     {
  275.         return 200;
  276.     }
  277.  
  278.     private boolean canSmelt()
  279.     {
  280.         if (this.furnaceItemStacks[0] == null)
  281.         {
  282.             return false;
  283.         }
  284.         else
  285.         {
  286.             ItemStack itemstack = FurnaceRecipes.instance().getSmeltingResult(this.furnaceItemStacks[0]);
  287.             if (itemstack == null) return false;
  288.             if (this.furnaceItemStacks[2] == null) return true;
  289.             if (!this.furnaceItemStacks[2].isItemEqual(itemstack)) return false;
  290.             int result = furnaceItemStacks[2].stackSize + itemstack.stackSize;
  291.             return result <= getInventoryStackLimit() && result <= this.furnaceItemStacks[2].getMaxStackSize(); //Forge BugFix: Make it respect stack sizes properly.
  292.         }
  293.     }
  294.  
  295.     public void smeltItem()
  296.     {
  297.         if (this.canSmelt())
  298.         {
  299.             ItemStack itemstack = FurnaceRecipes.instance().getSmeltingResult(this.furnaceItemStacks[0]);
  300.  
  301.             if (this.furnaceItemStacks[2] == null)
  302.             {
  303.                 this.furnaceItemStacks[2] = itemstack.copy();
  304.             }
  305.             else if (this.furnaceItemStacks[2].getItem() == itemstack.getItem())
  306.             {
  307.                 this.furnaceItemStacks[2].stackSize += itemstack.stackSize; // Forge BugFix: Results may have multiple items
  308.             }
  309.  
  310.             if (this.furnaceItemStacks[0].getItem() == Item.getItemFromBlock(Blocks.sponge) && this.furnaceItemStacks[0].getMetadata() == 1 && this.furnaceItemStacks[1] != null && this.furnaceItemStacks[1].getItem() == Items.bucket)
  311.             {
  312.                 this.furnaceItemStacks[1] = new ItemStack(Items.water_bucket);
  313.             }
  314.  
  315.             --this.furnaceItemStacks[0].stackSize;
  316.  
  317.             if (this.furnaceItemStacks[0].stackSize <= 0)
  318.             {
  319.                 this.furnaceItemStacks[0] = null;
  320.             }
  321.         }
  322.     }
  323.  
  324.     public static int getItemBurnTime(ItemStack p_145952_0_)
  325.     {
  326.         if (p_145952_0_ == null)
  327.         {
  328.             return 0;
  329.         }
  330.         else
  331.         {
  332.             Item item = p_145952_0_.getItem();
  333.  
  334.             if (item instanceof ItemBlock && Block.getBlockFromItem(item) != Blocks.air)
  335.             {
  336.                 Block block = Block.getBlockFromItem(item);
  337.  
  338.                 if (block == Blocks.wooden_slab)
  339.                 {
  340.                     return 150;
  341.                 }
  342.  
  343.                 if (block.getMaterial() == Material.wood)
  344.                 {
  345.                     return 300;
  346.                 }
  347.  
  348.                 if (block == Blocks.coal_block)
  349.                 {
  350.                     return 16000;
  351.                 }
  352.             }
  353.  
  354.             if (item instanceof ItemTool && ((ItemTool)item).getToolMaterialName().equals("WOOD")) return 200;
  355.             if (item instanceof ItemSword && ((ItemSword)item).getToolMaterialName().equals("WOOD")) return 200;
  356.             if (item instanceof ItemHoe && ((ItemHoe)item).getMaterialName().equals("WOOD")) return 200;
  357.             if (item == Items.stick) return 100;
  358.             if (item == Items.coal) return 1600;
  359.             if (item == Items.lava_bucket) return 20000;
  360.             if (item == Item.getItemFromBlock(Blocks.sapling)) return 100;
  361.             if (item == Items.blaze_rod) return 2400;
  362.             return net.minecraftforge.fml.common.registry.GameRegistry.getFuelValue(p_145952_0_);
  363.         }
  364.     }
  365.  
  366.     public static boolean isItemFuel(ItemStack p_145954_0_)
  367.     {
  368.         return getItemBurnTime(p_145954_0_) > 0;
  369.     }
  370.  
  371.     public boolean isUseableByPlayer(EntityPlayer player)
  372.     {
  373.         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;
  374.     }
  375.  
  376.     public void openInventory(EntityPlayer player) {}
  377.  
  378.     public void closeInventory(EntityPlayer player) {}
  379.  
  380.     public boolean isItemValidForSlot(int index, ItemStack stack)
  381.     {
  382.         return index == 2 ? false : (index != 1 ? true : isItemFuel(stack) || SlotFurnaceFuel.isBucket(stack));
  383.     }
  384.  
  385.     public int[] getSlotsForFace(EnumFacing side)
  386.     {
  387.         return side == EnumFacing.DOWN ? slotsBottom : (side == EnumFacing.UP ? slotsTop : slotsSides);
  388.     }
  389.  
  390.     public boolean canInsertItem(int index, ItemStack itemStackIn, EnumFacing direction)
  391.     {
  392.         return this.isItemValidForSlot(index, itemStackIn);
  393.     }
  394.  
  395.     public boolean canExtractItem(int index, ItemStack stack, EnumFacing direction)
  396.     {
  397.         if (direction == EnumFacing.DOWN && index == 1)
  398.         {
  399.             Item item = stack.getItem();
  400.  
  401.             if (item != Items.water_bucket && item != Items.bucket)
  402.             {
  403.                 return false;
  404.             }
  405.         }
  406.  
  407.         return true;
  408.     }
  409.  
  410.     public String getGuiID()
  411.     {
  412.         return "minecraft:furnace";
  413.     }
  414.  
  415.     public Container createContainer(InventoryPlayer playerInventory, EntityPlayer playerIn)
  416.     {
  417.         return new ContainerFurnace(playerInventory, this);
  418.     }
  419.  
  420.     public int getField(int id)
  421.     {
  422.         switch (id)
  423.         {
  424.             case 0:
  425.                 return this.furnaceBurnTime;
  426.             case 1:
  427.                 return this.currentItemBurnTime;
  428.             case 2:
  429.                 return this.cookTime;
  430.             case 3:
  431.                 return this.totalCookTime;
  432.             default:
  433.                 return 0;
  434.         }
  435.     }
  436.  
  437.     public void setField(int id, int value)
  438.     {
  439.         switch (id)
  440.         {
  441.             case 0:
  442.                 this.furnaceBurnTime = value;
  443.                 break;
  444.             case 1:
  445.                 this.currentItemBurnTime = value;
  446.                 break;
  447.             case 2:
  448.                 this.cookTime = value;
  449.                 break;
  450.             case 3:
  451.                 this.totalCookTime = value;
  452.         }
  453.     }
  454.  
  455.     public int getFieldCount()
  456.     {
  457.         return 4;
  458.     }
  459.  
  460.     public void clear()
  461.     {
  462.         for (int i = 0; i < this.furnaceItemStacks.length; ++i)
  463.         {
  464.             this.furnaceItemStacks[i] = null;
  465.         }
  466.     }
  467. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement