Skip to content

Commit

Permalink
Modem peripherals toggling
Browse files Browse the repository at this point in the history
  • Loading branch information
neumond committed Aug 10, 2020
1 parent cd879b0 commit 221e249
Show file tree
Hide file tree
Showing 4 changed files with 102 additions and 70 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,25 @@ protected void detachPeripheral( String name )
{
m_modem.detachPeripheral( name );
}

protected boolean enableSharing()
{
m_peripheral.attach( world, getPos(), getDirection() );
if( !m_peripheral.hasPeripheral() ) return false;

m_node.updatePeripherals( m_peripheral.toMap() );
updateBlockState();
return true;
}

protected void disableSharing()
{
m_peripheral.detach();
m_node.updatePeripherals( Collections.emptyMap() );
updateBlockState();
}
}

private boolean m_peripheralAccessAllowed;
private final WiredModemLocalPeripheral m_peripheral = new WiredModemLocalPeripheral( this::refreshPeripheral );

private boolean m_destroyed = false;
Expand Down Expand Up @@ -236,7 +252,7 @@ public void onNeighbourChange( @Nonnull BlockPos neighbour )
public void onNeighbourTileEntityChange( @Nonnull BlockPos neighbour )
{
super.onNeighbourTileEntityChange( neighbour );
if( !world.isRemote && m_peripheralAccessAllowed )
if( !world.isRemote && m_cable.isSharingPeripherals() )
{
Direction facing = getDirection();
if( getPos().offset( facing ).equals( neighbour ) ) refreshPeripheral();
Expand All @@ -261,7 +277,7 @@ public ActionResultType onActivate( PlayerEntity player, Hand hand, BlockRayTrac
if( getWorld().isRemote ) return ActionResultType.SUCCESS;

String oldName = m_peripheral.getConnectedName();
togglePeripheralAccess();
m_cable.setSharingPeripherals( !m_cable.isSharingPeripherals(), true );
String newName = m_peripheral.getConnectedName();
if( !Objects.equal( newName, oldName ) )
{
Expand All @@ -284,15 +300,15 @@ public ActionResultType onActivate( PlayerEntity player, Hand hand, BlockRayTrac
public void read( @Nonnull CompoundNBT nbt )
{
super.read( nbt );
m_peripheralAccessAllowed = nbt.getBoolean( NBT_PERIPHERAL_ENABLED );
m_cable.setSharingPeripherals( nbt.getBoolean( NBT_PERIPHERAL_ENABLED ), false );
m_peripheral.read( nbt, "" );
}

@Nonnull
@Override
public CompoundNBT write( CompoundNBT nbt )
{
nbt.putBoolean( NBT_PERIPHERAL_ENABLED, m_peripheralAccessAllowed );
nbt.putBoolean( NBT_PERIPHERAL_ENABLED, m_cable.isSharingPeripherals() );
m_peripheral.write( nbt, "" );
return super.write( nbt );
}
Expand All @@ -302,7 +318,7 @@ private void updateBlockState()
BlockState state = getBlockState();
CableModemVariant oldVariant = state.get( BlockCable.MODEM );
CableModemVariant newVariant = CableModemVariant
.from( oldVariant.getFacing(), m_modem.getModemState().isOpen(), m_peripheralAccessAllowed );
.from( oldVariant.getFacing(), m_modem.getModemState().isOpen(), m_cable.isSharingPeripherals() );

if( oldVariant != newVariant )
{
Expand Down Expand Up @@ -331,7 +347,7 @@ public void blockTick()
m_connectionsFormed = true;

connectionsChanged();
if( m_peripheralAccessAllowed )
if( m_cable.isSharingPeripherals() )
{
m_peripheral.attach( world, pos, modemDirection );
updateConnectedPeripherals();
Expand Down Expand Up @@ -378,44 +394,23 @@ void modemChanged()

// If we can no longer attach peripherals, then detach any
// which may have existed
if( !canAttachPeripheral() && m_peripheralAccessAllowed )
if( !canAttachPeripheral() && m_cable.isSharingPeripherals() )
{
m_peripheralAccessAllowed = false;
m_cable.setSharingPeripherals( false, false );
m_peripheral.detach();
m_node.updatePeripherals( Collections.emptyMap() );
markDirty();
updateBlockState();
}
}

private void togglePeripheralAccess()
{
if( !m_peripheralAccessAllowed )
{
m_peripheral.attach( world, getPos(), getDirection() );
if( !m_peripheral.hasPeripheral() ) return;

m_peripheralAccessAllowed = true;
m_node.updatePeripherals( m_peripheral.toMap() );
}
else
{
m_peripheral.detach();

m_peripheralAccessAllowed = false;
m_node.updatePeripherals( Collections.emptyMap() );
}

updateBlockState();
}

private void updateConnectedPeripherals()
{
Map<String, IPeripheral> peripherals = m_peripheral.toMap();
if( peripherals.isEmpty() )
{
// If there are no peripherals then disable access and update the display state.
m_peripheralAccessAllowed = false;
m_cable.setSharingPeripherals( false, false );
updateBlockState();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,12 +90,38 @@ public Vec3d getPosition()
BlockPos pos = m_entity.getPos();
return new Vec3d( pos.getX() + 0.5, pos.getY() + 0.5, pos.getZ() + 0.5 );
}

protected boolean enableSharing()
{
boolean hasAny = false;
for( Direction facing : DirectionUtil.FACINGS )
{
WiredModemLocalPeripheral peripheral = m_entity.m_peripherals[facing.ordinal()];
peripheral.attach( m_entity.world, m_entity.getPos(), facing );
hasAny |= peripheral.hasPeripheral();
}

if( !hasAny ) return false;

m_entity.m_node.updatePeripherals( m_entity.getConnectedPeripherals() );

m_entity.updateBlockState();

return true;
}

protected void disableSharing()
{
for( WiredModemLocalPeripheral peripheral : m_entity.m_peripherals ) peripheral.detach();
m_entity.m_node.updatePeripherals( Collections.emptyMap() );

m_entity.updateBlockState();
}
}

private final WiredModemPeripheral[] modems = new WiredModemPeripheral[6];
private final SidedCaps<IPeripheral> modemCaps = SidedCaps.ofNonNull( this::getPeripheral );

private boolean m_peripheralAccessAllowed = false;
private final WiredModemLocalPeripheral[] m_peripherals = new WiredModemLocalPeripheral[6];

private boolean m_destroyed = false;
Expand Down Expand Up @@ -169,7 +195,7 @@ public void onNeighbourChange( @Nonnull BlockPos neighbour )
@Override
public void onNeighbourTileEntityChange( @Nonnull BlockPos neighbour )
{
if( !world.isRemote && m_peripheralAccessAllowed )
if( !world.isRemote && m_element.isSharingPeripherals() )
{
for( Direction facing : DirectionUtil.FACINGS )
{
Expand All @@ -195,7 +221,7 @@ public ActionResultType onActivate( PlayerEntity player, Hand hand, BlockRayTrac

// On server, we interacted if a peripheral was found
Set<String> oldPeriphNames = getConnectedPeripheralNames();
togglePeripheralAccess();
m_element.setSharingPeripherals( !m_element.isSharingPeripherals(), true );
Set<String> periphNames = getConnectedPeripheralNames();

if( !Objects.equal( periphNames, oldPeriphNames ) )
Expand Down Expand Up @@ -228,23 +254,23 @@ private static void sendPeripheralChanges( PlayerEntity player, String kind, Col
public void read( @Nonnull CompoundNBT nbt )
{
super.read( nbt );
m_peripheralAccessAllowed = nbt.getBoolean( NBT_PERIPHERAL_ENABLED );
m_element.setSharingPeripherals( nbt.getBoolean( NBT_PERIPHERAL_ENABLED ), false );
for( int i = 0; i < m_peripherals.length; i++ ) m_peripherals[i].read( nbt, Integer.toString( i ) );
}

@Nonnull
@Override
public CompoundNBT write( CompoundNBT nbt )
{
nbt.putBoolean( NBT_PERIPHERAL_ENABLED, m_peripheralAccessAllowed );
nbt.putBoolean( NBT_PERIPHERAL_ENABLED, m_element.isSharingPeripherals() );
for( int i = 0; i < m_peripherals.length; i++ ) m_peripherals[i].write( nbt, Integer.toString( i ) );
return super.write( nbt );
}

private void updateBlockState()
{
BlockState state = getBlockState();
boolean modemOn = m_modemState.isOpen(), peripheralOn = m_peripheralAccessAllowed;
boolean modemOn = m_modemState.isOpen(), peripheralOn = m_element.isSharingPeripherals();
if( state.get( MODEM_ON ) == modemOn && state.get( PERIPHERAL_ON ) == peripheralOn ) return;

getWorld().setBlockState( getPos(), state.with( MODEM_ON, modemOn ).with( PERIPHERAL_ON, peripheralOn ) );
Expand All @@ -269,7 +295,7 @@ public void blockTick()
m_connectionsFormed = true;

connectionsChanged();
if( m_peripheralAccessAllowed )
if( m_element.isSharingPeripherals() )
{
for( Direction facing : DirectionUtil.FACINGS )
{
Expand Down Expand Up @@ -299,37 +325,9 @@ private void connectionsChanged()
}
}

private void togglePeripheralAccess()
{
if( !m_peripheralAccessAllowed )
{
boolean hasAny = false;
for( Direction facing : DirectionUtil.FACINGS )
{
WiredModemLocalPeripheral peripheral = m_peripherals[facing.ordinal()];
peripheral.attach( world, getPos(), facing );
hasAny |= peripheral.hasPeripheral();
}

if( !hasAny ) return;

m_peripheralAccessAllowed = true;
m_node.updatePeripherals( getConnectedPeripherals() );
}
else
{
m_peripheralAccessAllowed = false;

for( WiredModemLocalPeripheral peripheral : m_peripherals ) peripheral.detach();
m_node.updatePeripherals( Collections.emptyMap() );
}

updateBlockState();
}

private Set<String> getConnectedPeripheralNames()
{
if( !m_peripheralAccessAllowed ) return Collections.emptySet();
if( !m_element.isSharingPeripherals() ) return Collections.emptySet();

Set<String> peripherals = new HashSet<>( 6 );
for( WiredModemLocalPeripheral peripheral : m_peripherals )
Expand All @@ -342,7 +340,7 @@ private Set<String> getConnectedPeripheralNames()

private Map<String, IPeripheral> getConnectedPeripherals()
{
if( !m_peripheralAccessAllowed ) return Collections.emptyMap();
if( !m_element.isSharingPeripherals() ) return Collections.emptyMap();

Map<String, IPeripheral> peripherals = new HashMap<>( 6 );
for( WiredModemLocalPeripheral peripheral : m_peripherals ) peripheral.extendMap( peripherals );
Expand All @@ -355,7 +353,7 @@ private void updateConnectedPeripherals()
if( peripherals.isEmpty() )
{
// If there are no peripherals then disable access and update the display state.
m_peripheralAccessAllowed = false;
m_element.setSharingPeripherals( false, false );
updateBlockState();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ public abstract class WiredModemElement implements IWiredElement
{
private final IWiredNode node = new WiredNode( this );
private final Map<String, IPeripheral> remotePeripherals = new HashMap<>();
private boolean sharingPeripherals = false;

@Nonnull
@Override
Expand Down Expand Up @@ -61,4 +62,31 @@ public Map<String, IPeripheral> getRemotePeripherals()
protected abstract void attachPeripheral( String name, IPeripheral peripheral );

protected abstract void detachPeripheral( String name );

public boolean isSharingPeripherals()
{
return sharingPeripherals;
}

public boolean setSharingPeripherals( boolean enabled, boolean updateTile )
{
if( sharingPeripherals == enabled ) return sharingPeripherals;
sharingPeripherals = enabled;
if( updateTile )
{
if( enabled )
{
sharingPeripherals = enableSharing();
}
else
{
disableSharing();
}
}
return sharingPeripherals;
}

protected abstract boolean enableSharing();

protected abstract void disableSharing();
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@
public abstract class WiredModemPeripheral extends ModemPeripheral implements IWiredSender
{
private final WiredModemElement modem;

private final Map<IComputerAccess, ConcurrentMap<String, RemotePeripheralWrapper>> peripheralWrappers = new HashMap<>( 1 );

public WiredModemPeripheral( ModemState state, WiredModemElement modem )
Expand Down Expand Up @@ -195,6 +194,18 @@ public final Object[] getNameLocal()
return local == null ? null : new Object[] { local };
}

@LuaFunction
public final boolean isSharingPeripherals()
{
return modem.isSharingPeripherals();
}

@LuaFunction( mainThread = true )
public final boolean setSharingPeripherals( boolean enabled )
{
return modem.setSharingPeripherals( enabled, true );
}

@Override
public void attach( @Nonnull IComputerAccess computer )
{
Expand Down

0 comments on commit 221e249

Please sign in to comment.