From 221e249aa6f290ed7c908ce0fe95af1af0ea9b2b Mon Sep 17 00:00:00 2001 From: neumond Date: Mon, 27 Jul 2020 14:07:50 +0300 Subject: [PATCH] Modem peripherals toggling --- .../peripheral/modem/wired/TileCable.java | 57 +++++++------- .../modem/wired/TileWiredModemFull.java | 74 +++++++++---------- .../modem/wired/WiredModemElement.java | 28 +++++++ .../modem/wired/WiredModemPeripheral.java | 13 +++- 4 files changed, 102 insertions(+), 70 deletions(-) diff --git a/src/main/java/dan200/computercraft/shared/peripheral/modem/wired/TileCable.java b/src/main/java/dan200/computercraft/shared/peripheral/modem/wired/TileCable.java index 7d548ec5ad..fbdb821b10 100644 --- a/src/main/java/dan200/computercraft/shared/peripheral/modem/wired/TileCable.java +++ b/src/main/java/dan200/computercraft/shared/peripheral/modem/wired/TileCable.java @@ -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; @@ -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(); @@ -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 ) ) { @@ -284,7 +300,7 @@ 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, "" ); } @@ -292,7 +308,7 @@ public void read( @Nonnull CompoundNBT nbt ) @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 ); } @@ -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 ) { @@ -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(); @@ -378,9 +394,9 @@ 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(); @@ -388,34 +404,13 @@ void modemChanged() } } - 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 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(); } diff --git a/src/main/java/dan200/computercraft/shared/peripheral/modem/wired/TileWiredModemFull.java b/src/main/java/dan200/computercraft/shared/peripheral/modem/wired/TileWiredModemFull.java index 349cb6d289..715e72921d 100644 --- a/src/main/java/dan200/computercraft/shared/peripheral/modem/wired/TileWiredModemFull.java +++ b/src/main/java/dan200/computercraft/shared/peripheral/modem/wired/TileWiredModemFull.java @@ -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 modemCaps = SidedCaps.ofNonNull( this::getPeripheral ); - private boolean m_peripheralAccessAllowed = false; private final WiredModemLocalPeripheral[] m_peripherals = new WiredModemLocalPeripheral[6]; private boolean m_destroyed = false; @@ -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 ) { @@ -195,7 +221,7 @@ public ActionResultType onActivate( PlayerEntity player, Hand hand, BlockRayTrac // On server, we interacted if a peripheral was found Set oldPeriphNames = getConnectedPeripheralNames(); - togglePeripheralAccess(); + m_element.setSharingPeripherals( !m_element.isSharingPeripherals(), true ); Set periphNames = getConnectedPeripheralNames(); if( !Objects.equal( periphNames, oldPeriphNames ) ) @@ -228,7 +254,7 @@ 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 ) ); } @@ -236,7 +262,7 @@ public void read( @Nonnull CompoundNBT nbt ) @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 ); } @@ -244,7 +270,7 @@ public CompoundNBT write( CompoundNBT 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 ) ); @@ -269,7 +295,7 @@ public void blockTick() m_connectionsFormed = true; connectionsChanged(); - if( m_peripheralAccessAllowed ) + if( m_element.isSharingPeripherals() ) { for( Direction facing : DirectionUtil.FACINGS ) { @@ -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 getConnectedPeripheralNames() { - if( !m_peripheralAccessAllowed ) return Collections.emptySet(); + if( !m_element.isSharingPeripherals() ) return Collections.emptySet(); Set peripherals = new HashSet<>( 6 ); for( WiredModemLocalPeripheral peripheral : m_peripherals ) @@ -342,7 +340,7 @@ private Set getConnectedPeripheralNames() private Map getConnectedPeripherals() { - if( !m_peripheralAccessAllowed ) return Collections.emptyMap(); + if( !m_element.isSharingPeripherals() ) return Collections.emptyMap(); Map peripherals = new HashMap<>( 6 ); for( WiredModemLocalPeripheral peripheral : m_peripherals ) peripheral.extendMap( peripherals ); @@ -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(); } diff --git a/src/main/java/dan200/computercraft/shared/peripheral/modem/wired/WiredModemElement.java b/src/main/java/dan200/computercraft/shared/peripheral/modem/wired/WiredModemElement.java index 510e5e3422..d2a65cf846 100644 --- a/src/main/java/dan200/computercraft/shared/peripheral/modem/wired/WiredModemElement.java +++ b/src/main/java/dan200/computercraft/shared/peripheral/modem/wired/WiredModemElement.java @@ -19,6 +19,7 @@ public abstract class WiredModemElement implements IWiredElement { private final IWiredNode node = new WiredNode( this ); private final Map remotePeripherals = new HashMap<>(); + private boolean sharingPeripherals = false; @Nonnull @Override @@ -61,4 +62,31 @@ public Map 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(); } diff --git a/src/main/java/dan200/computercraft/shared/peripheral/modem/wired/WiredModemPeripheral.java b/src/main/java/dan200/computercraft/shared/peripheral/modem/wired/WiredModemPeripheral.java index bdd1187ca1..1c3445c5cf 100644 --- a/src/main/java/dan200/computercraft/shared/peripheral/modem/wired/WiredModemPeripheral.java +++ b/src/main/java/dan200/computercraft/shared/peripheral/modem/wired/WiredModemPeripheral.java @@ -33,7 +33,6 @@ public abstract class WiredModemPeripheral extends ModemPeripheral implements IWiredSender { private final WiredModemElement modem; - private final Map> peripheralWrappers = new HashMap<>( 1 ); public WiredModemPeripheral( ModemState state, WiredModemElement modem ) @@ -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 ) {