Advertisement
Exokem

Untitled

Apr 15th, 2019
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.78 KB | None | 0 0
  1. public class TileEntityArcFurnace extends TileEntity implements ITickable {
  2.  
  3.     private static final int IN1  = 0;
  4.     private static final int IN2  = 1;
  5.     private static final int IN3  = 2;
  6.     private static final int FUEL = 3;
  7.     private static final int OUT  = 4;
  8.  
  9.     protected final ItemStackHandler inventory = new ItemStackHandler(5) {
  10.         public ItemStack insertItem(int index, ItemStack stack, boolean simulate) {
  11.             if(isItemValid(index, stack)) {
  12.                 return super.insertItem(index, stack, simulate);
  13.             }
  14.             return stack;
  15.         }
  16.         public boolean isItemValid(int index, ItemStack stack) {
  17.             if(index == 4) {
  18.                 return false;
  19.             } else if(index != 3) {
  20.                 return true;
  21.             } else {
  22.                 return TileEntityFurnace.isItemFuel(stack);
  23.             }
  24.         }
  25.     };
  26.  
  27.     protected int burnTime; //0
  28.     protected int currentTime; //0
  29.     protected int fuelTime; //1600
  30.  
  31.     protected ItemStack smeltOutput;
  32.  
  33.     @Override
  34.     public void update() {
  35.  
  36.         if(world.isRemote) { return; }
  37.  
  38.         ItemStack fuel = ItemStack.EMPTY;
  39.         boolean canSmelt = canSmelt();
  40.  
  41.         if(!this.isBurning() && !(fuel = inventory.getStackInSlot(FUEL)).isEmpty()) {
  42.             if(canSmelt) { // begins the smelting process if the furnace is able to smelt
  43.                 burnFuel(fuel, false);
  44.                 System.out.println("smelting process initiated");
  45.             }
  46.         }
  47.  
  48.         boolean wasBurning = isBurning();
  49.  
  50.         if(isBurning()) {
  51.             burnTime--;
  52.             if(canSmelt) { // attempts to smelt if the furnace is able to smelt
  53.                 System.out.println("Attempting to smelt item");
  54.                 smelt();
  55.             } else currentTime = 0;
  56.         }
  57.  
  58.         if(!isBurning() && !(fuel = inventory.getStackInSlot(FUEL)).isEmpty()) {
  59.             if(canSmelt()) {
  60.                 burnFuel(fuel, wasBurning);
  61.             }
  62.         }
  63.  
  64.         if(wasBurning && !isBurning()) {
  65.             System.out.println("returning to dim state");
  66.             world.setBlockState(pos, world.getBlockState(pos).withProperty(MachineArcFurnace.ACTIVE, false));
  67.         }
  68.     }
  69.  
  70.     public void smelt() {
  71.         currentTime++;
  72.         if(this.currentTime >= this.getTime()) {
  73.             System.out.println("smelt verified");
  74.             this.currentTime = 0;
  75.             this.smeltItem();
  76.         }
  77.     }
  78.  
  79.     public void smeltItem() {
  80.         ItemStack[] in = { inventory.getStackInSlot(IN1), inventory.getStackInSlot(IN2), inventory.getStackInSlot(IN3) };
  81.         ItemStack out = getResult();
  82.  
  83.         ItemStack currentOut = inventory.getStackInSlot(OUT);
  84.         if(currentOut.isEmpty()) {
  85.             inventory.setStackInSlot(OUT, out);
  86.         } else if(ItemHandlerHelper.canItemStacksStack(currentOut, out)) {
  87.             currentOut.grow(out.getCount());
  88.         }
  89.  
  90.         System.out.println("Item smelted");
  91.  
  92.         for(ItemStack i : in) {
  93.             i.shrink(1);
  94.         }
  95.  
  96.         markDirty();
  97.     }
  98.  
  99.     public void burnFuel(ItemStack fuel, boolean burned) {
  100.         fuelTime = (burnTime = TileEntityFurnace.getItemBurnTime(fuel));
  101.  
  102.         if(this.isBurning()) {
  103.             Item item = fuel.getItem();
  104.             fuel.shrink(1);
  105.             if(fuel.isEmpty()) {
  106.                 inventory.setStackInSlot(FUEL, item.getContainerItem(fuel));
  107.             }
  108.         }
  109.  
  110.         if(isBurning() && !burned) {
  111.             System.out.println("active state");
  112.             world.setBlockState(pos, world.getBlockState(pos).withProperty(MachineArcFurnace.ACTIVE, true));
  113.         }
  114.  
  115.         markDirty();
  116.     }
  117.  
  118.     public boolean isBurning() {
  119.         return this.getBurnTime() > 0;
  120.     }
  121.  
  122.     public int getBurnTime() {
  123.         return this.burnTime;
  124.     }
  125.  
  126.     public int getFuelTime() {
  127.         return this.fuelTime;
  128.     }
  129.  
  130.     public int getCurrentTime() {
  131.         return this.currentTime;
  132.     }
  133.  
  134.     public int getTime() {
  135.         return 200;
  136.     }
  137.  
  138.     protected boolean canSmelt() {
  139.         return !getResult().isEmpty() && !this.inventory.getStackInSlot(FUEL).isEmpty();
  140.     }
  141.  
  142.     @Override
  143.     public boolean hasCapability(Capability<?> capability, @Nullable EnumFacing facing) {
  144.         if(capability == CapabilityItemHandler.ITEM_HANDLER_CAPABILITY) { return true; }
  145.         return super.hasCapability(capability, facing);
  146.     }
  147.  
  148.     private ItemStack getResult() {
  149.  
  150.         ItemStack[] in = { inventory.getStackInSlot(0), inventory.getStackInSlot(1), inventory.getStackInSlot(2) };
  151.         for(RecipeArcFurnace recipe : Exkva.ARC_FURNACE_RECIPES) {
  152.             if(recipe.matches(in)) {
  153.                 return this.smeltOutput = recipe.getOutput();
  154.             }
  155.         }
  156.  
  157.         return ItemStack.EMPTY;
  158.     }
  159.  
  160.     public ItemStackHandler getInventory() {
  161.         return inventory;
  162.     }
  163.  
  164.     @Override
  165.     public void readFromNBT(NBTTagCompound compound) {
  166.         super.readFromNBT(compound);
  167.         inventory.deserializeNBT(compound.getCompoundTag("inv"));
  168.         burnTime = compound.getInteger("burn_time");
  169.         fuelTime = compound.getInteger("fuel_length");
  170.         currentTime = compound.getInteger("current_cook_time");
  171.     }
  172.  
  173.     @Override
  174.     public NBTTagCompound writeToNBT(NBTTagCompound compound) {
  175.         compound = super.writeToNBT(compound);
  176.         compound.setTag("inv", inventory.serializeNBT());
  177.         compound.setInteger("burn_time", burnTime);
  178.         compound.setInteger("fuel_length", fuelTime);
  179.         compound.setInteger("current_cook_time", currentTime);
  180.         return compound;
  181.     }
  182.  
  183.     @Override
  184.     public <T> T getCapability(Capability<T> capability, EnumFacing facing) {
  185.         return super.getCapability(capability, facing);
  186.     }
  187. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement