Interface IPeripheral

All Known Subinterfaces:
IDynamicPeripheral

public interface IPeripheral
A peripheral is an external device that a computer can interact with.

Peripherals can be supplied by both a block (or block entity), or from turtle or pocket upgrades.

See the package documentation for more information on registering peripherals.

Peripherals should provide a series of methods to the user, typically by annotating Java methods with LuaFunction. Alternatively, IDynamicPeripheral may be used to provide a dynamic set of methods. Remember that peripheral methods are called on the computer thread, and so it is not safe to interact with the Minecraft world by default. One should use LuaFunction.mainThread() or ILuaContext.executeMainThreadTask(LuaTask) to run code on the main server thread.

  • Method Summary

    Modifier and Type
    Method
    Description
    default void
    Is called when a computer is attaching to the peripheral.
    default void
    Called when a computer is detaching from the peripheral.
    boolean
    equals(@Nullable IPeripheral other)
    Determine whether this peripheral is equivalent to another one.
    default Set<String>
    Return additional types/traits associated with this object.
    default @Nullable Object
    Get the object that this peripheral provides methods for.
    Should return a string that uniquely identifies this type of peripheral.
  • Method Details

    • getType

      String getType()
      Should return a string that uniquely identifies this type of peripheral. This can be queried from lua by calling peripheral.getType()
      Returns:
      A string identifying the type of peripheral.
      See Also:
    • getAdditionalTypes

      default Set<String> getAdditionalTypes()
      Return additional types/traits associated with this object.
      Returns:
      A collection of additional object traits.
      See Also:
    • attach

      default void attach(IComputerAccess computer)
      Is called when a computer is attaching to the peripheral.

      This will occur when a peripheral is placed next to an active computer, when a computer is turned on next to a peripheral, when a turtle travels into a square next to a peripheral, or when a wired modem adjacent to this peripheral is does any of the above.

      Between calls to attach and detach(dan200.computercraft.api.peripheral.IComputerAccess), the attached computer can make method calls on the peripheral using peripheral.call(). This method can be used to keep track of which computers are attached to the peripheral, or to take action when attachment occurs.

      Be aware that may be called from both the server thread and ComputerCraft Lua thread, and so must be thread-safe and reentrant. If you need to store a list of attached computers, it is recommended you use a AttachedComputerSet.

      Parameters:
      computer - The interface to the computer that is being attached. Remember that multiple computers can be attached to a peripheral at once.
      See Also:
    • detach

      default void detach(IComputerAccess computer)
      Called when a computer is detaching from the peripheral.

      This will occur when a computer shuts down, when the peripheral is removed while attached to computers, when a turtle moves away from a block attached to a peripheral, or when a wired modem adjacent to this peripheral is detached.

      This method can be used to keep track of which computers are attached to the peripheral, or to take action when detachment occurs.

      Be aware that this may be called from both the server and ComputerCraft Lua thread, and must be thread-safe and reentrant. If you need to store a list of attached computers, it is recommended you use a AttachedComputerSet.

      Parameters:
      computer - The interface to the computer that is being detached. Remember that multiple computers can be attached to a peripheral at once.
      See Also:
    • getTarget

      default @Nullable Object getTarget()
      Get the object that this peripheral provides methods for. This will generally be the block entity or block, but may be an inventory, entity, etc...
      Returns:
      The object this peripheral targets
    • equals

      boolean equals(@Nullable IPeripheral other)
      Determine whether this peripheral is equivalent to another one.

      The minimal example should at least check whether they are the same object. However, you may wish to check if they point to the same block or block entity.

      Parameters:
      other - The peripheral to compare against. This may be null.
      Returns:
      Whether these peripherals are equivalent.