Advertisement
Guest User

TileEntityReactor

a guest
Feb 10th, 2013
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 11.72 KB | None | 0 0
  1. package net.minecraft.src;
  2.  
  3. public class TileEntityReactor extends TileEntity implements IInventory
  4. {
  5.     /**
  6.      * The ItemStacks that hold the items currently being used in the furnace
  7.      */
  8.     private ItemStack[] reactorItemStacks = new ItemStack[3];
  9.  
  10.     /** The number of ticks that the furnace will keep burning */
  11.     public int furnaceBurnTime = 0;
  12.  
  13.     /**
  14.      * The number of ticks that a fresh copy of the currently-burning item would keep the furnace burning for
  15.      */
  16.     public int currentItemBurnTime = 0;
  17.  
  18.     /** The number of ticks that the current item has been cooking for */
  19.     public int reactorCookTime = 0;
  20.  
  21.     /**
  22.      * Returns the number of slots in the inventory.
  23.      */
  24.     public int getSizeInventory()
  25.     {
  26.         return this.reactorItemStacks.length;
  27.     }
  28.  
  29.     /**
  30.      * Returns the stack in slot i
  31.      */
  32.     public ItemStack getStackInSlot(int par1)
  33.     {
  34.         return this.reactorItemStacks[par1];
  35.     }
  36.  
  37.     /**
  38.      * Removes from an inventory slot (first arg) up to a specified number (second arg) of items and returns them in a
  39.      * new stack.
  40.      */
  41.     public ItemStack decrStackSize(int par1, int par2)
  42.     {
  43.         if (this.reactorItemStacks[par1] != null)
  44.         {
  45.             ItemStack var3;
  46.  
  47.             if (this.reactorItemStacks[par1].stackSize <= par2)
  48.             {
  49.                 var3 = this.reactorItemStacks[par1];
  50.                 this.reactorItemStacks[par1] = null;
  51.                 return var3;
  52.             }
  53.             else
  54.             {
  55.                 var3 = this.reactorItemStacks[par1].splitStack(par2);
  56.  
  57.                 if (this.reactorItemStacks[par1].stackSize == 0)
  58.                 {
  59.                     this.reactorItemStacks[par1] = null;
  60.                 }
  61.  
  62.                 return var3;
  63.             }
  64.         }
  65.         else
  66.         {
  67.             return null;
  68.         }
  69.     }
  70.  
  71.     /**
  72.      * When some containers are closed they call this on each slot, then drop whatever it returns as an EntityItem -
  73.      * like when you close a workbench GUI.
  74.      */
  75.     public ItemStack getStackInSlotOnClosing(int par1)
  76.     {
  77.         if (this.reactorItemStacks[par1] != null)
  78.         {
  79.             ItemStack var2 = this.reactorItemStacks[par1];
  80.             this.reactorItemStacks[par1] = null;
  81.             return var2;
  82.         }
  83.         else
  84.         {
  85.             return null;
  86.         }
  87.     }
  88.  
  89.     /**
  90.      * Sets the given item stack to the specified slot in the inventory (can be crafting or armor sections).
  91.      */
  92.     public void setInventorySlotContents(int par1, ItemStack par2ItemStack)
  93.     {
  94.         this.reactorItemStacks[par1] = par2ItemStack;
  95.  
  96.         if (par2ItemStack != null && par2ItemStack.stackSize > this.getInventoryStackLimit())
  97.         {
  98.             par2ItemStack.stackSize = this.getInventoryStackLimit();
  99.         }
  100.     }
  101.  
  102.     /**
  103.      * Returns the name of the inventory.
  104.      */
  105.     public String getInvName()
  106.     {
  107.         return "container.reactor";
  108.     }
  109.  
  110.     /**
  111.      * Reads a tile entity from NBT.
  112.      */
  113.     public void readFromNBT(NBTTagCompound par1NBTTagCompound)
  114.     {
  115.         super.readFromNBT(par1NBTTagCompound);
  116.         NBTTagList var2 = par1NBTTagCompound.getTagList("Items");
  117.         this.reactorItemStacks = new ItemStack[this.getSizeInventory()];
  118.  
  119.         for (int var3 = 0; var3 < var2.tagCount(); ++var3)
  120.         {
  121.             NBTTagCompound var4 = (NBTTagCompound)var2.tagAt(var3);
  122.             byte var5 = var4.getByte("Slot");
  123.  
  124.             if (var5 >= 0 && var5 < this.reactorItemStacks.length)
  125.             {
  126.                 this.reactorItemStacks[var5] = ItemStack.loadItemStackFromNBT(var4);
  127.             }
  128.         }
  129.  
  130.         this.furnaceBurnTime = par1NBTTagCompound.getShort("BurnTime");
  131.         this.reactorCookTime = par1NBTTagCompound.getShort("CookTime");
  132.         this.currentItemBurnTime = getItemBurnTime(this.reactorItemStacks[1]);
  133.     }
  134.  
  135.     /**
  136.      * Writes a tile entity to NBT.
  137.      */
  138.     public void writeToNBT(NBTTagCompound par1NBTTagCompound)
  139.     {
  140.         super.writeToNBT(par1NBTTagCompound);
  141.         par1NBTTagCompound.setShort("BurnTime", (short)this.furnaceBurnTime);
  142.         par1NBTTagCompound.setShort("CookTime", (short)this.reactorCookTime);
  143.         NBTTagList var2 = new NBTTagList();
  144.  
  145.         for (int var3 = 0; var3 < this.reactorItemStacks.length; ++var3)
  146.         {
  147.             if (this.reactorItemStacks[var3] != null)
  148.             {
  149.                 NBTTagCompound var4 = new NBTTagCompound();
  150.                 var4.setByte("Slot", (byte)var3);
  151.                 this.reactorItemStacks[var3].writeToNBT(var4);
  152.                 var2.appendTag(var4);
  153.             }
  154.         }
  155.  
  156.         par1NBTTagCompound.setTag("Items", var2);
  157.     }
  158.  
  159.     /**
  160.      * Returns the maximum stack size for a inventory slot. Seems to always be 64, possibly will be extended. *Isn't
  161.      * this more of a set than a get?*
  162.      */
  163.     public int getInventoryStackLimit()
  164.     {
  165.         return 64;
  166.     }
  167.  
  168.     /**
  169.      * Returns an integer between 0 and the passed value representing how close the current item is to being completely
  170.      * cooked
  171.      */
  172.     public int getCookProgressScaled(int par1)
  173.     {
  174.         return this.reactorCookTime * par1 / 200;
  175.     }
  176.  
  177.     /**
  178.      * Returns an integer between 0 and the passed value representing how much burn time is left on the current fuel
  179.      * item, where 0 means that the item is exhausted and the passed value means that the item is fresh
  180.      */
  181.     public int getBurnTimeRemainingScaled(int par1)
  182.     {
  183.         if (this.currentItemBurnTime == 0)
  184.         {
  185.             this.currentItemBurnTime = 200;
  186.         }
  187.  
  188.         return this.furnaceBurnTime * par1 / this.currentItemBurnTime;
  189.     }
  190.  
  191.     /**
  192.      * Returns true if the furnace is currently burning
  193.      */
  194.     public boolean isBurning()
  195.     {
  196.         return this.furnaceBurnTime > 0;
  197.     }
  198.  
  199.     /**
  200.      * Allows the entity to update its state. Overridden in most subclasses, e.g. the mob spawner uses this to count
  201.      * ticks and creates a new spawn inside its implementation.
  202.      */
  203.     public void updateEntity()
  204.     {
  205.         boolean var1 = this.furnaceBurnTime > 0;
  206.         boolean var2 = false;
  207.  
  208.         if (this.furnaceBurnTime > 0)
  209.         {
  210.             --this.furnaceBurnTime;
  211.         }
  212.  
  213.         if (!this.worldObj.isRemote)
  214.         {
  215.             if (this.furnaceBurnTime == 0 && this.canSmelt())
  216.             {
  217.                 this.currentItemBurnTime = this.furnaceBurnTime = getItemBurnTime(this.reactorItemStacks[1]);
  218.  
  219.                 if (this.furnaceBurnTime > 0)
  220.                 {
  221.                     var2 = true;
  222.  
  223.                     if (this.reactorItemStacks[1] != null)
  224.                     {
  225.                         --this.reactorItemStacks[1].stackSize;
  226.  
  227.                         if (this.reactorItemStacks[1].stackSize == 0)
  228.                         {
  229.                             Item var3 = this.reactorItemStacks[1].getItem().getContainerItem();
  230.                             this.reactorItemStacks[1] = var3 != null ? new ItemStack(var3) : null;
  231.                         }
  232.                     }
  233.                 }
  234.             }
  235.  
  236.             if (this.isBurning() && this.canSmelt())
  237.             {
  238.                 ++this.reactorCookTime;
  239.  
  240.                 if (this.reactorCookTime == 200)
  241.                 {
  242.                     this.reactorCookTime = 0;
  243.                     this.smeltItem();
  244.                     var2 = true;
  245.                 }
  246.             }
  247.             else
  248.             {
  249.                 this.reactorCookTime = 0;
  250.             }
  251.  
  252.             if (var1 != this.furnaceBurnTime > 0)
  253.             {
  254.                 var2 = true;
  255.                 BlockReactor.updateReactorBlockState(this.furnaceBurnTime > 0, this.worldObj, this.xCoord, this.yCoord, this.zCoord);
  256.             }
  257.         }
  258.  
  259.         if (var2)
  260.         {
  261.             this.onInventoryChanged();
  262.         }
  263.     }
  264.  
  265.     /**
  266.      * Returns true if the furnace can smelt an item, i.e. has a source item, destination stack isn't full, etc.
  267.      */
  268.     private boolean canSmelt()
  269.     {
  270.         if (this.reactorItemStacks[0] == null)
  271.         {
  272.             return false;
  273.         }
  274.         else
  275.         {
  276.             ItemStack var1 = FurnaceRecipes.smelting().getSmeltingResult(this.reactorItemStacks[0].getItem().itemID);
  277.             return var1 == null ? false : (this.reactorItemStacks[2] == null ? true : (!this.reactorItemStacks[2].isItemEqual(var1) ? false : (this.reactorItemStacks[2].stackSize < this.getInventoryStackLimit() && this.reactorItemStacks[2].stackSize < this.reactorItemStacks[2].getMaxStackSize() ? true : this.reactorItemStacks[2].stackSize < var1.getMaxStackSize())));
  278.         }
  279.     }
  280.  
  281.     /**
  282.      * Turn one item from the furnace source stack into the appropriate smelted item in the furnace result stack
  283.      */
  284.     public void smeltItem()
  285.     {
  286.         if (this.canSmelt())
  287.         {
  288.             ItemStack var1 = FurnaceRecipes.smelting().getSmeltingResult(this.reactorItemStacks[0].getItem().itemID);
  289.  
  290.             if (this.reactorItemStacks[2] == null)
  291.             {
  292.                 this.reactorItemStacks[2] = var1.copy();
  293.             }
  294.             else if (this.reactorItemStacks[2].itemID == var1.itemID)
  295.             {
  296.                 ++this.reactorItemStacks[2].stackSize;
  297.             }
  298.  
  299.             --this.reactorItemStacks[0].stackSize;
  300.  
  301.             if (this.reactorItemStacks[0].stackSize <= 0)
  302.             {
  303.                 Item var2 = this.reactorItemStacks[0].getItem().getContainerItem();
  304.                 this.reactorItemStacks[0] = var2 != null ? new ItemStack(var2) : null;
  305.             }
  306.         }
  307.     }
  308.  
  309.     /**
  310.      * Returns the number of ticks that the supplied fuel item will keep the furnace burning, or 0 if the item isn't
  311.      * fuel
  312.      */
  313.     public static int getItemBurnTime(ItemStack par0ItemStack)
  314.     {
  315.         if (par0ItemStack == null)
  316.         {
  317.             return 0;
  318.         }
  319.         else
  320.         {
  321.             int var1 = par0ItemStack.getItem().itemID;
  322.             Item var2 = par0ItemStack.getItem();
  323.  
  324.             if (var1 < 256 && Block.blocksList[var1] != null)
  325.             {
  326.                 Block var3 = Block.blocksList[var1];
  327.  
  328.                 if (var3 == Block.woodSingleSlab)
  329.                 {
  330.                     return 150;
  331.                 }
  332.  
  333.                 if (var3.blockMaterial == Material.wood)
  334.                 {
  335.                     return 300;
  336.                 }
  337.             }
  338.  
  339.             return var2 instanceof ItemTool && ((ItemTool)var2).getToolMaterialName().equals("WOOD") ? 200 : (var2 instanceof ItemSword && ((ItemSword)var2).getToolMaterialName().equals("WOOD") ? 200 : (var2 instanceof ItemHoe && ((ItemHoe)var2).func_77842_f().equals("WOOD") ? 200 : (var1 == Item.stick.itemID ? 100 : (var1 == Item.coal.itemID ? 1600 : (var1 == Item.bucketLava.itemID ? 20000 : (var1 == Block.sapling.blockID ? 100 : (var1 == Item.blazeRod.itemID ? 2400 : ModLoader.addAllFuel(par0ItemStack.itemID, par0ItemStack.getItemDamage()))))))));
  340.         }
  341.     }
  342.  
  343.     /**
  344.      * Return true if item is a fuel source (getItemBurnTime() > 0).
  345.      */
  346.     public static boolean isItemFuel(ItemStack par0ItemStack)
  347.     {
  348.         return getItemBurnTime(par0ItemStack) > 0;
  349.     }
  350.  
  351.     /**
  352.      * Do not make give this method the name canInteractWith because it clashes with Container
  353.      */
  354.     public boolean isUseableByPlayer(EntityPlayer par1EntityPlayer)
  355.     {
  356.         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;
  357.     }
  358.  
  359.     public void openChest() {}
  360.  
  361.     public void closeChest() {}
  362. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement