Guest User

TileEntityNaturalisFurnace

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