Guest User

TileAdvancedFurnace.java

a guest
Apr 4th, 2019
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.41 KB | None | 0 0
  1. package com.herobrine.future.tile.advancedfurnace;
  2.  
  3. import com.herobrine.future.blocks.BlockFurnaceAdvanced;
  4. import net.minecraft.block.state.IBlockState;
  5. import net.minecraft.entity.player.EntityPlayer;
  6. import net.minecraft.item.ItemStack;
  7. import net.minecraft.item.crafting.FurnaceRecipes;
  8. import net.minecraft.nbt.NBTTagCompound;
  9. import net.minecraft.tileentity.TileEntity;
  10. import net.minecraft.tileentity.TileEntityFurnace;
  11. import net.minecraft.util.EnumFacing;
  12. import net.minecraft.util.ITickable;
  13. import net.minecraft.util.math.BlockPos;
  14. import net.minecraft.world.World;
  15. import net.minecraftforge.common.capabilities.Capability;
  16. import net.minecraftforge.items.CapabilityItemHandler;
  17. import net.minecraftforge.items.ItemStackHandler;
  18. import net.minecraftforge.items.wrapper.CombinedInvWrapper;
  19.  
  20. public abstract class TileAdvancedFurnace extends TileEntity implements ITickable {
  21.     protected ItemStackHandler inputCraft = new ItemStackHandler() {
  22.         @Override
  23.         protected void onContentsChanged(int slot) {
  24.             TileAdvancedFurnace.this.markDirty();
  25.         }
  26.     };
  27.     protected ItemStackHandler inputFuel = new ItemStackHandler() {
  28.         @Override
  29.         protected void onContentsChanged(int slot) {
  30.             TileAdvancedFurnace.this.markDirty();
  31.         }
  32.     };
  33.     protected ItemStackHandler outputCraft = new ItemStackHandler() {
  34.         @Override
  35.         protected void onContentsChanged(int slot) {
  36.             TileAdvancedFurnace.this.markDirty();
  37.         }
  38.     };
  39.  
  40.     protected boolean isBurning;
  41.     private int ticksOff;
  42.  
  43.     public int fuelLeft, progress, itemOriginalFuel = 0; // Time before current fuel burns out
  44.     // The time this machine has been cooking
  45.  
  46.     protected CombinedInvWrapper combinedInventoryHandler = new CombinedInvWrapper(inputCraft, inputFuel, outputCraft);
  47.     protected BlockFurnaceAdvanced.FurnaceType type; // The type of block this is
  48.  
  49.     public TileAdvancedFurnace(BlockFurnaceAdvanced.FurnaceType type) {
  50.         this.type = type;
  51.     }
  52.  
  53.     @Override
  54.     public void readFromNBT(NBTTagCompound compound) {
  55.         super.readFromNBT(compound);
  56.         if(compound.hasKey("inputCraft")) {
  57.             inputCraft.deserializeNBT((NBTTagCompound) compound.getTag("inputCraft"));
  58.         }
  59.         if(compound.hasKey("inputFuel")) {
  60.             inputFuel.deserializeNBT((NBTTagCompound) compound.getTag("inputFuel"));
  61.         }
  62.         if(compound.hasKey("outputCraft")) {
  63.             outputCraft.deserializeNBT((NBTTagCompound) compound.getTag("outputCraft"));
  64.         }
  65.         progress = compound.getInteger("progress");
  66.         fuelLeft = compound.getInteger("fuelLeft");
  67.     }
  68.  
  69.     @Override
  70.     public NBTTagCompound writeToNBT(NBTTagCompound compound) {
  71.         super.writeToNBT(compound);
  72.         compound.setTag("inputCraft", inputCraft.serializeNBT());
  73.         compound.setTag("inputFuel", inputFuel.serializeNBT());
  74.         compound.setTag("outputCraft", outputCraft.serializeNBT());
  75.         compound.setInteger("progress", progress);
  76.         compound.setInteger("fuelLeft", fuelLeft);
  77.         return compound;
  78.     }
  79.  
  80.     @Override
  81.     public boolean shouldRefresh(World world, BlockPos pos, IBlockState oldState, IBlockState newSate) {
  82.         return false;
  83.     }
  84.  
  85.     @Override
  86.     public boolean hasCapability(Capability<?> capability, EnumFacing facing) {
  87.         if(capability == CapabilityItemHandler.ITEM_HANDLER_CAPABILITY) {
  88.             return true;
  89.         }
  90.         return super.hasCapability(capability, facing);
  91.     }
  92.  
  93.     @Override
  94.     public <T> T getCapability(Capability<T> capability, EnumFacing facing) {
  95.         if(capability == CapabilityItemHandler.ITEM_HANDLER_CAPABILITY) {
  96.             if (facing == null) {
  97.                 return CapabilityItemHandler.ITEM_HANDLER_CAPABILITY.cast(combinedInventoryHandler);
  98.             }
  99.             else if (facing == EnumFacing.UP) {
  100.                 return CapabilityItemHandler.ITEM_HANDLER_CAPABILITY.cast(inputCraft);
  101.             }
  102.             else if (facing == EnumFacing.DOWN) {
  103.                 return CapabilityItemHandler.ITEM_HANDLER_CAPABILITY.cast(outputCraft);
  104.             }
  105.             else {
  106.                 return CapabilityItemHandler.ITEM_HANDLER_CAPABILITY.cast(inputFuel);
  107.             }
  108.         }
  109.         return super.getCapability(capability, facing);
  110.     }
  111.  
  112.     boolean canInteractWith(EntityPlayer playerIn) {
  113.         return !isInvalid() && playerIn.getDistanceSq(pos.add(0.5D, 0.5D, 0.5D)) <= 64D;
  114.     }
  115.  
  116.     public abstract BlockFurnaceAdvanced.FurnaceType getType();
  117.  
  118.     protected void function() {
  119.         if(!world.isRemote) {
  120.             if(fuelLeft == 0) { // Skips if smelting
  121.                 setIsBurning(false);
  122.                 itemOriginalFuel = 0;
  123.                 if(trySmelt() && !isBurning) { // Checks if the machine has a valid recipe
  124.                     startSmelt(); // Sets the burnTime and consumes fuel
  125.                 }
  126.             }
  127.             if(fuelLeft != 0) {
  128.                 doSmelt(); // Updates the progress, fuelTime, and isBurning
  129.             }
  130.             if(progress == 100) {
  131.                 finishSmelt(); // Consumes the input stack, inserts the output stack, and resets the progress
  132.             }
  133.             if(inputCraft.getStackInSlot(0).isEmpty()) {
  134.                 ticksOff++;//cancelSmelt(); // Cancels the recipe if the input no longer exists
  135.                 if(ticksOff == 10) {
  136.                     cancelSmelt();
  137.                 }
  138.             }
  139.             else {
  140.                 if(ticksOff != 0) ticksOff = 0;
  141.             }
  142.         }
  143.     }
  144.  
  145.     protected void setIsBurning(boolean isBurning) {
  146.         if(this.isBurning != isBurning) {
  147.             this.isBurning = isBurning;
  148.             markDirty();
  149.             BlockFurnaceAdvanced.setState(isBurning, world, pos);
  150.             IBlockState state = world.getBlockState(pos);
  151.             getWorld().notifyBlockUpdate(pos, state, state, 3);
  152.         }
  153.         //BlockFurnaceAdvanced.setState(isBurning, world, pos);
  154.     }
  155.  
  156.     public boolean trySmelt() {
  157.         if(TileEntityFurnace.isItemFuel(fuelStack()) && getType().canCraft(inputCraft.getStackInSlot(0))) {
  158.             return putOutput(true);
  159.         }
  160.         return false;
  161.     }
  162.  
  163.     public ItemStack fuelStack() {
  164.         return inputFuel.getStackInSlot(0);
  165.     }
  166.  
  167.     public void startSmelt() {
  168.         int burnTime = TileEntityFurnace.getItemBurnTime(fuelStack());
  169.         fuelStack().shrink(1);
  170.         itemOriginalFuel = burnTime;
  171.         fuelLeft = burnTime;
  172.     }
  173.  
  174.     public void doSmelt() {
  175.         fuelLeft--;
  176.         progress++;
  177.         setIsBurning(true);
  178.     }
  179.  
  180.     private void finishSmelt() {
  181.         progress = 0;
  182.         if(putOutput(false)) {
  183.             //ItemStack result = FurnaceRecipes.instance().getSmeltingResult(inputCraft.getStackInSlot(0));
  184.             inputCraft.extractItem(0, 1, false);
  185.         }
  186.     }
  187.  
  188.     private boolean putOutput(boolean simulate) {
  189.         ItemStack result = FurnaceRecipes.instance().getSmeltingResult(new ItemStack(inputCraft.getStackInSlot(0).getItem(), 1, inputCraft.getStackInSlot(0).getMetadata()));
  190.         ItemStack remaining = outputCraft.insertItem(0, result.copy(), simulate);
  191.  
  192.         return remaining.isEmpty();
  193.     }
  194.  
  195.     private void cancelSmelt() {
  196.         if(progress > 0) progress--;
  197.         isBurning = false;
  198.     }
  199.  
  200.     @Override
  201.     public void update() {
  202.         function();
  203.     }
  204. }
Add Comment
Please, Sign In to add comment