2013-12-27 23:59:59 +01:00
|
|
|
package appeng.tile.qnb;
|
|
|
|
|
2014-02-09 02:34:52 +01:00
|
|
|
import io.netty.buffer.ByteBuf;
|
|
|
|
|
2014-01-20 17:41:37 +01:00
|
|
|
import java.io.IOException;
|
|
|
|
import java.util.EnumSet;
|
|
|
|
|
2013-12-27 23:59:59 +01:00
|
|
|
import net.minecraft.inventory.IInventory;
|
|
|
|
import net.minecraft.item.ItemStack;
|
2014-01-20 17:41:37 +01:00
|
|
|
import net.minecraft.nbt.NBTTagCompound;
|
|
|
|
import net.minecraft.tileentity.TileEntity;
|
2014-02-09 02:34:52 +01:00
|
|
|
import net.minecraftforge.common.util.ForgeDirection;
|
2013-12-27 23:59:59 +01:00
|
|
|
import appeng.api.AEApi;
|
2014-01-20 17:41:37 +01:00
|
|
|
import appeng.api.networking.GridFlags;
|
|
|
|
import appeng.api.networking.events.MENetworkEventSubscribe;
|
|
|
|
import appeng.api.networking.events.MENetworkPowerStatusChange;
|
|
|
|
import appeng.api.util.AECableType;
|
|
|
|
import appeng.api.util.DimensionalCoord;
|
|
|
|
import appeng.me.GridAccessException;
|
2013-12-27 23:59:59 +01:00
|
|
|
import appeng.me.cluster.IAECluster;
|
|
|
|
import appeng.me.cluster.IAEMultiBlock;
|
2014-01-20 17:41:37 +01:00
|
|
|
import appeng.me.cluster.implementations.QuantumCalculator;
|
2013-12-27 23:59:59 +01:00
|
|
|
import appeng.me.cluster.implementations.QuantumCluster;
|
2014-01-20 17:41:37 +01:00
|
|
|
import appeng.tile.events.AETileEventHandler;
|
|
|
|
import appeng.tile.events.TileEventType;
|
|
|
|
import appeng.tile.grid.AENetworkInvTile;
|
2013-12-27 23:59:59 +01:00
|
|
|
import appeng.tile.inventory.AppEngInternalInventory;
|
|
|
|
import appeng.tile.inventory.InvOperation;
|
|
|
|
import appeng.util.Platform;
|
|
|
|
|
2014-01-20 17:41:37 +01:00
|
|
|
public class TileQuantumBridge extends AENetworkInvTile implements IAEMultiBlock
|
2013-12-27 23:59:59 +01:00
|
|
|
{
|
|
|
|
|
2014-05-29 18:41:29 +02:00
|
|
|
final private static ItemStack ring = AEApi.instance().blocks().blockQuantumRing.stack(1);
|
|
|
|
|
2013-12-27 23:59:59 +01:00
|
|
|
final int sidesRing[] = new int[] {};
|
|
|
|
final int sidesLink[] = new int[] { 0 };
|
|
|
|
|
|
|
|
AppEngInternalInventory inv = new AppEngInternalInventory( this, 1 );
|
|
|
|
|
|
|
|
public final byte corner = 16;
|
|
|
|
final byte hasSingularity = 32;
|
|
|
|
final byte powered = 64;
|
|
|
|
|
2014-01-20 17:41:37 +01:00
|
|
|
private QuantumCalculator calc = new QuantumCalculator( this );
|
2013-12-27 23:59:59 +01:00
|
|
|
byte xdex = -1;
|
|
|
|
|
|
|
|
QuantumCluster clust;
|
|
|
|
public boolean bridgePowered;
|
|
|
|
|
2014-01-20 17:41:37 +01:00
|
|
|
private boolean updateStatus = false;
|
|
|
|
|
|
|
|
class QBridgeHandler extends AETileEventHandler
|
|
|
|
{
|
|
|
|
|
|
|
|
public QBridgeHandler() {
|
2014-01-26 07:46:16 +01:00
|
|
|
super( TileEventType.NETWORK, TileEventType.TICK );
|
2014-01-20 17:41:37 +01:00
|
|
|
gridProxy.setValidSides( EnumSet.noneOf( ForgeDirection.class ) );
|
|
|
|
gridProxy.setFlags( GridFlags.TIER_2_CAPACITY );
|
|
|
|
gridProxy.setIdlePowerUsage( 22 );
|
|
|
|
inv.setMaxStackSize( 1 );
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void Tick()
|
|
|
|
{
|
|
|
|
if ( updateStatus )
|
|
|
|
{
|
|
|
|
updateStatus = false;
|
|
|
|
if ( clust != null )
|
|
|
|
clust.updateStatus( true );
|
|
|
|
markForUpdate();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2014-02-09 02:34:52 +01:00
|
|
|
public void writeToStream(ByteBuf data) throws IOException
|
2014-01-20 17:41:37 +01:00
|
|
|
{
|
|
|
|
int out = xdex;
|
|
|
|
|
|
|
|
if ( getStackInSlot( 0 ) != null && xdex != -1 )
|
|
|
|
out = out | hasSingularity;
|
|
|
|
|
|
|
|
if ( gridProxy.isActive() && xdex != -1 )
|
|
|
|
out = out | powered;
|
|
|
|
|
|
|
|
data.writeByte( (byte) out );
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2014-02-09 02:34:52 +01:00
|
|
|
public boolean readFromStream(ByteBuf data) throws IOException
|
2014-01-20 17:41:37 +01:00
|
|
|
{
|
|
|
|
int oldValue = xdex;
|
|
|
|
xdex = data.readByte();
|
|
|
|
bridgePowered = (xdex | powered) == powered;
|
|
|
|
return xdex != oldValue;
|
|
|
|
}
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
public TileQuantumBridge() {
|
|
|
|
addNewHandler( new QBridgeHandler() );
|
|
|
|
gridProxy.setFlags( GridFlags.TIER_2_CAPACITY );
|
|
|
|
}
|
|
|
|
|
2013-12-27 23:59:59 +01:00
|
|
|
public IInventory getInternalInventory()
|
|
|
|
{
|
|
|
|
return inv;
|
|
|
|
}
|
|
|
|
|
2014-01-20 17:41:37 +01:00
|
|
|
@MENetworkEventSubscribe
|
|
|
|
public void PowerSwitch(MENetworkPowerStatusChange c)
|
|
|
|
{
|
|
|
|
updateStatus = true;
|
|
|
|
}
|
|
|
|
|
2013-12-27 23:59:59 +01:00
|
|
|
@Override
|
|
|
|
public void onChangeInventory(IInventory inv, int slot, InvOperation mc, ItemStack removed, ItemStack added)
|
|
|
|
{
|
2014-01-20 17:41:37 +01:00
|
|
|
if ( clust != null )
|
|
|
|
clust.updateStatus( true );
|
2013-12-27 23:59:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2014-03-14 03:59:37 +01:00
|
|
|
public int[] getAccessibleSlotsBySide(ForgeDirection side)
|
2013-12-27 23:59:59 +01:00
|
|
|
{
|
|
|
|
if ( isCenter() )
|
|
|
|
return sidesLink;
|
|
|
|
return sidesRing;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2014-06-13 21:39:36 +02:00
|
|
|
public void disconnect(boolean affectWorld)
|
2013-12-27 23:59:59 +01:00
|
|
|
{
|
2014-01-20 17:41:37 +01:00
|
|
|
if ( clust != null )
|
2014-06-13 21:39:36 +02:00
|
|
|
{
|
|
|
|
if ( ! affectWorld )
|
|
|
|
clust.updateStatus = false;
|
|
|
|
|
2014-01-20 17:41:37 +01:00
|
|
|
clust.destroy();
|
2014-06-13 21:39:36 +02:00
|
|
|
}
|
|
|
|
|
2014-01-20 17:41:37 +01:00
|
|
|
clust = null;
|
2014-06-13 21:39:36 +02:00
|
|
|
|
|
|
|
if ( affectWorld )
|
|
|
|
gridProxy.setValidSides( EnumSet.noneOf( ForgeDirection.class ) );
|
2013-12-27 23:59:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public IAECluster getCluster()
|
|
|
|
{
|
2014-01-20 17:41:37 +01:00
|
|
|
return clust;
|
2013-12-27 23:59:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean isValid()
|
|
|
|
{
|
2014-01-20 17:41:37 +01:00
|
|
|
return !isInvalid();
|
2013-12-27 23:59:59 +01:00
|
|
|
}
|
2014-05-29 18:41:29 +02:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onReady()
|
|
|
|
{
|
|
|
|
super.onReady();
|
|
|
|
if ( worldObj.getBlock( xCoord, yCoord, zCoord) == AEApi.instance().blocks().blockQuantumRing.block() )
|
|
|
|
gridProxy.setVisualRepresentation( ring );
|
|
|
|
}
|
2013-12-27 23:59:59 +01:00
|
|
|
|
2014-06-13 21:39:36 +02:00
|
|
|
@Override
|
|
|
|
public void invalidate()
|
2013-12-27 23:59:59 +01:00
|
|
|
{
|
2014-06-13 21:39:36 +02:00
|
|
|
disconnect(false);
|
|
|
|
super.invalidate();
|
|
|
|
}
|
2014-01-20 17:41:37 +01:00
|
|
|
|
2014-06-13 21:39:36 +02:00
|
|
|
@Override
|
|
|
|
public void onChunkUnload()
|
|
|
|
{
|
|
|
|
disconnect(false);
|
|
|
|
super.onChunkUnload();
|
|
|
|
}
|
2014-01-20 17:41:37 +01:00
|
|
|
|
2014-06-13 21:39:36 +02:00
|
|
|
public void updateStatus(QuantumCluster c, byte flags, boolean affectWorld)
|
|
|
|
{
|
|
|
|
clust = c;
|
|
|
|
|
|
|
|
if ( affectWorld )
|
2014-01-20 17:41:37 +01:00
|
|
|
{
|
2014-06-13 21:39:36 +02:00
|
|
|
if ( xdex != flags )
|
|
|
|
{
|
|
|
|
xdex = flags;
|
|
|
|
markForUpdate();
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( isCorner() || isCenter() )
|
|
|
|
{
|
|
|
|
gridProxy.setValidSides( getConnections() );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
gridProxy.setValidSides( EnumSet.allOf( ForgeDirection.class ) );
|
2014-01-20 17:41:37 +01:00
|
|
|
}
|
2013-12-27 23:59:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public long getQEDest()
|
|
|
|
{
|
2014-01-20 17:41:37 +01:00
|
|
|
ItemStack is = inv.getStackInSlot( 0 );
|
|
|
|
if ( is != null )
|
|
|
|
{
|
|
|
|
NBTTagCompound c = is.getTagCompound();
|
|
|
|
if ( c != null )
|
|
|
|
return c.getLong( "freq" );
|
|
|
|
}
|
2013-12-27 23:59:59 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
public boolean isCenter()
|
|
|
|
{
|
|
|
|
return getBlockType() == AEApi.instance().blocks().blockQuantumLink.block();
|
|
|
|
}
|
|
|
|
|
|
|
|
public boolean isCorner()
|
|
|
|
{
|
2014-01-20 17:41:37 +01:00
|
|
|
return (xdex & corner) == corner && xdex != -1;
|
2013-12-27 23:59:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public boolean isPowered()
|
|
|
|
{
|
|
|
|
if ( Platform.isClient() )
|
2014-01-20 17:41:37 +01:00
|
|
|
return (xdex & powered) == powered && xdex != -1;
|
|
|
|
|
|
|
|
try
|
|
|
|
{
|
|
|
|
return gridProxy.getEnergy().isNetworkPowered();
|
|
|
|
}
|
|
|
|
catch (GridAccessException e)
|
|
|
|
{
|
|
|
|
// :P
|
|
|
|
}
|
2013-12-27 23:59:59 +01:00
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
public boolean isFormed()
|
|
|
|
{
|
|
|
|
return xdex != -1;
|
|
|
|
}
|
|
|
|
|
2014-01-20 17:41:37 +01:00
|
|
|
@Override
|
|
|
|
public AECableType getCableConnectionType(ForgeDirection dir)
|
|
|
|
{
|
|
|
|
return AECableType.DENSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public DimensionalCoord getLocation()
|
|
|
|
{
|
|
|
|
return new DimensionalCoord( this );
|
|
|
|
}
|
|
|
|
|
|
|
|
public void neighborUpdate()
|
|
|
|
{
|
|
|
|
calc.calculateMultiblock( worldObj, getLocation() );
|
|
|
|
}
|
|
|
|
|
|
|
|
public EnumSet<ForgeDirection> getConnections()
|
|
|
|
{
|
|
|
|
EnumSet<ForgeDirection> set = EnumSet.noneOf( ForgeDirection.class );
|
|
|
|
|
|
|
|
for (ForgeDirection d : ForgeDirection.VALID_DIRECTIONS)
|
|
|
|
{
|
2014-02-09 02:34:52 +01:00
|
|
|
TileEntity te = worldObj.getTileEntity( xCoord + d.offsetX, yCoord + d.offsetY, zCoord + d.offsetZ );
|
2014-01-20 17:41:37 +01:00
|
|
|
if ( te instanceof TileQuantumBridge )
|
|
|
|
set.add( d );
|
|
|
|
}
|
|
|
|
|
|
|
|
return set;
|
|
|
|
}
|
|
|
|
|
|
|
|
public boolean hasQES()
|
|
|
|
{
|
|
|
|
if ( xdex == -1 )
|
|
|
|
return false;
|
|
|
|
return (xdex & hasSingularity) == hasSingularity;
|
|
|
|
}
|
|
|
|
|
2013-12-27 23:59:59 +01:00
|
|
|
}
|