Class AttachedComputerSet

java.lang.Object
dan200.computercraft.api.peripheral.AttachedComputerSet

public final class AttachedComputerSet extends Object
A thread-safe collection of computers.

This collection is intended to be used by peripherals that need to maintain a set of all attached computers.

It is recommended to use over Java's built-in concurrent collections (e.g. CopyOnWriteArraySet or ConcurrentHashMap), as AttachedComputerSet ensures that computers cannot be accessed after they are detached, guaranteeing that NotAttachedExceptions will not be thrown.

To ensure this, AttachedComputerSet is not directly iterable, as we cannot ensure that computers are not detached while the iterator is running (and so trying to use the computer would error). Instead, computers should be looped over using forEach(Consumer).

Example Link icon

public class ComputerTrackingPeripheral implements IPeripheral {
    private final AttachedComputerSet computers = new AttachedComputerSet();

    @Override
    public void attach(IComputerAccess computer) {
        computers.add(computer);
    }

    @Override
    public void detach(IComputerAccess computer) {
        computers.remove(computer);
    }

    @LuaFunction
    public final void sayHello() {
        // Queue a "hello" event on each computer.
        computers.forEach(x -> x.queueEvent("hello", x.getAttachmentName()));
    }

    @Override
    public String getType() {
        return "my_peripheral";
    }

    @Override
    public boolean equals(@Nullable IPeripheral other) {
        return this == other;
    }
}
See Also:
  • Constructor Details Link icon

    • AttachedComputerSet Link icon

      public AttachedComputerSet()
  • Method Details Link icon

    • add Link icon

      public void add(IComputerAccess computer)
      Add a computer to this collection of computers. This should be called from IPeripheral.attach(IComputerAccess).
      Parameters:
      computer - The computer to add.
    • remove Link icon

      public void remove(IComputerAccess computer)
      Remove a computer from this collection of computers. This should be called from IPeripheral.detach(IComputerAccess).
      Parameters:
      computer - The computer to remove.
    • forEach Link icon

      public void forEach(Consumer<? super IComputerAccess> action)
      Apply an action to each computer in this collection.
      Parameters:
      action - The action to apply.
    • queueEvent Link icon

      public void queueEvent(String event, @Nullable Object... arguments)
      Queue an event on all computers.
      Parameters:
      event - The name of the event to queue.
      arguments - The arguments for this event.
      See Also:
    • hasComputers Link icon

      public boolean hasComputers()
      Determine if this collection contains any computers.

      This method is primarily intended for presentation purposes (such as rendering an icon in the UI if a computer is attached to your peripheral). Due to the multi-threaded nature of peripherals, it is not recommended to guard any logic behind this check.

      For instance, if(computers.hasComputers()) computers.queueEvent("foo"); contains a race condition, as there's no guarantee that any computers are still attached within the body of the if statement.

      Returns:
      Whether this collection is non-empty.