Applied-Energistics-2-tiler.../src/main/java/appeng/tile/qnb/TileQuantumBridge.java

349 lines
7.9 KiB
Java
Raw Normal View History

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>.
*/
package appeng.tile.qnb;
2015-06-16 02:44:59 +02:00
import java.util.EnumSet;
import java.util.Optional;
2015-12-24 02:07:03 +01:00
import io.netty.buffer.ByteBuf;
import net.minecraft.block.Block;
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;
2015-06-16 02:44:59 +02:00
import net.minecraft.util.EnumFacing;
import net.minecraft.util.ITickable;
2016-01-01 01:47:22 +01:00
import appeng.api.AEApi;
import appeng.api.definitions.IBlockDefinition;
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;
2015-06-16 02:44:59 +02:00
import appeng.api.util.AEPartLocation;
2014-01-20 17:41:37 +01:00
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-08-28 09:39:52 +02:00
import appeng.tile.TileEvent;
2014-01-20 17:41:37 +01:00
import appeng.tile.events.TileEventType;
import appeng.tile.grid.AENetworkInvTile;
import appeng.tile.inventory.AppEngInternalInventory;
import appeng.tile.inventory.InvOperation;
import appeng.util.Platform;
public class TileQuantumBridge extends AENetworkInvTile implements IAEMultiBlock, ITickable
{
private final byte corner = 16;
private final int[] sidesRing = {};
private final int[] sidesLink = { 0 };
private final AppEngInternalInventory internalInventory = new AppEngInternalInventory( this, 1 );
private final byte hasSingularity = 32;
private final byte powered = 64;
private final QuantumCalculator calc = new QuantumCalculator( this );
private byte constructed = -1;
private QuantumCluster cluster;
2014-01-20 17:41:37 +01:00
private boolean updateStatus = false;
public TileQuantumBridge()
{
this.getProxy().setValidSides( EnumSet.noneOf( EnumFacing.class ) );
this.getProxy().setFlags( GridFlags.DENSE_CAPACITY );
this.getProxy().setIdlePowerUsage( 22 );
this.internalInventory.setMaxStackSize( 1 );
}
@TileEvent( TileEventType.TICK )
public void onTickEvent()
2014-01-20 17:41:37 +01:00
{
if( this.updateStatus )
2014-01-20 17:41:37 +01:00
{
2014-12-29 15:13:47 +01:00
this.updateStatus = false;
if( this.cluster != null )
2015-04-29 02:30:53 +02:00
{
2014-12-29 15:13:47 +01:00
this.cluster.updateStatus( true );
2015-04-29 02:30:53 +02:00
}
2014-12-29 15:13:47 +01:00
this.markForUpdate();
2014-01-20 17:41:37 +01:00
}
2014-08-28 09:39:52 +02:00
}
2014-01-20 17:41:37 +01:00
@TileEvent( TileEventType.NETWORK_WRITE )
2015-09-30 14:24:40 +02:00
public void onNetworkWriteEvent( final ByteBuf data )
2014-08-28 09:39:52 +02:00
{
2014-12-29 15:13:47 +01:00
int out = this.constructed;
2014-01-20 17:41:37 +01:00
if( this.getStackInSlot( 0 ) != null && this.constructed != -1 )
2015-04-29 02:30:53 +02:00
{
out |= this.hasSingularity;
2015-04-29 02:30:53 +02:00
}
2014-01-20 17:41:37 +01:00
if( this.getProxy().isActive() && this.constructed != -1 )
2015-04-29 02:30:53 +02:00
{
out |= this.powered;
2015-04-29 02:30:53 +02:00
}
2014-01-20 17:41:37 +01:00
2014-08-28 09:39:52 +02:00
data.writeByte( (byte) out );
}
2014-01-20 17:41:37 +01:00
@TileEvent( TileEventType.NETWORK_READ )
2015-09-30 14:24:40 +02:00
public boolean onNetworkReadEvent( final ByteBuf data )
2014-08-28 09:39:52 +02:00
{
2015-09-30 14:24:40 +02:00
final int oldValue = this.constructed;
2014-12-29 15:13:47 +01:00
this.constructed = data.readByte();
return this.constructed != oldValue;
2014-08-28 09:39:52 +02:00
}
2014-01-20 17:41:37 +01:00
@Override
public IInventory getInternalInventory()
{
return this.internalInventory;
}
@Override
2015-09-30 14:24:40 +02:00
public void onChangeInventory( final IInventory inv, final int slot, final InvOperation mc, final ItemStack removed, final ItemStack added )
{
if( this.cluster != null )
2015-04-29 02:30:53 +02:00
{
this.cluster.updateStatus( true );
2015-04-29 02:30:53 +02:00
}
}
@Override
2015-09-30 14:24:40 +02:00
public int[] getAccessibleSlotsBySide( final EnumFacing side )
{
if( this.isCenter() )
2015-04-29 02:30:53 +02:00
{
return this.sidesLink;
2015-04-29 02:30:53 +02:00
}
return this.sidesRing;
}
private boolean isCenter()
{
return AEApi.instance().definitions().blocks().quantumLink().maybeBlock()
.map( link -> getBlockType() == link )
.orElse( false );
}
2014-01-20 17:41:37 +01:00
@MENetworkEventSubscribe
2015-09-30 14:24:40 +02:00
public void onPowerStatusChange( final MENetworkPowerStatusChange c )
2014-01-20 17:41:37 +01:00
{
2014-12-29 15:13:47 +01:00
this.updateStatus = true;
2014-01-20 17:41:37 +01:00
}
@Override
public void onChunkUnload()
{
this.disconnect( false );
super.onChunkUnload();
}
@Override
public void onReady()
{
super.onReady();
final IBlockDefinition quantumRing = AEApi.instance().definitions().blocks().quantumRing();
final Optional<Block> maybeLinkBlock = quantumRing.maybeBlock();
final Optional<ItemStack> maybeLinkStack = quantumRing.maybeStack( 1 );
final boolean isPresent = maybeLinkBlock.isPresent() && maybeLinkStack.isPresent();
if( isPresent && this.getBlockType() == maybeLinkBlock.get() )
{
final ItemStack linkStack = maybeLinkStack.get();
this.getProxy().setVisualRepresentation( linkStack );
}
}
@Override
public void invalidate()
{
this.disconnect( false );
super.invalidate();
}
@Override
2015-09-30 14:24:40 +02:00
public void disconnect( final boolean affectWorld )
{
if( this.cluster != null )
{
if( !affectWorld )
2015-04-29 02:30:53 +02:00
{
this.cluster.setUpdateStatus( false );
2015-04-29 02:30:53 +02:00
}
2014-12-29 15:13:47 +01:00
this.cluster.destroy();
}
2014-12-29 15:13:47 +01:00
this.cluster = null;
if( affectWorld )
2015-04-29 02:30:53 +02:00
{
this.getProxy().setValidSides( EnumSet.noneOf( EnumFacing.class ) );
2015-04-29 02:30:53 +02:00
}
}
@Override
public IAECluster getCluster()
{
2014-12-29 15:13:47 +01:00
return this.cluster;
}
@Override
public boolean isValid()
{
2014-12-29 15:13:47 +01:00
return !this.isInvalid();
}
2015-09-30 14:24:40 +02:00
public void updateStatus( final QuantumCluster c, final byte flags, final boolean affectWorld )
{
2014-12-29 15:13:47 +01:00
this.cluster = c;
if( affectWorld )
2014-01-20 17:41:37 +01:00
{
if( this.constructed != flags )
{
2014-12-29 15:13:47 +01:00
this.constructed = flags;
this.markForUpdate();
}
if( this.isCorner() || this.isCenter() )
{
2015-09-30 14:24:40 +02:00
final EnumSet<EnumFacing> sides = EnumSet.noneOf( EnumFacing.class );
for( final EnumFacing dir : this.getAdjacentQuantumBridges() )
2015-12-24 02:11:17 +01:00
{
sides.add( dir );
2015-12-24 02:11:17 +01:00
}
this.getProxy().setValidSides( sides );
}
else
2015-04-29 02:30:53 +02:00
{
this.getProxy().setValidSides( EnumSet.allOf( EnumFacing.class ) );
2015-04-29 02:30:53 +02:00
}
2014-01-20 17:41:37 +01:00
}
}
public boolean isCorner()
{
return ( this.constructed & this.getCorner() ) == this.getCorner() && this.constructed != -1;
}
public EnumSet<EnumFacing> getAdjacentQuantumBridges()
{
final EnumSet<EnumFacing> set = EnumSet.noneOf( EnumFacing.class );
for( final EnumFacing d : EnumFacing.values() )
{
final TileEntity te = this.worldObj.getTileEntity( this.pos.offset( d ) );
if( te instanceof TileQuantumBridge )
2015-04-29 02:30:53 +02:00
{
set.add( d );
2015-04-29 02:30:53 +02:00
}
}
return set;
}
2014-11-04 12:32:33 +01:00
public long getQEFrequency()
{
2015-09-30 14:24:40 +02:00
final ItemStack is = this.internalInventory.getStackInSlot( 0 );
if( is != null )
2014-01-20 17:41:37 +01:00
{
2015-09-30 14:24:40 +02:00
final NBTTagCompound c = is.getTagCompound();
if( c != null )
2015-04-29 02:30:53 +02:00
{
2014-01-20 17:41:37 +01:00
return c.getLong( "freq" );
2015-04-29 02:30:53 +02:00
}
2014-01-20 17:41:37 +01:00
}
return 0;
}
public boolean isPowered()
{
if( Platform.isClient() )
2015-04-29 02:30:53 +02:00
{
return ( this.constructed & this.powered ) == this.powered && this.constructed != -1;
2015-04-29 02:30:53 +02:00
}
2014-01-20 17:41:37 +01:00
try
{
return this.getProxy().getEnergy().isNetworkPowered();
2014-01-20 17:41:37 +01:00
}
2015-09-30 14:24:40 +02:00
catch( final GridAccessException e )
2014-01-20 17:41:37 +01:00
{
// :P
}
return false;
}
public boolean isFormed()
{
2014-12-29 15:13:47 +01:00
return this.constructed != -1;
}
2014-01-20 17:41:37 +01:00
@Override
2015-09-30 14:24:40 +02:00
public AECableType getCableConnectionType( final AEPartLocation dir )
2014-01-20 17:41:37 +01:00
{
return AECableType.DENSE;
}
public void neighborUpdate()
{
2014-12-29 15:13:47 +01:00
this.calc.calculateMultiblock( this.worldObj, this.getLocation() );
2014-01-20 17:41:37 +01:00
}
@Override
public DimensionalCoord getLocation()
2014-01-20 17:41:37 +01:00
{
return new DimensionalCoord( this );
2014-01-20 17:41:37 +01:00
}
public boolean hasQES()
{
if( this.constructed == -1 )
2015-04-29 02:30:53 +02:00
{
2014-01-20 17:41:37 +01:00
return false;
2015-04-29 02:30:53 +02:00
}
return ( this.constructed & this.hasSingularity ) == this.hasSingularity;
2014-01-20 17:41:37 +01:00
}
public void breakCluster()
{
if( this.cluster != null )
2015-04-29 02:30:53 +02:00
{
2014-12-29 15:13:47 +01:00
this.cluster.destroy();
2015-04-29 02:30:53 +02:00
}
}
public byte getCorner()
{
return this.corner;
}
}