From f8263c9b2b5d84d33d8f5db73a2e452c6d78fae8 Mon Sep 17 00:00:00 2001 From: Rseifert Date: Wed, 10 Apr 2013 06:24:16 -0400 Subject: [PATCH] Updated CC api need to just no include this in the repo but rather have the ant script just download it each build --- .../assemblyline/client/ClientProxy.java | 2 - .../client/model/ModelCraneArmMount.java | 1 - .../client/render/RenderDetector.java | 2 - .../assemblyline/common/CommonProxy.java | 3 - .../common/block/BlockALMachine.java | 1 - .../common/block/BlockTurntable.java | 5 +- .../common/machine/TileEntityManipulator.java | 1 - .../common/machine/armbot/BlockArmbot.java | 1 - .../common/machine/command/CommandGrab.java | 3 +- .../common/machine/encoder/ItemDisk.java | 5 +- .../machine/imprinter/BlockImprintable.java | 1 - .../machine/imprinter/ItemImprinter.java | 5 +- .../dan200/computer/api/ComputerCraftAPI.java | 91 ++++++++++ .../dan200/computer/api/IComputerAccess.java | 25 ++- .../computer/api/IHostedPeripheral.java | 44 +++++ src/minecraft/dan200/computer/api/IMedia.java | 21 +++ .../dan200/computer/api/IPeripheral.java | 5 + .../computer/api/IPeripheralHandler.java | 15 ++ .../dan200/turtle/api/ITurtleAccess.java | 158 ++++++++++++++++++ .../turtle/api/ITurtleCommandHandler.java | 25 +++ .../dan200/turtle/api/ITurtleUpgrade.java | 92 ++++++++++ .../dan200/turtle/api/TurtleAPI.java | 78 +++++++++ .../dan200/turtle/api/TurtleSide.java | 23 +++ .../dan200/turtle/api/TurtleUpgradeType.java | 27 +++ .../dan200/turtle/api/TurtleVerb.java | 26 +++ 25 files changed, 624 insertions(+), 36 deletions(-) create mode 100644 src/minecraft/dan200/computer/api/ComputerCraftAPI.java create mode 100644 src/minecraft/dan200/computer/api/IHostedPeripheral.java create mode 100644 src/minecraft/dan200/computer/api/IMedia.java create mode 100644 src/minecraft/dan200/computer/api/IPeripheralHandler.java create mode 100644 src/minecraft/dan200/turtle/api/ITurtleAccess.java create mode 100644 src/minecraft/dan200/turtle/api/ITurtleCommandHandler.java create mode 100644 src/minecraft/dan200/turtle/api/ITurtleUpgrade.java create mode 100644 src/minecraft/dan200/turtle/api/TurtleAPI.java create mode 100644 src/minecraft/dan200/turtle/api/TurtleSide.java create mode 100644 src/minecraft/dan200/turtle/api/TurtleUpgradeType.java create mode 100644 src/minecraft/dan200/turtle/api/TurtleVerb.java diff --git a/src/minecraft/assemblyline/client/ClientProxy.java b/src/minecraft/assemblyline/client/ClientProxy.java index ea4b257a..b9cceb12 100644 --- a/src/minecraft/assemblyline/client/ClientProxy.java +++ b/src/minecraft/assemblyline/client/ClientProxy.java @@ -4,7 +4,6 @@ import net.minecraft.client.gui.GuiScreen; import net.minecraft.entity.player.EntityPlayer; import net.minecraft.tileentity.TileEntity; import net.minecraft.world.World; -import net.minecraftforge.client.MinecraftForgeClient; import net.minecraftforge.common.MinecraftForge; import assemblyline.client.gui.GuiEncoder; import assemblyline.client.gui.GuiImprinter; @@ -17,7 +16,6 @@ import assemblyline.client.render.RenderCrate; import assemblyline.client.render.RenderDetector; import assemblyline.client.render.RenderManipulator; import assemblyline.client.render.RenderRejector; -import assemblyline.common.AssemblyLine; import assemblyline.common.CommonProxy; import assemblyline.common.block.TileEntityCrate; import assemblyline.common.machine.TileEntityManipulator; diff --git a/src/minecraft/assemblyline/client/model/ModelCraneArmMount.java b/src/minecraft/assemblyline/client/model/ModelCraneArmMount.java index 49d4fae0..f3b2d162 100644 --- a/src/minecraft/assemblyline/client/model/ModelCraneArmMount.java +++ b/src/minecraft/assemblyline/client/model/ModelCraneArmMount.java @@ -2,7 +2,6 @@ package assemblyline.client.model; import net.minecraft.client.model.ModelBase; import net.minecraft.client.model.ModelRenderer; -import net.minecraft.entity.Entity; public class ModelCraneArmMount extends ModelBase { diff --git a/src/minecraft/assemblyline/client/render/RenderDetector.java b/src/minecraft/assemblyline/client/render/RenderDetector.java index 9c675cf7..65fc7a35 100644 --- a/src/minecraft/assemblyline/client/render/RenderDetector.java +++ b/src/minecraft/assemblyline/client/render/RenderDetector.java @@ -8,10 +8,8 @@ import static org.lwjgl.opengl.GL11.glPopMatrix; import static org.lwjgl.opengl.GL11.glPushMatrix; import static org.lwjgl.opengl.GL11.glTranslated; import net.minecraft.tileentity.TileEntity; -import net.minecraftforge.client.ForgeHooksClient; import universalelectricity.core.vector.Vector3; import assemblyline.client.model.ModelHelper; -import assemblyline.common.AssemblyLine; import cpw.mods.fml.relauncher.Side; import cpw.mods.fml.relauncher.SideOnly; diff --git a/src/minecraft/assemblyline/common/CommonProxy.java b/src/minecraft/assemblyline/common/CommonProxy.java index 53a74839..e0b47fec 100644 --- a/src/minecraft/assemblyline/common/CommonProxy.java +++ b/src/minecraft/assemblyline/common/CommonProxy.java @@ -1,7 +1,5 @@ package assemblyline.common; -import ic2.api.Items; - import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; @@ -11,7 +9,6 @@ import java.util.zip.ZipInputStream; import net.minecraft.entity.player.EntityPlayer; import net.minecraft.tileentity.TileEntity; import net.minecraft.world.World; -import universalelectricity.prefab.implement.IToolConfigurator; import universalelectricity.prefab.multiblock.TileEntityMulti; import assemblyline.common.block.TileEntityCrate; import assemblyline.common.machine.TileEntityManipulator; diff --git a/src/minecraft/assemblyline/common/block/BlockALMachine.java b/src/minecraft/assemblyline/common/block/BlockALMachine.java index 1032c7a9..cc75b13c 100644 --- a/src/minecraft/assemblyline/common/block/BlockALMachine.java +++ b/src/minecraft/assemblyline/common/block/BlockALMachine.java @@ -1,6 +1,5 @@ package assemblyline.common.block; -import ic2.api.Items; import net.minecraft.block.material.Material; import net.minecraft.client.renderer.texture.IconRegister; import net.minecraft.entity.player.EntityPlayer; diff --git a/src/minecraft/assemblyline/common/block/BlockTurntable.java b/src/minecraft/assemblyline/common/block/BlockTurntable.java index 595f6358..32415cf5 100644 --- a/src/minecraft/assemblyline/common/block/BlockTurntable.java +++ b/src/minecraft/assemblyline/common/block/BlockTurntable.java @@ -2,9 +2,6 @@ package assemblyline.common.block; import java.util.Random; -import cpw.mods.fml.relauncher.Side; -import cpw.mods.fml.relauncher.SideOnly; - import net.minecraft.block.Block; import net.minecraft.block.material.Material; import net.minecraft.client.renderer.texture.IconRegister; @@ -21,6 +18,8 @@ import universalelectricity.core.vector.Vector3; import universalelectricity.prefab.implement.IRotatable; import assemblyline.common.AssemblyLine; import assemblyline.common.TabAssemblyLine; +import cpw.mods.fml.relauncher.Side; +import cpw.mods.fml.relauncher.SideOnly; public class BlockTurntable extends BlockALMachine { diff --git a/src/minecraft/assemblyline/common/machine/TileEntityManipulator.java b/src/minecraft/assemblyline/common/machine/TileEntityManipulator.java index d6b6f1e1..bde30210 100644 --- a/src/minecraft/assemblyline/common/machine/TileEntityManipulator.java +++ b/src/minecraft/assemblyline/common/machine/TileEntityManipulator.java @@ -9,7 +9,6 @@ import net.minecraft.nbt.NBTTagCompound; import net.minecraft.tileentity.TileEntity; import net.minecraft.tileentity.TileEntityChest; import net.minecraft.util.AxisAlignedBB; -import net.minecraft.world.World; import net.minecraftforge.common.ForgeDirection; import net.minecraftforge.common.ISidedInventory; import universalelectricity.core.vector.Vector3; diff --git a/src/minecraft/assemblyline/common/machine/armbot/BlockArmbot.java b/src/minecraft/assemblyline/common/machine/armbot/BlockArmbot.java index 36cb7b47..4c35df9a 100644 --- a/src/minecraft/assemblyline/common/machine/armbot/BlockArmbot.java +++ b/src/minecraft/assemblyline/common/machine/armbot/BlockArmbot.java @@ -5,7 +5,6 @@ import java.util.Random; import net.minecraft.entity.player.EntityPlayer; import net.minecraft.item.ItemStack; import net.minecraft.tileentity.TileEntity; -import net.minecraft.world.IBlockAccess; import net.minecraft.world.World; import universalelectricity.core.UniversalElectricity; import universalelectricity.core.vector.Vector3; diff --git a/src/minecraft/assemblyline/common/machine/command/CommandGrab.java b/src/minecraft/assemblyline/common/machine/command/CommandGrab.java index 74731fe9..f7cdff92 100644 --- a/src/minecraft/assemblyline/common/machine/command/CommandGrab.java +++ b/src/minecraft/assemblyline/common/machine/command/CommandGrab.java @@ -2,8 +2,6 @@ package assemblyline.common.machine.command; import java.util.List; -import assemblyline.common.machine.belt.TileEntityConveyorBelt; - import net.minecraft.entity.Entity; import net.minecraft.entity.EntityAgeable; import net.minecraft.entity.player.EntityPlayer; @@ -12,6 +10,7 @@ import net.minecraft.nbt.NBTTagCompound; import net.minecraft.tileentity.TileEntity; import net.minecraft.util.AxisAlignedBB; import universalelectricity.core.vector.Vector3; +import assemblyline.common.machine.belt.TileEntityConveyorBelt; /** * Used by arms to search for entities in a region diff --git a/src/minecraft/assemblyline/common/machine/encoder/ItemDisk.java b/src/minecraft/assemblyline/common/machine/encoder/ItemDisk.java index 782643c4..712fab0a 100644 --- a/src/minecraft/assemblyline/common/machine/encoder/ItemDisk.java +++ b/src/minecraft/assemblyline/common/machine/encoder/ItemDisk.java @@ -3,9 +3,6 @@ package assemblyline.common.machine.encoder; import java.util.ArrayList; import java.util.List; -import cpw.mods.fml.relauncher.Side; -import cpw.mods.fml.relauncher.SideOnly; - import net.minecraft.client.renderer.texture.IconRegister; import net.minecraft.entity.player.EntityPlayer; import net.minecraft.item.Item; @@ -14,6 +11,8 @@ import net.minecraft.nbt.NBTTagCompound; import net.minecraft.nbt.NBTTagList; import assemblyline.common.AssemblyLine; import assemblyline.common.TabAssemblyLine; +import cpw.mods.fml.relauncher.Side; +import cpw.mods.fml.relauncher.SideOnly; public class ItemDisk extends Item { diff --git a/src/minecraft/assemblyline/common/machine/imprinter/BlockImprintable.java b/src/minecraft/assemblyline/common/machine/imprinter/BlockImprintable.java index 49e76f93..939b5c1a 100644 --- a/src/minecraft/assemblyline/common/machine/imprinter/BlockImprintable.java +++ b/src/minecraft/assemblyline/common/machine/imprinter/BlockImprintable.java @@ -12,7 +12,6 @@ import net.minecraft.world.World; import universalelectricity.prefab.implement.IRedstoneReceptor; import assemblyline.api.IFilterable; import assemblyline.common.block.BlockALMachine; -import cpw.mods.fml.common.FMLCommonHandler; /** * Extend this block class if a filter is allowed to be placed inside of this block. diff --git a/src/minecraft/assemblyline/common/machine/imprinter/ItemImprinter.java b/src/minecraft/assemblyline/common/machine/imprinter/ItemImprinter.java index f7c99969..9fe333ac 100644 --- a/src/minecraft/assemblyline/common/machine/imprinter/ItemImprinter.java +++ b/src/minecraft/assemblyline/common/machine/imprinter/ItemImprinter.java @@ -3,9 +3,6 @@ package assemblyline.common.machine.imprinter; import java.util.ArrayList; import java.util.List; -import cpw.mods.fml.relauncher.Side; -import cpw.mods.fml.relauncher.SideOnly; - import net.minecraft.client.renderer.texture.IconRegister; import net.minecraft.entity.Entity; import net.minecraft.entity.EntityList; @@ -17,6 +14,8 @@ import net.minecraft.nbt.NBTTagCompound; import net.minecraft.nbt.NBTTagList; import assemblyline.common.AssemblyLine; import assemblyline.common.TabAssemblyLine; +import cpw.mods.fml.relauncher.Side; +import cpw.mods.fml.relauncher.SideOnly; public class ItemImprinter extends Item { diff --git a/src/minecraft/dan200/computer/api/ComputerCraftAPI.java b/src/minecraft/dan200/computer/api/ComputerCraftAPI.java new file mode 100644 index 00000000..159dfe4a --- /dev/null +++ b/src/minecraft/dan200/computer/api/ComputerCraftAPI.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 java.lang.reflect.Method; + +/** + * 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 +{ + /** + * Get the creative mode tab that ComputerCraft items can be found on. + * Use this to add your peripherals to ComputerCraft's tab. + */ + public static net.minecraft.creativetab.CreativeTabs getCreativeTab() + { + findCC(); + if (computerCraft_getCreativeTab != null) + { + try { + return (net.minecraft.creativetab.CreativeTabs)( computerCraft_getCreativeTab.invoke(null) ); + } 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_getCreativeTab = findCCMethod( "getCreativeTab", new Class[] { } ); + computerCraft_registerExternalPeripheral = findCCMethod( "registerExternalPeripheral", new Class[] { + Class.class, IPeripheralHandler.class + } ); + } catch( Exception e ) { + System.out.println("ComputerCraftAPI: ComputerCraft not found."); + } finally { + ccSearched = true; + } + } + } + + private static Method findCCMethod( String name, Class[] args ) + { + try { + return computerCraft.getMethod( name, args ); + + } catch( NoSuchMethodException e ) { + System.out.println("ComputerCraftAPI: ComputerCraft method " + name + " not found."); + return null; + } + } + + private static boolean ccSearched = false; + private static Class computerCraft = null; + private static Method computerCraft_registerExternalPeripheral = null; + private static Method computerCraft_getCreativeTab = null; +} diff --git a/src/minecraft/dan200/computer/api/IComputerAccess.java b/src/minecraft/dan200/computer/api/IComputerAccess.java index c9150530..f41908dc 100644 --- a/src/minecraft/dan200/computer/api/IComputerAccess.java +++ b/src/minecraft/dan200/computer/api/IComputerAccess.java @@ -1,3 +1,8 @@ +/** + * 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; @@ -92,12 +97,6 @@ public interface IComputerAccess */ public int getID(); - /** - * Equivalent to queueEvent( String event, Object[] arguments ) with an empty arguments array. - * @see #queueEvent(String, Object[]) - */ - public void queueEvent( String event ); - /** * 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 @@ -116,12 +115,12 @@ public interface IComputerAccess public void queueEvent( String event, Object[] arguments ); /** - * Get a string indicating which "side" of the computer the IComputerAccess this peripheral - * has been created for is attached to, relative to the computers orientation. This can be used to - * uniquely identify the peripheral when raising events or returning values to the computer. - * The value returned by this function will be different for the IComputerAccess for each of - * the peripherals attached to the computer. - * @return One of "top", "bottom", "left", "right", "front" or "back" + * 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 getAttachmentSide(); + public String getAttachmentName(); } diff --git a/src/minecraft/dan200/computer/api/IHostedPeripheral.java b/src/minecraft/dan200/computer/api/IHostedPeripheral.java new file mode 100644 index 00000000..68cf3fd7 --- /dev/null +++ b/src/minecraft/dan200/computer/api/IHostedPeripheral.java @@ -0,0 +1,44 @@ +/** + * 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 dan200.computer.api.IPeripheral; + +/** + * 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/src/minecraft/dan200/computer/api/IMedia.java b/src/minecraft/dan200/computer/api/IMedia.java new file mode 100644 index 00000000..f4e5c989 --- /dev/null +++ b/src/minecraft/dan200/computer/api/IMedia.java @@ -0,0 +1,21 @@ +/** + * 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 IMedia +{ + public String getLabel( net.minecraft.item.ItemStack stack ); + public boolean setLabel( net.minecraft.item.ItemStack stack, String label ); + + public String getAudioTitle( net.minecraft.item.ItemStack stack ); + public String getAudioRecordName( net.minecraft.item.ItemStack stack ); + + public String mountData( net.minecraft.item.ItemStack stack, IComputerAccess computer ); +} diff --git a/src/minecraft/dan200/computer/api/IPeripheral.java b/src/minecraft/dan200/computer/api/IPeripheral.java index 9d565133..4577589e 100644 --- a/src/minecraft/dan200/computer/api/IPeripheral.java +++ b/src/minecraft/dan200/computer/api/IPeripheral.java @@ -1,3 +1,8 @@ +/** + * 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; diff --git a/src/minecraft/dan200/computer/api/IPeripheralHandler.java b/src/minecraft/dan200/computer/api/IPeripheralHandler.java new file mode 100644 index 00000000..18487d85 --- /dev/null +++ b/src/minecraft/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/src/minecraft/dan200/turtle/api/ITurtleAccess.java b/src/minecraft/dan200/turtle/api/ITurtleAccess.java new file mode 100644 index 00000000..f996bf47 --- /dev/null +++ b/src/minecraft/dan200/turtle/api/ITurtleAccess.java @@ -0,0 +1,158 @@ +/** + * 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.turtle.api; +import dan200.computer.api.*; + +/** + * The interface passed to upgrades by turtles, providing methods that they can call. + * This should not be implemented by your classes. Do not interact with turtles except via this interface and ITurtleUpgrade. + */ +public interface ITurtleAccess +{ + /** + * Returns the world in which the turtle resides. + * @return the world in which the turtle resides. + */ + public net.minecraft.world.World getWorld(); + + /** + * Returns a vector containing the integer block co-ordinates at which the turtle resides. + * @return a vector containing the integer block co-ordinates at which the turtle resides. + */ + public net.minecraft.util.Vec3 getPosition(); + + /** + * Returns a vector containing the co-ordinates at which the turtle is rendered. + * This will shift when the turtle is moving. + * @param f The subframe fraction + * @return a vector containing the integer block co-ordinates at which the turtle resides. + */ + public net.minecraft.util.Vec3 getVisualPosition( float f ); + + /** + * Returns the world direction the turtle is currently facing. + * @return the world direction the turtle is currently facing. + */ + public int getFacingDir(); + + /** + * Returns the size of the turtles inventory, in number of slots. This will currently always be 16. + * @return the size of the turtles inventory, in number of slots. This will currently always be 16. + */ + public int getInventorySize(); + + /** + * Returns which slot the turtle currently has selected in its inventory using turtle.select(). + * Unlike the 1-based lua representation, this will be between 0 and getInventorySize() - 1. + * @return which slot the turtle currently has selected in its inventory + */ + public int getSelectedSlot(); + + /** + * Returns the item stack that the turtle has in one of its inventory slots. + * @param index which inventory slot to retreive, should be between 0 and getInventorySize() - 1 + * @return the item stack that the turtle has in one of its inventory slots. May be null. + */ + public net.minecraft.item.ItemStack getSlotContents( int index ); + + /** + * Changes the item stack that the turtle has in one of its inventory slots. + * @param index which inventory slot to change, should be between 0 and getInventorySize() - 1 + * @param stack an item stack to put in the slot. May be null. + */ + public void setSlotContents( int index, net.minecraft.item.ItemStack stack ); + + /** + * Tries to store an item stack into the turtles current inventory, starting from the turtles + * currently selected inventory slot. + * @param stack The item stack to try and store. + * @return true if the stack was completely stored in the inventory, false if + * it was only partially stored, or could not fit at all. If false is returned + * and the stack was partially stored, the ItemStack passed into "stack" will now + * represent the stack of items that is left over. + */ + public boolean storeItemStack( net.minecraft.item.ItemStack stack ); + + /** + * Drops an item stack from the turtle onto the floor, or into an inventory is there is one + * adjacent to the turtle in the direction specified. + * @param stack The item stack to drop. + * @param dir The world direction to drop the item + * @return true if the stack was dropped, or completely stored in the adjacent inventory, false if + * it was only partially stored in the adjacent inventory, or could not fit at all. If false is returned + * and the stack was partially stored, the ItemStack passed into "stack" will now + * represent the stack of items that is left over. + */ + public boolean dropItemStack( net.minecraft.item.ItemStack stack, int dir ); + + /** + * "Deploys" an item stack in the direction specified. This simulates a player right clicking, and calls onItemUse() on the Item class. + * Will return true if some kind of deployment happened, and may modify the item stack. For block item types, this can be + * used to place blocks. Some kinds of items (such as shears when facing a sheep) may modify the turtles inventory during this call. + * @param stack The item stack to deploy + * @param dir The world direction to deploy the item + * @return true if the stack was deployed, false if it was not. + */ + public boolean deployWithItemStack( net.minecraft.item.ItemStack stack, int dir ); + + /** + * Tries to "attack" entities with an item stack in the direction specified. This simulates a player left clicking, but will + * not affect blocks. If an entity is attacked and killed during this call, its dropped items will end up in the turtles + * inventory. + * @param stack The item stack to attack with + * @param dir The world direction to attack with the item + * @return true if something was attacked, false if it was not + */ + public boolean attackWithItemStack( net.minecraft.item.ItemStack stack, int dir, float damageMultiplier ); + + /** + * Returns the current fuel level of the turtle, this is the same integer returned by turtle.getFuelLevel(), + * that decreases by 1 every time the turtle moves. Can be used to have your tool or peripheral require or supply + * fuel to the turtle. + * @return the fuel level + */ + public int getFuelLevel(); + + /** + * Tries to increase the fuel level of a turtle by burning an item stack. If the item passed in is a fuel source, fuel + * will increase and true will be returned. Otherwise, nothing will happen and false will be returned. + * @param stack The stack to try to refuel with + * @return Whether the turtle was refueled + */ + public boolean refuelWithItemStack( net.minecraft.item.ItemStack stack ); + + /** + * Removes some fuel from the turtles fuel supply. Negative numbers can be passed in to INCREASE the fuel level of the turtle. + * @return Whether the turtle was able to consume the ammount of fuel specified. Will return false if you supply a number + * greater than the current fuel level of the turtle. + */ + public boolean consumeFuel( int fuel ); + + /** + * Adds a custom command to the turtles command queue. Unlike peripheral methods, these custom commands will be executed + * on the main thread, so are guaranteed to be able to access Minecraft objects safely, and will be queued up + * with the turtles standard movement and tool commands. An issued command will return an unique integer, which will + * be supplied as a parameter to a "turtle_response" event issued to the turtle after the command has completed. Look at the + * lua source code for "rom/apis/turtle" for how to build a lua wrapper around this functionality. + * @param handler an object which will execute the custom command when its point in the queue is reached + * @return the unique command identifier described above + * @see ITurtleCommandHandler + */ + public int issueCommand( ITurtleCommandHandler handler ); + + /** + * Returns the upgrade on the specified side of the turtle, if there is one. + * @return the upgrade on the specified side of the turtle, if there is one. + */ + public ITurtleUpgrade getUpgrade( TurtleSide side ); + + /** + * Returns the peripheral created by the upgrade on the specified side of the turtle, if there is one. + * @return the peripheral created by the upgrade on the specified side of the turtle, if there is one. + */ + public IHostedPeripheral getPeripheral( TurtleSide side ); +} diff --git a/src/minecraft/dan200/turtle/api/ITurtleCommandHandler.java b/src/minecraft/dan200/turtle/api/ITurtleCommandHandler.java new file mode 100644 index 00000000..255d28e2 --- /dev/null +++ b/src/minecraft/dan200/turtle/api/ITurtleCommandHandler.java @@ -0,0 +1,25 @@ +/** + * 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.turtle.api; + +/** + * An interface for objects executing custom turtle commands, used with ITurtleAccess.issueCommand + * @see ITurtleAccess#issueCommand( ITurtleCommandHandler ) + */ +public interface ITurtleCommandHandler +{ + /** + * Will be called by the turtle on the main thread when it is time to execute the custom command. + * The handler should either perform the work of the command, and return true for success, or return + * false to indicate failure if the command cannot be executed at this time. + * @param turtle access to the turtle for whom the command was issued + * @return true for success, false for failure. If true is returned, the turtle will wait 0.4 seconds + * before executing the next command in its queue, as it does for the standard turtle commands. + * @see ITurtleAccess#issueCommand( ITurtleCommandHandler ) + */ + public boolean handleCommand( ITurtleAccess turtle ); +} diff --git a/src/minecraft/dan200/turtle/api/ITurtleUpgrade.java b/src/minecraft/dan200/turtle/api/ITurtleUpgrade.java new file mode 100644 index 00000000..3af5127b --- /dev/null +++ b/src/minecraft/dan200/turtle/api/ITurtleUpgrade.java @@ -0,0 +1,92 @@ +/** + * 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.turtle.api; +import net.minecraft.util.Icon; +import dan200.computer.api.*; + +/** + * The primary interface for defining an upgrade for Turtles. A turtle upgrade + * can either be a new tool, or a new peripheral. + * @see TurtleAPI#registerUpgrade( ITurtleUpgrade ) + */ +public interface ITurtleUpgrade +{ + /** + * Gets a unique numerical identifier representing this type of turtle upgrade. + * Like Minecraft block and item IDs, you should strive to make this number unique + * among all turtle upgrades that have been released for ComputerCraft. + * The ID must be in the range 64 to 255, as the ID is stored as an 8-bit value, + * and 0-64 is reserved for future use by ComputerCraft. The upgrade will + * fail registration if an already used ID is specified. + * @see TurtleAPI#registerUpgrade( ITurtleUpgrade ) + */ + public int getUpgradeID(); + + /** + * Return a String to describe this type of upgrade in turtle item names. + * Examples of built-in adjectives are "Wireless", "Mining" and "Crafty". + */ + public String getAdjective(); + + /** + * Return whether this upgrade adds a tool or a peripheral to the turtle. + * Currently, turtle crafting is restricted to one tool & one peripheral per turtle. + * @see TurtleUpgradeType for the differences between the two. + */ + public TurtleUpgradeType getType(); + + /** + * Return an item stack representing the type of item that a turtle must be crafted + * with to create a turtle which holds this upgrade. + * Currently, turtle crafting is restricted to one tool & one peripheral per turtle. + */ + public net.minecraft.item.ItemStack getCraftingItem(); + + /** + * Return whether this turtle upgrade is an easter egg, and should be attempted to be hidden + * from the creative mode inventory and recipe book plugins. + */ + public boolean isSecret(); + + /** + * Will only be called for Peripheral upgrades. Creates a peripheral for a turtle + * being placed using this upgrade. The peripheral created will be stored + * for the lifetime of the turtle, will have update() called once-per-tick, and will be + * attach'd detach'd and have methods called in the same manner as a Computer peripheral. + * @param turtle Access to the turtle that the peripheral is being created for. + * @param side Which side of the turtle (left or right) that the upgrade resides on. + * @returns The newly created peripheral. You may return null if this upgrade is a Tool + * and this method is not expected to be called. + */ + public IHostedPeripheral createPeripheral( ITurtleAccess turtle, TurtleSide side ); + + /** + * Will only be called for Tool upgrades. Called when turtle.dig() or turtle.attack() is called + * by the turtle, and the tool is required to do some work. + * @param turtle Access to the turtle that the tool resides on. + * @param side Which side of the turtle (left or right) the tool resides on. + * @param verb Which action (dig or attack) the turtle is being called on to perform. + * @param direction Which world direction the action should be performed in, relative to the turtles + * position. This will either be up, down, or the direction the turtle is facing, depending on + * whether dig, digUp or digDown was called. + * @return Whether the turtle was able to perform the action, and hence whether the turtle.dig() + * or turtle.attack() lua method should return true. If true is returned, the tool will perform + * a swinging animation. You may return false if this upgrade is a Peripheral + * and this method is not expected to be called. + */ + public boolean useTool( ITurtleAccess turtle, TurtleSide side, TurtleVerb verb, int direction ); + + /** + * Called to obtain the Icon to be used when rendering a turtle peripheral. Needs to be a "block" + * type Icon for now, as there is no way to determine which texture sheet an Icon is from by the + * Icon itself. + * @param turtle Access to the turtle that the peripheral resides on. + * @param side Which side of the turtle (left or right) the peripheral resides on. + * @return The Icon that you wish to be used to render your turtle peripheral. + */ + public Icon getIcon( ITurtleAccess turtle, TurtleSide side ); +} diff --git a/src/minecraft/dan200/turtle/api/TurtleAPI.java b/src/minecraft/dan200/turtle/api/TurtleAPI.java new file mode 100644 index 00000000..3e3c5c1f --- /dev/null +++ b/src/minecraft/dan200/turtle/api/TurtleAPI.java @@ -0,0 +1,78 @@ +/** + * 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.turtle.api; +import java.lang.reflect.Method; + +/** + * The static entry point to the ComputerCraft Turtle Upgrade API. + * Members in this class must be called after mod_CCTurtle has been initialised, + * but may be called before it is fully loaded. + */ +public class TurtleAPI +{ + /** + * Registers a new turtle upgrade for use in ComputerCraft. After calling this, + * users should be able to craft Turtles with your new upgrade. It is recommended to call + * this during the load() method of your mod. + * @throws Exception if you try to register an upgrade with an already used or reserved upgradeID + * @see ITurtleUpgrade + */ + public static void registerUpgrade( ITurtleUpgrade upgrade ) + { + if( upgrade != null ) + { + findCCTurtle(); + if( ccTurtle_registerTurtleUpgrade != null ) + { + try { + ccTurtle_registerTurtleUpgrade.invoke( null, new Object[]{ upgrade } ); + } 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 findCCTurtle() + { + if( !ccTurtleSearched ) { + // Search for CCTurtle + try { + ccTurtle = Class.forName( "dan200.CCTurtle" ); + ccTurtle_registerTurtleUpgrade = findCCTurtleMethod( "registerTurtleUpgrade", new Class[] { + ITurtleUpgrade.class + } ); + + } catch( ClassNotFoundException e ) { + System.out.println("ComputerCraftAPI: CCTurtle not found."); + + } finally { + ccTurtleSearched = true; + + } + } + } + + private static Method findCCTurtleMethod( String name, Class[] args ) + { + try { + return ccTurtle.getMethod( name, args ); + + } catch( NoSuchMethodException e ) { + System.out.println("ComputerCraftAPI: CCTurtle method " + name + " not found."); + return null; + } + } + + private static boolean ccTurtleSearched = false; + private static Class ccTurtle = null; + private static Method ccTurtle_registerTurtleUpgrade = null; +} diff --git a/src/minecraft/dan200/turtle/api/TurtleSide.java b/src/minecraft/dan200/turtle/api/TurtleSide.java new file mode 100644 index 00000000..3459d777 --- /dev/null +++ b/src/minecraft/dan200/turtle/api/TurtleSide.java @@ -0,0 +1,23 @@ +/** + * 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.turtle.api; + +/** + * An enum representing the two sides of the turtle that a turtle upgrade might reside. + */ +public enum TurtleSide +{ + /** + * The turtles left side (where the pickaxe usually is on a Wireless Mining Turtle) + */ + Left, + + /** + * The turtles right side (where the modem usually is on a Wireless Mining Turtle) + */ + Right, +} diff --git a/src/minecraft/dan200/turtle/api/TurtleUpgradeType.java b/src/minecraft/dan200/turtle/api/TurtleUpgradeType.java new file mode 100644 index 00000000..a65fed8a --- /dev/null +++ b/src/minecraft/dan200/turtle/api/TurtleUpgradeType.java @@ -0,0 +1,27 @@ +/** + * 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.turtle.api; + +/** + * An enum representing the two different types of upgrades that an ITurtleUpgrade + * implementation can add to a turtle. + * @see ITurtleUpgrade + */ +public enum TurtleUpgradeType +{ + /** + * A tool is rendered as an item on the side of the turtle, and responds to the turtle.dig() + * and turtle.attack() methods (Such as pickaxe or sword on Mining and Melee turtles). + */ + Tool, + + /** + * A peripheral adds a special peripheral which is attached to the side of the turtle, + * and can be interacted with the peripheral API (Such as the modem on Wireless Turtles). + */ + Peripheral, +} diff --git a/src/minecraft/dan200/turtle/api/TurtleVerb.java b/src/minecraft/dan200/turtle/api/TurtleVerb.java new file mode 100644 index 00000000..a1b2531a --- /dev/null +++ b/src/minecraft/dan200/turtle/api/TurtleVerb.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.turtle.api; + +/** + * An enum representing the two different actions that an ITurtleUpgrade of type + * Tool may be called on to perform by a turtle. + * @see ITurtleUpgrade + * @see ITurtleUpgrade#useTool + */ +public enum TurtleVerb +{ + /** + * The turtle called turtle.dig(), turtle.digUp() or turtle.digDown() + */ + Dig, + + /** + * The turtle called turtle.attack(), turtle.attackUp() or turtle.attackDown() + */ + Attack, +}