Skip to content

Commit 221e249

Browse files
committed
Modem peripherals toggling
1 parent cd879b0 commit 221e249

File tree

4 files changed

+102
-70
lines changed

4 files changed

+102
-70
lines changed

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

+26-31
Original file line numberDiff line numberDiff line change
@@ -75,9 +75,25 @@ protected void detachPeripheral( String name )
7575
{
7676
m_modem.detachPeripheral( name );
7777
}
78+
79+
protected boolean enableSharing()
80+
{
81+
m_peripheral.attach( world, getPos(), getDirection() );
82+
if( !m_peripheral.hasPeripheral() ) return false;
83+
84+
m_node.updatePeripherals( m_peripheral.toMap() );
85+
updateBlockState();
86+
return true;
87+
}
88+
89+
protected void disableSharing()
90+
{
91+
m_peripheral.detach();
92+
m_node.updatePeripherals( Collections.emptyMap() );
93+
updateBlockState();
94+
}
7895
}
7996

80-
private boolean m_peripheralAccessAllowed;
8197
private final WiredModemLocalPeripheral m_peripheral = new WiredModemLocalPeripheral( this::refreshPeripheral );
8298

8399
private boolean m_destroyed = false;
@@ -236,7 +252,7 @@ public void onNeighbourChange( @Nonnull BlockPos neighbour )
236252
public void onNeighbourTileEntityChange( @Nonnull BlockPos neighbour )
237253
{
238254
super.onNeighbourTileEntityChange( neighbour );
239-
if( !world.isRemote && m_peripheralAccessAllowed )
255+
if( !world.isRemote && m_cable.isSharingPeripherals() )
240256
{
241257
Direction facing = getDirection();
242258
if( getPos().offset( facing ).equals( neighbour ) ) refreshPeripheral();
@@ -261,7 +277,7 @@ public ActionResultType onActivate( PlayerEntity player, Hand hand, BlockRayTrac
261277
if( getWorld().isRemote ) return ActionResultType.SUCCESS;
262278

263279
String oldName = m_peripheral.getConnectedName();
264-
togglePeripheralAccess();
280+
m_cable.setSharingPeripherals( !m_cable.isSharingPeripherals(), true );
265281
String newName = m_peripheral.getConnectedName();
266282
if( !Objects.equal( newName, oldName ) )
267283
{
@@ -284,15 +300,15 @@ public ActionResultType onActivate( PlayerEntity player, Hand hand, BlockRayTrac
284300
public void read( @Nonnull CompoundNBT nbt )
285301
{
286302
super.read( nbt );
287-
m_peripheralAccessAllowed = nbt.getBoolean( NBT_PERIPHERAL_ENABLED );
303+
m_cable.setSharingPeripherals( nbt.getBoolean( NBT_PERIPHERAL_ENABLED ), false );
288304
m_peripheral.read( nbt, "" );
289305
}
290306

291307
@Nonnull
292308
@Override
293309
public CompoundNBT write( CompoundNBT nbt )
294310
{
295-
nbt.putBoolean( NBT_PERIPHERAL_ENABLED, m_peripheralAccessAllowed );
311+
nbt.putBoolean( NBT_PERIPHERAL_ENABLED, m_cable.isSharingPeripherals() );
296312
m_peripheral.write( nbt, "" );
297313
return super.write( nbt );
298314
}
@@ -302,7 +318,7 @@ private void updateBlockState()
302318
BlockState state = getBlockState();
303319
CableModemVariant oldVariant = state.get( BlockCable.MODEM );
304320
CableModemVariant newVariant = CableModemVariant
305-
.from( oldVariant.getFacing(), m_modem.getModemState().isOpen(), m_peripheralAccessAllowed );
321+
.from( oldVariant.getFacing(), m_modem.getModemState().isOpen(), m_cable.isSharingPeripherals() );
306322

307323
if( oldVariant != newVariant )
308324
{
@@ -331,7 +347,7 @@ public void blockTick()
331347
m_connectionsFormed = true;
332348

333349
connectionsChanged();
334-
if( m_peripheralAccessAllowed )
350+
if( m_cable.isSharingPeripherals() )
335351
{
336352
m_peripheral.attach( world, pos, modemDirection );
337353
updateConnectedPeripherals();
@@ -378,44 +394,23 @@ void modemChanged()
378394

379395
// If we can no longer attach peripherals, then detach any
380396
// which may have existed
381-
if( !canAttachPeripheral() && m_peripheralAccessAllowed )
397+
if( !canAttachPeripheral() && m_cable.isSharingPeripherals() )
382398
{
383-
m_peripheralAccessAllowed = false;
399+
m_cable.setSharingPeripherals( false, false );
384400
m_peripheral.detach();
385401
m_node.updatePeripherals( Collections.emptyMap() );
386402
markDirty();
387403
updateBlockState();
388404
}
389405
}
390406

391-
private void togglePeripheralAccess()
392-
{
393-
if( !m_peripheralAccessAllowed )
394-
{
395-
m_peripheral.attach( world, getPos(), getDirection() );
396-
if( !m_peripheral.hasPeripheral() ) return;
397-
398-
m_peripheralAccessAllowed = true;
399-
m_node.updatePeripherals( m_peripheral.toMap() );
400-
}
401-
else
402-
{
403-
m_peripheral.detach();
404-
405-
m_peripheralAccessAllowed = false;
406-
m_node.updatePeripherals( Collections.emptyMap() );
407-
}
408-
409-
updateBlockState();
410-
}
411-
412407
private void updateConnectedPeripherals()
413408
{
414409
Map<String, IPeripheral> peripherals = m_peripheral.toMap();
415410
if( peripherals.isEmpty() )
416411
{
417412
// If there are no peripherals then disable access and update the display state.
418-
m_peripheralAccessAllowed = false;
413+
m_cable.setSharingPeripherals( false, false );
419414
updateBlockState();
420415
}
421416

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

+36-38
Original file line numberDiff line numberDiff line change
@@ -90,12 +90,38 @@ public Vec3d getPosition()
9090
BlockPos pos = m_entity.getPos();
9191
return new Vec3d( pos.getX() + 0.5, pos.getY() + 0.5, pos.getZ() + 0.5 );
9292
}
93+
94+
protected boolean enableSharing()
95+
{
96+
boolean hasAny = false;
97+
for( Direction facing : DirectionUtil.FACINGS )
98+
{
99+
WiredModemLocalPeripheral peripheral = m_entity.m_peripherals[facing.ordinal()];
100+
peripheral.attach( m_entity.world, m_entity.getPos(), facing );
101+
hasAny |= peripheral.hasPeripheral();
102+
}
103+
104+
if( !hasAny ) return false;
105+
106+
m_entity.m_node.updatePeripherals( m_entity.getConnectedPeripherals() );
107+
108+
m_entity.updateBlockState();
109+
110+
return true;
111+
}
112+
113+
protected void disableSharing()
114+
{
115+
for( WiredModemLocalPeripheral peripheral : m_entity.m_peripherals ) peripheral.detach();
116+
m_entity.m_node.updatePeripherals( Collections.emptyMap() );
117+
118+
m_entity.updateBlockState();
119+
}
93120
}
94121

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

98-
private boolean m_peripheralAccessAllowed = false;
99125
private final WiredModemLocalPeripheral[] m_peripherals = new WiredModemLocalPeripheral[6];
100126

101127
private boolean m_destroyed = false;
@@ -169,7 +195,7 @@ public void onNeighbourChange( @Nonnull BlockPos neighbour )
169195
@Override
170196
public void onNeighbourTileEntityChange( @Nonnull BlockPos neighbour )
171197
{
172-
if( !world.isRemote && m_peripheralAccessAllowed )
198+
if( !world.isRemote && m_element.isSharingPeripherals() )
173199
{
174200
for( Direction facing : DirectionUtil.FACINGS )
175201
{
@@ -195,7 +221,7 @@ public ActionResultType onActivate( PlayerEntity player, Hand hand, BlockRayTrac
195221

196222
// On server, we interacted if a peripheral was found
197223
Set<String> oldPeriphNames = getConnectedPeripheralNames();
198-
togglePeripheralAccess();
224+
m_element.setSharingPeripherals( !m_element.isSharingPeripherals(), true );
199225
Set<String> periphNames = getConnectedPeripheralNames();
200226

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

235261
@Nonnull
236262
@Override
237263
public CompoundNBT write( CompoundNBT nbt )
238264
{
239-
nbt.putBoolean( NBT_PERIPHERAL_ENABLED, m_peripheralAccessAllowed );
265+
nbt.putBoolean( NBT_PERIPHERAL_ENABLED, m_element.isSharingPeripherals() );
240266
for( int i = 0; i < m_peripherals.length; i++ ) m_peripherals[i].write( nbt, Integer.toString( i ) );
241267
return super.write( nbt );
242268
}
243269

244270
private void updateBlockState()
245271
{
246272
BlockState state = getBlockState();
247-
boolean modemOn = m_modemState.isOpen(), peripheralOn = m_peripheralAccessAllowed;
273+
boolean modemOn = m_modemState.isOpen(), peripheralOn = m_element.isSharingPeripherals();
248274
if( state.get( MODEM_ON ) == modemOn && state.get( PERIPHERAL_ON ) == peripheralOn ) return;
249275

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

271297
connectionsChanged();
272-
if( m_peripheralAccessAllowed )
298+
if( m_element.isSharingPeripherals() )
273299
{
274300
for( Direction facing : DirectionUtil.FACINGS )
275301
{
@@ -299,37 +325,9 @@ private void connectionsChanged()
299325
}
300326
}
301327

302-
private void togglePeripheralAccess()
303-
{
304-
if( !m_peripheralAccessAllowed )
305-
{
306-
boolean hasAny = false;
307-
for( Direction facing : DirectionUtil.FACINGS )
308-
{
309-
WiredModemLocalPeripheral peripheral = m_peripherals[facing.ordinal()];
310-
peripheral.attach( world, getPos(), facing );
311-
hasAny |= peripheral.hasPeripheral();
312-
}
313-
314-
if( !hasAny ) return;
315-
316-
m_peripheralAccessAllowed = true;
317-
m_node.updatePeripherals( getConnectedPeripherals() );
318-
}
319-
else
320-
{
321-
m_peripheralAccessAllowed = false;
322-
323-
for( WiredModemLocalPeripheral peripheral : m_peripherals ) peripheral.detach();
324-
m_node.updatePeripherals( Collections.emptyMap() );
325-
}
326-
327-
updateBlockState();
328-
}
329-
330328
private Set<String> getConnectedPeripheralNames()
331329
{
332-
if( !m_peripheralAccessAllowed ) return Collections.emptySet();
330+
if( !m_element.isSharingPeripherals() ) return Collections.emptySet();
333331

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

343341
private Map<String, IPeripheral> getConnectedPeripherals()
344342
{
345-
if( !m_peripheralAccessAllowed ) return Collections.emptyMap();
343+
if( !m_element.isSharingPeripherals() ) return Collections.emptyMap();
346344

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

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

+28
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ public abstract class WiredModemElement implements IWiredElement
1919
{
2020
private final IWiredNode node = new WiredNode( this );
2121
private final Map<String, IPeripheral> remotePeripherals = new HashMap<>();
22+
private boolean sharingPeripherals = false;
2223

2324
@Nonnull
2425
@Override
@@ -61,4 +62,31 @@ public Map<String, IPeripheral> getRemotePeripherals()
6162
protected abstract void attachPeripheral( String name, IPeripheral peripheral );
6263

6364
protected abstract void detachPeripheral( String name );
65+
66+
public boolean isSharingPeripherals()
67+
{
68+
return sharingPeripherals;
69+
}
70+
71+
public boolean setSharingPeripherals( boolean enabled, boolean updateTile )
72+
{
73+
if( sharingPeripherals == enabled ) return sharingPeripherals;
74+
sharingPeripherals = enabled;
75+
if( updateTile )
76+
{
77+
if( enabled )
78+
{
79+
sharingPeripherals = enableSharing();
80+
}
81+
else
82+
{
83+
disableSharing();
84+
}
85+
}
86+
return sharingPeripherals;
87+
}
88+
89+
protected abstract boolean enableSharing();
90+
91+
protected abstract void disableSharing();
6492
}

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

+12-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@
3333
public abstract class WiredModemPeripheral extends ModemPeripheral implements IWiredSender
3434
{
3535
private final WiredModemElement modem;
36-
3736
private final Map<IComputerAccess, ConcurrentMap<String, RemotePeripheralWrapper>> peripheralWrappers = new HashMap<>( 1 );
3837

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

197+
@LuaFunction
198+
public final boolean isSharingPeripherals()
199+
{
200+
return modem.isSharingPeripherals();
201+
}
202+
203+
@LuaFunction( mainThread = true )
204+
public final boolean setSharingPeripherals( boolean enabled )
205+
{
206+
return modem.setSharingPeripherals( enabled, true );
207+
}
208+
198209
@Override
199210
public void attach( @Nonnull IComputerAccess computer )
200211
{

0 commit comments

Comments
 (0)