3636
3737import javax .annotation .Nonnull ;
3838import javax .annotation .Nullable ;
39+ import java .util .concurrent .atomic .AtomicBoolean ;
3940import java .util .*;
4041
4142import static dan200 .computercraft .shared .Capabilities .CAPABILITY_PERIPHERAL ;
@@ -90,12 +91,22 @@ public Vec3d getPosition()
9091 BlockPos pos = m_entity .getPos ();
9192 return new Vec3d ( pos .getX () + 0.5 , pos .getY () + 0.5 , pos .getZ () + 0.5 );
9293 }
94+
95+ @ Override
96+ public void togglePeripheralAccess ()
97+ {
98+ if ( !m_entity .requestToTogglePeripherals .getAndSet ( true ) )
99+ {
100+ TickScheduler .schedule ( m_entity );
101+ }
102+ }
93103 }
94104
95105 private final WiredModemPeripheral [] modems = new WiredModemPeripheral [6 ];
96106 private final SidedCaps <IPeripheral > modemCaps = SidedCaps .ofNonNull ( this ::getPeripheral );
97107
98108 private boolean m_peripheralAccessAllowed = false ;
109+ public AtomicBoolean requestToTogglePeripherals = new AtomicBoolean ( false );
99110 private final WiredModemLocalPeripheral [] m_peripherals = new WiredModemLocalPeripheral [6 ];
100111
101112 private boolean m_destroyed = false ;
@@ -196,6 +207,7 @@ public ActionResultType onActivate( PlayerEntity player, Hand hand, BlockRayTrac
196207 // On server, we interacted if a peripheral was found
197208 Set <String > oldPeriphNames = getConnectedPeripheralNames ();
198209 togglePeripheralAccess ();
210+ updateBlockState ();
199211 Set <String > periphNames = getConnectedPeripheralNames ();
200212
201213 if ( !Objects .equal ( periphNames , oldPeriphNames ) )
@@ -262,7 +274,14 @@ public void blockTick()
262274 {
263275 if ( getWorld ().isRemote ) return ;
264276
265- if ( m_modemState .pollChanged () ) updateBlockState ();
277+ boolean needUpdate = false ;
278+
279+ if ( requestToTogglePeripherals .getAndSet ( false ) )
280+ {
281+ if ( togglePeripheralAccess () ) needUpdate = true ;
282+ }
283+ if ( m_modemState .pollChanged () ) needUpdate = true ;
284+ if ( needUpdate ) updateBlockState ();
266285
267286 if ( !m_connectionsFormed )
268287 {
@@ -299,7 +318,7 @@ private void connectionsChanged()
299318 }
300319 }
301320
302- private void togglePeripheralAccess ()
321+ private boolean togglePeripheralAccess ()
303322 {
304323 if ( !m_peripheralAccessAllowed )
305324 {
@@ -311,7 +330,7 @@ private void togglePeripheralAccess()
311330 hasAny |= peripheral .hasPeripheral ();
312331 }
313332
314- if ( !hasAny ) return ;
333+ if ( !hasAny ) return false ;
315334
316335 m_peripheralAccessAllowed = true ;
317336 m_node .updatePeripherals ( getConnectedPeripherals () );
@@ -323,8 +342,7 @@ private void togglePeripheralAccess()
323342 for ( WiredModemLocalPeripheral peripheral : m_peripherals ) peripheral .detach ();
324343 m_node .updatePeripherals ( Collections .emptyMap () );
325344 }
326-
327- updateBlockState ();
345+ return true ;
328346 }
329347
330348 private Set <String > getConnectedPeripheralNames ()
0 commit comments