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-01-20 17:41:37 +01:00
package appeng.parts.p2p ;
2015-01-18 20:46:37 +01:00
2014-01-20 17:41:37 +01:00
import java.util.EnumSet ;
2014-01-22 05:52:33 +01:00
import java.util.Iterator ;
import java.util.LinkedList ;
2016-08-29 09:47:17 +02:00
import java.util.List ;
2014-01-20 17:41:37 +01:00
2014-01-27 05:00:36 +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 ;
2016-06-21 11:03:10 +02:00
import net.minecraft.util.EnumHand ;
2016-08-29 09:47:17 +02:00
import net.minecraft.util.ResourceLocation ;
2015-12-24 02:07:03 +01:00
2014-01-22 05:52:33 +01:00
import appeng.api.AEApi ;
2014-01-27 05:00:36 +01:00
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 ;
2015-06-16 02:44:59 +02:00
import appeng.api.util.AEPartLocation ;
2014-02-07 21:37:22 +01:00
import appeng.core.AELog ;
2014-03-17 20:59:50 +01:00
import appeng.core.settings.TickRates ;
2014-03-02 09:35:11 +01:00
import appeng.hooks.TickHandler ;
2016-08-29 09:47:17 +02:00
import appeng.items.parts.PartModels ;
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 ;
2015-01-18 20:46:37 +01:00
2014-01-22 05:52:33 +01:00
public class PartP2PTunnelME extends PartP2PTunnel < PartP2PTunnelME > implements IGridTickable
2014-01-20 17:41:37 +01:00
{
2016-08-29 09:47:17 +02:00
private static final P2PModels MODELS = new P2PModels ( " part/p2p/p2p_tunnel_me " ) ;
@PartModels
public static List < ResourceLocation > getModels ( )
{
return MODELS . getModels ( ) ;
}
2015-10-08 15:42:42 +02:00
private final Connections connection = new Connections ( this ) ;
private final AENetworkProxy outerProxy = new AENetworkProxy ( this , " outer " , null , true ) ;
2014-01-20 17:41:37 +01:00
2015-09-30 14:24:40 +02:00
public PartP2PTunnelME ( final ItemStack is )
2015-01-18 20:46:37 +01:00
{
2014-01-20 17:41:37 +01:00
super ( is ) ;
2015-10-08 15:42:42 +02:00
this . getProxy ( ) . setFlags ( GridFlags . REQUIRE_CHANNEL , GridFlags . COMPRESSED_CHANNEL ) ;
2014-12-29 15:13:47 +01:00
this . outerProxy . setFlags ( GridFlags . DENSE_CAPACITY , GridFlags . CANNOT_CARRY_COMPRESSED ) ;
2014-01-20 17:41:37 +01:00
}
@Override
2015-09-30 14:24:40 +02:00
public void readFromNBT ( final NBTTagCompound extra )
2014-01-20 17:41:37 +01:00
{
super . readFromNBT ( extra ) ;
2014-12-29 15:13:47 +01:00
this . outerProxy . readFromNBT ( extra ) ;
2014-01-20 17:41:37 +01:00
}
@Override
2015-09-30 14:24:40 +02:00
public void writeToNBT ( final NBTTagCompound extra )
2014-01-20 17:41:37 +01:00
{
super . writeToNBT ( extra ) ;
2014-12-29 15:13:47 +01:00
this . outerProxy . writeToNBT ( extra ) ;
2014-01-20 17:41:37 +01:00
}
@Override
2015-04-03 08:54:31 +02:00
public void onTunnelNetworkChange ( )
2014-01-20 17:41:37 +01:00
{
2015-04-03 08:54:31 +02:00
super . onTunnelNetworkChange ( ) ;
2015-10-08 15:42:42 +02:00
if ( ! this . isOutput ( ) )
2015-04-03 08:54:31 +02:00
{
try
{
2015-10-08 15:42:42 +02:00
this . getProxy ( ) . getTick ( ) . wakeDevice ( this . getProxy ( ) . getNode ( ) ) ;
2015-04-03 08:54:31 +02:00
}
2015-09-30 14:24:40 +02:00
catch ( final GridAccessException e )
2015-04-03 08:54:31 +02:00
{
// :P
}
}
}
@Override
2015-09-30 14:24:40 +02:00
public AECableType getCableConnectionType ( final AEPartLocation dir )
2015-04-03 08:54:31 +02:00
{
return AECableType . DENSE ;
2014-01-20 17:41:37 +01:00
}
@Override
public void removeFromWorld ( )
{
super . removeFromWorld ( ) ;
2014-12-29 15:13:47 +01:00
this . outerProxy . invalidate ( ) ;
2014-01-20 17:41:37 +01:00
}
@Override
2015-04-03 08:54:31 +02:00
public void addToWorld ( )
2014-01-20 17:41:37 +01:00
{
2015-04-03 08:54:31 +02:00
super . addToWorld ( ) ;
this . outerProxy . onReady ( ) ;
2014-01-20 17:41:37 +01:00
}
@Override
2015-09-30 14:24:40 +02:00
public void setPartHostInfo ( final AEPartLocation side , final IPartHost host , final TileEntity tile )
2014-01-20 17:41:37 +01:00
{
2015-04-03 08:54:31 +02:00
super . setPartHostInfo ( side , host , tile ) ;
2015-06-16 02:44:59 +02:00
this . outerProxy . setValidSides ( EnumSet . of ( side . getFacing ( ) ) ) ;
2014-01-20 17:41:37 +01:00
}
2014-01-22 05:52:33 +01:00
@Override
2015-04-03 08:54:31 +02:00
public IGridNode getExternalFacingNode ( )
2014-01-22 05:52:33 +01:00
{
2015-04-03 08:54:31 +02:00
return this . outerProxy . getNode ( ) ;
}
@Override
2016-06-21 11:03:10 +02:00
public void onPlacement ( final EntityPlayer player , final EnumHand hand , final ItemStack held , final AEPartLocation side )
2015-04-03 08:54:31 +02:00
{
2016-06-21 11:03:10 +02:00
super . onPlacement ( player , hand , held , side ) ;
2015-04-03 08:54:31 +02:00
this . outerProxy . setOwner ( player ) ;
2014-01-22 05:52:33 +01:00
}
@Override
2015-09-30 14:24:40 +02:00
public TickingRequest getTickingRequest ( final IGridNode node )
2014-01-22 05:52:33 +01:00
{
2015-10-08 15:42:42 +02:00
return new TickingRequest ( TickRates . METunnel . getMin ( ) , TickRates . METunnel . getMax ( ) , true , false ) ;
2014-01-22 05:52:33 +01:00
}
@Override
2015-09-30 14:24:40 +02:00
public TickRateModulation tickingRequest ( final IGridNode node , final int ticksSinceLastCall )
2014-01-22 05:52:33 +01:00
{
// just move on...
try
{
2015-10-08 15:42:42 +02:00
if ( ! this . getProxy ( ) . getPath ( ) . isNetworkBooting ( ) )
2014-01-22 05:52:33 +01:00
{
2015-10-08 15:42:42 +02:00
if ( ! this . getProxy ( ) . getEnergy ( ) . isNetworkPowered ( ) )
2014-01-22 05:52:33 +01:00
{
2014-12-29 15:13:47 +01:00
this . connection . markDestroy ( ) ;
2015-10-08 15:42:42 +02:00
TickHandler . INSTANCE . addCallable ( this . getTile ( ) . getWorld ( ) , this . connection ) ;
2014-01-22 05:52:33 +01:00
}
else
{
2015-10-08 15:42:42 +02:00
if ( this . getProxy ( ) . isActive ( ) )
2014-01-22 05:52:33 +01:00
{
2014-12-29 15:13:47 +01:00
this . connection . markCreate ( ) ;
2015-10-08 15:42:42 +02:00
TickHandler . INSTANCE . addCallable ( this . getTile ( ) . getWorld ( ) , this . connection ) ;
2014-01-22 05:52:33 +01:00
}
else
{
2014-12-29 15:13:47 +01:00
this . connection . markDestroy ( ) ;
2015-10-08 15:42:42 +02:00
TickHandler . INSTANCE . addCallable ( this . getTile ( ) . getWorld ( ) , this . connection ) ;
2014-01-22 05:52:33 +01:00
}
}
return TickRateModulation . SLEEP ;
}
}
2015-09-30 14:24:40 +02:00
catch ( final GridAccessException e )
2014-01-22 05:52:33 +01:00
{
// meh?
}
return TickRateModulation . IDLE ;
}
2015-09-30 14:24:40 +02:00
public void updateConnections ( final Connections connections )
2014-01-22 05:52:33 +01:00
{
2015-10-08 15:42:42 +02:00
if ( connections . isDestroy ( ) )
2014-01-22 05:52:33 +01:00
{
2015-10-08 15:42:42 +02:00
for ( final TunnelConnection cw : this . connection . getConnections ( ) . values ( ) )
2015-04-29 02:30:53 +02:00
{
2015-10-08 15:42:42 +02:00
cw . getConnection ( ) . destroy ( ) ;
2015-04-29 02:30:53 +02:00
}
2014-01-22 05:52:33 +01:00
2015-10-08 15:42:42 +02:00
this . connection . getConnections ( ) . clear ( ) ;
2014-01-22 05:52:33 +01:00
}
2015-10-08 15:42:42 +02:00
else if ( connections . isCreate ( ) )
2014-01-22 05:52:33 +01:00
{
2015-10-08 15:42:42 +02:00
final Iterator < TunnelConnection > i = this . connection . getConnections ( ) . values ( ) . iterator ( ) ;
2015-04-03 08:54:31 +02:00
while ( i . hasNext ( ) )
2014-01-22 05:52:33 +01:00
{
2015-09-30 14:24:40 +02:00
final TunnelConnection cw = i . next ( ) ;
2014-01-22 05:52:33 +01:00
try
{
2015-10-08 15:42:42 +02:00
if ( cw . getTunnel ( ) . getProxy ( ) . getGrid ( ) ! = this . getProxy ( ) . getGrid ( ) )
2014-01-22 05:52:33 +01:00
{
2015-10-08 15:42:42 +02:00
cw . getConnection ( ) . destroy ( ) ;
2014-01-22 05:52:33 +01:00
i . remove ( ) ;
}
2015-10-08 15:42:42 +02:00
else if ( ! cw . getTunnel ( ) . getProxy ( ) . isActive ( ) )
2014-01-22 05:52:33 +01:00
{
2015-10-08 15:42:42 +02:00
cw . getConnection ( ) . destroy ( ) ;
2014-01-22 05:52:33 +01:00
i . remove ( ) ;
}
}
2015-09-30 14:24:40 +02:00
catch ( final GridAccessException e )
2014-01-22 05:52:33 +01:00
{
2014-02-07 21:37:22 +01:00
// :P
2014-01-22 05:52:33 +01:00
}
}
2015-09-30 14:24:40 +02:00
final LinkedList < PartP2PTunnelME > newSides = new LinkedList < PartP2PTunnelME > ( ) ;
2014-01-22 05:52:33 +01:00
try
{
2015-09-30 14:24:40 +02:00
for ( final PartP2PTunnelME me : this . getOutputs ( ) )
2014-01-22 05:52:33 +01:00
{
2015-10-08 15:42:42 +02:00
if ( me . getProxy ( ) . isActive ( ) & & connections . getConnections ( ) . get ( me . getGridNode ( ) ) = = null )
2014-01-22 05:52:33 +01:00
{
newSides . add ( me ) ;
}
}
2015-09-30 14:24:40 +02:00
for ( final PartP2PTunnelME me : newSides )
2014-01-22 05:52:33 +01:00
{
2014-01-27 05:00:36 +01:00
try
{
2015-10-08 15:42:42 +02:00
connections . getConnections ( ) . put ( me . getGridNode ( ) , new TunnelConnection ( me , AEApi . instance ( ) . createGridConnection ( this . outerProxy . getNode ( ) , me . outerProxy . getNode ( ) ) ) ) ;
2014-01-27 05:00:36 +01:00
}
2015-09-30 14:24:40 +02:00
catch ( final FailedConnection e )
2014-01-27 05:00:36 +01:00
{
2015-01-18 20:46:37 +01:00
final TileEntity start = this . getTile ( ) ;
final TileEntity end = me . getTile ( ) ;
2015-11-22 15:54:29 +01:00
AELog . warn ( " Failed to establish a ME P2P Tunnel between the tunnels at [x=%d, y=%d, z=%d] and [x=%d, y=%d, z=%d] " , start . getPos ( ) . getX ( ) , start . getPos ( ) . getY ( ) , start . getPos ( ) . getZ ( ) , end . getPos ( ) . getX ( ) , end . getPos ( ) . getY ( ) , end . getPos ( ) . getZ ( ) ) ;
2014-01-27 05:00:36 +01:00
// :(
}
2014-01-22 05:52:33 +01:00
}
}
2015-09-30 14:24:40 +02:00
catch ( final GridAccessException e )
2014-01-22 05:52:33 +01:00
{
2015-11-22 15:54:29 +01:00
AELog . debug ( e ) ;
2014-01-22 05:52:33 +01:00
}
}
}
2016-08-29 09:47:17 +02:00
@Override
public List < ResourceLocation > getStaticModels ( )
{
return MODELS . getModel ( isPowered ( ) , isActive ( ) ) ;
}
2014-01-20 17:41:37 +01:00
}