@@ -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
0 commit comments