Overview | Package | Class | Tree | Deprecated | Index | Help
PREV CLASS NEXT CLASS | FRAMES NO FRAMES
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD

## dan200.computer.api
Interface IPeripheral

All Known Subinterfaces:
  IHostedPeripheral

    public interface IPeripheral

The interface that defines a peripheral. This should be implemented by the TileEntity of any block that you wish to be interacted with by computer or turtle.

Method Summary
`void` | `attach(IComputerAccess computer)` Is called when canAttachToSide has returned true, and a computer is attaching to the peripheral.
`java.lang.Object[]` | `callMethod(IComputerAccess computer, ILuaContext context, int method, java.lang.Object[] arguments)` This is called when a lua program on an attached computer calls peripheral.call() with one of the methods exposed by getMethodNames(). Be aware that this will be called from the ComputerCraft Lua thread, and must be thread-safe when interacting with minecraft objects.
`boolean` | `canAttachToSide(int side)` Is called before the computer attempts to attach to the peripheral, and should return whether to allow the attachment.
`void` | `detach(IComputerAccess computer)` Is called when a computer is detaching from the peripheral.
`java.lang.String[]` | `getMethodNames()` Should return an array of strings that identify the methods that this peripheral exposes to Lua.
`java.lang.String` | `getType()` Should return a string that uniquely identifies this type of peripheral.

Method Detail

### getType

    java.lang.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.

### getMethodNames

    java.lang.String[] getMethodNames()

  Should return an array of strings that identify the methods that this peripheral exposes to Lua. This will be called once before each attachment, and should not change when called multiple times.

  Returns:
    An array of strings representing method names.
  See Also:
    `callMethod(dan200.computer.api.IComputerAccess, dan200.computer.api.ILuaContext, int, java.lang.Object[])`

### callMethod

    java.lang.Object[] callMethod(IComputerAccess computer,
                                  ILuaContext context,
                                  int method,
                                  java.lang.Object[] arguments)
                                  throws java.lang.Exception

  This is called when a lua program on an attached computer calls peripheral.call() with one of the methods exposed by getMethodNames().
  Be aware that this will be called from the ComputerCraft Lua thread, and must be thread-safe when interacting with minecraft objects.

  Parameters:
    `computer` - The interface to the computer that is making the call. Remember that multiple computers can be attached to a peripheral at once.
    `context` - The context of the currently running lua thread. This can be used to wait for events or otherwise yield.
    `method` - An integer identifying which of the methods from getMethodNames() the computer wishes to call. The integer indicates the index into the getMethodNames() table that corresponds to the string passed into peripheral.call()
    `arguments` - An array of objects, representing the arguments passed into peripheral.call().
    Lua values of type "string" will be represented by Object type String.
    Lua values of type "number" will be represented by Object type Double.
    Lua values of type "boolean" will be represented by Object type Boolean.
    Lua values of any other type will be represented by a null object.
    This array will be empty if no arguments are passed.
  Returns:
    An array of objects, representing values you wish to return to the lua program.
    Integers, Doubles, Floats, Strings, Booleans and null be converted to their corresponding lua type.
    All other types will be converted to nil.
    You may return null to indicate no values should be returned.
  Throws:
    `java.lang.Exception` - If you throw any exception from this function, a lua error will be raised with the same message as your exception. Use this to throw appropriate errors if the wrong arguments are supplied to your method.
  See Also:
    `getMethodNames()`

### canAttachToSide

    boolean canAttachToSide(int side)

  Is called before the computer attempts to attach to the peripheral, and should return whether to allow the attachment. Use this to restrict the number of computers that can attach, or to limit attachments to certain world directions.
  If true is returned, attach() will be called shortly afterwards, and the computer will be able to make method calls. If false is returned, attach() will not be called, and the peripheral will be invisible to the computer.

  Parameters:
    `side` - The world direction (0=bottom, 1=top, etc) that the computer lies relative to the peripheral.
  Returns:
    Whether to allow the attachment, as a boolean.
  See Also:
    `attach(dan200.computer.api.IComputerAccess)`

### attach

    void attach(IComputerAccess computer)

  Is called when canAttachToSide has returned true, and 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, or when a turtle travels into a square next to a peripheral. Between calls to attach() and detach(), 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 this will be called from the ComputerCraft Lua thread, and must be thread-safe when interacting with minecraft objects.

  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:
    `canAttachToSide(int)`, `detach(dan200.computer.api.IComputerAccess)`

### detach

    void detach(IComputerAccess computer)

  Is 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, or when a turtle moves away from a square attached to a peripheral. 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 will be called from the ComputerCraft Lua thread, and must be thread-safe when interacting with minecraft objects.

  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:
    `canAttachToSide(int)`, `detach(dan200.computer.api.IComputerAccess)`

Overview | Package | Class | Tree | Deprecated | Index | Help
PREV CLASS NEXT CLASS | FRAMES NO FRAMES
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD