1
0
mirror of https://github.com/SquidDev-CC/CC-Tweaked synced 2025-01-10 17:30:29 +00:00

Add a method for checking peripheral equality

This feels a little overkill, but nice to standardise how this code
looks.

There's a bit of me which wonders if we should remove
IPeripheral.equals, and just use Object.equals, but I do also kinda like
the explicitness of the current interface? IDK.
This commit is contained in:
Jonathan Coates 2024-03-16 14:01:22 +00:00
parent 61f9b1d0c6
commit f8ef40d378
No known key found for this signature in database
GPG Key ID: B9E431FF07C98D06
6 changed files with 38 additions and 8 deletions

View File

@ -6,6 +6,7 @@ package dan200.computercraft.impl.network.wired;
import dan200.computercraft.api.network.wired.WiredNetworkChange; import dan200.computercraft.api.network.wired.WiredNetworkChange;
import dan200.computercraft.api.peripheral.IPeripheral; import dan200.computercraft.api.peripheral.IPeripheral;
import dan200.computercraft.core.util.PeripheralHelpers;
import java.util.Collections; import java.util.Collections;
import java.util.HashMap; import java.util.HashMap;
@ -52,7 +53,7 @@ final class WiredNetworkChangeImpl implements WiredNetworkChange {
var oldValue = entry.getValue(); var oldValue = entry.getValue();
if (newPeripherals.containsKey(oldKey)) { if (newPeripherals.containsKey(oldKey)) {
var rightValue = added.get(oldKey); var rightValue = added.get(oldKey);
if (oldValue.equals(rightValue)) { if (PeripheralHelpers.equals(oldValue, rightValue)) {
added.remove(oldKey); added.remove(oldKey);
} else { } else {
removed.put(oldKey, oldValue); removed.put(oldKey, oldValue);

View File

@ -6,6 +6,7 @@ package dan200.computercraft.shared.peripheral.modem.wired;
import dan200.computercraft.api.ComputerCraftTags; import dan200.computercraft.api.ComputerCraftTags;
import dan200.computercraft.api.peripheral.IPeripheral; import dan200.computercraft.api.peripheral.IPeripheral;
import dan200.computercraft.core.util.PeripheralHelpers;
import dan200.computercraft.shared.computer.core.ServerContext; import dan200.computercraft.shared.computer.core.ServerContext;
import dan200.computercraft.shared.platform.ComponentAccess; import dan200.computercraft.shared.platform.ComponentAccess;
import net.minecraft.core.BlockPos; import net.minecraft.core.BlockPos;
@ -65,7 +66,7 @@ public final class WiredModemLocalPeripheral {
this.id = ServerContext.get(assertNonNull(world.getServer())).getNextId("peripheral." + type); this.id = ServerContext.get(assertNonNull(world.getServer())).getNextId("peripheral." + type);
} }
return oldPeripheral == null || !oldPeripheral.equals(peripheral); return !PeripheralHelpers.equals(oldPeripheral, peripheral);
} }
} }

View File

@ -218,8 +218,7 @@ public abstract class WiredModemPeripheral extends ModemPeripheral implements Wi
ConcurrentMap<String, RemotePeripheralWrapper> wrappers; ConcurrentMap<String, RemotePeripheralWrapper> wrappers;
synchronized (peripheralWrappers) { synchronized (peripheralWrappers) {
wrappers = peripheralWrappers.get(computer); wrappers = peripheralWrappers.computeIfAbsent(computer, k -> new ConcurrentHashMap<>());
if (wrappers == null) peripheralWrappers.put(computer, wrappers = new ConcurrentHashMap<>());
} }
synchronized (modem.getRemotePeripherals()) { synchronized (modem.getRemotePeripherals()) {

View File

@ -14,6 +14,7 @@ import dan200.computercraft.api.turtle.TurtleCommand;
import dan200.computercraft.api.turtle.TurtleSide; import dan200.computercraft.api.turtle.TurtleSide;
import dan200.computercraft.api.upgrades.UpgradeData; import dan200.computercraft.api.upgrades.UpgradeData;
import dan200.computercraft.core.computer.ComputerSide; import dan200.computercraft.core.computer.ComputerSide;
import dan200.computercraft.core.util.PeripheralHelpers;
import dan200.computercraft.impl.TurtleUpgrades; import dan200.computercraft.impl.TurtleUpgrades;
import dan200.computercraft.shared.computer.core.ComputerFamily; import dan200.computercraft.shared.computer.core.ComputerFamily;
import dan200.computercraft.shared.computer.core.ServerComputer; import dan200.computercraft.shared.computer.core.ServerComputer;
@ -589,7 +590,7 @@ public class TurtleBrain implements TurtleAccessInternal {
} }
var existing = peripherals.get(side); var existing = peripherals.get(side);
if (existing == peripheral || (existing != null && peripheral != null && existing.equals(peripheral))) { if (PeripheralHelpers.equals(existing, peripheral)) {
// If the peripheral is the same, just use that. // If the peripheral is the same, just use that.
peripheral = existing; peripheral = existing;
} else { } else {

View File

@ -11,6 +11,7 @@ import dan200.computercraft.core.apis.IAPIEnvironment;
import dan200.computercraft.core.filesystem.FileSystem; import dan200.computercraft.core.filesystem.FileSystem;
import dan200.computercraft.core.metrics.MetricsObserver; import dan200.computercraft.core.metrics.MetricsObserver;
import dan200.computercraft.core.terminal.Terminal; import dan200.computercraft.core.terminal.Terminal;
import dan200.computercraft.core.util.PeripheralHelpers;
import it.unimi.dsi.fastutil.ints.Int2ObjectMap; import it.unimi.dsi.fastutil.ints.Int2ObjectMap;
import it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap; import it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap;
@ -268,9 +269,7 @@ public final class Environment implements IAPIEnvironment {
synchronized (peripherals) { synchronized (peripherals) {
var index = side.ordinal(); var index = side.ordinal();
var existing = peripherals[index]; var existing = peripherals[index];
if ((existing == null && peripheral != null) || if (!PeripheralHelpers.equals(existing, peripheral)) {
(existing != null && peripheral == null) ||
(existing != null && !existing.equals(peripheral))) {
peripherals[index] = peripheral; peripherals[index] = peripheral;
if (peripheralListener != null) peripheralListener.onPeripheralChanged(side, peripheral); if (peripheralListener != null) peripheralListener.onPeripheralChanged(side, peripheral);
} }

View File

@ -0,0 +1,29 @@
// SPDX-FileCopyrightText: 2023 The CC: Tweaked Developers
//
// SPDX-License-Identifier: MPL-2.0
package dan200.computercraft.core.util;
import dan200.computercraft.api.peripheral.IPeripheral;
import javax.annotation.Nullable;
/**
* Utilities for working with {@linkplain IPeripheral peripherals}.
*/
public final class PeripheralHelpers {
private PeripheralHelpers() {
}
/**
* Determine if two peripherals are equal. This is equivalent to {@link java.util.Objects#equals(Object, Object)},
* but using {@link IPeripheral#equals(IPeripheral)} instead.
*
* @param a The first peripheral.
* @param b The second peripheral.
* @return If the two peripherals are equal.
*/
public static boolean equals(@Nullable IPeripheral a, @Nullable IPeripheral b) {
return a == b || (a != null && b != null && a.equals(b));
}
}