@@ -90,12 +90,38 @@ public Vec3d getPosition()
90
90
BlockPos pos = m_entity .getPos ();
91
91
return new Vec3d ( pos .getX () + 0.5 , pos .getY () + 0.5 , pos .getZ () + 0.5 );
92
92
}
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
+ }
93
120
}
94
121
95
122
private final WiredModemPeripheral [] modems = new WiredModemPeripheral [6 ];
96
123
private final SidedCaps <IPeripheral > modemCaps = SidedCaps .ofNonNull ( this ::getPeripheral );
97
124
98
- private boolean m_peripheralAccessAllowed = false ;
99
125
private final WiredModemLocalPeripheral [] m_peripherals = new WiredModemLocalPeripheral [6 ];
100
126
101
127
private boolean m_destroyed = false ;
@@ -169,7 +195,7 @@ public void onNeighbourChange( @Nonnull BlockPos neighbour )
169
195
@ Override
170
196
public void onNeighbourTileEntityChange ( @ Nonnull BlockPos neighbour )
171
197
{
172
- if ( !world .isRemote && m_peripheralAccessAllowed )
198
+ if ( !world .isRemote && m_element . isSharingPeripherals () )
173
199
{
174
200
for ( Direction facing : DirectionUtil .FACINGS )
175
201
{
@@ -195,7 +221,7 @@ public ActionResultType onActivate( PlayerEntity player, Hand hand, BlockRayTrac
195
221
196
222
// On server, we interacted if a peripheral was found
197
223
Set <String > oldPeriphNames = getConnectedPeripheralNames ();
198
- togglePeripheralAccess ( );
224
+ m_element . setSharingPeripherals ( ! m_element . isSharingPeripherals (), true );
199
225
Set <String > periphNames = getConnectedPeripheralNames ();
200
226
201
227
if ( !Objects .equal ( periphNames , oldPeriphNames ) )
@@ -228,23 +254,23 @@ private static void sendPeripheralChanges( PlayerEntity player, String kind, Col
228
254
public void read ( @ Nonnull CompoundNBT nbt )
229
255
{
230
256
super .read ( nbt );
231
- m_peripheralAccessAllowed = nbt .getBoolean ( NBT_PERIPHERAL_ENABLED );
257
+ m_element . setSharingPeripherals ( nbt .getBoolean ( NBT_PERIPHERAL_ENABLED ), false );
232
258
for ( int i = 0 ; i < m_peripherals .length ; i ++ ) m_peripherals [i ].read ( nbt , Integer .toString ( i ) );
233
259
}
234
260
235
261
@ Nonnull
236
262
@ Override
237
263
public CompoundNBT write ( CompoundNBT nbt )
238
264
{
239
- nbt .putBoolean ( NBT_PERIPHERAL_ENABLED , m_peripheralAccessAllowed );
265
+ nbt .putBoolean ( NBT_PERIPHERAL_ENABLED , m_element . isSharingPeripherals () );
240
266
for ( int i = 0 ; i < m_peripherals .length ; i ++ ) m_peripherals [i ].write ( nbt , Integer .toString ( i ) );
241
267
return super .write ( nbt );
242
268
}
243
269
244
270
private void updateBlockState ()
245
271
{
246
272
BlockState state = getBlockState ();
247
- boolean modemOn = m_modemState .isOpen (), peripheralOn = m_peripheralAccessAllowed ;
273
+ boolean modemOn = m_modemState .isOpen (), peripheralOn = m_element . isSharingPeripherals () ;
248
274
if ( state .get ( MODEM_ON ) == modemOn && state .get ( PERIPHERAL_ON ) == peripheralOn ) return ;
249
275
250
276
getWorld ().setBlockState ( getPos (), state .with ( MODEM_ON , modemOn ).with ( PERIPHERAL_ON , peripheralOn ) );
@@ -269,7 +295,7 @@ public void blockTick()
269
295
m_connectionsFormed = true ;
270
296
271
297
connectionsChanged ();
272
- if ( m_peripheralAccessAllowed )
298
+ if ( m_element . isSharingPeripherals () )
273
299
{
274
300
for ( Direction facing : DirectionUtil .FACINGS )
275
301
{
@@ -299,37 +325,9 @@ private void connectionsChanged()
299
325
}
300
326
}
301
327
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
-
330
328
private Set <String > getConnectedPeripheralNames ()
331
329
{
332
- if ( !m_peripheralAccessAllowed ) return Collections .emptySet ();
330
+ if ( !m_element . isSharingPeripherals () ) return Collections .emptySet ();
333
331
334
332
Set <String > peripherals = new HashSet <>( 6 );
335
333
for ( WiredModemLocalPeripheral peripheral : m_peripherals )
@@ -342,7 +340,7 @@ private Set<String> getConnectedPeripheralNames()
342
340
343
341
private Map <String , IPeripheral > getConnectedPeripherals ()
344
342
{
345
- if ( !m_peripheralAccessAllowed ) return Collections .emptyMap ();
343
+ if ( !m_element . isSharingPeripherals () ) return Collections .emptyMap ();
346
344
347
345
Map <String , IPeripheral > peripherals = new HashMap <>( 6 );
348
346
for ( WiredModemLocalPeripheral peripheral : m_peripherals ) peripheral .extendMap ( peripherals );
@@ -355,7 +353,7 @@ private void updateConnectedPeripherals()
355
353
if ( peripherals .isEmpty () )
356
354
{
357
355
// If there are no peripherals then disable access and update the display state.
358
- m_peripheralAccessAllowed = false ;
356
+ m_element . setSharingPeripherals ( false , false ) ;
359
357
updateBlockState ();
360
358
}
361
359
0 commit comments