Applied-Energistics-2-tiler.../src/main/java/appeng/tile/powersink/AERootPoweredTile.java

327 lines
8.2 KiB
Java
Raw Normal View History

2014-11-14 12:02:52 +01:00
/*
* This file is part of Applied Energistics 2.
* Copyright (c) 2013 - 2014, AlgorithmX2, All rights reserved.
*
* Applied Energistics 2 is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Applied Energistics 2 is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Applied Energistics 2. If not, see <http://www.gnu.org/licenses/lgpl>.
*/
2014-09-24 02:26:27 +02:00
package appeng.tile.powersink;
2014-09-24 02:26:27 +02:00
import java.util.EnumSet;
import javax.annotation.Nullable;
2014-09-24 02:26:27 +02:00
import net.minecraft.nbt.NBTTagCompound;
2015-06-16 02:44:59 +02:00
import net.minecraft.util.EnumFacing;
import net.minecraftforge.common.capabilities.Capability;
import net.minecraftforge.energy.CapabilityEnergy;
import net.minecraftforge.energy.IEnergyStorage;
2015-12-24 02:07:03 +01:00
2014-09-24 02:26:27 +02:00
import appeng.api.config.AccessRestriction;
import appeng.api.config.Actionable;
import appeng.api.config.PowerMultiplier;
import appeng.api.config.PowerUnits;
import appeng.api.networking.energy.IAEPowerStorage;
import appeng.api.networking.events.MENetworkPowerStorage.PowerEventType;
import appeng.capabilities.Capabilities;
2016-10-28 00:23:04 +02:00
import appeng.integration.modules.IC2;
import appeng.integration.modules.ic2.IC2PowerSink;
2014-09-24 02:26:27 +02:00
import appeng.tile.AEBaseInvTile;
import appeng.tile.TileEvent;
import appeng.tile.events.TileEventType;
public abstract class AERootPoweredTile extends AEBaseInvTile implements IAEPowerStorage, IExternalPowerSink
2014-09-24 02:26:27 +02:00
{
2014-09-24 02:26:27 +02:00
// values that determine general function, are set by inheriting classes if
// needed. These should generally remain static.
private double internalMaxPower = 10000;
private boolean internalPublicPowerStorage = false;
private AccessRestriction internalPowerFlow = AccessRestriction.READ_WRITE;
2014-09-24 02:26:27 +02:00
// the current power buffer.
private double internalCurrentPower = 0;
2015-06-16 02:44:59 +02:00
private EnumSet<EnumFacing> internalPowerSides = EnumSet.allOf( EnumFacing.class );
private final IEnergyStorage forgeEnergyAdapter;
private Object teslaEnergyAdapter;
2016-10-28 00:23:04 +02:00
private IC2PowerSink ic2Sink;
public AERootPoweredTile()
{
forgeEnergyAdapter = new ForgeEnergyAdapter( this );
if( Capabilities.TESLA_CONSUMER != null )
{
teslaEnergyAdapter = new TeslaEnergyAdapter( this );
}
ic2Sink = IC2.createPowerSink( this, this );
2016-10-28 00:23:04 +02:00
ic2Sink.setValidFaces( internalPowerSides );
}
2014-09-24 02:26:27 +02:00
2015-06-16 02:44:59 +02:00
protected EnumSet<EnumFacing> getPowerSides()
2014-09-24 02:26:27 +02:00
{
return this.internalPowerSides.clone();
2014-09-24 02:26:27 +02:00
}
2015-09-30 14:24:40 +02:00
protected void setPowerSides( final EnumSet<EnumFacing> sides )
2014-09-24 02:26:27 +02:00
{
this.internalPowerSides = sides;
2016-10-28 00:23:04 +02:00
ic2Sink.setValidFaces( sides );
// trigger re-calc!
2014-09-24 02:26:27 +02:00
}
@TileEvent( TileEventType.WORLD_NBT_WRITE )
2015-09-30 14:24:40 +02:00
public void writeToNBT_AERootPoweredTile( final NBTTagCompound data )
2014-09-24 02:26:27 +02:00
{
data.setDouble( "internalCurrentPower", this.getInternalCurrentPower() );
2014-09-24 02:26:27 +02:00
}
@TileEvent( TileEventType.WORLD_NBT_READ )
2015-09-30 14:24:40 +02:00
public void readFromNBT_AERootPoweredTile( final NBTTagCompound data )
2014-09-24 02:26:27 +02:00
{
this.setInternalCurrentPower( data.getDouble( "internalCurrentPower" ) );
2014-09-24 02:26:27 +02:00
}
@Override
public final double getExternalPowerDemand( final PowerUnits externalUnit, final double maxPowerRequired )
2014-09-24 02:26:27 +02:00
{
2014-12-29 15:13:47 +01:00
return PowerUnits.AE.convertTo( externalUnit, Math.max( 0.0, this.getFunnelPowerDemand( externalUnit.convertTo( PowerUnits.AE, maxPowerRequired ) ) ) );
2014-09-24 02:26:27 +02:00
}
2015-09-30 14:24:40 +02:00
protected double getFunnelPowerDemand( final double maxRequired )
2014-09-24 02:26:27 +02:00
{
return this.getInternalMaxPower() - this.getInternalCurrentPower();
2014-09-24 02:26:27 +02:00
}
@Override
2015-09-30 14:24:40 +02:00
public final double injectExternalPower( final PowerUnits input, final double amt )
2014-09-24 02:26:27 +02:00
{
2014-12-29 15:13:47 +01:00
return PowerUnits.AE.convertTo( input, this.funnelPowerIntoStorage( input.convertTo( PowerUnits.AE, amt ), Actionable.MODULATE ) );
2014-09-24 02:26:27 +02:00
}
2015-09-30 14:24:40 +02:00
protected double funnelPowerIntoStorage( final double power, final Actionable mode )
2014-09-24 02:26:27 +02:00
{
return this.injectAEPower( power, mode );
2014-09-24 02:26:27 +02:00
}
@Override
2015-09-30 14:24:40 +02:00
public final double injectAEPower( double amt, final Actionable mode )
2014-09-24 02:26:27 +02:00
{
if( amt < 0.000001 )
2015-04-29 02:30:53 +02:00
{
2014-09-24 02:26:27 +02:00
return 0;
2015-04-29 02:30:53 +02:00
}
2014-09-24 02:26:27 +02:00
if( mode == Actionable.SIMULATE )
2014-09-24 02:26:27 +02:00
{
final double fakeBattery = this.getInternalCurrentPower() + amt;
2014-09-24 02:26:27 +02:00
if( fakeBattery > this.getInternalMaxPower() )
2015-04-29 02:30:53 +02:00
{
return fakeBattery - this.getInternalMaxPower();
2015-04-29 02:30:53 +02:00
}
2014-09-24 02:26:27 +02:00
return 0;
}
else
{
if( this.getInternalCurrentPower() < 0.01 && amt > 0.01 )
2015-04-29 02:30:53 +02:00
{
2014-12-29 15:13:47 +01:00
this.PowerEvent( PowerEventType.PROVIDE_POWER );
2015-04-29 02:30:53 +02:00
}
2014-09-24 02:26:27 +02:00
this.setInternalCurrentPower( this.getInternalCurrentPower() + amt );
if( this.getInternalCurrentPower() > this.getInternalMaxPower() )
2014-09-24 02:26:27 +02:00
{
amt = this.getInternalCurrentPower() - this.getInternalMaxPower();
this.setInternalCurrentPower( this.getInternalMaxPower() );
2014-09-24 02:26:27 +02:00
return amt;
}
return 0;
}
}
2015-09-30 14:24:40 +02:00
protected void PowerEvent( final PowerEventType x )
2014-09-24 02:26:27 +02:00
{
// nothing.
}
@Override
public final double getAEMaxPower()
2014-09-24 02:26:27 +02:00
{
return this.getInternalMaxPower();
2014-09-24 02:26:27 +02:00
}
@Override
public final double getAECurrentPower()
2014-09-24 02:26:27 +02:00
{
return this.getInternalCurrentPower();
2014-09-24 02:26:27 +02:00
}
@Override
public final boolean isAEPublicPowerStorage()
2014-09-24 02:26:27 +02:00
{
return this.isInternalPublicPowerStorage();
2014-09-24 02:26:27 +02:00
}
@Override
public final AccessRestriction getPowerFlow()
2014-09-24 02:26:27 +02:00
{
return this.getInternalPowerFlow();
2014-09-24 02:26:27 +02:00
}
@Override
2015-09-30 14:24:40 +02:00
public final double extractAEPower( final double amt, final Actionable mode, final PowerMultiplier multiplier )
{
return multiplier.divide( this.extractAEPower( multiplier.multiply( amt ), mode ) );
}
2015-09-30 14:24:40 +02:00
protected double extractAEPower( double amt, final Actionable mode )
{
if( mode == Actionable.SIMULATE )
{
if( this.getInternalCurrentPower() > amt )
2015-04-29 02:30:53 +02:00
{
return amt;
2015-04-29 02:30:53 +02:00
}
return this.getInternalCurrentPower();
}
final boolean wasFull = this.getInternalCurrentPower() >= this.getInternalMaxPower() - 0.001;
if( wasFull && amt > 0.001 )
{
this.PowerEvent( PowerEventType.REQUEST_POWER );
}
if( this.getInternalCurrentPower() > amt )
{
this.setInternalCurrentPower( this.getInternalCurrentPower() - amt );
return amt;
}
amt = this.getInternalCurrentPower();
this.setInternalCurrentPower( 0 );
return amt;
}
public double getInternalCurrentPower()
{
return this.internalCurrentPower;
}
public void setInternalCurrentPower( final double internalCurrentPower )
{
this.internalCurrentPower = internalCurrentPower;
}
public double getInternalMaxPower()
{
return this.internalMaxPower;
}
public void setInternalMaxPower( final double internalMaxPower )
{
this.internalMaxPower = internalMaxPower;
}
private boolean isInternalPublicPowerStorage()
{
return this.internalPublicPowerStorage;
}
public void setInternalPublicPowerStorage( final boolean internalPublicPowerStorage )
{
this.internalPublicPowerStorage = internalPublicPowerStorage;
}
private AccessRestriction getInternalPowerFlow()
{
return this.internalPowerFlow;
}
public void setInternalPowerFlow( final AccessRestriction internalPowerFlow )
{
this.internalPowerFlow = internalPowerFlow;
}
2016-10-28 00:23:04 +02:00
@Override
public void onReady()
{
super.onReady();
ic2Sink.onLoad();
}
@Override
public void onChunkUnload()
{
super.onChunkUnload();
ic2Sink.onChunkUnload();
}
@Override
public void invalidate()
{
super.invalidate();
ic2Sink.invalidate();
}
@Override
public boolean hasCapability( Capability<?> capability, EnumFacing facing )
{
if( capability == CapabilityEnergy.ENERGY )
{
if( this.getPowerSides().contains( facing ) )
{
return true;
}
}
else if( capability == Capabilities.TESLA_CONSUMER )
{
if( this.getPowerSides().contains( facing ) )
{
return true;
}
}
return super.hasCapability( capability, facing );
}
@SuppressWarnings( "unchecked" )
@Override
public <T> T getCapability( Capability<T> capability, @Nullable EnumFacing facing )
{
if( capability == CapabilityEnergy.ENERGY )
{
if( this.getPowerSides().contains( facing ) )
{
return (T) forgeEnergyAdapter;
}
}
else if( capability == Capabilities.TESLA_CONSUMER )
{
if( this.getPowerSides().contains( facing ) )
{
return (T) teslaEnergyAdapter;
}
}
return super.getCapability( capability, facing );
}
2014-09-24 02:26:27 +02:00
}