Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class TileEntityArcFurnace extends TileEntity implements ITickable {
- private static final int IN1 = 0;
- private static final int IN2 = 1;
- private static final int IN3 = 2;
- private static final int FUEL = 3;
- private static final int OUT = 4;
- protected final ItemStackHandler inventory = new ItemStackHandler(5) {
- public ItemStack insertItem(int index, ItemStack stack, boolean simulate) {
- if(isItemValid(index, stack)) {
- return super.insertItem(index, stack, simulate);
- }
- return stack;
- }
- public boolean isItemValid(int index, ItemStack stack) {
- if(index == 4) {
- return false;
- } else if(index != 3) {
- return true;
- } else {
- return TileEntityFurnace.isItemFuel(stack);
- }
- }
- };
- protected int burnTime; //0
- protected int currentTime; //0
- protected int fuelTime; //1600
- protected ItemStack smeltOutput;
- @Override
- public void update() {
- if(world.isRemote) { return; }
- ItemStack fuel = ItemStack.EMPTY;
- boolean canSmelt = canSmelt();
- if(!this.isBurning() && !(fuel = inventory.getStackInSlot(FUEL)).isEmpty()) {
- if(canSmelt) { // begins the smelting process if the furnace is able to smelt
- burnFuel(fuel, false);
- System.out.println("smelting process initiated");
- }
- }
- boolean wasBurning = isBurning();
- if(isBurning()) {
- burnTime--;
- if(canSmelt) { // attempts to smelt if the furnace is able to smelt
- System.out.println("Attempting to smelt item");
- smelt();
- } else currentTime = 0;
- }
- if(!isBurning() && !(fuel = inventory.getStackInSlot(FUEL)).isEmpty()) {
- if(canSmelt()) {
- burnFuel(fuel, wasBurning);
- }
- }
- if(wasBurning && !isBurning()) {
- System.out.println("returning to dim state");
- world.setBlockState(pos, world.getBlockState(pos).withProperty(MachineArcFurnace.ACTIVE, false));
- }
- }
- public void smelt() {
- currentTime++;
- if(this.currentTime >= this.getTime()) {
- System.out.println("smelt verified");
- this.currentTime = 0;
- this.smeltItem();
- }
- }
- public void smeltItem() {
- ItemStack[] in = { inventory.getStackInSlot(IN1), inventory.getStackInSlot(IN2), inventory.getStackInSlot(IN3) };
- ItemStack out = getResult();
- ItemStack currentOut = inventory.getStackInSlot(OUT);
- if(currentOut.isEmpty()) {
- inventory.setStackInSlot(OUT, out);
- } else if(ItemHandlerHelper.canItemStacksStack(currentOut, out)) {
- currentOut.grow(out.getCount());
- }
- System.out.println("Item smelted");
- for(ItemStack i : in) {
- i.shrink(1);
- }
- markDirty();
- }
- public void burnFuel(ItemStack fuel, boolean burned) {
- fuelTime = (burnTime = TileEntityFurnace.getItemBurnTime(fuel));
- if(this.isBurning()) {
- Item item = fuel.getItem();
- fuel.shrink(1);
- if(fuel.isEmpty()) {
- inventory.setStackInSlot(FUEL, item.getContainerItem(fuel));
- }
- }
- if(isBurning() && !burned) {
- System.out.println("active state");
- world.setBlockState(pos, world.getBlockState(pos).withProperty(MachineArcFurnace.ACTIVE, true));
- }
- markDirty();
- }
- public boolean isBurning() {
- return this.getBurnTime() > 0;
- }
- public int getBurnTime() {
- return this.burnTime;
- }
- public int getFuelTime() {
- return this.fuelTime;
- }
- public int getCurrentTime() {
- return this.currentTime;
- }
- public int getTime() {
- return 200;
- }
- protected boolean canSmelt() {
- return !getResult().isEmpty() && !this.inventory.getStackInSlot(FUEL).isEmpty();
- }
- @Override
- public boolean hasCapability(Capability<?> capability, @Nullable EnumFacing facing) {
- if(capability == CapabilityItemHandler.ITEM_HANDLER_CAPABILITY) { return true; }
- return super.hasCapability(capability, facing);
- }
- private ItemStack getResult() {
- ItemStack[] in = { inventory.getStackInSlot(0), inventory.getStackInSlot(1), inventory.getStackInSlot(2) };
- for(RecipeArcFurnace recipe : Exkva.ARC_FURNACE_RECIPES) {
- if(recipe.matches(in)) {
- return this.smeltOutput = recipe.getOutput();
- }
- }
- return ItemStack.EMPTY;
- }
- public ItemStackHandler getInventory() {
- return inventory;
- }
- @Override
- public void readFromNBT(NBTTagCompound compound) {
- super.readFromNBT(compound);
- inventory.deserializeNBT(compound.getCompoundTag("inv"));
- burnTime = compound.getInteger("burn_time");
- fuelTime = compound.getInteger("fuel_length");
- currentTime = compound.getInteger("current_cook_time");
- }
- @Override
- public NBTTagCompound writeToNBT(NBTTagCompound compound) {
- compound = super.writeToNBT(compound);
- compound.setTag("inv", inventory.serializeNBT());
- compound.setInteger("burn_time", burnTime);
- compound.setInteger("fuel_length", fuelTime);
- compound.setInteger("current_cook_time", currentTime);
- return compound;
- }
- @Override
- public <T> T getCapability(Capability<T> capability, EnumFacing facing) {
- return super.getCapability(capability, facing);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement