Skip to content

Commit 4e11f98

Browse files
committed
Modem peripherals toggling
1 parent cd879b0 commit 4e11f98

File tree

3 files changed

+34
-5
lines changed

3 files changed

+34
-5
lines changed

src/main/java/dan200/computercraft/shared/peripheral/modem/wired/TileWiredModemFull.java

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636

3737
import javax.annotation.Nonnull;
3838
import javax.annotation.Nullable;
39+
import java.util.concurrent.atomic.AtomicBoolean;
3940
import java.util.*;
4041

4142
import 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()

src/main/java/dan200/computercraft/shared/peripheral/modem/wired/WiredModemElement.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,4 +61,9 @@ public Map<String, IPeripheral> getRemotePeripherals()
6161
protected abstract void attachPeripheral( String name, IPeripheral peripheral );
6262

6363
protected abstract void detachPeripheral( String name );
64+
65+
public void togglePeripheralAccess()
66+
{
67+
68+
}
6469
}

src/main/java/dan200/computercraft/shared/peripheral/modem/wired/WiredModemPeripheral.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,12 @@ public final Object[] getNameLocal()
195195
return local == null ? null : new Object[] { local };
196196
}
197197

198+
@LuaFunction
199+
public final void togglePeripherals()
200+
{
201+
modem.togglePeripheralAccess();
202+
}
203+
198204
@Override
199205
public void attach( @Nonnull IComputerAccess computer )
200206
{

0 commit comments

Comments
 (0)