Advertisement
Guest User

Untitled

a guest
Oct 12th, 2015
224
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 10.26 KB | None | 0 0
  1. package com.halestormxv.tile_entity;
  2.  
  3. import com.halestormxv.blocks.CelestialCraft_blocks;
  4. import com.halestormxv.blocks.CelestialFurnace;
  5. import com.halestormxv.handler.CelestialFurnaceRecipes;
  6. import com.halestormxv.item.CelestialCraft_items;
  7.  
  8. import cpw.mods.fml.common.registry.GameRegistry;
  9. import cpw.mods.fml.relauncher.Side;
  10. import cpw.mods.fml.relauncher.SideOnly;
  11. import net.minecraft.block.Block;
  12. import net.minecraft.block.material.Material;
  13. import net.minecraft.entity.player.EntityPlayer;
  14. import net.minecraft.init.Blocks;
  15. import net.minecraft.init.Items;
  16. import net.minecraft.inventory.ISidedInventory;
  17. import net.minecraft.item.Item;
  18. import net.minecraft.item.ItemBlock;
  19. import net.minecraft.item.ItemStack;
  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.tileentity.TileEntity;
  25.  
  26. public class TileEntityCelestialFurnace extends TileEntity implements ISidedInventory {
  27.  
  28.     //Slots Definitions
  29.     private static final int[] slotsTop = new int[]{0};
  30.     private static final int[] slotsBottom = new int[]{2, 1};
  31.     private static final int[] slotsSides = new int[] {1};
  32.    
  33.     private ItemStack[] slots = new ItemStack[3]; //Slots in the Furnace
  34.    
  35.     public int furnaceSpeed = 45; //Speed at which the furnace cooks.
  36.    
  37.     public int furnaceBurnTime; //number of ticks furnace will keep burning
  38.     public int currentBurnTime; //number of ticks that a fresh copy of the currently burning item would keep the furnace burning for
  39.    
  40.     public int furnaceCookTime; //number of ticks that the current items has been cooking for.
  41.    
  42.     private String localizedName;
  43.    
  44.     public void setGuiDisplayName(String displayName){
  45.         this.localizedName = displayName;
  46.     }
  47.    
  48.     @Override
  49.     public int getSizeInventory() {
  50.         return this.slots.length;
  51.     }
  52.  
  53.     @Override
  54.     public ItemStack getStackInSlot(int slot) {
  55.         return this.slots[slot];
  56.     }
  57.  
  58.     @Override
  59.     public ItemStack decrStackSize(int par1, int par2) {
  60.         if(this.slots[par1] != null){
  61.             ItemStack itemstack;
  62.             if(this.slots[par1].stackSize <= par2){
  63.                 itemstack = this.slots[par1];
  64.                 this.slots[par1] = null;
  65.                 return itemstack;
  66.             }else{
  67.                 itemstack = this.slots[par1].splitStack(par2);
  68.                
  69.                 if (this.slots[par1].stackSize == 0){
  70.                     this.slots[par1] = null;
  71.                 }
  72.                 return itemstack;
  73.             }
  74.         }else{
  75.             return null;
  76.         }
  77.     }
  78.  
  79.     @Override
  80.     public ItemStack getStackInSlotOnClosing(int slot) {
  81.         if(this.slots[slot] != null){
  82.             ItemStack itemstack = this.slots[slot];
  83.             this.slots[slot] = null;
  84.             return itemstack;
  85.         }
  86.         return null;
  87.  
  88.     }
  89.  
  90.     @Override
  91.     public void setInventorySlotContents(int slot, ItemStack itemstack) {
  92.         this.slots[slot] = itemstack;
  93.        
  94.         if(itemstack != null && itemstack.stackSize > this.getInventoryStackLimit()){
  95.             itemstack.stackSize = this.getInventoryStackLimit();
  96.         }
  97.        
  98.     }
  99.  
  100.     @Override
  101.     public String getInventoryName() {
  102.         return this.hasCustomInventoryName() ? this.localizedName : "container.celestialFurnace";
  103.     }
  104.  
  105.     @Override
  106.     public boolean hasCustomInventoryName() {
  107.         // TODO Auto-generated method stub
  108.         return this.localizedName != null && this.localizedName.length() > 0;
  109.     }
  110.  
  111.     @Override
  112.     public int getInventoryStackLimit() {
  113.         // TODO Auto-generated method stub
  114.         return 64;
  115.     }
  116.  
  117.     public void readFromNBT(NBTTagCompound nbt){
  118.         super.readFromNBT(nbt);
  119.         NBTTagList tagList = nbt.getTagList("Items", 10);
  120.         this.slots = new ItemStack[this.getSizeInventory()];
  121.        
  122.         for(int i = 0; i < tagList.tagCount(); i++){
  123.             NBTTagCompound compound = (NBTTagCompound) tagList.getCompoundTagAt(i);
  124.             byte byte0 = compound.getByte("Slot");
  125.            
  126.             if(byte0 >= 0 && byte0 < this.slots.length){
  127.                 this.slots[byte0] = ItemStack.loadItemStackFromNBT(compound);
  128.             }
  129.         }
  130.        
  131.         this.furnaceBurnTime = (int)nbt.getShort("BurnTime");
  132.         this.furnaceCookTime = (int)nbt.getShort("CookTime");
  133.         this.currentBurnTime = (int)nbt.getShort("CurrentBurnTime");
  134.        
  135.         if(nbt.hasKey("CustomName")){
  136.             this.localizedName = nbt.getString("CustomName");
  137.         }
  138.     }
  139.    
  140.     public void writeToNBT(NBTTagCompound nbt){
  141.         super.writeToNBT(nbt);
  142.         nbt.setShort("BurnTime", (short) this.furnaceBurnTime);
  143.         nbt.setShort("CookTime", (short) this.furnaceCookTime);
  144.         nbt.setShort("CurrentBurnTime", (short) this.currentBurnTime);
  145.         NBTTagList tagList = new NBTTagList();
  146.        
  147.         for(int i = 0; i < this.slots.length; i++){
  148.             if(this.slots[i] != null){
  149.                 NBTTagCompound compound = new NBTTagCompound();
  150.                 compound.setByte("Slot", (byte) i);
  151.                 this.slots[i].writeToNBT(compound);
  152.                 tagList.appendTag(compound);
  153.             }
  154.         }
  155.        
  156.         nbt.setTag("Items", tagList);
  157.        
  158.         if(this.hasCustomInventoryName()){
  159.             nbt.setString("CustomName", this.localizedName);
  160.         }
  161.     }
  162.    
  163.     @SideOnly(Side.CLIENT)
  164.     public int getCookProgressScaled(int par1){
  165.         return this.furnaceCookTime * par1 / this.furnaceSpeed;
  166.     }
  167.    
  168.     @SideOnly(Side.CLIENT)
  169.     public int getBurnTimeRemainingScaled(int par1){
  170.         if(this.currentBurnTime == 0){
  171.             this.currentBurnTime = this.furnaceSpeed;
  172.         }
  173.         return this.furnaceBurnTime * par1 / this.currentBurnTime;
  174.     }
  175.    
  176.     public boolean isBurning(){
  177.         return this.furnaceBurnTime > 0;
  178.     }
  179.    
  180.     public void updateEntity(){
  181.         boolean flag = this.furnaceBurnTime > 0;
  182.         boolean flag1 = false;
  183.        
  184.         if(this.isBurning()){
  185.             --this.furnaceBurnTime;
  186.         }
  187.        
  188.         if(!this.worldObj.isRemote){
  189.             if(this.furnaceBurnTime == 0 && this.canSmelt() || this.furnaceBurnTime == 0 && this.canCelestialSmelt()){
  190.                 this.currentBurnTime = this.furnaceBurnTime = getItemBurnTime(this.slots[1]);
  191.                
  192.                 if(this.isBurning()){
  193.                     flag1 = true;
  194.                     if(this.slots[1] != null){
  195.                         --this.slots[1].stackSize;
  196.                        
  197.                         if(this.slots[1].stackSize == 0){
  198.                             this.slots[1] = this.slots[1].getItem().getContainerItem(this.slots[1]);
  199.                         }
  200.                     }
  201.                 }
  202.             }
  203.            
  204.             if(this.isBurning() && this.canSmelt() || this.isBurning() && this.canCelestialSmelt()){
  205.                 ++this.furnaceCookTime;
  206.                 if(this.furnaceCookTime == this.furnaceSpeed){
  207.                     this.furnaceCookTime = 0;
  208.                     this.smeltItem();
  209.                     this.celestialSmeltItem();
  210.                     flag1 = true;
  211.                 }
  212.             }else{
  213.                 this.furnaceCookTime = 0;
  214.             }
  215.         }
  216.        
  217.         if(flag != this.isBurning()){
  218.             flag1 = true;
  219.             CelestialFurnace.updateCelestialFurnaceBlockState(this.furnaceBurnTime > 0, this.worldObj, this.xCoord, this.yCoord, this.zCoord);
  220.         }
  221.        
  222.         if(flag1){
  223.             this.markDirty();
  224.         }
  225.     }
  226.    
  227.     public boolean canSmelt(){
  228.         if(this.slots[0] == null){
  229.             return false;
  230.         }else{
  231.             ItemStack itemstack = FurnaceRecipes.smelting().getSmeltingResult(this.slots[0]);
  232.             if(itemstack == null) return false;
  233.             if(this.slots[2] == null) return true;
  234.             if(!this.slots[2].isItemEqual(itemstack)) return false;
  235.            
  236.             int result = this.slots[2].stackSize + itemstack.stackSize;
  237.             return ( result <= getInventoryStackLimit() && result <= itemstack.getMaxStackSize() );
  238.         }
  239.     }
  240.    
  241.     public boolean canCelestialSmelt(){
  242.         if(this.slots[0] == null){
  243.             return false;
  244.         }else{
  245.             ItemStack itemstack = CelestialFurnaceRecipes.smelting().getSmeltingResult(this.slots[0]);
  246.             if(itemstack == null) return false;
  247.             if(this.slots[2] == null) return true;
  248.             if(!this.slots[2].isItemEqual(itemstack)) return false;
  249.            
  250.             int result = this.slots[2].stackSize + itemstack.stackSize;
  251.             return ( result <= getInventoryStackLimit() && result <= itemstack.getMaxStackSize() );
  252.         }
  253.     }
  254.    
  255.     public void smeltItem(){
  256.         if(this.canSmelt())
  257.         {
  258.             ItemStack itemstack = FurnaceRecipes.smelting().getSmeltingResult(this.slots[0]);
  259.             if(this.slots[2] == null){
  260.                 this.slots[2] = itemstack.copy();
  261.             }else if(this.slots[2].isItemEqual(itemstack)){
  262.                 this.slots[2].stackSize += itemstack.stackSize;
  263.             }
  264.             --this.slots[0].stackSize;
  265.             ++this.slots[2].stackSize;
  266.             ++this.slots[2].stackSize;
  267.            
  268.             if (this.slots[0].stackSize <= 0 ){
  269.                 this.slots[0] = null;
  270.             }
  271.         }
  272.     }
  273.    
  274.     public void celestialSmeltItem(){
  275.         if(this.canCelestialSmelt())
  276.         {
  277.             ItemStack itemstack = CelestialFurnaceRecipes.smelting().getSmeltingResult(this.slots[0]);
  278.             if(this.slots[2] == null){
  279.                 this.slots[2] = itemstack.copy();
  280.             }else if(this.slots[2].isItemEqual(itemstack)){
  281.                 this.slots[2].stackSize += itemstack.stackSize;
  282.             }
  283.             --this.slots[0].stackSize;         
  284.             if (this.slots[0].stackSize <= 0 ){
  285.                 this.slots[0] = null;
  286.             }
  287.         }
  288.     }
  289.    
  290.     //Check if item is burnable
  291.     public static int getItemBurnTime(ItemStack itemstack){
  292.         if(itemstack == null){
  293.             return 0;
  294.         }else{
  295.             Item item = itemstack.getItem();           
  296.             if(item instanceof ItemBlock && Block.getBlockFromItem(item) != Blocks.air){
  297.                 Block block = Block.getBlockFromItem(item);
  298.                 if(block == CelestialCraft_blocks.GalaxyStone){
  299.                     return 3800;
  300.                 }              
  301.                 if(block.getMaterial() == Material.rock){
  302.                     return 300;
  303.                 }
  304.             }
  305.             if(item == CelestialCraft_items.starSeedItem) return 2400;
  306.             if(item == Items.lava_bucket) return 100;
  307.             if(item instanceof ItemTool && ((ItemTool) item).getToolMaterialName().equals(("EMERALD"))) return 400;
  308.             return GameRegistry.getFuelValue(itemstack);
  309.         }
  310.     }
  311.    
  312.     public static boolean isItemFuel(ItemStack itemstack){
  313.         return getItemBurnTime(itemstack) > 0;
  314.     }
  315.    
  316.     @Override
  317.     public boolean isUseableByPlayer(EntityPlayer player) {
  318.         return this.worldObj.getTileEntity(this.xCoord, this.yCoord, this.zCoord) != this ? false : player.getDistanceSq((double) this.xCoord + 0.5D, (double) this.yCoord + 0.5D, (double) this.zCoord + 0.5D) <= 64.0D;
  319.     }
  320.  
  321.     @Override
  322.     public void openInventory() {
  323.  
  324.        
  325.     }
  326.  
  327.     @Override
  328.     public void closeInventory() {
  329.  
  330.        
  331.     }
  332.  
  333.     @Override
  334.     //What can be placed into furnace slots
  335.     public boolean isItemValidForSlot(int par1, ItemStack itemstack) {
  336.         return par1 == 2 ? false : (par1 == 1 ? isItemFuel(itemstack) : true);
  337.     }
  338.  
  339.     @Override
  340.     //What Sides access which slots
  341.     public int[] getAccessibleSlotsFromSide(int par1) {
  342.         return par1 == 0 ? slotsBottom : (par1 == 1 ? slotsTop : slotsSides);
  343.     }
  344.  
  345.     @Override
  346.     public boolean canInsertItem(int par1, ItemStack itemstack, int par3) {
  347.         return this.isItemValidForSlot(par1, itemstack);
  348.     }
  349.  
  350.     @Override
  351.     public boolean canExtractItem(int par1, ItemStack itemstack, int par3) {
  352.         return par3 != 0 || par1 != 1 || itemstack.getItem() == Items.bucket;
  353.     }
  354.  
  355. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement