mirror of
https://github.com/SquidDev-CC/CC-Tweaked
synced 2025-10-22 17:37:38 +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:
@@ -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);
|
||||
}
|
||||
|
@@ -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));
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user