Guest User

TileEntity Furnace Chest

a guest
Apr 27th, 2013
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 14.52 KB | None | 0 0
  1. public class TileEntityFurnaceChest extends TileEntityMaz implements IInventory {
  2.  
  3.     private String inventoryName = "FurnaceChest";
  4.     private ItemStack[] furnaceChestContents = new ItemStack[54];
  5.     private int slotCurrentlySmeltingFrom = -1;
  6.  
  7.     /** The number of ticks that the furnace will keep burning */
  8.     public int furnaceBurnTime = 0;
  9.  
  10.     /**
  11.      * The number of ticks that a fresh copy of the currently-burning item would keep the furnace burning for
  12.      */
  13.     public int currentItemBurnTime = 0;
  14.  
  15.     /** The number of ticks that the current item has been cooking for */
  16.     public int furnaceCookTime = 0;
  17.     private String name = "TileEntity.FurnaceChest";
  18.  
  19.    
  20.     /*
  21.      * Called when the block needs to update it's ON/OFF State.
  22.      */
  23.     private void updateBlockIsActive() {
  24.         // TODO Auto-generated method stub
  25.        
  26.     }
  27.    
  28.    
  29.     /**
  30.      * Returns the number of slots in the inventory.
  31.      */
  32.     public int getSizeInventory()
  33.     {
  34.         return this.furnaceChestContents.length;
  35.     }
  36.  
  37.     /**
  38.      * Returns the stack in slot i
  39.      */
  40.     public ItemStack getStackInSlot(int par1)
  41.     {
  42.         return this.furnaceChestContents[par1];
  43.     }
  44.  
  45.     /**
  46.      * Removes from an inventory slot (first arg) up to a specified number (second arg) of items and returns them in a
  47.      * new stack.
  48.      */
  49.     public ItemStack decrStackSize(int slotNumber, int itemsToRemove)
  50.     {
  51.         if (this.furnaceChestContents[slotNumber] != null)
  52.         {
  53.             ItemStack itemstack;
  54.  
  55.             if (this.furnaceChestContents[slotNumber].stackSize <= itemsToRemove)
  56.             {
  57.                 itemstack = this.furnaceChestContents[slotNumber];
  58.                 this.furnaceChestContents[slotNumber] = null;
  59.                 return itemstack;
  60.             }
  61.             else
  62.             {
  63.                 itemstack = this.furnaceChestContents[slotNumber].splitStack(itemsToRemove);
  64.  
  65.                 if (this.furnaceChestContents[slotNumber].stackSize == 0)
  66.                 {
  67.                     this.furnaceChestContents[slotNumber] = null;
  68.                 }
  69.  
  70.                 return itemstack;
  71.             }
  72.         }
  73.         else
  74.         {
  75.             return null;
  76.         }
  77.     }
  78.  
  79.     /**
  80.      * When some containers are closed they call this on each slot, then drop whatever it returns as an EntityItem -
  81.      * like when you close a workbench GUI.
  82.      */
  83.     public ItemStack getStackInSlotOnClosing(int par1)
  84.     {
  85.         if (this.furnaceChestContents[par1] != null)
  86.         {
  87.             ItemStack itemstack = this.furnaceChestContents[par1];
  88.             this.furnaceChestContents[par1] = null;
  89.             return itemstack;
  90.         }
  91.         else
  92.         {
  93.             return null;
  94.         }
  95.     }
  96.  
  97.     /**
  98.      * Sets the given item stack to the specified slot in the inventory (can be crafting or armor sections).
  99.      */
  100.     public void setInventorySlotContents(int par1, ItemStack par2ItemStack)
  101.     {
  102.         this.furnaceChestContents[par1] = par2ItemStack;
  103.  
  104.         if (par2ItemStack != null && par2ItemStack.stackSize > this.getInventoryStackLimit())
  105.         {
  106.             par2ItemStack.stackSize = this.getInventoryStackLimit();
  107.         }
  108.     }
  109.  
  110.     /**
  111.      * Returns the name of the inventory.
  112.      */
  113.     public String getInvName()
  114.     {
  115.         return inventoryName;
  116.     }
  117.  
  118.     /**
  119.      * If this returns false, the inventory name will be used as an unlocalized name, and translated into the player's
  120.      * language. Otherwise it will be used directly.
  121.      */
  122.     public boolean isInvNameLocalized()
  123.     {
  124.         return true;
  125.     }
  126.  
  127.     public void func_94129_a(String par1Str)
  128.     {
  129.         this.inventoryName = par1Str;
  130.     }
  131.  
  132.     /**
  133.      * Reads a tile entity from NBT.
  134.      */
  135.     public void readFromNBT(NBTTagCompound par1NBTTagCompound)
  136.     {
  137.         super.readFromNBT(par1NBTTagCompound);
  138.         NBTTagList nbttaglist = par1NBTTagCompound.getTagList("Items");
  139.         this.furnaceChestContents = new ItemStack[this.getSizeInventory()];
  140.  
  141.         for (int i = 0; i < nbttaglist.tagCount(); ++i)
  142.         {
  143.             NBTTagCompound nbttagcompound1 = (NBTTagCompound)nbttaglist.tagAt(i);
  144.             byte b0 = nbttagcompound1.getByte("Slot");
  145.  
  146.             if (b0 >= 0 && b0 < this.furnaceChestContents.length)
  147.             {
  148.                 this.furnaceChestContents[b0] = ItemStack.loadItemStackFromNBT(nbttagcompound1);
  149.             }
  150.         }
  151.  
  152.         this.furnaceBurnTime = par1NBTTagCompound.getShort("BurnTime");
  153.         this.furnaceCookTime = par1NBTTagCompound.getShort("CookTime");
  154.         this.currentItemBurnTime = getItemBurnTime(this.furnaceChestContents[1]);
  155.  
  156.         if (par1NBTTagCompound.hasKey("CustomName"))
  157.         {
  158.             this.name = par1NBTTagCompound.getString("CustomName");
  159.         }
  160.     }
  161.  
  162.     /**
  163.      * Writes a tile entity to NBT.
  164.      */
  165.     public void writeToNBT(NBTTagCompound par1NBTTagCompound)
  166.     {
  167.         super.writeToNBT(par1NBTTagCompound);
  168.         par1NBTTagCompound.setShort("BurnTime", (short)this.furnaceBurnTime);
  169.         par1NBTTagCompound.setShort("CookTime", (short)this.furnaceCookTime);
  170.         NBTTagList nbttaglist = new NBTTagList();
  171.  
  172.         for (int i = 0; i < this.furnaceChestContents.length; ++i)
  173.         {
  174.             if (this.furnaceChestContents[i] != null)
  175.             {
  176.                 NBTTagCompound nbttagcompound1 = new NBTTagCompound();
  177.                 nbttagcompound1.setByte("Slot", (byte)i);
  178.                 this.furnaceChestContents[i].writeToNBT(nbttagcompound1);
  179.                 nbttaglist.appendTag(nbttagcompound1);
  180.             }
  181.         }
  182.  
  183.         par1NBTTagCompound.setTag("Items", nbttaglist);
  184.  
  185.         if (this.isInvNameLocalized())
  186.         {
  187.             par1NBTTagCompound.setString("CustomName", this.name);
  188.         }
  189.     }
  190.  
  191.     /**
  192.      * Returns the maximum stack size for a inventory slot. Seems to always be 64, possibly will be extended. *Isn't
  193.      * this more of a set than a get?*
  194.      */
  195.     public int getInventoryStackLimit()
  196.     {
  197.         return 64;
  198.     }
  199.  
  200.     @SideOnly(Side.CLIENT)
  201.  
  202.     /**
  203.      * Returns an integer between 0 and the passed value representing how close the current item is to being completely
  204.      * cooked
  205.      */
  206.     public int getCookProgressScaled(int par1)
  207.     {
  208.         return this.furnaceCookTime * par1 / 200;
  209.     }
  210.  
  211.     @SideOnly(Side.CLIENT)
  212.  
  213.     /**
  214.      * Returns an integer between 0 and the passed value representing how much burn time is left on the current fuel
  215.      * item, where 0 means that the item is exhausted and the passed value means that the item is fresh
  216.      */
  217.     public int getBurnTimeRemainingScaled(int par1)
  218.     {
  219.         if (this.currentItemBurnTime == 0)
  220.         {
  221.             this.currentItemBurnTime = 200;
  222.         }
  223.  
  224.         return this.furnaceBurnTime * par1 / this.currentItemBurnTime;
  225.     }
  226.  
  227.     /**
  228.      * Returns true if the furnace is currently burning
  229.      */
  230.     public boolean isBurning()
  231.     {
  232.         return this.furnaceBurnTime > 0;
  233.     }
  234.  
  235.     /**
  236.      * Allows the entity to update its state. Overridden in most subclasses, e.g. the mob spawner uses this to count
  237.      * ticks and creates a new spawn inside its implementation.
  238.      */
  239.     public void updateEntity()
  240.     {
  241.         boolean isBurning = this.furnaceBurnTime > 0;
  242.         boolean isSmeltingItem = false;
  243.         int slotWithFuel = -1;
  244.  
  245.         if (this.furnaceBurnTime > 0)
  246.         {
  247.             --this.furnaceBurnTime;
  248.         }
  249.  
  250.         if (!this.worldObj.isRemote)
  251.         {
  252.             slotWithFuel = getSlotWithFuelItem();
  253.             if (slotCurrentlySmeltingFrom == -1)
  254.                 slotCurrentlySmeltingFrom = getSlotWithSmeltableItem();
  255.            
  256.             if (this.furnaceBurnTime == 0 && slotCurrentlySmeltingFrom != -1 && slotWithFuel != -1)
  257.             {
  258.                
  259.                 this.currentItemBurnTime = this.furnaceBurnTime = getItemBurnTime(this.furnaceChestContents[slotWithFuel]);
  260.  
  261.                 if (this.furnaceBurnTime > 0)
  262.                 {
  263.                     isSmeltingItem = true;
  264.  
  265.                     if (this.furnaceChestContents[slotWithFuel] != null)
  266.                     {
  267.                         --this.furnaceChestContents[slotWithFuel].stackSize;
  268.  
  269.                         if (this.furnaceChestContents[slotWithFuel].stackSize == 0)
  270.                         {
  271.                             this.furnaceChestContents[slotWithFuel] = this.furnaceChestContents[slotWithFuel].getItem().getContainerItemStack(furnaceChestContents[slotWithFuel]);
  272.                         }
  273.                     }
  274.                 }
  275.             }
  276.  
  277.             if (this.isBurning() && slotCurrentlySmeltingFrom != -1)
  278.             {
  279.                 ++this.furnaceCookTime;
  280.  
  281.                 if (this.furnaceCookTime == 200)
  282.                 {
  283.                     this.furnaceCookTime = 0;
  284.                     this.smeltItem();
  285.                     isSmeltingItem = true;
  286.                 }
  287.             }
  288.             else
  289.             {
  290.                 this.furnaceCookTime = 0;
  291.             }
  292.  
  293.             if (isBurning != this.furnaceBurnTime > 0)
  294.             {
  295.                 isSmeltingItem = true;
  296.                 this.updateBlockIsActive();
  297.             }
  298.         }
  299.  
  300.         if (isSmeltingItem)
  301.         {
  302.             this.onInventoryChanged();
  303.         }
  304.     }
  305.    
  306.    
  307.     private int getSlotWithFuelItem(){
  308.         for (int i = 0; i < furnaceChestContents.length; i++) {
  309.             if (getItemBurnTime( furnaceChestContents[i]) > 0)
  310.                 return i;
  311.         }
  312.         return -1;
  313.     }
  314.  
  315.     private int getSlotWithSmeltableItem(){
  316.         for (int i = 0; i < furnaceChestContents.length; i++) {
  317.             ItemStack item = furnaceChestContents[i];
  318.             if (canSmeltItem(item))
  319.                 return i;
  320.         }
  321.         return -1;
  322.     }
  323.    
  324.     private boolean canSmeltItem(ItemStack item){
  325.         ItemStack smeltingResult = FurnaceRecipes.smelting().getSmeltingResult(item);
  326.         if (smeltingResult != null && item != null)
  327.             return true;
  328.         return false;
  329.     }
  330.  
  331.     /**
  332.      * Turn one item from the furnace source stack into the appropriate smelted item in the furnace result stack
  333.      */
  334.     public void smeltItem()
  335.     {
  336.         if (slotCurrentlySmeltingFrom != -1)
  337.         {
  338.             ItemStack outputItem = FurnaceRecipes.smelting().getSmeltingResult(this.furnaceChestContents[slotCurrentlySmeltingFrom]);
  339.            
  340.             // Get empty slot or slot Containing same item, then try to place that item inn the slot.
  341.             // returns true if it's possible to place item.
  342.             if (placeSmeltedItemIntoChest(outputItem)){
  343.                 --this.furnaceChestContents[slotCurrentlySmeltingFrom].stackSize;
  344.  
  345.                 if (this.furnaceChestContents[slotCurrentlySmeltingFrom].stackSize <= 0)
  346.                 {
  347.                     this.furnaceChestContents[slotCurrentlySmeltingFrom] = null;
  348.                 }
  349.                 slotCurrentlySmeltingFrom = -1; // Done smelting current slot
  350.             }
  351.         }
  352.     }
  353.    
  354.     private boolean placeSmeltedItemIntoChest(ItemStack outputItem){
  355.        
  356.        
  357.         // Try place into free open slot or similar item stack.
  358.         for (int i = 0; i < furnaceChestContents.length; i++) {
  359.             if (furnaceChestContents[i] == null)
  360.             {
  361.                 furnaceChestContents[i] = outputItem.copy();
  362.             }
  363.             else if (furnaceChestContents[i].isItemEqual(outputItem))
  364.             {
  365.                 if (furnaceChestContents[i].stackSize + outputItem.stackSize <= furnaceChestContents[i].getMaxStackSize())
  366.                 furnaceChestContents[i].stackSize += outputItem.stackSize;
  367.                 else
  368.                 {
  369.                     outputItem.stackSize = (furnaceChestContents[i].stackSize + outputItem.stackSize) - furnaceChestContents[i].getMaxStackSize();
  370.                     furnaceChestContents[i].stackSize = furnaceChestContents[i].getMaxStackSize();
  371.                    
  372.                
  373.                 }
  374.             }
  375.         }
  376.         return false;
  377.     }
  378.     /**
  379.      * Returns the number of ticks that the supplied fuel item will keep the furnace burning, or 0 if the item isn't
  380.      * fuel
  381.      */
  382.     public static int getItemBurnTime(ItemStack par0ItemStack)
  383.     {
  384.         if (par0ItemStack == null)
  385.         {
  386.             return 0;
  387.         }
  388.         else
  389.         {
  390.             int i = par0ItemStack.getItem().itemID;
  391.             Item item = par0ItemStack.getItem();
  392.  
  393.             if (par0ItemStack.getItem() instanceof ItemBlock && Block.blocksList[i] != null)
  394.             {
  395.                 Block block = Block.blocksList[i];
  396.  
  397.                 if (block == Block.woodSingleSlab)
  398.                 {
  399.                     return 150;
  400.                 }
  401.  
  402.                 if (block.blockMaterial == Material.wood)
  403.                 {
  404.                     return 300;
  405.                 }
  406.             }
  407.  
  408.             if (item instanceof ItemTool && ((ItemTool) item).getToolMaterialName().equals("WOOD")) return 200;
  409.             if (item instanceof ItemSword && ((ItemSword) item).getToolMaterialName().equals("WOOD")) return 200;
  410.             if (item instanceof ItemHoe && ((ItemHoe) item).func_77842_f().equals("WOOD")) return 200;
  411.             if (i == Item.stick.itemID) return 100;
  412.             if (i == Item.coal.itemID) return 1600;
  413.             if (i == Item.bucketLava.itemID) return 20000;
  414.             if (i == Block.sapling.blockID) return 100;
  415.             if (i == Item.blazeRod.itemID) return 2400;
  416.             return GameRegistry.getFuelValue(par0ItemStack);
  417.         }
  418.     }
  419.  
  420.     /**
  421.      * Return true if item is a fuel source (getItemBurnTime() > 0).
  422.      */
  423.     public static boolean isItemFuel(ItemStack par0ItemStack)
  424.     {
  425.         return getItemBurnTime(par0ItemStack) > 0;
  426.     }
  427.  
  428.     /**
  429.      * Do not make give this method the name canInteractWith because it clashes with Container
  430.      */
  431.     public boolean isUseableByPlayer(EntityPlayer par1EntityPlayer)
  432.     {
  433.         return this.worldObj.getBlockTileEntity(this.xCoord, this.yCoord, this.zCoord) != this ? false : par1EntityPlayer.getDistanceSq((double)this.xCoord + 0.5D, (double)this.yCoord + 0.5D, (double)this.zCoord + 0.5D) <= 64.0D;
  434.     }
  435.  
  436.     public void openChest() {}
  437.  
  438.     public void closeChest() {}
  439.  
  440.  
  441.     @Override
  442.     public boolean isStackValidForSlot(int i, ItemStack itemstack) {
  443.         // TODO Auto-generated method stub
  444.         return false;
  445.     }
  446.  
  447.  
  448. }
Advertisement
Add Comment
Please, Sign In to add comment