ActuallyAdditions/src/main/java/de/ellpeck/actuallyadditions/mod/tile/TileEntityOilGenerator.java

189 lines
6 KiB
Java
Raw Normal View History

2015-08-29 14:33:25 +02:00
/*
2016-05-16 22:52:27 +02:00
* This file ("TileEntityOilGenerator.java") is part of the Actually Additions mod for Minecraft.
2015-08-29 14:33:25 +02:00
* It is created and owned by Ellpeck and distributed
* under the Actually Additions License to be found at
2016-05-16 22:52:27 +02:00
* http://ellpeck.de/actaddlicense
2015-08-29 14:33:25 +02:00
* View the source code at https://github.com/Ellpeck/ActuallyAdditions
*
2016-05-16 22:52:27 +02:00
* © 2015-2016 Ellpeck Ellpeck
2015-08-29 14:33:25 +02:00
*/
2016-01-05 04:47:35 +01:00
package de.ellpeck.actuallyadditions.mod.tile;
2015-05-20 22:39:43 +02:00
import cofh.api.energy.EnergyStorage;
import cofh.api.energy.IEnergyProvider;
2016-01-29 17:38:31 +01:00
import de.ellpeck.actuallyadditions.mod.fluids.InitFluids;
2016-01-08 13:31:58 +01:00
import de.ellpeck.actuallyadditions.mod.util.PosUtil;
import de.ellpeck.actuallyadditions.mod.util.Util;
2016-01-05 04:47:35 +01:00
import de.ellpeck.actuallyadditions.mod.util.WorldUtil;
2015-05-20 22:39:43 +02:00
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.util.EnumFacing;
2015-05-20 22:39:43 +02:00
import net.minecraftforge.fluids.*;
2016-01-07 18:20:59 +01:00
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;
2015-05-20 22:39:43 +02:00
public class TileEntityOilGenerator extends TileEntityBase implements IEnergyProvider, IFluidHandler, IEnergySaver, IFluidSaver{
2015-05-20 22:39:43 +02:00
2015-12-01 19:48:09 +01:00
public static final int ENERGY_PRODUCED = 76;
private static final int BURN_TIME = 100;
2015-06-13 14:39:46 +02:00
public EnergyStorage storage = new EnergyStorage(50000);
public FluidTank tank = new FluidTank(2*Util.BUCKET);
2015-05-20 22:39:43 +02:00
public int currentBurnTime;
2015-10-03 10:16:18 +02:00
private int lastEnergy;
private int lastTank;
private int lastBurnTime;
2015-05-20 22:39:43 +02:00
public TileEntityOilGenerator(){
super("oilGenerator");
2015-05-20 22:39:43 +02:00
}
2015-12-01 19:48:09 +01:00
@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, boolean sync){
compound.setInteger("BurnTime", this.currentBurnTime);
this.storage.writeToNBT(compound);
this.tank.writeToNBT(compound);
super.writeSyncableNBT(compound, sync);
}
@Override
public void readSyncableNBT(NBTTagCompound compound, boolean sync){
this.currentBurnTime = compound.getInteger("BurnTime");
this.storage.readFromNBT(compound);
this.tank.readFromNBT(compound);
super.readSyncableNBT(compound, sync);
}
2015-05-20 22:39:43 +02:00
@Override
@SuppressWarnings("unchecked")
public void updateEntity(){
super.updateEntity();
2016-05-02 17:46:53 +02:00
if(!this.worldObj.isRemote){
2015-06-21 02:28:49 +02:00
boolean flag = this.currentBurnTime > 0;
2015-05-20 22:39:43 +02:00
if(this.currentBurnTime > 0){
this.currentBurnTime--;
this.storage.receiveEnergy(ENERGY_PRODUCED, false);
2015-05-20 22:39:43 +02:00
}
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.drain(fuelUsed, true);
2015-05-20 22:39:43 +02:00
}
}
if(this.storage.getEnergyStored() > 0){
2016-05-02 17:46:53 +02:00
WorldUtil.pushEnergyToAllSides(this.worldObj, this.pos, this.storage);
2015-05-20 22:39:43 +02:00
}
2015-06-21 02:28:49 +02:00
if(flag != this.currentBurnTime > 0){
this.markDirty();
2016-05-02 17:46:53 +02:00
int meta = PosUtil.getMetadata(this.pos, this.worldObj);
2015-06-21 02:28:49 +02:00
if(meta == 1){
if(!(ENERGY_PRODUCED*BURN_TIME <= this.storage.getMaxEnergyStored()-this.storage.getEnergyStored() && this.currentBurnTime <= 0 && this.tank.getFluidAmount() >= fuelUsed)){
2016-05-02 17:46:53 +02:00
PosUtil.setMetadata(this.pos, this.worldObj, 0, 2);
2015-10-02 16:48:01 +02:00
}
}
else{
2016-05-02 17:46:53 +02:00
PosUtil.setMetadata(this.pos, this.worldObj, 1, 2);
2015-06-21 02:28:49 +02:00
}
}
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;
}
2015-05-20 22:39:43 +02:00
}
}
@Override
public int extractEnergy(EnumFacing from, int maxExtract, boolean simulate){
2015-05-20 22:39:43 +02:00
return this.storage.extractEnergy(maxExtract, simulate);
}
@Override
public int getEnergyStored(EnumFacing from){
2015-05-20 22:39:43 +02:00
return this.storage.getEnergyStored();
}
@Override
public int getMaxEnergyStored(EnumFacing from){
2015-05-20 22:39:43 +02:00
return this.storage.getMaxEnergyStored();
}
@Override
public boolean canConnectEnergy(EnumFacing from){
2015-05-20 22:39:43 +02:00
return true;
}
@Override
public int fill(EnumFacing from, FluidStack resource, boolean doFill){
2016-01-29 17:38:31 +01:00
if(resource.getFluid() == InitFluids.fluidOil){
2015-10-03 10:16:18 +02:00
return this.tank.fill(resource, doFill);
}
2015-05-20 22:39:43 +02:00
return 0;
}
@Override
public FluidStack drain(EnumFacing from, FluidStack resource, boolean doDrain){
2015-05-20 22:39:43 +02:00
return null;
}
@Override
public FluidStack drain(EnumFacing from, int maxDrain, boolean doDrain){
2015-05-20 22:39:43 +02:00
return null;
}
@Override
public boolean canFill(EnumFacing from, Fluid fluid){
2016-01-29 17:38:31 +01:00
return from != EnumFacing.DOWN && fluid == InitFluids.fluidOil;
2015-05-20 22:39:43 +02:00
}
@Override
public boolean canDrain(EnumFacing from, Fluid fluid){
2015-05-20 22:39:43 +02:00
return false;
}
@Override
public FluidTankInfo[] getTankInfo(EnumFacing from){
2015-05-20 22:39:43 +02:00
return new FluidTankInfo[]{this.tank.getInfo()};
}
@Override
public int getEnergy(){
return this.storage.getEnergyStored();
}
@Override
public void setEnergy(int energy){
this.storage.setEnergyStored(energy);
}
@Override
public FluidStack[] getFluids(){
return new FluidStack[]{this.tank.getFluid()};
}
@Override
public void setFluids(FluidStack[] fluids){
this.tank.setFluid(fluids[0]);
}
2015-05-20 22:39:43 +02:00
}