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

212 lines
7.7 KiB
Java
Raw Normal View History

2015-08-29 14:33:25 +02:00
/*
* 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://github.com/Ellpeck/ActuallyAdditions/blob/master/README.md
* View the source code at https://github.com/Ellpeck/ActuallyAdditions
*
* <EFBFBD> 2015 Ellpeck
*/
2015-05-20 22:39:43 +02:00
package ellpeck.actuallyadditions.tile;
import cofh.api.energy.EnergyStorage;
import cofh.api.energy.IEnergyProvider;
import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;
import ellpeck.actuallyadditions.blocks.InitBlocks;
2015-05-22 22:12:59 +02:00
import ellpeck.actuallyadditions.config.values.ConfigIntValues;
import ellpeck.actuallyadditions.network.sync.IPacketSyncerToClient;
import ellpeck.actuallyadditions.network.sync.PacketSyncerToClient;
2015-05-20 22:39:43 +02:00
import ellpeck.actuallyadditions.util.WorldUtil;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraftforge.common.util.ForgeDirection;
import net.minecraftforge.fluids.*;
public class TileEntityOilGenerator extends TileEntityInventoryBase implements IEnergyProvider, IFluidHandler, IPacketSyncerToClient{
2015-05-20 22:39:43 +02:00
2015-06-13 14:39:46 +02:00
public EnergyStorage storage = new EnergyStorage(50000);
private int lastEnergy;
2015-05-20 22:39:43 +02:00
public FluidTank tank = new FluidTank(2*FluidContainerRegistry.BUCKET_VOLUME);
private int lastTank;
2015-05-20 22:39:43 +02:00
public int currentBurnTime;
private int lastBurnTime;
2015-05-20 22:39:43 +02:00
public TileEntityOilGenerator(){
super(2, "oilGenerator");
}
@Override
@SuppressWarnings("unchecked")
public void updateEntity(){
if(!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--;
2015-08-15 20:41:45 +02:00
this.storage.receiveEnergy(ConfigIntValues.OIL_GEN_ENERGY_PRODUCED.getValue(), false);
2015-05-20 22:39:43 +02:00
}
2015-08-15 20:41:45 +02:00
if(ConfigIntValues.OIL_GEN_ENERGY_PRODUCED.getValue()*ConfigIntValues.OIL_GEN_BURN_TIME.getValue() <= this.getMaxEnergyStored(ForgeDirection.UNKNOWN)-this.getEnergyStored(ForgeDirection.UNKNOWN)){
if(this.currentBurnTime <= 0 && this.tank.getFluidAmount() >= ConfigIntValues.OIL_GEN_FUEL_USED.getValue()){
this.currentBurnTime = ConfigIntValues.OIL_GEN_BURN_TIME.getValue();
this.tank.drain(ConfigIntValues.OIL_GEN_FUEL_USED.getValue(), true);
2015-05-20 22:39:43 +02:00
}
}
WorldUtil.emptyBucket(tank, slots, 0, 1, InitBlocks.fluidOil);
2015-05-20 22:39:43 +02:00
if(this.getEnergyStored(ForgeDirection.UNKNOWN) > 0){
WorldUtil.pushEnergy(worldObj, xCoord, yCoord, zCoord, ForgeDirection.UP, storage);
WorldUtil.pushEnergy(worldObj, xCoord, yCoord, zCoord, ForgeDirection.DOWN, storage);
WorldUtil.pushEnergy(worldObj, xCoord, yCoord, zCoord, ForgeDirection.NORTH, storage);
WorldUtil.pushEnergy(worldObj, xCoord, yCoord, zCoord, ForgeDirection.EAST, storage);
WorldUtil.pushEnergy(worldObj, xCoord, yCoord, zCoord, ForgeDirection.SOUTH, storage);
WorldUtil.pushEnergy(worldObj, xCoord, yCoord, zCoord, ForgeDirection.WEST, storage);
}
2015-06-21 02:28:49 +02:00
if(flag != this.currentBurnTime > 0){
this.markDirty();
2015-06-21 02:28:49 +02:00
int meta = worldObj.getBlockMetadata(xCoord, yCoord, zCoord);
if(meta == 1){
2015-10-02 16:48:01 +02:00
if(!(ConfigIntValues.OIL_GEN_ENERGY_PRODUCED.getValue()*ConfigIntValues.OIL_GEN_BURN_TIME.getValue() <= this.getMaxEnergyStored(ForgeDirection.UNKNOWN)-this.getEnergyStored(ForgeDirection.UNKNOWN) && FluidContainerRegistry.BUCKET_VOLUME <= this.tank.getCapacity()-this.tank.getFluidAmount())){
2015-06-21 02:28:49 +02:00
worldObj.setBlockMetadataWithNotify(xCoord, yCoord, zCoord, 0, 2);
2015-10-02 16:48:01 +02:00
}
}
else{
worldObj.setBlockMetadataWithNotify(xCoord, yCoord, zCoord, 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.lastEnergy = this.storage.getEnergyStored();
this.lastTank = this.tank.getFluidAmount();
this.lastBurnTime = this.currentBurnTime;
this.sendUpdate();
}
2015-05-20 22:39:43 +02:00
}
}
@SideOnly(Side.CLIENT)
public int getEnergyScaled(int i){
2015-10-02 16:48:01 +02:00
return this.storage.getEnergyStored()*i/this.storage.getMaxEnergyStored();
2015-05-20 22:39:43 +02:00
}
@SideOnly(Side.CLIENT)
public int getTankScaled(int i){
2015-10-02 16:48:01 +02:00
return this.tank.getFluidAmount()*i/this.tank.getCapacity();
2015-05-20 22:39:43 +02:00
}
@SideOnly(Side.CLIENT)
public int getBurningScaled(int i){
2015-10-02 16:48:01 +02:00
return this.currentBurnTime*i/ConfigIntValues.OIL_GEN_BURN_TIME.getValue();
2015-05-20 22:39:43 +02:00
}
@Override
public void writeToNBT(NBTTagCompound compound){
compound.setInteger("BurnTime", this.currentBurnTime);
this.storage.writeToNBT(compound);
this.tank.writeToNBT(compound);
super.writeToNBT(compound);
}
@Override
public void readFromNBT(NBTTagCompound compound){
this.currentBurnTime = compound.getInteger("BurnTime");
this.storage.readFromNBT(compound);
this.tank.readFromNBT(compound);
super.readFromNBT(compound);
}
@Override
public boolean isItemValidForSlot(int i, ItemStack stack){
return FluidContainerRegistry.containsFluid(stack, new FluidStack(InitBlocks.fluidOil, FluidContainerRegistry.BUCKET_VOLUME)) && i == 0;
2015-05-20 22:39:43 +02:00
}
@Override
public boolean canInsertItem(int slot, ItemStack stack, int side){
return this.isItemValidForSlot(slot, stack);
}
@Override
public boolean canExtractItem(int slot, ItemStack stack, int side){
return slot == 1;
}
@Override
public int extractEnergy(ForgeDirection from, int maxExtract, boolean simulate){
return this.storage.extractEnergy(maxExtract, simulate);
}
@Override
public int getEnergyStored(ForgeDirection from){
return this.storage.getEnergyStored();
}
@Override
public int getMaxEnergyStored(ForgeDirection from){
return this.storage.getMaxEnergyStored();
}
@Override
public boolean canConnectEnergy(ForgeDirection from){
return true;
}
@Override
public int fill(ForgeDirection from, FluidStack resource, boolean doFill){
2015-05-25 17:00:54 +02:00
if(resource.getFluid() == InitBlocks.fluidOil) return this.tank.fill(resource, doFill);
2015-05-20 22:39:43 +02:00
return 0;
}
@Override
public FluidStack drain(ForgeDirection from, FluidStack resource, boolean doDrain){
return null;
}
@Override
public FluidStack drain(ForgeDirection from, int maxDrain, boolean doDrain){
return null;
}
@Override
public boolean canFill(ForgeDirection from, Fluid fluid){
return from != ForgeDirection.DOWN && fluid == InitBlocks.fluidOil;
2015-05-20 22:39:43 +02:00
}
@Override
public boolean canDrain(ForgeDirection from, Fluid fluid){
return false;
}
@Override
public FluidTankInfo[] getTankInfo(ForgeDirection from){
return new FluidTankInfo[]{this.tank.getInfo()};
}
@Override
public int[] getValues(){
return new int[]{this.storage.getEnergyStored(), this.currentBurnTime, this.tank.getFluidAmount(), this.tank.getFluid() == null ? -1 : this.tank.getFluid().getFluidID()};
}
@Override
public void setValues(int[] values){
this.storage.setEnergyStored(values[0]);
this.currentBurnTime = values[1];
if(values[3] != -1){
this.tank.setFluid(new FluidStack(FluidRegistry.getFluid(values[3]), values[2]));
}
2015-10-02 16:48:01 +02:00
else{
this.tank.setFluid(null);
}
}
@Override
public void sendUpdate(){
PacketSyncerToClient.sendPacket(this);
}
2015-05-20 22:39:43 +02:00
}