diff --git a/MODSRC/dan200/computer/api/ComputerCraftAPI.java b/MODSRC/dan200/computer/api/ComputerCraftAPI.java new file mode 100644 index 00000000..f0403079 --- /dev/null +++ b/MODSRC/dan200/computer/api/ComputerCraftAPI.java @@ -0,0 +1,164 @@ +/** + * This file is part of the public ComputerCraft API - http://www.computercraft.info + * Copyright Daniel Ratcliffe, 2011-2013. This API may be redistributed unmodified and in full only. + * For help using the API, and posting your mods, visit the forums at computercraft.info. + */ + +package dan200.computer.api; +import java.lang.reflect.Method; + +import net.minecraft.world.World; + +/** + * The static entry point to the ComputerCraft API. + * Members in this class must be called after mod_ComputerCraft has been initialised, + * but may be called before it is fully loaded. + */ +public class ComputerCraftAPI +{ + /** + * Creates a numbered directory in a subfolder of the save directory for a given world, and returns that number.
+ * Use in conjuction with createSaveDirMount() to create a unique place for your peripherals or media items to store files.
+ * @param world The world for which the save dir should be created. This should be the serverside world object. + * @param parentSubPath The folder path within the save directory where the new directory should be created. eg: "computer/disk" + * @return The numerical value of the name of the new folder, or -1 if the folder could not be created for some reason.
+ * eg: if createUniqueNumberedSaveDir( world, "computer/disk" ) was called returns 42, then "computer/disk/42" is now available for writing. + * @see #createSaveDirMount(World, String) + */ + public static int createUniqueNumberedSaveDir( World world, String parentSubPath ) + { + findCC(); + if( computerCraft_createUniqueNumberedSaveDir != null ) + { + try { + return ((Integer)computerCraft_createUniqueNumberedSaveDir.invoke( null, world, parentSubPath )).intValue(); + } catch (Exception e){ + // It failed + } + } + return -1; + } + + /** + * Creates a file system mount that maps to a subfolder of the save directory for a given world, and returns it.
+ * Use in conjuction with IComputerAccess.mount() or IComputerAccess.mountWritable() to mount a folder from the + * users save directory onto a computers file system.
+ * @param world The world for which the save dir can be found. This should be the serverside world object. + * @param subPath The folder path within the save directory that the mount should map to. eg: "computer/disk/42".
+ * Use createUniqueNumberedSaveDir() to create a new numbered folder to use. + * @param capacity The ammount of data that can be stored in the directory before it fills up, in bytes. + * @return The mount, or null if it could be created for some reason. Use IComputerAccess.mount() or IComputerAccess.mountWritable() + * to mount this on a Computers' file system. + * @see #createUniqueNumberedSaveDir(World, String) + * @see IComputerAccess#mount(String, IMount) + * @see IComputerAccess#mountWritable(String, IWritableMount) + * @see IMount + * @see IMountWritable + */ + public static IWritableMount createSaveDirMount( World world, String subPath, long capacity ) + { + findCC(); + if( computerCraft_createSaveDirMount != null ) + { + try { + return (IWritableMount)computerCraft_createSaveDirMount.invoke( null, world, subPath, capacity ); + } catch (Exception e){ + // It failed + } + } + return null; + } + + /** + * Creates a file system mount to a resource folder, and returns it.
+ * Use in conjuction with IComputerAccess.mount() or IComputerAccess.mountWritable() to mount a resource folder onto a computers file system.
+ * The files in this mount will be a combination of files in the specified mod jar, and resource packs that contain resources with the same domain and path.
+ * @param class A class in whose jar to look first for the resources to mount. Using your main mod class is recommended. eg: MyMod.class + * @param domain The domain under which to look for resources. eg: "mymod" + * @param subPath The domain under which to look for resources. eg: "mymod/lua/myfiles" + * @return The mount, or null if it could be created for some reason. Use IComputerAccess.mount() or IComputerAccess.mountWritable() + * to mount this on a Computers' file system. + * @see IComputerAccess#mount(String, IMount) + * @see IComputerAccess#mountWritable(String, IMountWritable) + * @see IMount + */ + public static IMount createResourceMount( Class modClass, String domain, String subPath ) + { + findCC(); + if( computerCraft_createResourceMount != null ) + { + try { + return (IMount)computerCraft_createResourceMount.invoke( null, modClass, domain, subPath ); + } catch (Exception e){ + // It failed + } + } + return null; + } + + /** + * Registers a peripheral handler for a TileEntity that you do not have access to. Only + * use this if you want to expose IPeripheral on a TileEntity from another mod. For your own + * mod, just implement IPeripheral on the TileEntity directly. + * @see IPeripheral + * @see IPeripheralHandler + */ + public static void registerExternalPeripheral( Class clazz, IPeripheralHandler handler ) + { + findCC(); + if (computerCraft_registerExternalPeripheral != null) + { + try { + computerCraft_registerExternalPeripheral.invoke(null, clazz, handler); + } catch (Exception e){ + // It failed + } + } + } + + // The functions below here are private, and are used to interface with the non-API ComputerCraft classes. + // Reflection is used here so you can develop your mod in MCP without decompiling ComputerCraft and including + // it in your solution. + + private static void findCC() + { + if( !ccSearched ) { + try { + computerCraft = Class.forName( "dan200.ComputerCraft" ); + computerCraft_createUniqueNumberedSaveDir = findCCMethod( "createUniqueNumberedSaveDir", new Class[] { + World.class, String.class + } ); + computerCraft_createSaveDirMount = findCCMethod( "createSaveDirMount", new Class[] { + World.class, String.class, Long.TYPE + } ); + computerCraft_createResourceMount = findCCMethod( "createResourceMount", new Class[] { + Class.class, String.class, String.class + } ); + computerCraft_registerExternalPeripheral = findCCMethod( "registerExternalPeripheral", new Class[] { + Class.class, IPeripheralHandler.class + } ); + } catch( Exception e ) { + net.minecraft.server.MinecraftServer.getServer().logInfo( "ComputerCraftAPI: ComputerCraft not found." ); + } finally { + ccSearched = true; + } + } + } + + private static Method findCCMethod( String name, Class[] args ) + { + try { + return computerCraft.getMethod( name, args ); + } catch( NoSuchMethodException e ) { + net.minecraft.server.MinecraftServer.getServer().logInfo( "ComputerCraftAPI: ComputerCraft method " + name + " not found." ); + return null; + } + } + + private static boolean ccSearched = false; + private static Class computerCraft = null; + private static Method computerCraft_createUniqueNumberedSaveDir = null; + private static Method computerCraft_createSaveDirMount = null; + private static Method computerCraft_createResourceMount = null; + private static Method computerCraft_registerExternalPeripheral = null; +} diff --git a/MODSRC/dan200/computer/api/IComputerAccess.java b/MODSRC/dan200/computer/api/IComputerAccess.java new file mode 100644 index 00000000..2eac5d0b --- /dev/null +++ b/MODSRC/dan200/computer/api/IComputerAccess.java @@ -0,0 +1,91 @@ +/** + * This file is part of the public ComputerCraft API - http://www.computercraft.info + * Copyright Daniel Ratcliffe, 2011-2013. This API may be redistributed unmodified and in full only. + * For help using the API, and posting your mods, visit the forums at computercraft.info. + */ + +package dan200.computer.api; + +import net.minecraft.world.World; + +/** + * The interface passed to peripherals by computers or turtles, providing methods + * that they can call. This should not be implemented by your classes. Do not interact + * with computers except via this interface. + */ +public interface IComputerAccess +{ + /** + * Mount a mount onto the computers' file system in a read only mode.
+ * @param desiredLoction The location on the computer's file system where you would like the mount to be mounted. + * @param mount The mount object to mount on the computer. These can be obtained by calling ComputerCraftAPI.createSaveDirMount(), ComputerCraftAPI.createResourceMount() or by creating your own objects that implement the IMount interface. + * @return The location on the computer's file system where you the mount mounted, or null if there was already a file in the desired location. Store this value if you wish to unmount the mount later. + * @see ComputerCraftAPI#createSaveDirMount(World, String) + * @see ComputerCraftAPI#createResourceMount(Class, String, String) + * @see #mountWritable(String, IWritableMount) + * @see #unmount(String) + * @see IMount + */ + public String mount( String desiredLocation, IMount mount ); + + /** + * Mount a mount onto the computers' file system in a writable mode.
+ * @param desiredLoction The location on the computer's file system where you would like the mount to be mounted. + * @param mount The mount object to mount on the computer. These can be obtained by calling ComputerCraftAPI.createSaveDirMount() or by creating your own objects that implement the IWritableMount interface. + * @return The location on the computer's file system where you the mount mounted, or null if there was already a file in the desired location. Store this value if you wish to unmount the mount later. + * @see ComputerCraftAPI#createSaveDirMount(World, String) + * @see ComputerCraftAPI#createResourceMount(Class, String, String) + * @see #mount(String, IMount) + * @see #unmount(String) + * @see IMount + */ + public String mountWritable( String desiredLocation, IWritableMount mount ); + + /** + * Unmounts a directory previously mounted onto the computers file system by mount() or mountWritable().
+ * When a directory is unmounted, it will disappear from the computers file system, and the user will no longer be able to + * access it. All directories mounted by a mount or mountWritable are automatically unmounted when the peripheral + * is attached if they have not been explicitly unmounted. + * @param location The desired location in the computers file system of the directory to unmount. + * This must be the location of a directory previously mounted by mount() or mountWritable(), as + * indicated by their return value. + * @see #mount(String, IMount) + * @see #mountWritable(String, IWritableMount) + */ + public void unmount( String location ); + + /** + * Returns the numerical ID of this computer.
+ * This is the same number obtained by calling os.getComputerID() or running the "id" program from lua, + * and is guarunteed unique. This number will be positive. + * @return The identifier. + */ + public int getID(); + + /** + * Causes an event to be raised on this computer, which the computer can respond to by calling + * os.pullEvent(). This can be used to notify the computer when things happen in the world or to + * this peripheral. + * @param event A string identifying the type of event that has occurred, this will be + * returned as the first value from os.pullEvent(). It is recommended that you + * you choose a name that is unique, and recognisable as originating from your + * peripheral. eg: If your peripheral type is "button", a suitable event would be + * "button_pressed". + * @param arguments In addition to a name, you may pass an array of extra arguments to the event, that will + * be supplied as extra return values to os.pullEvent(). Objects in the array will be converted + * to lua data types in the same fashion as the return values of IPeripheral.callMethod().
+ * You may supply null to indicate that no arguments are to be supplied. + * @see IPeripheral#callMethod + */ + public void queueEvent( String event, Object[] arguments ); + + /** + * Get a string, unique to the computer, by which the computer refers to this peripheral. + * For directly attached peripherals this will be "left","right","front","back",etc, but + * for peripherals attached remotely it will be different. It is good practice to supply + * this string when raising events to the computer, so that the computer knows from + * which peripheral the event came. + * @return A string unique to the computer, but not globally. + */ + public String getAttachmentName(); +} diff --git a/MODSRC/dan200/computer/api/IHostedPeripheral.java b/MODSRC/dan200/computer/api/IHostedPeripheral.java new file mode 100644 index 00000000..89902a65 --- /dev/null +++ b/MODSRC/dan200/computer/api/IHostedPeripheral.java @@ -0,0 +1,43 @@ +/** + * This file is part of the public ComputerCraft API - http://www.computercraft.info + * Copyright Daniel Ratcliffe, 2011-2013. This API may be redistributed unmodified and in full only. + * For help using the API, and posting your mods, visit the forums at computercraft.info. + */ + +package dan200.computer.api; + +/** + * A subclass of IPeripheral specifically for peripherals + * created by ITurtleUpgrade's of type Peripheral. When an + * IHostedPeripheral is created, its IPeripheral methods will be called + * just as if the peripheral was a seperate adjacent block in the world, + * and update() will be called once per tick. + * @see ITurtleUpgrade + */ +public interface IHostedPeripheral extends IPeripheral +{ + /** + * A method called on each hosted peripheral once per tick, on the main thread + * over the lifetime of the turtle or block. May be used to update the state + * of the peripheral, and may interact with IComputerAccess or ITurtleAccess + * however it likes at this time. + */ + public void update(); + + /** + * A method called whenever data is read from the Turtle's NBTTag, + * over the lifetime of the turtle. You should only use this for + * reading data you want to stay with the peripheral. + * @param nbttagcompound The peripheral's NBTTag + */ + public void readFromNBT( net.minecraft.nbt.NBTTagCompound nbttagcompound ); + + /** + * A method called whenever data is written to the Turtle's NBTTag, + * over the lifetime of the turtle. You should only use this for + * writing data you want to stay with the peripheral. + * @param nbttagcompound The peripheral's NBTTag. + * @param ID The turtle's ID. + */ + public void writeToNBT( net.minecraft.nbt.NBTTagCompound nbttagcompound ); +} diff --git a/MODSRC/dan200/computer/api/ILuaContext.java b/MODSRC/dan200/computer/api/ILuaContext.java new file mode 100644 index 00000000..2bda0260 --- /dev/null +++ b/MODSRC/dan200/computer/api/ILuaContext.java @@ -0,0 +1,45 @@ +/** + * This file is part of the public ComputerCraft API - http://www.computercraft.info + * Copyright Daniel Ratcliffe, 2011-2013. This API may be redistributed unmodified and in full only. + * For help using the API, and posting your mods, visit the forums at computercraft.info. + */ + +package dan200.computer.api; + +/** + * An interface passed to peripherals and ILuaObjects' by computers or turtles, providing methods + * that allow the peripheral call to wait for events before returning, just like in lua. + * This is very useful if you need to signal work to be performed on the main thread, and don't want to return + * until the work has been completed. + */ +public interface ILuaContext +{ + /** + * Wait for an event to occur on the computer, suspending the thread until it arises. This method is exactly equivalent to os.pullEvent() in lua. + * @param filter A specific event to wait for, or null to wait for any event + * @return An object array containing the name of the event that occurred, and any event parameters + * @throws Exception If the user presses CTRL+T to terminate the current program while pullEvent() is waiting for an event, a "Terminated" exception will be thrown here. + * Do not attempt to block this exception, unless you wish to prevent termination, which is not recommended. + * @throws InterruptedException If the user shuts down or reboots the computer while pullEvent() is waiting for an event, InterruptedException will be thrown. This exception must not be caught or intercepted, or the computer will leak memory and end up in a broken state. + */ + public Object[] pullEvent( String filter ) throws Exception, InterruptedException; + + /** + * The same as pullEvent(), except "terminated" events are ignored. Only use this if you want to prevent program termination, which is not recommended. This method is exactly equivalent to os.pullEventRaw() in lua. + * @param filter A specific event to wait for, or null to wait for any event + * @return An object array containing the name of the event that occurred, and any event parameters + * @throws InterruptedException If the user shuts down or reboots the computer while pullEventRaw() is waiting for an event, InterruptedException will be thrown. This exception must not be caught or intercepted, or the computer will leak memory and end up in a broken state. + * @see #pullEvent(String) + */ + public Object[] pullEventRaw( String filter ) throws InterruptedException; + + + /** + * Yield the current coroutine with some arguments until it is resumed. This method is exactly equivalent to coroutine.yield() in lua. Use pullEvent() if you wish to wait for events. + * @param arguments An object array containing the arguments to pass to coroutine.yield() + * @return An object array containing the return values from coroutine.yield() + * @throws InterruptedException If the user shuts down or reboots the computer the coroutine is suspended, InterruptedException will be thrown. This exception must not be caught or intercepted, or the computer will leak memory and end up in a broken state. + * @see #pullEvent(String) + */ + public Object[] yield( Object[] arguments ) throws InterruptedException; +} diff --git a/MODSRC/dan200/computer/api/ILuaObject.java b/MODSRC/dan200/computer/api/ILuaObject.java new file mode 100644 index 00000000..a063fa5b --- /dev/null +++ b/MODSRC/dan200/computer/api/ILuaObject.java @@ -0,0 +1,26 @@ +/** + * This file is part of the public ComputerCraft API - http://www.computercraft.info + * Copyright Daniel Ratcliffe, 2011-2013. This API may be redistributed unmodified and in full only. + * For help using the API, and posting your mods, visit the forums at computercraft.info. + */ + +package dan200.computer.api; + +/** + * An interface for representing custom objects returned by IPeripheral.callMethod() calls. + * Return objects implementing this interface to expose objects with methods to lua. + */ +public interface ILuaObject +{ + /** + * Get the names of the methods that this object implements. This works the same as IPeripheral.getMethodNames(). See that method for detailed documentation. + * @see IPeripheral#getMethodNames() + */ + public String[] getMethodNames(); + + /** + * Called when a user calls one of the methods that this object implements. This works the same as IPeripheral.callMethod(). See that method for detailed documentation. + * @see IPeripheral#callMethod(IComputerAccess, ILuaContext, int, Object[]) + */ + public Object[] callMethod( ILuaContext context, int method, Object[] arguments ) throws Exception; +} diff --git a/MODSRC/dan200/computer/api/IMount.java b/MODSRC/dan200/computer/api/IMount.java new file mode 100644 index 00000000..74f9bdeb --- /dev/null +++ b/MODSRC/dan200/computer/api/IMount.java @@ -0,0 +1,59 @@ +/** + * This file is part of the public ComputerCraft API - http://www.computercraft.info + * Copyright Daniel Ratcliffe, 2011-2013. This API may be redistributed unmodified and in full only. + * For help using the API, and posting your mods, visit the forums at computercraft.info. + */ + +package dan200.computer.api; + +import java.io.IOException; +import java.io.InputStream; +import java.util.List; + +import net.minecraft.world.World; + +/** + * Represents a read only part of a virtual filesystem that can be mounted onto a computer using IComputerAccess.mount(). + * Ready made implementations of this interface can be created using ComputerCraftAPI.createSaveDirMount() or ComputerCraftAPI.createResourceMount(), or you're free to implement it yourselves! + * @see ComputerCraftAPI#createSaveDirMount(World, String) + * @see ComputerCraftAPI#createResourceMount(Class, String, String) + * @see IComputerAccess#mount(String, IMount) + * @see IWritableMount + */ +public interface IMount +{ + /** + * Returns whether a file with a given path exists or not. + * @param path A file path in normalised format, relative to the mount location. ie: "programs/myprogram" + * @return true if the file exists, false otherwise + */ + public boolean exists( String path ) throws IOException; + + /** + * Returns whether a file with a given path is a directory or not. + * @param path A file path in normalised format, relative to the mount location. ie: "programs/myprograms" + * @return true if the file exists and is a directory, false otherwise + */ + public boolean isDirectory( String path ) throws IOException; + + /** + * Returns the file names of all the files in a directory. + * @param path A file path in normalised format, relative to the mount location. ie: "programs/myprograms" + * @param contents A list of strings. Add all the file names to this list + */ + public void list( String path, List contents ) throws IOException; + + /** + * Returns the size of a file with a given path, in bytes + * @param path A file path in normalised format, relative to the mount location. ie: "programs/myprogram" + * @return the size of the file, in bytes + */ + public long getSize( String path ) throws IOException; + + /** + * Opens a file with a given path, and returns an inputstream representing it's contents. + * @param path A file path in normalised format, relative to the mount location. ie: "programs/myprogram" + * @return a stream representing the contents of the file + */ + public InputStream openForRead( String path ) throws IOException; +} diff --git a/MODSRC/dan200/computer/api/IPeripheral.java b/MODSRC/dan200/computer/api/IPeripheral.java new file mode 100644 index 00000000..76d5a38f --- /dev/null +++ b/MODSRC/dan200/computer/api/IPeripheral.java @@ -0,0 +1,106 @@ +/** + * This file is part of the public ComputerCraft API - http://www.computercraft.info + * Copyright Daniel Ratcliffe, 2011-2013. This API may be redistributed unmodified and in full only. + * For help using the API, and posting your mods, visit the forums at computercraft.info. + */ + +package dan200.computer.api; + +/** + * 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. + */ +public interface IPeripheral +{ + /** + * Should return a string that uniquely identifies this type of peripheral. + * This can be queried from lua by calling peripheral.getType() + * @return A string identifying the type of peripheral. + */ + public String getType(); + + /** + * 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. + * @return An array of strings representing method names. + * @see #callMethod + */ + public String[] getMethodNames(); + + /** + * 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. + * @param computer The interface to the computer that is making the call. Remember that multiple + * computers can be attached to a peripheral at once. + * @param context The context of the currently running lua thread. This can be used to wait for events + * or otherwise yield. + * @param 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() + * @param 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. + * @return 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 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 #getMethodNames + */ + public Object[] callMethod( IComputerAccess computer, ILuaContext context, int method, Object[] arguments ) throws Exception; + + /** + * 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. + * @param side The world direction (0=bottom, 1=top, etc) that the computer lies relative to the peripheral. + * @return Whether to allow the attachment, as a boolean. + * @see #attach + */ + public boolean canAttachToSide( int side ); + + /** + * 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. + * @param computer The interface to the computer that is being attached. Remember that multiple + * computers can be attached to a peripheral at once. + * @see #canAttachToSide + * @see #detach + */ + public void attach( 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. + * @param computer The interface to the computer that is being detached. Remember that multiple + * computers can be attached to a peripheral at once. + * @see #canAttachToSide + * @see #detach + */ + public void detach( IComputerAccess computer ); +} diff --git a/MODSRC/dan200/computer/api/IPeripheralHandler.java b/MODSRC/dan200/computer/api/IPeripheralHandler.java new file mode 100644 index 00000000..0c6a9e56 --- /dev/null +++ b/MODSRC/dan200/computer/api/IPeripheralHandler.java @@ -0,0 +1,15 @@ +/** + * This file is part of the public ComputerCraft API - http://www.computercraft.info + * Copyright Daniel Ratcliffe, 2011-2013. This API may be redistributed unmodified and in full only. + * For help using the API, and posting your mods, visit the forums at computercraft.info. + */ + +package dan200.computer.api; + +/** + * TODO: Document me + */ +public interface IPeripheralHandler +{ + public IHostedPeripheral getPeripheral( net.minecraft.tileentity.TileEntity tile ); +} diff --git a/MODSRC/dan200/computer/api/IWritableMount.java b/MODSRC/dan200/computer/api/IWritableMount.java new file mode 100644 index 00000000..05d3c01d --- /dev/null +++ b/MODSRC/dan200/computer/api/IWritableMount.java @@ -0,0 +1,54 @@ +/** + * This file is part of the public ComputerCraft API - http://www.computercraft.info + * Copyright Daniel Ratcliffe, 2011-2013. This API may be redistributed unmodified and in full only. + * For help using the API, and posting your mods, visit the forums at computercraft.info. + */ + +package dan200.computer.api; + +import java.io.IOException; +import java.io.OutputStream; + +import net.minecraft.world.World; + +/** + * Represents a part of a virtual filesystem that can be mounted onto a computer using IComputerAccess.mount() or IComputerAccess.mountWritable(), that can also be written to. + * Ready made implementations of this interface can be created using ComputerCraftAPI.createSaveDirMount(), or you're free to implement it yourselves! + * @see ComputerCraftAPI#createSaveDirMount(World, String) + * @see IComputerAccess#mountWritable(String, IMount) + * @see IMount + */ +public interface IWritableMount extends IMount +{ + /** + * Creates a directory at a given path inside the virtual file system. + * @param path A file path in normalised format, relative to the mount location. ie: "programs/mynewprograms" + */ + public void makeDirectory( String path ) throws IOException; + + /** + * Deletes a directory at a given path inside the virtual file system. + * @param path A file path in normalised format, relative to the mount location. ie: "programs/myoldprograms" + */ + public void delete( String path ) throws IOException; + + /** + * Opens a file with a given path, and returns an outputstream for writing to it. + * @param path A file path in normalised format, relative to the mount location. ie: "programs/myprogram" + * @return a stream for writing to + */ + public OutputStream openForWrite( String path ) throws IOException; + + /** + * Opens a file with a given path, and returns an outputstream for appending to it. + * @param path A file path in normalised format, relative to the mount location. ie: "programs/myprogram" + * @return a stream for writing to + */ + public OutputStream openForAppend( String path ) throws IOException; + + /** + * Get the ammount of free space on the mount, in bytes. You should decrease this value as the user writes to the mount, and write operations should fail once it reaches zero. + * @return The ammount of free space, in bytes. + */ + public long getRemainingSpace() throws IOException; +} diff --git a/MODSRC/vazkii/tinkerer/common/block/BlockAnimationTablet.java b/MODSRC/vazkii/tinkerer/common/block/BlockAnimationTablet.java index 16b40acd..071e9cc2 100644 --- a/MODSRC/vazkii/tinkerer/common/block/BlockAnimationTablet.java +++ b/MODSRC/vazkii/tinkerer/common/block/BlockAnimationTablet.java @@ -27,6 +27,7 @@ import net.minecraft.tileentity.TileEntity; import net.minecraft.util.ChunkCoordinates; import net.minecraft.util.Icon; import net.minecraft.util.MathHelper; +import net.minecraft.util.StatCollector; import net.minecraft.world.World; import net.minecraftforge.common.ForgeDirection; import thaumcraft.common.items.wands.ItemWandCasting; @@ -170,9 +171,9 @@ public class BlockAnimationTablet extends BlockModContainer { int meta = par1World.getBlockMetadata(par2, par3, par4); boolean activated = (meta & 8) != 0; if(!activated && !tablet.getIsBreaking() && tablet.swingProgress == 0) { - par1World.setBlockMetadataWithNotify(par2, par3, par4, meta == 5 ? + 2 : meta + 1, 1 | 2); - par1World.playSoundEffect(par2, par3, par4, "thaumcraft.tool", 0.6F, 1F); - } else par5EntityPlayer.addChatMessage("You can not rotate the Tablet while it's activated."); + par1World.setBlockMetadataWithNotify(par2, par3, par4, meta == 5 ? 2 : meta + 1, 1 | 2); + par1World.playSoundEffect(par2, par3, par4, "thaumcraft:tool", 0.6F, 1F); + } else par5EntityPlayer.addChatMessage(StatCollector.translateToLocal("ttmisc.animationTablet.notRotatable")); // Rare chance this might happen, but better to cope for it. return true; diff --git a/MODSRC/vazkii/tinkerer/common/block/tile/TileMagnet.java b/MODSRC/vazkii/tinkerer/common/block/tile/TileMagnet.java index 2c0ec4cc..565891fc 100644 --- a/MODSRC/vazkii/tinkerer/common/block/tile/TileMagnet.java +++ b/MODSRC/vazkii/tinkerer/common/block/tile/TileMagnet.java @@ -25,8 +25,11 @@ import net.minecraftforge.common.ForgeDirection; import thaumcraft.client.codechicken.core.vec.Vector3; import vazkii.tinkerer.common.ThaumicTinkerer; import vazkii.tinkerer.common.core.helper.MiscHelper; +import dan200.computer.api.IComputerAccess; +import dan200.computer.api.ILuaContext; +import dan200.computer.api.IPeripheral; -public class TileMagnet extends TileEntity { +public class TileMagnet extends TileEntity implements IPeripheral { @Override public void updateEntity() { @@ -72,4 +75,52 @@ public class TileMagnet extends TileEntity { }; } + @Override + public String getType() { + return "tt_magnet"; + } + + @Override + public String[] getMethodNames() { + return new String[] { "isPulling", "setPulling", "getSignal" }; + } + + @Override + public Object[] callMethod(IComputerAccess computer, ILuaContext context, int method, Object[] arguments) throws Exception { + switch(method) { + case 0 : return new Object[] { (getBlockMetadata() & 1) == 0 }; + case 1 : { + boolean pull = (Boolean) arguments[0]; + int meta = (getBlockMetadata() & 2) + (pull ? 0 : 1); + + worldObj.setBlockMetadataWithNotify(xCoord, yCoord, zCoord, meta, 1 | 2); + return null; + } + case 2 : { + int redstone = 0; + for(ForgeDirection dir : ForgeDirection.VALID_DIRECTIONS) + redstone = Math.max(redstone, worldObj.getIndirectPowerLevelTo(xCoord + dir.offsetX, yCoord + dir.offsetY, zCoord + dir.offsetZ, dir.ordinal())); + + return new Object[] { redstone }; + } + } + + return null; + } + + @Override + public boolean canAttachToSide(int side) { + return true; + } + + @Override + public void attach(IComputerAccess computer) { + // NO-OP + } + + @Override + public void detach(IComputerAccess computer) { + // NO-OP + } + } diff --git a/MODSRC/vazkii/tinkerer/common/block/tile/TileMobMagnet.java b/MODSRC/vazkii/tinkerer/common/block/tile/TileMobMagnet.java index 61386f71..708b50c1 100644 --- a/MODSRC/vazkii/tinkerer/common/block/tile/TileMobMagnet.java +++ b/MODSRC/vazkii/tinkerer/common/block/tile/TileMobMagnet.java @@ -14,6 +14,9 @@ */ package vazkii.tinkerer.common.block.tile; +import java.util.Arrays; +import java.util.List; + import net.minecraft.command.IEntitySelector; import net.minecraft.entity.Entity; import net.minecraft.entity.EntityAgeable; @@ -29,6 +32,9 @@ import net.minecraft.network.packet.Packet; import net.minecraft.network.packet.Packet132TileEntityData; import vazkii.tinkerer.common.item.ItemSoulMould; import vazkii.tinkerer.common.lib.LibBlockNames; +import cpw.mods.fml.common.network.PacketDispatcher; +import dan200.computer.api.IComputerAccess; +import dan200.computer.api.ILuaContext; public class TileMobMagnet extends TileMagnet implements IInventory { @@ -184,6 +190,30 @@ public class TileMobMagnet extends TileMagnet implements IInventory { return true; } + @Override + public String[] getMethodNames() { + List methods = Arrays.asList(super.getMethodNames()); + methods.add("getAdultSearch"); + methods.add("setAdultSearch"); + return methods.toArray(new String[methods.size()]); + } + + @Override + public Object[] callMethod(IComputerAccess computer, ILuaContext context, int method, Object[] arguments) throws Exception { + switch(method) { + case 3 : return new Object[] { adult }; + case 4 : { + boolean adult = (Boolean) arguments[0]; + + this.adult = adult; + PacketDispatcher.sendPacketToAllPlayers(getDescriptionPacket()); + return null; + + } + } + return super.callMethod(computer, context, method, arguments); + } + @Override public Packet getDescriptionPacket() { diff --git a/MODSRC/vazkii/tinkerer/common/block/tile/peripheral/PeripheralAspectContainer.java b/MODSRC/vazkii/tinkerer/common/block/tile/peripheral/PeripheralAspectContainer.java new file mode 100644 index 00000000..61af8bfe --- /dev/null +++ b/MODSRC/vazkii/tinkerer/common/block/tile/peripheral/PeripheralAspectContainer.java @@ -0,0 +1,96 @@ +/** + * This class was created by . It's distributed as + * part of the ThaumicTinkerer Mod. + * + * ThaumicTinkerer is Open Source and distributed under a + * Creative Commons Attribution-NonCommercial-ShareAlike 3.0 License + * (http://creativecommons.org/licenses/by-nc-sa/3.0/deed.en_GB) + * + * ThaumicTinkerer is a Derivative Work on Thaumcraft 4. + * Thaumcraft 4 (c) Azanor 2012 + * (http://www.minecraftforum.net/topic/1585216-) + * + * File Created @ [13 Sep 2013, 00:58:21 (GMT)] + */ +package vazkii.tinkerer.common.block.tile.peripheral; + +import java.util.ArrayList; +import java.util.List; + +import net.minecraft.nbt.NBTTagCompound; +import thaumcraft.api.aspects.Aspect; +import thaumcraft.api.aspects.IAspectContainer; +import dan200.computer.api.IComputerAccess; +import dan200.computer.api.IHostedPeripheral; +import dan200.computer.api.ILuaContext; + +public class PeripheralAspectContainer implements IHostedPeripheral { + + IAspectContainer container; + + public PeripheralAspectContainer(IAspectContainer container) { + this.container = container; + } + + @Override + public String getType() { + return "tt_aspectContainer"; + } + + @Override + public String[] getMethodNames() { + return new String[] { "getAspects", "getAspectCount" }; + } + + @Override + public Object[] callMethod(IComputerAccess computer, ILuaContext context, int method, Object[] arguments) throws Exception { + switch(method) { + case 0 : { + List returnStuff = new ArrayList(); + for(Aspect aspect : container.getAspects().getAspectsSorted()) + returnStuff.add(aspect.getTag()); + + return returnStuff.toArray(); + } + case 1 : { + String aspectName = (String) arguments[0]; + Aspect aspect = Aspect.getAspect(aspectName); + + return new Object[] { container.getAspects().getAmount(aspect) }; + } + } + + return null; + } + + @Override + public boolean canAttachToSide(int side) { + return true; + } + + @Override + public void attach(IComputerAccess computer) { + // NO-OP + } + + @Override + public void detach(IComputerAccess computer) { + // NO-OP + } + + @Override + public void update() { + // NO-OP + } + + @Override + public void readFromNBT(NBTTagCompound nbttagcompound) { + // NO-OP + } + + @Override + public void writeToNBT(NBTTagCompound nbttagcompound) { + // NO-OP + } + +} diff --git a/MODSRC/vazkii/tinkerer/common/block/tile/peripheral/PeripheralDeconstructor.java b/MODSRC/vazkii/tinkerer/common/block/tile/peripheral/PeripheralDeconstructor.java new file mode 100644 index 00000000..b24c189b --- /dev/null +++ b/MODSRC/vazkii/tinkerer/common/block/tile/peripheral/PeripheralDeconstructor.java @@ -0,0 +1,82 @@ +/** + * This class was created by . It's distributed as + * part of the ThaumicTinkerer Mod. + * + * ThaumicTinkerer is Open Source and distributed under a + * Creative Commons Attribution-NonCommercial-ShareAlike 3.0 License + * (http://creativecommons.org/licenses/by-nc-sa/3.0/deed.en_GB) + * + * ThaumicTinkerer is a Derivative Work on Thaumcraft 4. + * Thaumcraft 4 (c) Azanor 2012 + * (http://www.minecraftforum.net/topic/1585216-) + * + * File Created @ [13 Sep 2013, 01:10:52 (GMT)] + */ +package vazkii.tinkerer.common.block.tile.peripheral; + +import net.minecraft.nbt.NBTTagCompound; +import thaumcraft.common.tiles.TileDeconstructionTable; +import dan200.computer.api.IComputerAccess; +import dan200.computer.api.IHostedPeripheral; +import dan200.computer.api.ILuaContext; + +public class PeripheralDeconstructor implements IHostedPeripheral { + + TileDeconstructionTable deconstructor; + + public PeripheralDeconstructor(TileDeconstructionTable deconstructor) { + this.deconstructor = deconstructor; + } + + @Override + public String getType() { + return "tt_deconstructor"; + } + + @Override + public String[] getMethodNames() { + return new String[] { "hasAspect", "hasItem", "getAspect" }; + } + + @Override + public Object[] callMethod(IComputerAccess computer, ILuaContext context, int method, Object[] arguments) throws Exception { + switch(method) { + case 0 : return new Object[] { deconstructor.aspect != null }; + case 1 : return new Object[] { deconstructor.getStackInSlot(0) != null }; + case 2 : return deconstructor.aspect == null ? null : new Object[] { deconstructor.aspect.getTag() }; + } + + return null; + } + + @Override + public boolean canAttachToSide(int side) { + return true; + } + + @Override + public void attach(IComputerAccess computer) { + // NO-OP + } + + @Override + public void detach(IComputerAccess computer) { + // NO-OP + } + + @Override + public void update() { + // NO-OP + } + + @Override + public void readFromNBT(NBTTagCompound nbttagcompound) { + // NO-OP + } + + @Override + public void writeToNBT(NBTTagCompound nbttagcompound) { + // NO-OP + } + +} diff --git a/MODSRC/vazkii/tinkerer/common/block/tile/peripheral/PeripheralHandler.java b/MODSRC/vazkii/tinkerer/common/block/tile/peripheral/PeripheralHandler.java new file mode 100644 index 00000000..f234c608 --- /dev/null +++ b/MODSRC/vazkii/tinkerer/common/block/tile/peripheral/PeripheralHandler.java @@ -0,0 +1,36 @@ +/** + * This class was created by . It's distributed as + * part of the ThaumicTinkerer Mod. + * + * ThaumicTinkerer is Open Source and distributed under a + * Creative Commons Attribution-NonCommercial-ShareAlike 3.0 License + * (http://creativecommons.org/licenses/by-nc-sa/3.0/deed.en_GB) + * + * ThaumicTinkerer is a Derivative Work on Thaumcraft 4. + * Thaumcraft 4 (c) Azanor 2012 + * (http://www.minecraftforum.net/topic/1585216-) + * + * File Created @ [13 Sep 2013, 00:56:19 (GMT)] + */ +package vazkii.tinkerer.common.block.tile.peripheral; + +import net.minecraft.tileentity.TileEntity; +import thaumcraft.api.aspects.IAspectContainer; +import thaumcraft.common.tiles.TileDeconstructionTable; +import dan200.computer.api.IHostedPeripheral; +import dan200.computer.api.IPeripheralHandler; + +public final class PeripheralHandler implements IPeripheralHandler { + + @Override + public IHostedPeripheral getPeripheral(TileEntity tile) { + if(tile instanceof IAspectContainer) + return new PeripheralAspectContainer((IAspectContainer) tile); + + if(tile instanceof TileDeconstructionTable) + return new PeripheralDeconstructor((TileDeconstructionTable) tile); + + return null; + } + +} diff --git a/MODSRC/vazkii/tinkerer/common/block/tile/tablet/TileAnimationTablet.java b/MODSRC/vazkii/tinkerer/common/block/tile/tablet/TileAnimationTablet.java index 7a219d97..c6d1699d 100644 --- a/MODSRC/vazkii/tinkerer/common/block/tile/tablet/TileAnimationTablet.java +++ b/MODSRC/vazkii/tinkerer/common/block/tile/tablet/TileAnimationTablet.java @@ -45,8 +45,11 @@ import net.minecraftforge.event.entity.player.PlayerInteractEvent.Action; import vazkii.tinkerer.common.block.ModBlocks; import vazkii.tinkerer.common.lib.LibBlockNames; import cpw.mods.fml.common.network.PacketDispatcher; +import dan200.computer.api.IComputerAccess; +import dan200.computer.api.ILuaContext; +import dan200.computer.api.IPeripheral; -public class TileAnimationTablet extends TileEntity implements IInventory { +public class TileAnimationTablet extends TileEntity implements IInventory, IPeripheral { private static final String TAG_LEFT_CLICK = "leftClick"; private static final String TAG_REDSTONE = "redstone"; @@ -501,4 +504,71 @@ public class TileAnimationTablet extends TileEntity implements IInventory { readCustomNBT(packet.customParam1); } + @Override + public String getType() { + return "tt_animationTablet"; + } + + @Override + public String[] getMethodNames() { + return new String[]{ "getRedstone", "setRedstone", "getLeftClick", "setLeftClick", "getRotation", "setRotation", "hasItem", "trigger" }; + } + + @Override + public Object[] callMethod(IComputerAccess computer, ILuaContext context, int method, Object[] arguments) throws Exception { + switch(method) { + case 0 : return new Object[]{ redstone }; + case 1 : { + boolean redstone = (Boolean) arguments[0]; + this.redstone = redstone; + PacketDispatcher.sendPacketToAllPlayers(getDescriptionPacket()); + return null; + } + case 2 : return new Object[]{ leftClick }; + case 3 : { + boolean leftClick = (Boolean) arguments[0]; + this.leftClick = leftClick; + PacketDispatcher.sendPacketToAllPlayers(getDescriptionPacket()); + return null; + } + case 4 : return new Object[] { getBlockMetadata() - 2 }; + case 5 : { + int rotation = (int) ((Double) arguments[0]).doubleValue(); + + if(rotation > 3) + throw new Exception("Invalid value: " + rotation + "."); + + worldObj.setBlockMetadataWithNotify(xCoord, yCoord, zCoord, rotation + 2, 1 | 2); + return null; + } + case 6 : return new Object[] { getStackInSlot(0) != null }; + case 7 : { + if(swingProgress != 0) + return new Object[] { false }; + + findEntities(getTargetLoc()); + initiateSwing(); + worldObj.addBlockEvent(xCoord, yCoord, zCoord, ModBlocks.animationTablet.blockID, 0, 0); + + return new Object[] { true }; + } + } + return null; + } + + @Override + public boolean canAttachToSide(int side) { + return true; + } + + @Override + public void attach(IComputerAccess computer) { + // NO-OP + } + + @Override + public void detach(IComputerAccess computer) { + // NO-OP + } + } \ No newline at end of file diff --git a/MODSRC/vazkii/tinkerer/common/core/proxy/TTCommonProxy.java b/MODSRC/vazkii/tinkerer/common/core/proxy/TTCommonProxy.java index 785bf21b..d6dd9a98 100644 --- a/MODSRC/vazkii/tinkerer/common/core/proxy/TTCommonProxy.java +++ b/MODSRC/vazkii/tinkerer/common/core/proxy/TTCommonProxy.java @@ -14,8 +14,13 @@ */ package vazkii.tinkerer.common.core.proxy; +import thaumcraft.api.nodes.TileNodeBase; +import thaumcraft.common.tiles.TileAlembic; +import thaumcraft.common.tiles.TileCrucible; +import thaumcraft.common.tiles.TileDeconstructionTable; import vazkii.tinkerer.common.ThaumicTinkerer; import vazkii.tinkerer.common.block.ModBlocks; +import vazkii.tinkerer.common.block.tile.peripheral.PeripheralHandler; import vazkii.tinkerer.common.core.handler.ConfigHandler; import vazkii.tinkerer.common.enchantment.ModEnchantments; import vazkii.tinkerer.common.item.ModItems; @@ -27,6 +32,8 @@ import cpw.mods.fml.common.event.FMLInitializationEvent; import cpw.mods.fml.common.event.FMLPostInitializationEvent; import cpw.mods.fml.common.event.FMLPreInitializationEvent; import cpw.mods.fml.common.network.NetworkRegistry; +import dan200.computer.api.ComputerCraftAPI; +import dan200.computer.api.IPeripheralHandler; public class TTCommonProxy { @@ -35,6 +42,8 @@ public class TTCommonProxy { ModBlocks.initBlocks(); ModItems.initItems(); + + initCCPeripherals(); } public void init(FMLInitializationEvent event) { @@ -48,4 +57,12 @@ public class TTCommonProxy { ModRecipes.initRecipes(); ModResearch.initResearch(); } + + private void initCCPeripherals() { + IPeripheralHandler handler = new PeripheralHandler(); + ComputerCraftAPI.registerExternalPeripheral(TileDeconstructionTable.class, handler); + ComputerCraftAPI.registerExternalPeripheral(TileAlembic.class, handler); + ComputerCraftAPI.registerExternalPeripheral(TileCrucible.class, handler); + ComputerCraftAPI.registerExternalPeripheral(TileNodeBase.class, handler); + } } diff --git a/resources/assets/ttinkerer/lang/en_US.lang b/resources/assets/ttinkerer/lang/en_US.lang index 6dea9bdc..4c18af87 100644 --- a/resources/assets/ttinkerer/lang/en_US.lang +++ b/resources/assets/ttinkerer/lang/en_US.lang @@ -11,6 +11,7 @@ ttmisc.connector.toofar=The Transvector Interface of origin is too far away. ttmisc.leftClick=Left Click ttmisc.rightClick=Right Click ttmisc.redstoneControl=Redstone Control +ttmisc.animationTablet.notRotatable=You can not rotate the Tablet while it's activated. ttmisc.focusDislocation.tooltip=Currently Dislocating: %s ttmisc.focusDislocation.tooltipExtra=Extra Contents ttmisc.inactive=§cInactive