diff --git a/projects/common/src/main/java/dan200/computercraft/impl/network/wired/WiredNetworkChangeImpl.java b/projects/common/src/main/java/dan200/computercraft/impl/network/wired/WiredNetworkChangeImpl.java index 007b660c1..c302f3b1b 100644 --- a/projects/common/src/main/java/dan200/computercraft/impl/network/wired/WiredNetworkChangeImpl.java +++ b/projects/common/src/main/java/dan200/computercraft/impl/network/wired/WiredNetworkChangeImpl.java @@ -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); diff --git a/projects/common/src/main/java/dan200/computercraft/shared/peripheral/modem/wired/WiredModemLocalPeripheral.java b/projects/common/src/main/java/dan200/computercraft/shared/peripheral/modem/wired/WiredModemLocalPeripheral.java index cb4bdd498..3893b4ac4 100644 --- a/projects/common/src/main/java/dan200/computercraft/shared/peripheral/modem/wired/WiredModemLocalPeripheral.java +++ b/projects/common/src/main/java/dan200/computercraft/shared/peripheral/modem/wired/WiredModemLocalPeripheral.java @@ -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); } } diff --git a/projects/common/src/main/java/dan200/computercraft/shared/peripheral/modem/wired/WiredModemPeripheral.java b/projects/common/src/main/java/dan200/computercraft/shared/peripheral/modem/wired/WiredModemPeripheral.java index 96e2ef1ed..2255fdf25 100644 --- a/projects/common/src/main/java/dan200/computercraft/shared/peripheral/modem/wired/WiredModemPeripheral.java +++ b/projects/common/src/main/java/dan200/computercraft/shared/peripheral/modem/wired/WiredModemPeripheral.java @@ -218,8 +218,7 @@ public abstract class WiredModemPeripheral extends ModemPeripheral implements Wi ConcurrentMap 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()) { diff --git a/projects/common/src/main/java/dan200/computercraft/shared/turtle/core/TurtleBrain.java b/projects/common/src/main/java/dan200/computercraft/shared/turtle/core/TurtleBrain.java index aa5f99b18..64f484198 100644 --- a/projects/common/src/main/java/dan200/computercraft/shared/turtle/core/TurtleBrain.java +++ b/projects/common/src/main/java/dan200/computercraft/shared/turtle/core/TurtleBrain.java @@ -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 { diff --git a/projects/core/src/main/java/dan200/computercraft/core/computer/Environment.java b/projects/core/src/main/java/dan200/computercraft/core/computer/Environment.java index 467faf06f..15d38bfa1 100644 --- a/projects/core/src/main/java/dan200/computercraft/core/computer/Environment.java +++ b/projects/core/src/main/java/dan200/computercraft/core/computer/Environment.java @@ -11,6 +11,7 @@ import dan200.computercraft.core.apis.IAPIEnvironment; import dan200.computercraft.core.filesystem.FileSystem; import dan200.computercraft.core.metrics.MetricsObserver; 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.Int2ObjectOpenHashMap; @@ -268,9 +269,7 @@ public final class Environment implements IAPIEnvironment { synchronized (peripherals) { var index = side.ordinal(); var existing = peripherals[index]; - if ((existing == null && peripheral != null) || - (existing != null && peripheral == null) || - (existing != null && !existing.equals(peripheral))) { + if (!PeripheralHelpers.equals(existing, peripheral)) { peripherals[index] = peripheral; if (peripheralListener != null) peripheralListener.onPeripheralChanged(side, peripheral); } diff --git a/projects/core/src/main/java/dan200/computercraft/core/util/PeripheralHelpers.java b/projects/core/src/main/java/dan200/computercraft/core/util/PeripheralHelpers.java new file mode 100644 index 000000000..6ac48da4e --- /dev/null +++ b/projects/core/src/main/java/dan200/computercraft/core/util/PeripheralHelpers.java @@ -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)); + } +}