Applied-Energistics-2-tiler.../parts/p2p/PartP2PBCPower.java

442 lines
10 KiB
Java
Raw Normal View History

2014-01-20 17:41:37 +01:00
package appeng.parts.p2p;
2014-02-09 02:34:52 +01:00
import net.minecraft.init.Blocks;
2014-01-20 17:41:37 +01:00
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.tileentity.TileEntity;
2014-02-09 02:34:52 +01:00
import net.minecraft.util.IIcon;
2014-01-20 17:41:37 +01:00
import net.minecraft.world.World;
2014-02-09 02:34:52 +01:00
import net.minecraftforge.common.util.ForgeDirection;
2014-01-20 17:41:37 +01:00
import appeng.api.config.PowerUnits;
import appeng.api.config.TunnelType;
2014-01-20 17:41:37 +01:00
import appeng.api.networking.IGridNode;
import appeng.api.networking.ticking.IGridTickable;
import appeng.api.networking.ticking.TickRateModulation;
import appeng.api.networking.ticking.TickingRequest;
import appeng.core.AppEng;
2014-03-17 20:59:50 +01:00
import appeng.core.settings.TickRates;
2014-05-11 23:13:37 +02:00
import appeng.integration.abstraction.IMJ5;
import appeng.integration.abstraction.helpers.BaseMJperdition;
2014-01-20 17:41:37 +01:00
import appeng.me.GridAccessException;
import appeng.me.cache.helpers.TunnelCollection;
import appeng.transformer.annotations.integration.Interface;
2014-05-11 23:13:37 +02:00
import appeng.transformer.annotations.integration.InterfaceList;
import appeng.transformer.annotations.integration.Method;
import buildcraft.api.mj.IBatteryObject;
import buildcraft.api.mj.ISidedBatteryProvider;
import buildcraft.api.mj.MjAPI;
2014-01-20 17:41:37 +01:00
import buildcraft.api.power.IPowerReceptor;
import buildcraft.api.power.PowerHandler;
import buildcraft.api.power.PowerHandler.PowerReceiver;
import buildcraft.api.power.PowerHandler.Type;
import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;
2014-05-11 23:13:37 +02:00
@InterfaceList(value = { @Interface(iface = "buildcraft.api.mj.ISidedBatteryProvider", iname = "MJ6"),
2014-05-12 00:01:31 +02:00
@Interface(iface = "buildcraft.api.mj.IBatteryObject", iname = "MJ6"), @Interface(iface = "buildcraft.api.power.IPowerReceptor", iname = "MJ5"),
2014-05-11 23:13:37 +02:00
@Interface(iface = "appeng.api.networking.ticking.IGridTickable", iname = "MJ5") })
public class PartP2PBCPower extends PartP2PTunnel<PartP2PBCPower> implements IPowerReceptor, ISidedBatteryProvider, IBatteryObject, IGridTickable
2014-01-20 17:41:37 +01:00
{
2014-05-11 23:13:37 +02:00
BaseMJperdition pp;
2014-01-20 17:41:37 +01:00
public TunnelType getTunnelType()
{
return TunnelType.BC_POWER;
}
2014-01-20 17:41:37 +01:00
public PartP2PBCPower(ItemStack is) {
super( is );
2014-05-10 04:08:56 +02:00
if ( !AppEng.instance.isIntegrationEnabled( "MJ5" ) && !AppEng.instance.isIntegrationEnabled( "MJ6" ) )
throw new RuntimeException( "MJ Not installed!" );
2014-05-11 23:13:37 +02:00
if ( AppEng.instance.isIntegrationEnabled( "MJ5" ) )
{
pp = (BaseMJperdition) ((IMJ5) AppEng.instance.getIntegration( "MJ5" )).createPerdition( this );
if ( pp != null )
pp.configure( 1, 380, 1.0f / 5.0f, 1000 );
}
2014-01-20 17:41:37 +01:00
}
@Override
2014-05-11 23:13:37 +02:00
@Method(iname = "MJ5")
2014-01-20 17:41:37 +01:00
public TickingRequest getTickingRequest(IGridNode node)
{
2014-03-17 20:59:50 +01:00
return new TickingRequest( TickRates.MJTunnel.min, TickRates.MJTunnel.max, false, false );
2014-01-20 17:41:37 +01:00
}
@Override
2014-05-11 23:13:37 +02:00
@Method(iname = "MJ5")
2014-01-20 17:41:37 +01:00
public TickRateModulation tickingRequest(IGridNode node, int TicksSinceLastCall)
{
if ( !output && proxy.isActive() )
{
float totalRequiredPower = 0.0f;
TunnelCollection<PartP2PBCPower> tunnelset;
try
{
tunnelset = getOutputs();
}
catch (GridAccessException e)
{
return TickRateModulation.IDLE;
}
for (PartP2PBCPower o : tunnelset)
{
IPowerReceptor target = o.getPowerTarget();
if ( target != null )
{
PowerReceiver tp = target.getPowerReceiver( side.getOpposite() );
if ( tp != null )
{
double howmuch = tp.powerRequest();
2014-01-20 17:41:37 +01:00
if ( howmuch > tp.getMaxEnergyReceived() )
howmuch = tp.getMaxEnergyReceived();
if ( howmuch > 0.01 && howmuch > tp.getMinEnergyReceived() )
{
totalRequiredPower += howmuch;
}
}
}
}
if ( totalRequiredPower < 0.1 )
return TickRateModulation.SLOWER;
2014-05-11 23:13:37 +02:00
double currentTotal = pp.getPowerReceiver().getEnergyStored();
2014-01-20 17:41:37 +01:00
if ( currentTotal < 0.01 )
return TickRateModulation.SLOWER;
for (PartP2PBCPower o : tunnelset)
{
IPowerReceptor target = o.getPowerTarget();
if ( target != null )
{
PowerReceiver tp = target.getPowerReceiver( side.getOpposite() );
if ( tp != null )
{
2014-05-13 04:20:22 +02:00
double howmuch = tp.powerRequest();
2014-01-20 17:41:37 +01:00
if ( howmuch > tp.getMaxEnergyReceived() )
howmuch = tp.getMaxEnergyReceived();
if ( howmuch > 0.01 && howmuch > tp.getMinEnergyReceived() )
{
double toPull = currentTotal * (howmuch / totalRequiredPower);
double pulled = pp.useEnergy( 0, toPull, true );
2014-05-02 07:40:46 +02:00
QueueTunnelDrain( PowerUnits.MJ, pulled );
2014-01-20 17:41:37 +01:00
tp.receiveEnergy( Type.PIPE, pulled, o.side.getOpposite() );
}
}
}
}
return TickRateModulation.FASTER;
}
return TickRateModulation.SLOWER;
}
public float getPowerDrainPerTick()
{
return 0.5f;
};
2014-05-11 23:13:37 +02:00
@Method(iname = "MJ6")
private IBatteryObject getTargetBattery()
{
TileEntity te = getWorld().getTileEntity( tile.xCoord + side.offsetX, tile.yCoord + side.offsetY, tile.zCoord + side.offsetZ );
if ( te != null )
return MjAPI.getMjBattery( te );
return null;
}
@Method(iname = "MJ5")
2014-01-20 17:41:37 +01:00
private IPowerReceptor getPowerTarget()
{
2014-02-09 02:34:52 +01:00
TileEntity te = getWorld().getTileEntity( tile.xCoord + side.offsetX, tile.yCoord + side.offsetY, tile.zCoord + side.offsetZ );
2014-01-20 17:41:37 +01:00
if ( te != null )
{
if ( te instanceof IPowerReceptor )
return (IPowerReceptor) te;
}
return null;
}
@Override
public void writeToNBT(NBTTagCompound tag)
{
super.writeToNBT( tag );
2014-05-11 23:13:37 +02:00
if ( pp != null )
pp.writeToNBT( tag );
2014-01-20 17:41:37 +01:00
}
@Override
public void readFromNBT(NBTTagCompound tag)
{
super.readFromNBT( tag );
2014-05-11 23:13:37 +02:00
if ( pp != null )
pp.readFromNBT( tag );
2014-01-20 17:41:37 +01:00
}
@SideOnly(Side.CLIENT)
2014-02-09 02:34:52 +01:00
public IIcon getTypeTexture()
2014-01-20 17:41:37 +01:00
{
2014-02-09 02:34:52 +01:00
return Blocks.emerald_block.getBlockTextureFromSide( 0 );
2014-01-20 17:41:37 +01:00
}
@Override
2014-05-11 23:13:37 +02:00
@Method(iname = "MJ5")
2014-01-20 17:41:37 +01:00
public PowerReceiver getPowerReceiver(ForgeDirection side)
{
if ( side.equals( side ) )
2014-05-11 23:13:37 +02:00
return ((BaseMJperdition) pp).getPowerReceiver();
2014-01-20 17:41:37 +01:00
return null;
}
@Override
2014-05-11 23:13:37 +02:00
@Method(iname = "MJ5")
2014-01-20 17:41:37 +01:00
public void doWork(PowerHandler workProvider)
{
}
@Override
public World getWorld()
{
2014-02-09 02:34:52 +01:00
return tile.getWorldObj();
2014-01-20 17:41:37 +01:00
}
2014-05-11 23:13:37 +02:00
@Override
@Method(iname = "MJ6")
public IBatteryObject getMjBattery(String kind)
{
return this;
}
@Override
@Method(iname = "MJ6")
public IBatteryObject getMjBattery(String kind, ForgeDirection direction)
{
return this;
}
@Override
@Method(iname = "MJ6")
public double getEnergyRequested()
{
try
{
double totalRequiredPower = 0.0f;
for (PartP2PBCPower g : getOutputs())
{
IBatteryObject o = g.getTargetBattery();
if ( o != null )
totalRequiredPower += o.getEnergyRequested();
}
return totalRequiredPower;
}
catch (GridAccessException e)
{
return 0;
}
}
@Override
@Method(iname = "MJ6")
public double addEnergy(double mj)
{
return addEnergyInternal( mj, false, false );
}
@Override
@Method(iname = "MJ6")
public double addEnergy(double mj, boolean ignoreCycleLimit)
{
return addEnergyInternal( mj, true, ignoreCycleLimit );
}
@Method(iname = "MJ6")
private double addEnergyInternal(double mj, boolean cycleLimitMode, boolean ignoreCycleLimit)
{
if ( !output && proxy.isActive() )
return 0;
double originaInput = mj;
try
{
TunnelCollection<PartP2PBCPower> outs = getOutputs();
double outputs = 0;
for (PartP2PBCPower g : outs)
{
IBatteryObject o = g.getTargetBattery();
if ( o != null )
{
outputs = outputs + 1.0;
}
}
if ( outputs < 0.0000001 )
return 0;
for (PartP2PBCPower g : outs)
{
IBatteryObject o = g.getTargetBattery();
if ( o != null )
{
double fraction = originaInput / outputs;
if ( cycleLimitMode )
fraction = o.addEnergy( fraction );
else
fraction = o.addEnergy( fraction, ignoreCycleLimit );
mj -= fraction;
}
}
if ( mj > 0 )
{
for (PartP2PBCPower g : outs)
{
IBatteryObject o = g.getTargetBattery();
if ( o != null )
{
if ( cycleLimitMode )
mj = mj - o.addEnergy( mj );
else
mj = mj - o.addEnergy( mj, ignoreCycleLimit );
}
}
}
return originaInput - mj;
}
catch (GridAccessException e)
{
return 0;
}
}
@Override
@Method(iname = "MJ6")
public double getEnergyStored()
{
try
{
double totalRequiredPower = 0.0f;
for (PartP2PBCPower g : getOutputs())
{
IBatteryObject o = g.getTargetBattery();
if ( o != null )
totalRequiredPower += o.getEnergyStored();
}
return totalRequiredPower;
}
catch (GridAccessException e)
{
return 0;
}
}
@Override
@Method(iname = "MJ6")
public void setEnergyStored(double mj)
{
// EHh?!
}
@Override
public double maxCapacity()
{
try
{
double totalRequiredPower = 0.0f;
for (PartP2PBCPower g : getOutputs())
{
IBatteryObject o = g.getTargetBattery();
if ( o != null )
totalRequiredPower += o.maxCapacity();
}
return totalRequiredPower;
}
catch (GridAccessException e)
{
return 0;
}
}
@Override
@Method(iname = "MJ6")
public double minimumConsumption()
{
try
{
double totalRequiredPower = 1000000000000.0;
for (PartP2PBCPower g : getOutputs())
{
IBatteryObject o = g.getTargetBattery();
if ( o != null )
totalRequiredPower = Math.min( totalRequiredPower, o.minimumConsumption() );
}
return totalRequiredPower;
}
catch (GridAccessException e)
{
return 0;
}
}
@Override
@Method(iname = "MJ6")
public double maxReceivedPerCycle()
{
try
{
double totalRequiredPower = 1000000.0;
for (PartP2PBCPower g : getOutputs())
{
IBatteryObject o = g.getTargetBattery();
if ( o != null )
totalRequiredPower = Math.min( totalRequiredPower, o.maxReceivedPerCycle() );
}
return totalRequiredPower;
}
catch (GridAccessException e)
{
return 0;
}
}
@Override
@Method(iname = "MJ6")
public IBatteryObject reconfigure(double maxCapacity, double maxReceivedPerCycle, double minimumConsumption)
{
return this;
}
@Override
@Method(iname = "MJ6")
public String kind()
{
return "tunnel";
}
2014-01-20 17:41:37 +01:00
}