Applied-Energistics-2-tiler.../tile/qnb/TileQuantumBridge.java

287 lines
5.9 KiB
Java
Raw Normal View History

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;
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;
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;
import appeng.me.cluster.IAECluster;
import appeng.me.cluster.IAEMultiBlock;
2014-01-20 17:41:37 +01:00
import appeng.me.cluster.implementations.QuantumCalculator;
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;
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
{
final private static ItemStack ring = AEApi.instance().blocks().blockQuantumRing.stack(1);
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 );
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 );
}
public IInventory getInternalInventory()
{
return inv;
}
2014-01-20 17:41:37 +01:00
@MENetworkEventSubscribe
public void PowerSwitch(MENetworkPowerStatusChange c)
{
updateStatus = true;
}
@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 );
}
@Override
public int[] getAccessibleSlotsBySide(ForgeDirection side)
{
if ( isCenter() )
return sidesLink;
return sidesRing;
}
@Override
public void disconnect(boolean affectWorld)
{
2014-01-20 17:41:37 +01:00
if ( clust != null )
{
if ( ! affectWorld )
clust.updateStatus = false;
2014-01-20 17:41:37 +01:00
clust.destroy();
}
2014-01-20 17:41:37 +01:00
clust = null;
if ( affectWorld )
gridProxy.setValidSides( EnumSet.noneOf( ForgeDirection.class ) );
}
@Override
public IAECluster getCluster()
{
2014-01-20 17:41:37 +01:00
return clust;
}
@Override
public boolean isValid()
{
2014-01-20 17:41:37 +01:00
return !isInvalid();
}
@Override
public void onReady()
{
super.onReady();
if ( worldObj.getBlock( xCoord, yCoord, zCoord) == AEApi.instance().blocks().blockQuantumRing.block() )
gridProxy.setVisualRepresentation( ring );
}
@Override
public void invalidate()
{
disconnect(false);
super.invalidate();
}
2014-01-20 17:41:37 +01:00
@Override
public void onChunkUnload()
{
disconnect(false);
super.onChunkUnload();
}
2014-01-20 17:41:37 +01:00
public void updateStatus(QuantumCluster c, byte flags, boolean affectWorld)
{
clust = c;
if ( affectWorld )
2014-01-20 17:41:37 +01: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
}
}
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" );
}
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;
}
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
}
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;
}
}