2013-12-27 23:59:59 +01:00
|
|
|
package appeng.me.cluster.implementations;
|
|
|
|
|
|
|
|
import java.util.Iterator;
|
|
|
|
|
2014-03-07 04:24:01 +01:00
|
|
|
import net.minecraft.tileentity.TileEntity;
|
2014-03-07 05:30:59 +01:00
|
|
|
import net.minecraft.world.World;
|
2013-12-27 23:59:59 +01:00
|
|
|
import net.minecraft.world.chunk.Chunk;
|
2014-03-07 05:30:59 +01:00
|
|
|
import net.minecraftforge.common.DimensionManager;
|
2013-12-27 23:59:59 +01:00
|
|
|
import net.minecraftforge.common.MinecraftForge;
|
2014-02-09 02:34:52 +01:00
|
|
|
import net.minecraftforge.common.util.ForgeDirection;
|
2013-12-27 23:59:59 +01:00
|
|
|
import net.minecraftforge.event.world.WorldEvent;
|
|
|
|
import appeng.api.AEApi;
|
|
|
|
import appeng.api.events.LocatableEventAnnounce;
|
|
|
|
import appeng.api.events.LocatableEventAnnounce.LocatableEvent;
|
2014-01-27 05:00:36 +01:00
|
|
|
import appeng.api.exceptions.FailedConnection;
|
2013-12-27 23:59:59 +01:00
|
|
|
import appeng.api.features.ILocatable;
|
|
|
|
import appeng.api.networking.IGridHost;
|
2014-01-20 17:41:37 +01:00
|
|
|
import appeng.api.networking.IGridNode;
|
2013-12-27 23:59:59 +01:00
|
|
|
import appeng.api.util.WorldCoord;
|
2014-01-21 16:58:41 +01:00
|
|
|
import appeng.me.cache.helpers.ConnectionWrapper;
|
2013-12-27 23:59:59 +01:00
|
|
|
import appeng.me.cluster.IAECluster;
|
|
|
|
import appeng.tile.qnb.TileQuantumBridge;
|
|
|
|
import appeng.util.iterators.ChainedIterator;
|
2014-02-09 02:34:52 +01:00
|
|
|
import cpw.mods.fml.common.eventhandler.SubscribeEvent;
|
2013-12-27 23:59:59 +01:00
|
|
|
|
|
|
|
public class QuantumCluster implements ILocatable, IAECluster
|
|
|
|
{
|
|
|
|
|
|
|
|
public WorldCoord min;
|
|
|
|
public WorldCoord max;
|
|
|
|
public boolean isDestroyed = false;
|
2014-06-06 07:17:45 +02:00
|
|
|
private boolean updateStatus = true;
|
2013-12-27 23:59:59 +01:00
|
|
|
|
|
|
|
boolean registered = false;
|
|
|
|
private long thisSide;
|
|
|
|
private long otherSide;
|
|
|
|
|
2014-01-21 16:58:41 +01:00
|
|
|
ConnectionWrapper connection;
|
2014-01-20 17:41:37 +01:00
|
|
|
|
2013-12-27 23:59:59 +01:00
|
|
|
public TileQuantumBridge Ring[];
|
|
|
|
private TileQuantumBridge center;
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public Iterator<IGridHost> getTiles()
|
|
|
|
{
|
|
|
|
return new ChainedIterator<IGridHost>( Ring[0], Ring[1], Ring[2], Ring[3], Ring[4], Ring[5], Ring[6], Ring[7], center );
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setCenter(TileQuantumBridge c)
|
|
|
|
{
|
|
|
|
registered = true;
|
|
|
|
MinecraftForge.EVENT_BUS.register( this );
|
|
|
|
center = c;
|
|
|
|
}
|
|
|
|
|
|
|
|
public QuantumCluster(WorldCoord _min, WorldCoord _max) {
|
|
|
|
min = _min;
|
|
|
|
max = _max;
|
|
|
|
Ring = new TileQuantumBridge[8];
|
|
|
|
}
|
|
|
|
|
|
|
|
public boolean canUseNode(long qe)
|
|
|
|
{
|
|
|
|
QuantumCluster qc = (QuantumCluster) AEApi.instance().registries().locateable().findLocateableBySerial( qe );
|
2014-01-20 17:41:37 +01:00
|
|
|
if ( qc != null && qc.center instanceof TileQuantumBridge )
|
2013-12-27 23:59:59 +01:00
|
|
|
{
|
2014-03-07 05:30:59 +01:00
|
|
|
World theWorld = qc.getCenter().getWorldObj();
|
2013-12-27 23:59:59 +01:00
|
|
|
if ( !qc.isDestroyed )
|
|
|
|
{
|
2014-03-07 05:30:59 +01:00
|
|
|
Chunk c = theWorld.getChunkFromBlockCoords( qc.center.xCoord, qc.center.zCoord );
|
2013-12-27 23:59:59 +01:00
|
|
|
if ( c.isChunkLoaded )
|
2014-03-07 04:24:01 +01:00
|
|
|
{
|
2014-03-07 05:30:59 +01:00
|
|
|
int id = theWorld.provider.dimensionId;
|
|
|
|
World cur = DimensionManager.getWorld( id );
|
|
|
|
|
|
|
|
TileEntity te = theWorld.getTileEntity( qc.center.xCoord, qc.center.yCoord, qc.center.zCoord );
|
|
|
|
return te != qc.center || theWorld != cur;
|
2014-03-07 04:24:01 +01:00
|
|
|
}
|
2013-12-27 23:59:59 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-02-09 02:34:52 +01:00
|
|
|
@SubscribeEvent
|
2013-12-27 23:59:59 +01:00
|
|
|
public void onUnload(WorldEvent.Unload e)
|
|
|
|
{
|
2014-02-09 02:34:52 +01:00
|
|
|
if ( center.getWorldObj() == e.world )
|
2013-12-27 23:59:59 +01:00
|
|
|
{
|
2014-06-06 07:17:45 +02:00
|
|
|
updateStatus = false;
|
2013-12-27 23:59:59 +01:00
|
|
|
destroy();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void updateStatus(boolean updateGrid)
|
|
|
|
{
|
|
|
|
long qe;
|
|
|
|
|
|
|
|
qe = center.getQEDest();
|
|
|
|
|
|
|
|
if ( thisSide != qe && thisSide != -qe )
|
|
|
|
{
|
|
|
|
if ( qe != 0 )
|
|
|
|
{
|
|
|
|
if ( thisSide != 0 )
|
|
|
|
MinecraftForge.EVENT_BUS.post( new LocatableEventAnnounce( this, LocatableEvent.Unregister ) );
|
|
|
|
|
|
|
|
if ( canUseNode( -qe ) )
|
|
|
|
{
|
|
|
|
otherSide = qe;
|
|
|
|
thisSide = -qe;
|
|
|
|
}
|
|
|
|
else if ( canUseNode( qe ) )
|
|
|
|
{
|
|
|
|
thisSide = qe;
|
|
|
|
otherSide = -qe;
|
|
|
|
}
|
|
|
|
|
|
|
|
MinecraftForge.EVENT_BUS.post( new LocatableEventAnnounce( this, LocatableEvent.Register ) );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
MinecraftForge.EVENT_BUS.post( new LocatableEventAnnounce( this, LocatableEvent.Unregister ) );
|
|
|
|
|
|
|
|
otherSide = 0;
|
|
|
|
thisSide = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-01-20 17:41:37 +01:00
|
|
|
Object myOtherSide = otherSide == 0 ? null : AEApi.instance().registries().locateable().findLocateableBySerial( otherSide );
|
2013-12-27 23:59:59 +01:00
|
|
|
|
2014-01-20 17:41:37 +01:00
|
|
|
boolean shutdown = false;
|
|
|
|
|
|
|
|
if ( myOtherSide instanceof QuantumCluster )
|
2013-12-27 23:59:59 +01:00
|
|
|
{
|
2014-01-20 17:41:37 +01:00
|
|
|
QuantumCluster sideA = (QuantumCluster) this;
|
|
|
|
QuantumCluster sideB = (QuantumCluster) myOtherSide;
|
2013-12-27 23:59:59 +01:00
|
|
|
|
2014-01-20 17:41:37 +01:00
|
|
|
if ( sideA.isActive() && sideB.isActive() )
|
|
|
|
{
|
|
|
|
if ( connection != null && connection.connection != null )
|
|
|
|
{
|
|
|
|
IGridNode a = connection.connection.a();
|
|
|
|
IGridNode b = connection.connection.b();
|
|
|
|
IGridNode sa = sideA.getNode();
|
|
|
|
IGridNode sb = sideB.getNode();
|
|
|
|
if ( (a == sa || b == sa) && (a == sb || b == sb) )
|
|
|
|
return;
|
|
|
|
}
|
2013-12-27 23:59:59 +01:00
|
|
|
|
2014-01-27 05:00:36 +01:00
|
|
|
try
|
|
|
|
{
|
2014-03-07 05:30:59 +01:00
|
|
|
if ( sideA.connection != null )
|
|
|
|
{
|
|
|
|
if ( sideA.connection.connection != null )
|
|
|
|
{
|
|
|
|
sideA.connection.connection.destroy();
|
|
|
|
sideA.connection = new ConnectionWrapper( null );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( sideB.connection != null )
|
|
|
|
{
|
|
|
|
if ( sideB.connection.connection != null )
|
|
|
|
{
|
|
|
|
sideB.connection.connection.destroy();
|
|
|
|
sideB.connection = new ConnectionWrapper( null );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-01-27 05:00:36 +01:00
|
|
|
sideA.connection = sideB.connection = new ConnectionWrapper( AEApi.instance().createGridConnection( sideA.getNode(), sideB.getNode() ) );
|
|
|
|
}
|
|
|
|
catch (FailedConnection e)
|
|
|
|
{
|
|
|
|
// :(
|
|
|
|
}
|
2014-01-20 17:41:37 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
shutdown = true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
shutdown = true;
|
2013-12-27 23:59:59 +01:00
|
|
|
|
2014-01-20 17:41:37 +01:00
|
|
|
if ( shutdown && connection != null )
|
|
|
|
{
|
|
|
|
if ( connection.connection != null )
|
|
|
|
{
|
|
|
|
connection.connection.destroy();
|
|
|
|
connection.connection = null;
|
2014-01-21 16:58:41 +01:00
|
|
|
connection = new ConnectionWrapper( null );
|
2014-01-20 17:41:37 +01:00
|
|
|
}
|
|
|
|
}
|
2013-12-27 23:59:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void destroy()
|
|
|
|
{
|
|
|
|
if ( isDestroyed )
|
|
|
|
return;
|
|
|
|
isDestroyed = true;
|
|
|
|
|
|
|
|
if ( registered )
|
|
|
|
{
|
|
|
|
MinecraftForge.EVENT_BUS.unregister( this );
|
|
|
|
registered = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( getLocatableSerial() != 0 )
|
|
|
|
{
|
2014-01-20 17:41:37 +01:00
|
|
|
updateStatus( true );
|
2013-12-27 23:59:59 +01:00
|
|
|
MinecraftForge.EVENT_BUS.post( new LocatableEventAnnounce( this, LocatableEvent.Unregister ) );
|
|
|
|
}
|
|
|
|
|
2014-06-06 07:17:45 +02:00
|
|
|
if ( updateStatus )
|
2013-12-27 23:59:59 +01:00
|
|
|
{
|
2014-06-06 07:17:45 +02:00
|
|
|
center.updateStatus( null, (byte) -1 );
|
|
|
|
|
|
|
|
for (TileQuantumBridge r : Ring)
|
|
|
|
{
|
|
|
|
r.updateStatus( null, (byte) -1 );
|
|
|
|
}
|
2013-12-27 23:59:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
center = null;
|
|
|
|
Ring = new TileQuantumBridge[8];
|
|
|
|
}
|
|
|
|
|
|
|
|
public boolean isCorner(TileQuantumBridge tileQuantumBridge)
|
|
|
|
{
|
|
|
|
return Ring[0] == tileQuantumBridge || Ring[2] == tileQuantumBridge || Ring[4] == tileQuantumBridge || Ring[6] == tileQuantumBridge;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public long getLocatableSerial()
|
|
|
|
{
|
|
|
|
return thisSide;
|
|
|
|
}
|
|
|
|
|
|
|
|
public TileQuantumBridge getCenter()
|
|
|
|
{
|
|
|
|
return center;
|
|
|
|
}
|
|
|
|
|
|
|
|
public boolean hasQES()
|
|
|
|
{
|
|
|
|
return getLocatableSerial() != 0;
|
|
|
|
}
|
|
|
|
|
2014-01-20 17:41:37 +01:00
|
|
|
private IGridNode getNode()
|
|
|
|
{
|
|
|
|
return center.getGridNode( ForgeDirection.UNKNOWN );
|
|
|
|
}
|
|
|
|
|
|
|
|
private boolean isActive()
|
|
|
|
{
|
|
|
|
if ( isDestroyed || !registered )
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return center.isPowered() && hasQES();
|
|
|
|
}
|
|
|
|
|
2013-12-27 23:59:59 +01:00
|
|
|
}
|