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:
@@ -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.
|
||||
|
||||
@@ -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 );
|
||||
}
|
||||
|
||||
@@ -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 );
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user