mirror of
https://github.com/SquidDev-CC/CC-Tweaked
synced 2025-11-26 01:44:48 +00:00
Various improvements to peripheral invalidation
- Abstract peripheral ID and type checking into separate class - Update peripherals directly rather than marking as invalid then fetching from the network. - Update peripherals when adjacent tiles change This does result in a slightly more ugly interface, but reduces the amount of work needed to perform partial updates of peripherals, such as those done by neighbouring tile updates.
This commit is contained in:
@@ -1,5 +1,7 @@
|
||||
package dan200.computercraft.shared.wired;
|
||||
|
||||
import com.google.common.base.Preconditions;
|
||||
import com.google.common.collect.ImmutableMap;
|
||||
import dan200.computercraft.api.network.Packet;
|
||||
import dan200.computercraft.api.network.wired.IWiredNetwork;
|
||||
import dan200.computercraft.api.network.wired.IWiredNode;
|
||||
@@ -283,9 +285,10 @@ public final class WiredNetwork implements IWiredNetwork
|
||||
}
|
||||
|
||||
@Override
|
||||
public void invalidate( @Nonnull IWiredNode node )
|
||||
public void updatePeripherals( @Nonnull IWiredNode node, @Nonnull Map<String, IPeripheral> newPeripherals )
|
||||
{
|
||||
WiredNode wired = checkNode( node );
|
||||
Preconditions.checkNotNull( peripherals, "peripherals cannot be null" );
|
||||
|
||||
lock.writeLock().lock();
|
||||
try
|
||||
@@ -293,11 +296,10 @@ public final class WiredNetwork implements IWiredNetwork
|
||||
if( wired.network != this ) throw new IllegalStateException( "Node is not on this network" );
|
||||
|
||||
Map<String, IPeripheral> oldPeripherals = wired.peripherals;
|
||||
Map<String, IPeripheral> newPeripherals = wired.element.getPeripherals();
|
||||
WiredNetworkChange change = WiredNetworkChange.changeOf( oldPeripherals, newPeripherals );
|
||||
if( change.isEmpty() ) return;
|
||||
|
||||
wired.peripherals = newPeripherals;
|
||||
wired.peripherals = ImmutableMap.copyOf( newPeripherals );
|
||||
|
||||
// Detach the old peripherals then remove them.
|
||||
peripherals.keySet().removeAll( change.peripheralsRemoved().keySet() );
|
||||
|
||||
Reference in New Issue
Block a user