1
0
mirror of https://github.com/SquidDev-CC/CC-Tweaked synced 2025-10-30 05:12:58 +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
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.peripheral.IPeripheral;
import dan200.computercraft.core.util.PeripheralHelpers;
import java.util.Collections;
import java.util.HashMap;
@@ -52,7 +53,7 @@ final class WiredNetworkChangeImpl implements WiredNetworkChange {
var oldValue = entry.getValue();
if (newPeripherals.containsKey(oldKey)) {
var rightValue = added.get(oldKey);
if (oldValue.equals(rightValue)) {
if (PeripheralHelpers.equals(oldValue, rightValue)) {
added.remove(oldKey);
} else {
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.peripheral.IPeripheral;
import dan200.computercraft.core.util.PeripheralHelpers;
import dan200.computercraft.shared.computer.core.ServerContext;
import dan200.computercraft.shared.platform.ComponentAccess;
import net.minecraft.core.BlockPos;
@@ -65,7 +66,7 @@ public final class WiredModemLocalPeripheral {
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;
synchronized (peripheralWrappers) {
wrappers = peripheralWrappers.get(computer);
if (wrappers == null) peripheralWrappers.put(computer, wrappers = new ConcurrentHashMap<>());
wrappers = peripheralWrappers.computeIfAbsent(computer, k -> new ConcurrentHashMap<>());
}
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.upgrades.UpgradeData;
import dan200.computercraft.core.computer.ComputerSide;
import dan200.computercraft.core.util.PeripheralHelpers;
import dan200.computercraft.impl.TurtleUpgrades;
import dan200.computercraft.shared.computer.core.ComputerFamily;
import dan200.computercraft.shared.computer.core.ServerComputer;
@@ -589,7 +590,7 @@ public class TurtleBrain implements TurtleAccessInternal {
}
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.
peripheral = existing;
} else {