2018-02-21 15:40:08 +00:00
|
|
|
/*
|
|
|
|
* This file is part of ComputerCraft - http://www.computercraft.info
|
2018-12-27 10:10:44 +00:00
|
|
|
* Copyright Daniel Ratcliffe, 2011-2018. Do not distribute without permission.
|
2018-02-21 15:40:08 +00:00
|
|
|
* Send enquiries to dratcliffe@gmail.com
|
|
|
|
*/
|
|
|
|
|
2018-12-27 10:10:44 +00:00
|
|
|
package dan200.computercraft.shared.peripheral.modem.wired;
|
2018-02-21 15:40:08 +00:00
|
|
|
|
|
|
|
import com.google.common.base.Objects;
|
|
|
|
import dan200.computercraft.ComputerCraft;
|
|
|
|
import dan200.computercraft.api.network.wired.IWiredElement;
|
|
|
|
import dan200.computercraft.api.network.wired.IWiredNode;
|
|
|
|
import dan200.computercraft.api.peripheral.IPeripheral;
|
2018-11-29 12:21:14 +00:00
|
|
|
import dan200.computercraft.shared.command.CommandCopy;
|
2018-02-21 15:40:08 +00:00
|
|
|
import dan200.computercraft.shared.peripheral.common.TilePeripheralBase;
|
2018-12-27 10:10:44 +00:00
|
|
|
import dan200.computercraft.shared.peripheral.modem.ModemState;
|
2018-03-24 11:57:36 +00:00
|
|
|
import dan200.computercraft.shared.wired.CapabilityWiredElement;
|
2018-02-21 15:40:08 +00:00
|
|
|
import net.minecraft.entity.player.EntityPlayer;
|
|
|
|
import net.minecraft.nbt.NBTTagCompound;
|
|
|
|
import net.minecraft.util.EnumFacing;
|
|
|
|
import net.minecraft.util.math.AxisAlignedBB;
|
|
|
|
import net.minecraft.util.math.BlockPos;
|
|
|
|
import net.minecraft.util.math.Vec3d;
|
2018-11-29 12:21:14 +00:00
|
|
|
import net.minecraft.util.text.TextComponentString;
|
2018-02-21 15:40:08 +00:00
|
|
|
import net.minecraft.util.text.TextComponentTranslation;
|
|
|
|
import net.minecraft.world.World;
|
2018-03-24 11:57:36 +00:00
|
|
|
import net.minecraftforge.common.capabilities.Capability;
|
2018-02-21 15:40:08 +00:00
|
|
|
|
|
|
|
import javax.annotation.Nonnull;
|
2018-03-24 11:57:36 +00:00
|
|
|
import javax.annotation.Nullable;
|
2018-02-21 15:40:08 +00:00
|
|
|
import java.util.*;
|
|
|
|
|
2018-03-24 11:57:36 +00:00
|
|
|
public class TileWiredModemFull extends TilePeripheralBase
|
2018-02-21 15:40:08 +00:00
|
|
|
{
|
|
|
|
private static class FullElement extends WiredModemElement
|
|
|
|
{
|
|
|
|
private final TileWiredModemFull m_entity;
|
|
|
|
|
|
|
|
private FullElement( TileWiredModemFull m_entity )
|
|
|
|
{
|
|
|
|
this.m_entity = m_entity;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void attachPeripheral( String name, IPeripheral peripheral )
|
|
|
|
{
|
|
|
|
for( int i = 0; i < 6; i++ )
|
|
|
|
{
|
|
|
|
WiredModemPeripheral modem = m_entity.m_modems[i];
|
2018-04-07 09:23:11 +00:00
|
|
|
if( modem != null ) modem.attachPeripheral( name, peripheral );
|
2018-02-21 15:40:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void detachPeripheral( String name )
|
|
|
|
{
|
|
|
|
for( int i = 0; i < 6; i++ )
|
|
|
|
{
|
|
|
|
WiredModemPeripheral modem = m_entity.m_modems[i];
|
|
|
|
if( modem != null ) modem.detachPeripheral( name );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Nonnull
|
|
|
|
@Override
|
|
|
|
public World getWorld()
|
|
|
|
{
|
|
|
|
return m_entity.getWorld();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Nonnull
|
|
|
|
@Override
|
|
|
|
public Vec3d getPosition()
|
|
|
|
{
|
|
|
|
BlockPos pos = m_entity.getPos();
|
|
|
|
return new Vec3d( pos.getX() + 0.5, pos.getY() + 0.5, pos.getZ() + 0.5 );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private WiredModemPeripheral[] m_modems = new WiredModemPeripheral[6];
|
|
|
|
|
|
|
|
private boolean m_peripheralAccessAllowed = false;
|
2018-04-16 17:22:28 +00:00
|
|
|
private WiredModemLocalPeripheral[] m_peripherals = new WiredModemLocalPeripheral[6];
|
2018-02-21 15:40:08 +00:00
|
|
|
|
|
|
|
private boolean m_destroyed = false;
|
|
|
|
private boolean m_connectionsFormed = false;
|
|
|
|
|
2018-11-17 11:52:12 +00:00
|
|
|
private final ModemState m_modemState = new ModemState();
|
2018-02-21 15:40:08 +00:00
|
|
|
private final WiredModemElement m_element = new FullElement( this );
|
2018-04-16 16:10:08 +00:00
|
|
|
private final IWiredNode m_node = m_element.getNode();
|
2018-02-21 15:40:08 +00:00
|
|
|
|
|
|
|
public TileWiredModemFull()
|
|
|
|
{
|
2018-04-16 17:22:28 +00:00
|
|
|
for( int i = 0; i < m_peripherals.length; i++ ) m_peripherals[i] = new WiredModemLocalPeripheral();
|
2018-02-21 15:40:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private void remove()
|
|
|
|
{
|
|
|
|
if( world == null || !world.isRemote )
|
|
|
|
{
|
2018-04-16 16:10:08 +00:00
|
|
|
m_node.remove();
|
2018-02-21 15:40:08 +00:00
|
|
|
m_connectionsFormed = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void destroy()
|
|
|
|
{
|
|
|
|
if( !m_destroyed )
|
|
|
|
{
|
|
|
|
m_destroyed = true;
|
|
|
|
remove();
|
|
|
|
}
|
|
|
|
super.destroy();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onChunkUnload()
|
|
|
|
{
|
|
|
|
super.onChunkUnload();
|
|
|
|
remove();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void invalidate()
|
|
|
|
{
|
|
|
|
super.invalidate();
|
|
|
|
remove();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public EnumFacing getDirection()
|
|
|
|
{
|
|
|
|
return EnumFacing.NORTH;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void setDirection( EnumFacing dir )
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onNeighbourChange()
|
|
|
|
{
|
|
|
|
if( !world.isRemote && m_peripheralAccessAllowed )
|
|
|
|
{
|
2018-04-16 17:22:28 +00:00
|
|
|
boolean hasChanged = false;
|
|
|
|
for( EnumFacing facing : EnumFacing.VALUES )
|
2018-02-21 15:40:08 +00:00
|
|
|
{
|
2018-04-16 17:22:28 +00:00
|
|
|
hasChanged |= m_peripherals[facing.ordinal()].attach( world, getPos(), facing );
|
2018-02-21 15:40:08 +00:00
|
|
|
}
|
|
|
|
|
2018-04-16 17:22:28 +00:00
|
|
|
if( hasChanged ) updateConnectedPeripherals();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onNeighbourTileEntityChange( @Nonnull BlockPos neighbour )
|
|
|
|
{
|
|
|
|
if( !world.isRemote && m_peripheralAccessAllowed )
|
|
|
|
{
|
|
|
|
for( EnumFacing facing : EnumFacing.VALUES )
|
|
|
|
{
|
|
|
|
if( getPos().offset( facing ).equals( neighbour ) )
|
|
|
|
{
|
|
|
|
WiredModemLocalPeripheral peripheral = m_peripherals[facing.ordinal()];
|
|
|
|
if( peripheral.attach( world, getPos(), facing ) ) updateConnectedPeripherals();
|
|
|
|
}
|
|
|
|
}
|
2018-02-21 15:40:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Nonnull
|
|
|
|
@Override
|
|
|
|
public AxisAlignedBB getBounds()
|
|
|
|
{
|
|
|
|
return BlockCable.FULL_BLOCK_AABB;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean onActivate( EntityPlayer player, EnumFacing side, float hitX, float hitY, float hitZ )
|
|
|
|
{
|
|
|
|
if( !getWorld().isRemote )
|
|
|
|
{
|
|
|
|
// On server, we interacted if a peripheral was found
|
2018-11-29 12:21:14 +00:00
|
|
|
Set<String> oldPeriphNames = getConnectedPeripheralNames();
|
2018-02-21 15:40:08 +00:00
|
|
|
togglePeripheralAccess();
|
2018-11-29 12:21:14 +00:00
|
|
|
Set<String> periphNames = getConnectedPeripheralNames();
|
2018-02-21 15:40:08 +00:00
|
|
|
|
2018-11-29 12:21:14 +00:00
|
|
|
if( !Objects.equal( periphNames, oldPeriphNames ) )
|
2018-02-21 15:40:08 +00:00
|
|
|
{
|
2018-11-29 12:21:14 +00:00
|
|
|
sendPeripheralChanges( player, "gui.computercraft:wired_modem.peripheral_disconnected", oldPeriphNames );
|
|
|
|
sendPeripheralChanges( player, "gui.computercraft:wired_modem.peripheral_connected", periphNames );
|
2018-02-21 15:40:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// On client, we can't know this, so we assume so to be safe
|
|
|
|
// The server will correct us if we're wrong
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-29 12:21:14 +00:00
|
|
|
private static void sendPeripheralChanges( EntityPlayer player, String kind, Collection<String> peripherals )
|
|
|
|
{
|
|
|
|
if( peripherals.isEmpty() ) return;
|
|
|
|
|
|
|
|
List<String> names = new ArrayList<>( peripherals );
|
|
|
|
names.sort( Comparator.naturalOrder() );
|
|
|
|
|
|
|
|
TextComponentString base = new TextComponentString( "" );
|
|
|
|
for( int i = 0; i < names.size(); i++ )
|
|
|
|
{
|
|
|
|
if( i > 0 ) base.appendText( ", " );
|
|
|
|
base.appendSibling( CommandCopy.createCopyText( names.get( i ) ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
player.sendMessage( new TextComponentTranslation( kind, base ) );
|
|
|
|
}
|
|
|
|
|
2018-02-21 15:40:08 +00:00
|
|
|
@Override
|
|
|
|
public void readFromNBT( NBTTagCompound tag )
|
|
|
|
{
|
|
|
|
super.readFromNBT( tag );
|
|
|
|
m_peripheralAccessAllowed = tag.getBoolean( "peripheralAccess" );
|
2018-04-16 17:22:28 +00:00
|
|
|
for( int i = 0; i < m_peripherals.length; i++ ) m_peripherals[i].readNBT( tag, "_" + i );
|
2018-02-21 15:40:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Nonnull
|
|
|
|
@Override
|
|
|
|
public NBTTagCompound writeToNBT( NBTTagCompound tag )
|
|
|
|
{
|
|
|
|
tag = super.writeToNBT( tag );
|
|
|
|
tag.setBoolean( "peripheralAccess", m_peripheralAccessAllowed );
|
2018-04-16 17:22:28 +00:00
|
|
|
for( int i = 0; i < m_peripherals.length; i++ ) m_peripherals[i].writeNBT( tag, "_" + i );
|
2018-02-21 15:40:08 +00:00
|
|
|
return tag;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected void updateAnim()
|
|
|
|
{
|
|
|
|
int anim = 0;
|
2018-11-17 11:52:12 +00:00
|
|
|
if( m_modemState.isOpen() ) anim |= 1;
|
|
|
|
if( m_peripheralAccessAllowed ) anim |= 2;
|
2018-02-21 15:40:08 +00:00
|
|
|
setAnim( anim );
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public final void readDescription( @Nonnull NBTTagCompound tag )
|
|
|
|
{
|
|
|
|
super.readDescription( tag );
|
|
|
|
updateBlock();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void update()
|
|
|
|
{
|
|
|
|
if( !getWorld().isRemote )
|
|
|
|
{
|
2018-11-17 11:52:12 +00:00
|
|
|
if( m_modemState.pollChanged() ) updateAnim();
|
2018-02-21 15:40:08 +00:00
|
|
|
|
|
|
|
if( !m_connectionsFormed )
|
|
|
|
{
|
|
|
|
m_connectionsFormed = true;
|
2018-04-16 17:22:28 +00:00
|
|
|
|
2018-04-16 16:10:08 +00:00
|
|
|
connectionsChanged();
|
2018-04-16 17:22:28 +00:00
|
|
|
if( m_peripheralAccessAllowed )
|
|
|
|
{
|
|
|
|
for( EnumFacing facing : EnumFacing.VALUES )
|
|
|
|
{
|
|
|
|
m_peripherals[facing.ordinal()].attach( world, getPos(), facing );
|
|
|
|
}
|
|
|
|
updateConnectedPeripherals();
|
|
|
|
}
|
2018-02-21 15:40:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
super.update();
|
|
|
|
}
|
|
|
|
|
2018-04-16 14:47:32 +00:00
|
|
|
private void connectionsChanged()
|
2018-02-21 15:40:08 +00:00
|
|
|
{
|
|
|
|
if( getWorld().isRemote ) return;
|
|
|
|
|
|
|
|
World world = getWorld();
|
|
|
|
BlockPos current = getPos();
|
|
|
|
for( EnumFacing facing : EnumFacing.VALUES )
|
|
|
|
{
|
2018-03-24 11:57:36 +00:00
|
|
|
BlockPos offset = current.offset( facing );
|
|
|
|
if( !world.isBlockLoaded( offset ) ) continue;
|
2018-02-21 15:40:08 +00:00
|
|
|
|
2018-03-24 11:57:36 +00:00
|
|
|
IWiredElement element = ComputerCraft.getWiredElementAt( world, offset, facing.getOpposite() );
|
2018-02-21 15:40:08 +00:00
|
|
|
if( element == null ) continue;
|
|
|
|
|
|
|
|
// If we can connect to it then do so
|
2018-04-16 16:10:08 +00:00
|
|
|
m_node.connectTo( element.getNode() );
|
2018-02-21 15:40:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// private stuff
|
|
|
|
private void togglePeripheralAccess()
|
|
|
|
{
|
|
|
|
if( !m_peripheralAccessAllowed )
|
|
|
|
{
|
2018-04-16 17:22:28 +00:00
|
|
|
boolean hasAny = false;
|
|
|
|
for( EnumFacing facing : EnumFacing.VALUES )
|
2018-02-21 15:40:08 +00:00
|
|
|
{
|
2018-04-16 17:22:28 +00:00
|
|
|
WiredModemLocalPeripheral peripheral = m_peripherals[facing.ordinal()];
|
|
|
|
peripheral.attach( world, getPos(), facing );
|
|
|
|
hasAny |= peripheral.hasPeripheral();
|
2018-02-21 15:40:08 +00:00
|
|
|
}
|
2018-04-16 17:22:28 +00:00
|
|
|
|
|
|
|
if( !hasAny ) return;
|
|
|
|
|
|
|
|
m_peripheralAccessAllowed = true;
|
|
|
|
m_node.updatePeripherals( getConnectedPeripherals() );
|
2018-02-21 15:40:08 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
m_peripheralAccessAllowed = false;
|
2018-04-16 17:22:28 +00:00
|
|
|
|
|
|
|
for( WiredModemLocalPeripheral peripheral : m_peripherals ) peripheral.detach();
|
|
|
|
m_node.updatePeripherals( Collections.emptyMap() );
|
2018-02-21 15:40:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
updateAnim();
|
|
|
|
}
|
|
|
|
|
2018-04-16 17:22:28 +00:00
|
|
|
private Set<String> getConnectedPeripheralNames()
|
2018-02-21 15:40:08 +00:00
|
|
|
{
|
2018-04-16 17:22:28 +00:00
|
|
|
if( !m_peripheralAccessAllowed ) return Collections.emptySet();
|
2018-02-21 15:40:08 +00:00
|
|
|
|
2018-04-16 17:22:28 +00:00
|
|
|
Set<String> peripherals = new HashSet<>( 6 );
|
|
|
|
for( WiredModemLocalPeripheral m_peripheral : m_peripherals )
|
2018-02-21 15:40:08 +00:00
|
|
|
{
|
2018-04-16 17:22:28 +00:00
|
|
|
String name = m_peripheral.getConnectedName();
|
|
|
|
if( name != null ) peripherals.add( name );
|
2018-02-21 15:40:08 +00:00
|
|
|
}
|
2018-04-16 17:22:28 +00:00
|
|
|
return peripherals;
|
|
|
|
}
|
2018-02-21 15:40:08 +00:00
|
|
|
|
2018-04-16 17:22:28 +00:00
|
|
|
private Map<String, IPeripheral> getConnectedPeripherals()
|
|
|
|
{
|
|
|
|
if( !m_peripheralAccessAllowed ) return Collections.emptyMap();
|
|
|
|
|
|
|
|
Map<String, IPeripheral> peripherals = new HashMap<>( 6 );
|
|
|
|
for( WiredModemLocalPeripheral m_peripheral : m_peripherals ) m_peripheral.extendMap( peripherals );
|
2018-02-21 15:40:08 +00:00
|
|
|
return peripherals;
|
|
|
|
}
|
|
|
|
|
2018-04-16 17:22:28 +00:00
|
|
|
private void updateConnectedPeripherals()
|
2018-02-21 15:40:08 +00:00
|
|
|
{
|
2018-04-16 17:22:28 +00:00
|
|
|
Map<String, IPeripheral> peripherals = getConnectedPeripherals();
|
|
|
|
if( peripherals.isEmpty() )
|
|
|
|
{
|
|
|
|
// If there are no peripherals then disable access and update the display state.
|
|
|
|
m_peripheralAccessAllowed = false;
|
|
|
|
updateAnim();
|
|
|
|
}
|
2018-02-21 15:40:08 +00:00
|
|
|
|
2018-04-16 17:22:28 +00:00
|
|
|
m_node.updatePeripherals( peripherals );
|
2018-02-21 15:40:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// IWiredElementTile
|
|
|
|
|
|
|
|
@Override
|
2018-03-24 11:57:36 +00:00
|
|
|
public boolean hasCapability( @Nonnull Capability<?> capability, @Nullable EnumFacing facing )
|
|
|
|
{
|
|
|
|
return capability == CapabilityWiredElement.CAPABILITY || super.hasCapability( capability, facing );
|
|
|
|
}
|
|
|
|
|
|
|
|
@Nullable
|
|
|
|
@Override
|
|
|
|
public <T> T getCapability( @Nonnull Capability<T> capability, @Nullable EnumFacing facing )
|
2018-02-21 15:40:08 +00:00
|
|
|
{
|
2018-03-24 11:57:36 +00:00
|
|
|
if( capability == CapabilityWiredElement.CAPABILITY )
|
|
|
|
{
|
|
|
|
return CapabilityWiredElement.CAPABILITY.cast( m_element );
|
|
|
|
}
|
|
|
|
|
|
|
|
return super.getCapability( capability, facing );
|
2018-02-21 15:40:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// IPeripheralTile
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public IPeripheral getPeripheral( EnumFacing side )
|
|
|
|
{
|
|
|
|
WiredModemPeripheral peripheral = m_modems[side.ordinal()];
|
|
|
|
if( peripheral == null )
|
|
|
|
{
|
2018-06-29 19:29:04 +00:00
|
|
|
WiredModemLocalPeripheral localPeripheral = m_peripherals[side.ordinal()];
|
2018-11-17 11:52:12 +00:00
|
|
|
peripheral = m_modems[side.ordinal()] = new WiredModemPeripheral( m_modemState, m_element )
|
2018-02-21 15:40:08 +00:00
|
|
|
{
|
2018-06-29 19:29:04 +00:00
|
|
|
@Nonnull
|
2018-04-07 09:23:11 +00:00
|
|
|
@Override
|
2018-06-29 19:29:04 +00:00
|
|
|
protected WiredModemLocalPeripheral getLocalPeripheral()
|
2018-04-07 09:23:11 +00:00
|
|
|
{
|
2018-06-29 19:29:04 +00:00
|
|
|
return localPeripheral;
|
2018-04-07 09:23:11 +00:00
|
|
|
}
|
|
|
|
|
2018-02-21 15:40:08 +00:00
|
|
|
@Nonnull
|
|
|
|
@Override
|
|
|
|
public Vec3d getPosition()
|
|
|
|
{
|
|
|
|
BlockPos pos = getPos().offset( side );
|
|
|
|
return new Vec3d( pos.getX() + 0.5, pos.getY() + 0.5, pos.getZ() + 0.5 );
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
return peripheral;
|
|
|
|
}
|
|
|
|
}
|