1
0
mirror of https://github.com/SquidDev-CC/CC-Tweaked synced 2025-11-16 13:17:10 +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:
SquidDev
2018-04-16 18:22:28 +01:00
parent 04f162ef25
commit 6cf32f1f74
8 changed files with 289 additions and 201 deletions

View File

@@ -1,11 +1,8 @@
package dan200.computercraft.api.network.wired;
import dan200.computercraft.api.ComputerCraftAPI;
import dan200.computercraft.api.peripheral.IPeripheral;
import javax.annotation.Nonnull;
import java.util.Collections;
import java.util.Map;
/**
* An object which may be part of a wired network.
@@ -19,21 +16,6 @@ import java.util.Map;
*/
public interface IWiredElement extends IWiredSender
{
/**
* Fetch the peripherals this network element provides.
*
* This is only called when initially attaching to a network and after a call to {@link IWiredNode#invalidate()}}, so
* one does not <em>need</em> to cache the return value.
*
* @return The peripherals this node provides.
* @see IWiredNode#invalidate()
*/
@Nonnull
default Map<String, IPeripheral> getPeripherals()
{
return Collections.emptyMap();
}
/**
* Called when objects on the network change. This may occur when network nodes are added or removed, or when
* peripherals change.

View File

@@ -1,6 +1,9 @@
package dan200.computercraft.api.network.wired;
import dan200.computercraft.api.peripheral.IPeripheral;
import javax.annotation.Nonnull;
import java.util.Map;
/**
* A wired network is composed of one of more {@link IWiredNode}s, a set of connections between them, and a series
@@ -51,7 +54,8 @@ public interface IWiredNetwork
/**
* Sever all connections this node has, removing it from this network.
*
* This should only be used on the server thread.
* This should only be used on the server thread. You should only call this on nodes
* that your network element owns.
*
* @param node The node to remove
* @return Whether this node was removed from the network. One cannot remove a node from a network where it is the
@@ -62,13 +66,15 @@ public interface IWiredNetwork
boolean remove( @Nonnull IWiredNode node );
/**
* Mark this node's peripherals as having changed.
* Update the peripherals a node provides.
*
* This should only be used on the server thread.
* This should only be used on the server thread. You should only call this on nodes
* that your network element owns.
*
* @param node The node to mark as invalid.
* @param node The node to attach peripherals for.
* @param peripherals The new peripherals for this node.
* @throws IllegalArgumentException If the node is not in the network.
* @see IWiredElement#getPeripherals()
* @see IWiredNode#updatePeripherals(Map)
*/
void invalidate( @Nonnull IWiredNode node );
void updatePeripherals( @Nonnull IWiredNode node, @Nonnull Map<String, IPeripheral> peripherals );
}

View File

@@ -1,8 +1,10 @@
package dan200.computercraft.api.network.wired;
import dan200.computercraft.api.network.IPacketNetwork;
import dan200.computercraft.api.peripheral.IPeripheral;
import javax.annotation.Nonnull;
import java.util.Map;
/**
* Wired nodes act as a layer between {@link IWiredElement}s and {@link IWiredNetwork}s.
@@ -72,7 +74,8 @@ public interface IWiredNode extends IPacketNetwork
/**
* Sever all connections this node has, removing it from this network.
*
* This should only be used on the server thread.
* This should only be used on the server thread. You should only call this on nodes
* that your network element owns.
*
* @return Whether this node was removed from the network. One cannot remove a node from a network where it is the
* only element.
@@ -87,12 +90,14 @@ public interface IWiredNode extends IPacketNetwork
/**
* Mark this node's peripherals as having changed.
*
* This should only be used on the server thread.
* This should only be used on the server thread. You should only call this on nodes
* that your network element owns.
*
* @see IWiredElement#getPeripherals()
* @param peripherals The new peripherals for this node.
* @see IWiredNetwork#updatePeripherals(IWiredNode, Map)
*/
default void invalidate()
default void updatePeripherals( @Nonnull Map<String, IPeripheral> peripherals )
{
getNetwork().invalidate( this );
getNetwork().updatePeripherals( this, peripherals );
}
}