Remove some deprecated code

Some of this is technically an API break, but 1.20.4 is pretty unstable.

 - Remove WiredNetwork from the public API
 - Remove legacy computer selectors
This commit is contained in:
Jonathan Coates 2024-03-22 21:36:52 +00:00
parent da5885ef35
commit 0a9e5c78f3
No known key found for this signature in database
GPG Key ID: B9E431FF07C98D06
12 changed files with 29 additions and 188 deletions

View File

@ -1,92 +0,0 @@
// SPDX-FileCopyrightText: 2018 The CC: Tweaked Developers
//
// SPDX-License-Identifier: MPL-2.0
package dan200.computercraft.api.network.wired;
import dan200.computercraft.api.peripheral.IPeripheral;
import org.jetbrains.annotations.ApiStatus;
import java.util.Map;
/**
* A wired network is composed of one of more {@link WiredNode}s, a set of connections between them, and a series
* of peripherals.
* <p>
* Networks from a connected graph. This means there is some path between all nodes on the network. Further more, if
* there is some path between two nodes then they must be on the same network. {@link WiredNetwork} will automatically
* handle the merging and splitting of networks (and thus changing of available nodes and peripherals) as connections
* change.
* <p>
* This does mean one can not rely on the network remaining consistent between subsequent operations. Consequently,
* it is generally preferred to use the methods provided by {@link WiredNode}.
*
* @see WiredNode#getNetwork()
*/
@ApiStatus.NonExtendable
public interface WiredNetwork {
/**
* Create a connection between two nodes.
* <p>
* This should only be used on the server thread.
*
* @param left The first node to connect
* @param right The second node to connect
* @return {@code true} if a connection was created or {@code false} if the connection already exists.
* @throws IllegalStateException If neither node is on the network.
* @throws IllegalArgumentException If {@code left} and {@code right} are equal.
* @see WiredNode#connectTo(WiredNode)
* @see WiredNetwork#connect(WiredNode, WiredNode)
* @deprecated Use {@link WiredNode#connectTo(WiredNode)}
*/
@Deprecated
boolean connect(WiredNode left, WiredNode right);
/**
* Destroy a connection between this node and another.
* <p>
* This should only be used on the server thread.
*
* @param left The first node in the connection.
* @param right The second node in the connection.
* @return {@code true} if a connection was destroyed or {@code false} if no connection exists.
* @throws IllegalArgumentException If either node is not on the network.
* @throws IllegalArgumentException If {@code left} and {@code right} are equal.
* @see WiredNode#disconnectFrom(WiredNode)
* @see WiredNetwork#connect(WiredNode, WiredNode)
* @deprecated Use {@link WiredNode#disconnectFrom(WiredNode)}
*/
@Deprecated
boolean disconnect(WiredNode left, WiredNode right);
/**
* Sever all connections this node has, removing it from this network.
* <p>
* 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
* only element.
* @throws IllegalArgumentException If the node is not in the network.
* @see WiredNode#remove()
* @deprecated Use {@link WiredNode#remove()}
*/
@Deprecated
boolean remove(WiredNode node);
/**
* Update the peripherals a node provides.
* <p>
* 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 attach peripherals for.
* @param peripherals The new peripherals for this node.
* @throws IllegalArgumentException If the node is not in the network.
* @see WiredNode#updatePeripherals(Map)
* @deprecated Use {@link WiredNode#updatePeripherals(Map)}
*/
@Deprecated
void updatePeripherals(WiredNode node, Map<String, IPeripheral> peripherals);
}

View File

@ -11,7 +11,7 @@
import java.util.Map;
/**
* Wired nodes act as a layer between {@link WiredElement}s and {@link WiredNetwork}s.
* A single node on a wired network.
* <p>
* Firstly, a node acts as a packet network, capable of sending and receiving modem messages to connected nodes. These
* methods may be safely used on any thread.
@ -32,18 +32,6 @@ public interface WiredNode extends PacketNetwork {
*/
WiredElement getElement();
/**
* The network this node is currently connected to. Note that this may change
* after any network operation, so it should not be cached.
* <p>
* This should only be used on the server thread.
*
* @return This node's network.
* @deprecated Use the connect/disconnect/remove methods on {@link WiredNode}.
*/
@Deprecated
WiredNetwork getNetwork();
/**
* Create a connection from this node to another.
* <p>

View File

@ -8,10 +8,12 @@
/**
* An object on a {@link WiredNetwork} capable of sending packets.
* An object on a wired network capable of sending packets.
* <p>
* Unlike a regular {@link PacketSender}, this must be associated with the node you are attempting to
* to send the packet from.
*
* @see WiredElement
*/
public interface WiredSender extends PacketSender {
/**

View File

@ -5,7 +5,6 @@
package dan200.computercraft.impl.network.wired;
import dan200.computercraft.api.network.Packet;
import dan200.computercraft.api.network.wired.WiredNetwork;
import dan200.computercraft.api.network.wired.WiredNode;
import dan200.computercraft.api.peripheral.IPeripheral;
import dan200.computercraft.core.util.Nullability;
@ -14,7 +13,7 @@
import java.util.concurrent.locks.ReadWriteLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;
final class WiredNetworkImpl implements WiredNetwork {
final class WiredNetworkImpl {
final ReadWriteLock lock = new ReentrantReadWriteLock();
Set<WiredNodeImpl> nodes;
private Map<String, IPeripheral> peripherals = new HashMap<>();
@ -28,7 +27,6 @@ private WiredNetworkImpl(Set<WiredNodeImpl> nodes) {
this.nodes = nodes;
}
@Override
public boolean connect(WiredNode nodeU, WiredNode nodeV) {
var wiredU = checkNode(nodeU);
var wiredV = checkNode(nodeV);
@ -88,7 +86,6 @@ public boolean connect(WiredNode nodeU, WiredNode nodeV) {
}
}
@Override
public boolean disconnect(WiredNode nodeU, WiredNode nodeV) {
var wiredU = checkNode(nodeU);
var wiredV = checkNode(nodeV);
@ -159,7 +156,6 @@ public boolean disconnect(WiredNode nodeU, WiredNode nodeV) {
}
}
@Override
public boolean remove(WiredNode node) {
var wired = checkNode(node);
@ -316,7 +312,6 @@ There are efficient (near-logarithmic) algorithms for this (e.g. https://arxiv.o
}
}
@Override
public void updatePeripherals(WiredNode node, Map<String, IPeripheral> newPeripherals) {
var wired = checkNode(node);
Objects.requireNonNull(peripherals, "peripherals cannot be null");

View File

@ -4,10 +4,10 @@
package dan200.computercraft.impl.network.wired;
import com.google.common.annotations.VisibleForTesting;
import dan200.computercraft.api.network.Packet;
import dan200.computercraft.api.network.PacketReceiver;
import dan200.computercraft.api.network.wired.WiredElement;
import dan200.computercraft.api.network.wired.WiredNetwork;
import dan200.computercraft.api.network.wired.WiredNode;
import dan200.computercraft.api.network.wired.WiredSender;
import dan200.computercraft.api.peripheral.IPeripheral;
@ -128,8 +128,8 @@ public WiredElement getElement() {
return element;
}
@Override
public WiredNetwork getNetwork() {
@VisibleForTesting
public WiredNetworkImpl getNetwork() {
return network;
}

View File

@ -159,7 +159,6 @@ private static int dump(CommandSourceStack source) {
*/
private static int dumpComputer(CommandSourceStack source, ServerComputer computer) {
var table = new TableBuilder("Dump");
table.row(header("Instance ID"), text(Integer.toString(computer.getInstanceID())));
table.row(header("Instance UUID"), text(computer.getInstanceUUID().toString()));
table.row(header("Id"), text(Integer.toString(computer.getID())));
table.row(header("Label"), text(computer.getLabel()));

View File

@ -35,7 +35,6 @@
public record ComputerSelector(
String selector,
OptionalInt instanceId,
@Nullable UUID instanceUuid,
OptionalInt computerId,
@Nullable String label,
@ -43,9 +42,9 @@ public record ComputerSelector(
@Nullable AABB bounds,
@Nullable MinMaxBounds.Doubles range
) {
private static final ComputerSelector all = new ComputerSelector("@c[]", OptionalInt.empty(), null, OptionalInt.empty(), null, null, null, null);
private static final ComputerSelector all = new ComputerSelector("@c[]", null, OptionalInt.empty(), null, null, null, null);
private static UuidArgument uuidArgument = UuidArgument.uuid();
private static final UuidArgument uuidArgument = UuidArgument.uuid();
/**
* A {@link ComputerSelector} which matches all computers.
@ -64,10 +63,6 @@ public static ComputerSelector all() {
*/
public Stream<ServerComputer> find(CommandSourceStack source) {
var context = ServerContext.get(source.getServer());
if (instanceId().isPresent()) {
var computer = context.registry().get(instanceId().getAsInt());
return computer != null && matches(source, computer) ? Stream.of(computer) : Stream.of();
}
if (instanceUuid() != null) {
var computer = context.registry().get(instanceUuid());
@ -114,8 +109,7 @@ public ServerComputer findOne(CommandSourceStack source) throws CommandSyntaxExc
* @return If this computer is matched by the selector.
*/
public boolean matches(CommandSourceStack source, ServerComputer computer) {
return (instanceId().isEmpty() || computer.getInstanceID() == instanceId().getAsInt())
&& (instanceUuid() == null || computer.getInstanceUUID().equals(instanceUuid()))
return (instanceUuid() == null || computer.getInstanceUUID().equals(instanceUuid()))
&& (computerId().isEmpty() || computer.getID() == computerId().getAsInt())
&& (label == null || Objects.equals(computer.getLabel(), label))
&& (family == null || computer.getFamily() == family)
@ -138,24 +132,12 @@ public static ComputerSelector parse(StringReader reader) throws CommandSyntaxEx
if (consume(reader, "@c[")) {
parseSelector(builder, reader);
} else {
// TODO(1.20.5): Only parse computer ids here.
var kind = reader.peek();
if (kind == '@') {
reader.skip();
builder.label = reader.readString();
} else if (kind == '~') {
reader.skip();
builder.family = parseFamily(reader);
} else if (kind == '#') {
reader.skip();
builder.computerId = OptionalInt.of(reader.readInt());
} else {
builder.instanceId = OptionalInt.of(reader.readInt());
}
if (reader.peek() == '#') reader.skip();
builder.computerId = OptionalInt.of(reader.readInt());
}
var selector = reader.getString().substring(start, reader.getCursor());
return new ComputerSelector(selector, builder.instanceId, builder.instanceUuid, builder.computerId, builder.label, builder.family, builder.bounds, builder.range);
return new ComputerSelector(selector, builder.instanceUuid, builder.computerId, builder.label, builder.family, builder.bounds, builder.range);
}
private static void parseSelector(Builder builder, StringReader reader) throws CommandSyntaxException {
@ -222,7 +204,7 @@ public static CompletableFuture<Suggestions> suggest(CommandContext<?> context,
} else if (remaining.startsWith("#")) {
return suggestComputers(c -> "#" + c.getID()).suggest(context, builder);
} else {
return suggestComputers(c -> Integer.toString(c.getInstanceID())).suggest(context, builder);
return suggestComputers(c -> Integer.toString(c.getID())).suggest(context, builder);
}
}

View File

@ -31,7 +31,6 @@
import java.util.function.Function;
public class ServerComputer implements InputHandler, ComputerEnvironment {
private final int instanceID;
private final UUID instanceUUID = UUID.randomUUID();
private ServerLevel level;
@ -54,7 +53,6 @@ public ServerComputer(
this.family = family;
var context = ServerContext.get(level.getServer());
instanceID = context.registry().getUnusedInstanceID();
terminal = new NetworkedTerminal(terminalWidth, terminalHeight, family != ComputerFamily.NORMAL, this::markTerminalChanged);
metrics = context.metrics().createMetricObserver(this);
@ -148,10 +146,6 @@ private void sendToAllInteracting(Function<AbstractContainerMenu, NetworkMessage
protected void onRemoved() {
}
public int getInstanceID() {
return instanceID;
}
public UUID getInstanceUUID() {
return instanceUUID;
}

View File

@ -4,9 +4,6 @@
package dan200.computercraft.shared.computer.core;
import it.unimi.dsi.fastutil.ints.Int2ObjectMap;
import it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap;
import javax.annotation.Nullable;
import java.util.*;
@ -14,23 +11,12 @@ public class ServerComputerRegistry {
private static final Random RANDOM = new Random();
private final int sessionId = RANDOM.nextInt();
private final Int2ObjectMap<ServerComputer> computersByInstanceId = new Int2ObjectOpenHashMap<>();
private final Map<UUID, ServerComputer> computersByInstanceUuid = new HashMap<>();
private int nextInstanceId;
public int getSessionID() {
return sessionId;
}
int getUnusedInstanceID() {
return nextInstanceId++;
}
@Nullable
public ServerComputer get(int instanceID) {
return instanceID >= 0 ? computersByInstanceId.get(instanceID) : null;
}
@Nullable
public ServerComputer get(@Nullable UUID instanceID) {
return instanceID != null ? computersByInstanceUuid.get(instanceID) : null;
@ -56,35 +42,27 @@ void update() {
}
void add(ServerComputer computer) {
var instanceID = computer.getInstanceID();
var instanceUUID = computer.getInstanceUUID();
if (computersByInstanceId.containsKey(instanceID)) {
throw new IllegalStateException("Duplicate computer " + instanceID);
}
if (computersByInstanceUuid.containsKey(instanceUUID)) {
throw new IllegalStateException("Duplicate computer " + instanceUUID);
}
computersByInstanceId.put(instanceID, computer);
computersByInstanceUuid.put(instanceUUID, computer);
}
void remove(ServerComputer computer) {
computer.unload();
computer.onRemoved();
computersByInstanceId.remove(computer.getInstanceID());
computersByInstanceUuid.remove(computer.getInstanceUUID());
}
void close() {
for (var computer : getComputers()) computer.unload();
computersByInstanceId.clear();
computersByInstanceUuid.clear();
}
public Collection<ServerComputer> getComputers() {
return computersByInstanceId.values();
return computersByInstanceUuid.values();
}
}

View File

@ -4,7 +4,6 @@
package dan200.computercraft.impl.network.wired;
import dan200.computercraft.api.network.wired.WiredNetwork;
import dan200.computercraft.impl.network.wired.NetworkTest.NetworkElement;
import dan200.computercraft.shared.util.DirectionUtil;
import it.unimi.dsi.fastutil.objects.Object2IntMap;
@ -136,8 +135,8 @@ public void setup() {
}
}
private static Object2IntMap<WiredNetwork> countNetworks(Grid<WiredNodeImpl> grid) {
Object2IntMap<WiredNetwork> networks = new Object2IntOpenHashMap<>();
private static Object2IntMap<WiredNetworkImpl> countNetworks(Grid<WiredNodeImpl> grid) {
Object2IntMap<WiredNetworkImpl> networks = new Object2IntOpenHashMap<>();
grid.forEach((node, pos) -> networks.put(node.network, networks.getOrDefault(node.network, 0) + 1));
return networks;
}

View File

@ -5,7 +5,6 @@
package dan200.computercraft.impl.network.wired;
import dan200.computercraft.api.network.wired.WiredElement;
import dan200.computercraft.api.network.wired.WiredNetwork;
import dan200.computercraft.api.network.wired.WiredNetworkChange;
import dan200.computercraft.api.network.wired.WiredNode;
import dan200.computercraft.api.peripheral.IPeripheral;
@ -293,8 +292,8 @@ public boolean equals(@Nullable IPeripheral other) {
}
}
private static Set<WiredNodeImpl> nodes(WiredNetwork network) {
return ((WiredNetworkImpl) network).nodes;
private static Set<WiredNodeImpl> nodes(WiredNetworkImpl network) {
return network.nodes;
}
private static Set<WiredNodeImpl> neighbours(WiredNode node) {

View File

@ -39,20 +39,17 @@ public void Parse_basic_inputs(String input, ComputerSelector expected) throws C
public static Arguments[] getArgumentTestCases() {
return new Arguments[]{
// Legacy selectors
Arguments.of("@some_label", new ComputerSelector("@some_label", OptionalInt.empty(), null, OptionalInt.empty(), "some_label", null, null, null)),
Arguments.of("~normal", new ComputerSelector("~normal", OptionalInt.empty(), null, OptionalInt.empty(), null, ComputerFamily.NORMAL, null, null)),
Arguments.of("#123", new ComputerSelector("#123", OptionalInt.empty(), null, OptionalInt.of(123), null, null, null, null)),
Arguments.of("123", new ComputerSelector("123", OptionalInt.of(123), null, OptionalInt.empty(), null, null, null, null)),
Arguments.of("#123", new ComputerSelector("#123", null, OptionalInt.of(123), null, null, null, null)),
Arguments.of("123", new ComputerSelector("123", null, OptionalInt.of(123), null, null, null, null)),
// New selectors
Arguments.of("@c[]", new ComputerSelector("@c[]", OptionalInt.empty(), null, OptionalInt.empty(), null, null, null, null)),
Arguments.of("@c[instance=5e18f505-62f7-46f8-83f3-792f03224724]", new ComputerSelector("@c[instance=5e18f505-62f7-46f8-83f3-792f03224724]", OptionalInt.empty(), UUID.fromString("5e18f505-62f7-46f8-83f3-792f03224724"), OptionalInt.empty(), null, null, null, null)),
Arguments.of("@c[id=123]", new ComputerSelector("@c[id=123]", OptionalInt.empty(), null, OptionalInt.of(123), null, null, null, null)),
Arguments.of("@c[label=\"foo\"]", new ComputerSelector("@c[label=\"foo\"]", OptionalInt.empty(), null, OptionalInt.empty(), "foo", null, null, null)),
Arguments.of("@c[family=normal]", new ComputerSelector("@c[family=normal]", OptionalInt.empty(), null, OptionalInt.empty(), null, ComputerFamily.NORMAL, null, null)),
Arguments.of("@c[]", new ComputerSelector("@c[]", null, OptionalInt.empty(), null, null, null, null)),
Arguments.of("@c[instance=5e18f505-62f7-46f8-83f3-792f03224724]", new ComputerSelector("@c[instance=5e18f505-62f7-46f8-83f3-792f03224724]", UUID.fromString("5e18f505-62f7-46f8-83f3-792f03224724"), OptionalInt.empty(), null, null, null, null)),
Arguments.of("@c[id=123]", new ComputerSelector("@c[id=123]", null, OptionalInt.of(123), null, null, null, null)),
Arguments.of("@c[label=\"foo\"]", new ComputerSelector("@c[label=\"foo\"]", null, OptionalInt.empty(), "foo", null, null, null)),
Arguments.of("@c[family=normal]", new ComputerSelector("@c[family=normal]", null, OptionalInt.empty(), null, ComputerFamily.NORMAL, null, null)),
// Complex selectors
Arguments.of("@c[ id = 123 , ]", new ComputerSelector("@c[ id = 123 , ]", OptionalInt.empty(), null, OptionalInt.of(123), null, null, null, null)),
Arguments.of("@c[id=123,family=normal]", new ComputerSelector("@c[id=123,family=normal]", OptionalInt.empty(), null, OptionalInt.of(123), null, ComputerFamily.NORMAL, null, null)),
Arguments.of("@c[ id = 123 , ]", new ComputerSelector("@c[ id = 123 , ]", null, OptionalInt.of(123), null, null, null, null)),
Arguments.of("@c[id=123,family=normal]", new ComputerSelector("@c[id=123,family=normal]", null, OptionalInt.of(123), null, ComputerFamily.NORMAL, null, null)),
};
}