Florian Nücke | 61f53b9 | 2013-08-20 21:03:38 +0200 | [diff] [blame] | 1 | /** |
| 2 | * This file is part of the public ComputerCraft API - http://www.computercraft.info |
| 3 | * Copyright Daniel Ratcliffe, 2011-2013. This API may be redistributed unmodified and in full only. |
| 4 | * For help using the API, and posting your mods, visit the forums at computercraft.info. |
| 5 | */ |
| 6 | |
| 7 | package dan200.computer.api; |
| 8 | |
| 9 | /** |
| 10 | * The interface that defines a peripheral. This should be implemented by the |
| 11 | * TileEntity of any block that you wish to be interacted with by |
| 12 | * computer or turtle. |
| 13 | */ |
| 14 | public interface IPeripheral |
| 15 | { |
| 16 | /** |
| 17 | * Should return a string that uniquely identifies this type of peripheral. |
| 18 | * This can be queried from lua by calling peripheral.getType() |
| 19 | * @return A string identifying the type of peripheral. |
| 20 | */ |
| 21 | public String getType(); |
| 22 | |
| 23 | /** |
| 24 | * Should return an array of strings that identify the methods that this |
| 25 | * peripheral exposes to Lua. This will be called once before each attachment, |
| 26 | * and should not change when called multiple times. |
| 27 | * @return An array of strings representing method names. |
| 28 | * @see #callMethod |
| 29 | */ |
| 30 | public String[] getMethodNames(); |
| 31 | |
| 32 | /** |
| 33 | * This is called when a lua program on an attached computer calls peripheral.call() with |
| 34 | * one of the methods exposed by getMethodNames().<br> |
| 35 | * <br> |
| 36 | * Be aware that this will be called from the ComputerCraft Lua thread, and must be thread-safe |
| 37 | * when interacting with minecraft objects. |
| 38 | * @param computer The interface to the computer that is making the call. Remember that multiple |
| 39 | * computers can be attached to a peripheral at once. |
| 40 | * @param context The context of the currently running lua thread. This can be used to wait for events |
| 41 | * or otherwise yield. |
| 42 | * @param method An integer identifying which of the methods from getMethodNames() the computer |
| 43 | * wishes to call. The integer indicates the index into the getMethodNames() table |
| 44 | * that corresponds to the string passed into peripheral.call() |
| 45 | * @param arguments An array of objects, representing the arguments passed into peripheral.call().<br> |
| 46 | * Lua values of type "string" will be represented by Object type String.<br> |
| 47 | * Lua values of type "number" will be represented by Object type Double.<br> |
| 48 | * Lua values of type "boolean" will be represented by Object type Boolean.<br> |
| 49 | * Lua values of any other type will be represented by a null object.<br> |
| 50 | * This array will be empty if no arguments are passed. |
| 51 | * @return An array of objects, representing values you wish to return to the lua program.<br> |
| 52 | * Integers, Doubles, Floats, Strings, Booleans and null be converted to their corresponding lua type.<br> |
| 53 | * All other types will be converted to nil.<br> |
| 54 | * You may return null to indicate no values should be returned. |
| 55 | * @throws Exception If you throw any exception from this function, a lua error will be raised with the |
| 56 | * same message as your exception. Use this to throw appropriate errors if the wrong |
| 57 | * arguments are supplied to your method. |
| 58 | * @see #getMethodNames |
| 59 | */ |
| 60 | public Object[] callMethod( IComputerAccess computer, ILuaContext context, int method, Object[] arguments ) throws Exception; |
| 61 | |
| 62 | /** |
| 63 | * Is called before the computer attempts to attach to the peripheral, and should return whether to allow |
| 64 | * the attachment. Use this to restrict the number of computers that can attach, or to limit attachments to |
| 65 | * certain world directions.<br> |
| 66 | * If true is returned, attach() will be called shortly afterwards, and the computer will be able to make method calls. |
| 67 | * If false is returned, attach() will not be called, and the peripheral will be invisible to the computer. |
| 68 | * @param side The world direction (0=bottom, 1=top, etc) that the computer lies relative to the peripheral. |
| 69 | * @return Whether to allow the attachment, as a boolean. |
| 70 | * @see #attach |
| 71 | */ |
| 72 | public boolean canAttachToSide( int side ); |
| 73 | |
| 74 | /** |
| 75 | * Is called when canAttachToSide has returned true, and a computer is attaching to the peripheral. |
| 76 | * This will occur when a peripheral is placed next to an active computer, when a computer is turned on next to a peripheral, |
| 77 | * or when a turtle travels into a square next to a peripheral. |
| 78 | * Between calls to attach() and detach(), the attached computer can make method calls on the peripheral using peripheral.call(). |
| 79 | * This method can be used to keep track of which computers are attached to the peripheral, or to take action when attachment |
| 80 | * occurs.<br> |
| 81 | * <br> |
| 82 | * Be aware that this will be called from the ComputerCraft Lua thread, and must be thread-safe |
| 83 | * when interacting with minecraft objects. |
| 84 | * @param computer The interface to the computer that is being attached. Remember that multiple |
| 85 | * computers can be attached to a peripheral at once. |
| 86 | * @see #canAttachToSide |
| 87 | * @see #detach |
| 88 | */ |
| 89 | public void attach( IComputerAccess computer ); |
| 90 | |
| 91 | /** |
| 92 | * Is called when a computer is detaching from the peripheral. |
| 93 | * This will occur when a computer shuts down, when the peripheral is removed while attached to computers, |
| 94 | * or when a turtle moves away from a square attached to a peripheral. |
| 95 | * This method can be used to keep track of which computers are attached to the peripheral, or to take action when detachment |
| 96 | * occurs.<br> |
| 97 | * <br> |
| 98 | * Be aware that this will be called from the ComputerCraft Lua thread, and must be thread-safe |
| 99 | * when interacting with minecraft objects. |
| 100 | * @param computer The interface to the computer that is being detached. Remember that multiple |
| 101 | * computers can be attached to a peripheral at once. |
| 102 | * @see #canAttachToSide |
| 103 | * @see #detach |
| 104 | */ |
| 105 | public void detach( IComputerAccess computer ); |
| 106 | } |