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;
|
2014-01-27 05:00:36 +01:00
|
|
|
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;
|
2014-02-09 06:08:27 +01:00
|
|
|
import appeng.core.AEConfig;
|
2014-04-06 08:55:24 +02:00
|
|
|
import appeng.core.AppEng;
|
2014-03-17 20:59:50 +01:00
|
|
|
import appeng.core.settings.TickRates;
|
2014-01-20 17:41:37 +01:00
|
|
|
import appeng.me.GridAccessException;
|
|
|
|
import appeng.me.cache.helpers.TunnelCollection;
|
2014-04-06 08:55:24 +02:00
|
|
|
import appeng.transformer.annotations.integration.Interface;
|
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-04-06 08:55:24 +02:00
|
|
|
@Interface(iface = "buildcraft.api.power.IPowerReceptor", iname = "BC")
|
2014-01-20 17:41:37 +01:00
|
|
|
public class PartP2PBCPower extends PartP2PTunnel<PartP2PBCPower> implements IPowerReceptor, IGridTickable
|
|
|
|
{
|
|
|
|
|
|
|
|
PowerHandler pp;
|
|
|
|
|
2014-01-27 05:00:36 +01:00
|
|
|
public TunnelType getTunnelType()
|
|
|
|
{
|
|
|
|
return TunnelType.BC_POWER;
|
|
|
|
}
|
|
|
|
|
2014-01-20 17:41:37 +01:00
|
|
|
public PartP2PBCPower(ItemStack is) {
|
|
|
|
super( is );
|
2014-04-06 08:55:24 +02:00
|
|
|
|
|
|
|
if ( !AppEng.instance.isIntegrationEnabled( "MJ" ) )
|
|
|
|
throw new RuntimeException( "MJ Not installed!" );
|
|
|
|
|
2014-01-20 17:41:37 +01:00
|
|
|
pp = new PowerHandler( this, Type.MACHINE );
|
|
|
|
pp.configure( 1f, 320f, 800f, 640f );
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
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
|
|
|
|
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 )
|
|
|
|
{
|
2014-02-16 19:41:45 +01: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() )
|
|
|
|
{
|
|
|
|
totalRequiredPower += howmuch;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( totalRequiredPower < 0.1 )
|
|
|
|
return TickRateModulation.SLOWER;
|
|
|
|
|
2014-02-16 19:41:45 +01:00
|
|
|
double currentTotal = pp.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-02-16 19:41:45 +01:00
|
|
|
double howmuch = tp.powerRequest(); // orientation.getOpposite()
|
2014-01-28 18:29:17 +01:00
|
|
|
// );
|
2014-01-20 17:41:37 +01:00
|
|
|
if ( howmuch > tp.getMaxEnergyReceived() )
|
|
|
|
howmuch = tp.getMaxEnergyReceived();
|
|
|
|
|
|
|
|
if ( howmuch > 0.01 && howmuch > tp.getMinEnergyReceived() )
|
|
|
|
{
|
2014-02-16 19:41:45 +01:00
|
|
|
double toPull = currentTotal * (howmuch / totalRequiredPower);
|
|
|
|
double pulled = pp.useEnergy( 0, toPull, true );
|
2014-02-09 06:08:27 +01:00
|
|
|
QueueTunnelDrain( PowerUnits.MJ, pulled * AEConfig.TunnelPowerLoss );
|
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;
|
|
|
|
};
|
|
|
|
|
|
|
|
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 );
|
|
|
|
pp.writeToNBT( tag );
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void readFromNBT(NBTTagCompound tag)
|
|
|
|
{
|
|
|
|
super.readFromNBT( tag );
|
|
|
|
pp.readFromNBT( tag );
|
|
|
|
}
|
|
|
|
|
|
|
|
@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
|
|
|
|
public PowerReceiver getPowerReceiver(ForgeDirection side)
|
|
|
|
{
|
|
|
|
if ( side.equals( side ) )
|
|
|
|
return pp.getPowerReceiver();
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
}
|