mirror of
https://github.com/Ellpeck/ActuallyAdditions.git
synced 2024-06-28 15:39:09 +02:00
196 lines
6.4 KiB
Java
196 lines
6.4 KiB
Java
/*
|
|
* This file ("TileEntityOilGenerator.java") is part of the Actually Additions mod for Minecraft.
|
|
* It is created and owned by Ellpeck and distributed
|
|
* under the Actually Additions License to be found at
|
|
* http://ellpeck.de/actaddlicense
|
|
* View the source code at https://github.com/Ellpeck/ActuallyAdditions
|
|
*
|
|
* © 2015-2016 Ellpeck
|
|
*/
|
|
|
|
package de.ellpeck.actuallyadditions.mod.tile;
|
|
|
|
import cofh.api.energy.EnergyStorage;
|
|
import cofh.api.energy.IEnergyProvider;
|
|
import de.ellpeck.actuallyadditions.mod.fluids.InitFluids;
|
|
import de.ellpeck.actuallyadditions.mod.util.Util;
|
|
import net.minecraft.nbt.NBTTagCompound;
|
|
import net.minecraft.util.EnumFacing;
|
|
import net.minecraftforge.fluids.*;
|
|
import net.minecraftforge.fluids.capability.IFluidHandler;
|
|
import net.minecraftforge.fluids.capability.IFluidTankProperties;
|
|
import net.minecraftforge.fml.relauncher.Side;
|
|
import net.minecraftforge.fml.relauncher.SideOnly;
|
|
|
|
public class TileEntityOilGenerator extends TileEntityBase implements IEnergyProvider, net.minecraftforge.fluids.IFluidHandler{
|
|
|
|
public static final int ENERGY_PRODUCED = 76;
|
|
private static final int BURN_TIME = 100;
|
|
public final EnergyStorage storage = new EnergyStorage(50000);
|
|
public final FluidTank tank = new FluidTank(2*Util.BUCKET){
|
|
@Override
|
|
public boolean canDrain(){
|
|
return false;
|
|
}
|
|
|
|
@Override
|
|
public boolean canFillFluidType(FluidStack fluid){
|
|
return fluid.getFluid() == InitFluids.fluidOil;
|
|
}
|
|
};
|
|
public int currentBurnTime;
|
|
private int lastEnergy;
|
|
private int lastTank;
|
|
private int lastBurnTime;
|
|
|
|
public TileEntityOilGenerator(){
|
|
super("oilGenerator");
|
|
}
|
|
|
|
@SideOnly(Side.CLIENT)
|
|
public int getEnergyScaled(int i){
|
|
return this.storage.getEnergyStored()*i/this.storage.getMaxEnergyStored();
|
|
}
|
|
|
|
@SideOnly(Side.CLIENT)
|
|
public int getTankScaled(int i){
|
|
return this.tank.getFluidAmount()*i/this.tank.getCapacity();
|
|
}
|
|
|
|
@SideOnly(Side.CLIENT)
|
|
public int getBurningScaled(int i){
|
|
return this.currentBurnTime*i/BURN_TIME;
|
|
}
|
|
|
|
@Override
|
|
public void writeSyncableNBT(NBTTagCompound compound, NBTType type){
|
|
if(type != NBTType.SAVE_BLOCK){
|
|
compound.setInteger("BurnTime", this.currentBurnTime);
|
|
}
|
|
this.storage.writeToNBT(compound);
|
|
this.tank.writeToNBT(compound);
|
|
super.writeSyncableNBT(compound, type);
|
|
}
|
|
|
|
@Override
|
|
public void readSyncableNBT(NBTTagCompound compound, NBTType type){
|
|
if(type != NBTType.SAVE_BLOCK){
|
|
this.currentBurnTime = compound.getInteger("BurnTime");
|
|
}
|
|
this.storage.readFromNBT(compound);
|
|
this.tank.readFromNBT(compound);
|
|
super.readSyncableNBT(compound, type);
|
|
}
|
|
|
|
@Override
|
|
public void updateEntity(){
|
|
super.updateEntity();
|
|
if(!this.worldObj.isRemote){
|
|
boolean flag = this.currentBurnTime > 0;
|
|
|
|
if(this.currentBurnTime > 0){
|
|
this.currentBurnTime--;
|
|
this.storage.receiveEnergy(ENERGY_PRODUCED, false);
|
|
}
|
|
|
|
int fuelUsed = 50;
|
|
if(ENERGY_PRODUCED*BURN_TIME <= this.storage.getMaxEnergyStored()-this.storage.getEnergyStored()){
|
|
if(this.currentBurnTime <= 0 && this.tank.getFluidAmount() >= fuelUsed){
|
|
this.currentBurnTime = BURN_TIME;
|
|
this.tank.drainInternal(fuelUsed, true);
|
|
}
|
|
}
|
|
|
|
if(flag != this.currentBurnTime > 0){
|
|
this.markDirty();
|
|
}
|
|
|
|
if((this.storage.getEnergyStored() != this.lastEnergy || this.tank.getFluidAmount() != this.lastTank || this.lastBurnTime != this.currentBurnTime) && this.sendUpdateWithInterval()){
|
|
this.lastEnergy = this.storage.getEnergyStored();
|
|
this.lastTank = this.tank.getFluidAmount();
|
|
this.lastBurnTime = this.currentBurnTime;
|
|
}
|
|
}
|
|
}
|
|
|
|
@Override
|
|
public int extractEnergy(EnumFacing from, int maxExtract, boolean simulate){
|
|
return this.storage.extractEnergy(maxExtract, simulate);
|
|
}
|
|
|
|
@Override
|
|
public int getEnergyStored(EnumFacing from){
|
|
return this.storage.getEnergyStored();
|
|
}
|
|
|
|
@Override
|
|
public int getMaxEnergyStored(EnumFacing from){
|
|
return this.storage.getMaxEnergyStored();
|
|
}
|
|
|
|
@Override
|
|
public boolean canConnectEnergy(EnumFacing from){
|
|
return true;
|
|
}
|
|
|
|
@Override
|
|
public IFluidHandler getFluidHandler(EnumFacing facing){
|
|
return facing != EnumFacing.DOWN ? this.tank : null;
|
|
}
|
|
|
|
@Override
|
|
public int fill(EnumFacing from, FluidStack resource, boolean doFill){
|
|
IFluidHandler handler = this.getFluidHandler(from);
|
|
return handler == null ? 0 : handler.fill(resource, doFill);
|
|
}
|
|
|
|
@Override
|
|
public FluidStack drain(EnumFacing from, FluidStack resource, boolean doDrain){
|
|
IFluidHandler handler = this.getFluidHandler(from);
|
|
return handler == null ? null : handler.drain(resource, doDrain);
|
|
}
|
|
|
|
@Override
|
|
public FluidStack drain(EnumFacing from, int maxDrain, boolean doDrain){
|
|
IFluidHandler handler = this.getFluidHandler(from);
|
|
return handler == null ? null : handler.drain(maxDrain, doDrain);
|
|
}
|
|
|
|
@Override
|
|
public boolean canFill(EnumFacing from, Fluid fluid){
|
|
IFluidHandler handler = this.getFluidHandler(from);
|
|
if(handler != null){
|
|
for(IFluidTankProperties prop : handler.getTankProperties()){
|
|
if(prop != null && prop.canFillFluidType(new FluidStack(fluid, Integer.MAX_VALUE))){
|
|
return true;
|
|
}
|
|
}
|
|
}
|
|
return false;
|
|
}
|
|
|
|
@Override
|
|
public boolean canDrain(EnumFacing from, Fluid fluid){
|
|
IFluidHandler handler = this.getFluidHandler(from);
|
|
if(handler != null){
|
|
for(IFluidTankProperties prop : handler.getTankProperties()){
|
|
if(prop != null && prop.canDrainFluidType(new FluidStack(fluid, Integer.MAX_VALUE))){
|
|
return true;
|
|
}
|
|
}
|
|
}
|
|
return false;
|
|
}
|
|
|
|
@Override
|
|
public FluidTankInfo[] getTankInfo(EnumFacing from){
|
|
IFluidHandler handler = this.getFluidHandler(from);
|
|
if(handler instanceof IFluidTank){
|
|
return new FluidTankInfo[]{((IFluidTank)handler).getInfo()};
|
|
}
|
|
else{
|
|
return null;
|
|
}
|
|
}
|
|
}
|