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

229 lines
4.9 KiB
Java
Raw Normal View History

2014-01-20 17:41:37 +01:00
package appeng.parts.p2p;
import java.util.EnumSet;
2014-01-22 05:52:33 +01:00
import java.util.Iterator;
import java.util.LinkedList;
2014-01-20 17:41:37 +01:00
import net.minecraft.entity.player.EntityPlayer;
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.minecraftforge.common.util.ForgeDirection;
2014-01-22 05:52:33 +01:00
import appeng.api.AEApi;
import appeng.api.config.TunnelType;
import appeng.api.exceptions.FailedConnection;
2014-01-20 17:41:37 +01:00
import appeng.api.networking.GridFlags;
import appeng.api.networking.IGridNode;
2014-01-22 05:52:33 +01:00
import appeng.api.networking.ticking.IGridTickable;
import appeng.api.networking.ticking.TickRateModulation;
import appeng.api.networking.ticking.TickingRequest;
2014-01-20 17:41:37 +01:00
import appeng.api.parts.IPartHost;
import appeng.api.util.AECableType;
import appeng.core.AELog;
2014-03-17 20:59:50 +01:00
import appeng.core.settings.TickRates;
import appeng.hooks.TickHandler;
2014-01-22 05:52:33 +01:00
import appeng.me.GridAccessException;
import appeng.me.cache.helpers.Connections;
import appeng.me.cache.helpers.TunnelConnection;
2014-01-20 17:41:37 +01:00
import appeng.me.helpers.AENetworkProxy;
2014-01-22 05:52:33 +01:00
public class PartP2PTunnelME extends PartP2PTunnel<PartP2PTunnelME> implements IGridTickable
2014-01-20 17:41:37 +01:00
{
public TunnelType getTunnelType()
{
return TunnelType.ME;
}
2014-01-23 17:28:12 +01:00
AENetworkProxy outerProxy = new AENetworkProxy( this, "outer", null, true );
2014-01-22 05:52:33 +01:00
public Connections connection = new Connections( this );
2014-01-20 17:41:37 +01:00
public PartP2PTunnelME(ItemStack is) {
super( is );
2014-01-31 22:12:15 +01:00
proxy.setFlags( GridFlags.REQUIRE_CHANNEL, GridFlags.DENSE_CHANNEL );
2014-01-22 05:52:33 +01:00
outerProxy.setFlags( GridFlags.TIER_2_CAPACITY, GridFlags.CANNOT_CARRY_DENSE );
2014-01-20 17:41:37 +01:00
}
@Override
public void setPartHostInfo(ForgeDirection side, IPartHost host, TileEntity tile)
{
super.setPartHostInfo( side, host, tile );
outerProxy.setValidSides( EnumSet.of( side ) );
}
@Override
public void readFromNBT(NBTTagCompound extra)
{
super.readFromNBT( extra );
outerProxy.readFromNBT( extra );
}
@Override
public void writeToNBT(NBTTagCompound extra)
{
super.writeToNBT( extra );
outerProxy.writeToNBT( extra );
}
@Override
public void addToWorld()
{
super.addToWorld();
outerProxy.onReady();
}
@Override
public void removeFromWorld()
{
super.removeFromWorld();
outerProxy.invalidate();
}
@Override
public IGridNode getExternalFacingNode()
{
return outerProxy.getNode();
}
@Override
public AECableType getCableConnectionType(ForgeDirection dir)
{
return AECableType.DENSE;
}
2014-01-22 05:52:33 +01:00
@Override
public void onTunnelNetworkChange()
2014-01-22 05:52:33 +01:00
{
super.onTunnelNetworkChange();
2014-01-22 05:52:33 +01:00
if ( !output )
{
try
{
proxy.getTick().wakeDevice( proxy.getNode() );
}
catch (GridAccessException e)
{
// :P
}
}
}
@Override
public TickingRequest getTickingRequest(IGridNode node)
{
return new TickingRequest( TickRates.METunnel.min, TickRates.METunnel.max, true, false );
2014-01-22 05:52:33 +01:00
}
@Override
public TickRateModulation tickingRequest(IGridNode node, int TicksSinceLastCall)
{
// just move on...
try
{
if ( !proxy.getPath().isNetworkBooting() )
{
if ( !proxy.getEnergy().isNetworkPowered() )
{
connection.markDestroy();
TickHandler.instance.addCallable( connection );
}
else
{
if ( proxy.isActive() )
{
connection.markCreate();
TickHandler.instance.addCallable( connection );
}
else
{
connection.markDestroy();
TickHandler.instance.addCallable( connection );
}
}
return TickRateModulation.SLEEP;
}
}
catch (GridAccessException e)
{
// meh?
}
return TickRateModulation.IDLE;
}
@Override
public void onPlacement(EntityPlayer player, ItemStack held, ForgeDirection side)
{
super.onPlacement( player, held, side );
outerProxy.setOwner( player );
}
2014-01-22 05:52:33 +01:00
public void updateConnections(Connections connections)
{
if ( connections.destroy )
{
for (TunnelConnection cw : connection.connections.values())
cw.c.destroy();
connection.connections.clear();
}
else if ( connections.create )
{
Iterator<TunnelConnection> i = connection.connections.values().iterator();
while (i.hasNext())
{
TunnelConnection cw = i.next();
try
{
if ( cw.tunnel.proxy.getGrid() != proxy.getGrid() )
{
cw.c.destroy();
i.remove();
}
else if ( !cw.tunnel.proxy.isActive() )
{
cw.c.destroy();
i.remove();
}
}
catch (GridAccessException e)
{
// :P
2014-01-22 05:52:33 +01:00
}
}
LinkedList<PartP2PTunnelME> newSides = new LinkedList<PartP2PTunnelME>();
try
{
for (PartP2PTunnelME me : getOutputs())
{
if ( me.proxy.isActive() && connections.connections.get( me.getGridNode() ) == null )
{
newSides.add( me );
}
}
for (PartP2PTunnelME me : newSides)
{
try
{
connections.connections.put( me.getGridNode(),
new TunnelConnection( me, AEApi.instance().createGridConnection( outerProxy.getNode(), me.outerProxy.getNode() ) ) );
}
catch (FailedConnection e)
{
AELog.error( e );
// :(
}
2014-01-22 05:52:33 +01:00
}
}
catch (GridAccessException e)
{
AELog.error( e );
2014-01-22 05:52:33 +01:00
}
}
}
2014-01-20 17:41:37 +01:00
}