From 4f953d3277147d1860100d6fb0a25d7ffdc45cbf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Florian=20N=C3=BCcke?= Date: Sun, 12 Oct 2014 13:25:45 +0200 Subject: [PATCH] Minor API change, added methods to query list of all block/item drivers. Mainly to allow third-party mods to make any use at all of the EnvironmentAware interface. --- src/main/java/li/cil/oc/api/API.java | 2 +- src/main/java/li/cil/oc/api/Driver.java | 36 +++++++++++++++++++ src/main/java/li/cil/oc/api/Machine.java | 3 +- .../java/li/cil/oc/api/detail/DriverAPI.java | 24 +++++++++++++ .../java/li/cil/oc/api/detail/MachineAPI.java | 4 ++- .../java/li/cil/oc/api/network/Component.java | 12 ++++--- .../li/cil/oc/server/driver/Registry.scala | 5 +++ .../li/cil/oc/server/machine/Machine.scala | 2 +- 8 files changed, 80 insertions(+), 8 deletions(-) diff --git a/src/main/java/li/cil/oc/api/API.java b/src/main/java/li/cil/oc/api/API.java index b5df5fcd0..13e82e70e 100644 --- a/src/main/java/li/cil/oc/api/API.java +++ b/src/main/java/li/cil/oc/api/API.java @@ -11,7 +11,7 @@ import li.cil.oc.api.detail.*; */ public class API { public static final String ID_OWNER = "OpenComputers|Core"; - public static final String VERSION = "3.1.0"; + public static final String VERSION = "3.1.1"; public static DriverAPI driver = null; public static FileSystemAPI fileSystem = null; diff --git a/src/main/java/li/cil/oc/api/Driver.java b/src/main/java/li/cil/oc/api/Driver.java index 44dca8bbf..0fa301f05 100644 --- a/src/main/java/li/cil/oc/api/Driver.java +++ b/src/main/java/li/cil/oc/api/Driver.java @@ -7,6 +7,8 @@ import li.cil.oc.api.driver.Item; import net.minecraft.item.ItemStack; import net.minecraft.world.World; +import java.util.Collection; + /** * This API allows registering new drivers with the mod. *

@@ -131,6 +133,40 @@ public final class Driver { return null; } + /** + * Get a list of all registered block drivers. + *

+ * This is intended to allow checking for particular drivers using more + * customized logic, and in particular to check for drivers with the + * {@link li.cil.oc.api.driver.EnvironmentAware} interface. + *

+ * The returned collection is read-only. + * + * @return the list of all registered block drivers. + */ + public static Collection blockDrivers() { + if (API.driver != null) + return API.driver.blockDrivers(); + return null; + } + + /** + * Get a list of all registered item drivers. + *

+ * This is intended to allow checking for particular drivers using more + * customized logic, and in particular to check for drivers with the + * {@link li.cil.oc.api.driver.EnvironmentAware} interface. + *

+ * The returned collection is read-only. + * + * @return the list of all registered item drivers. + */ + public static Collection itemDrivers() { + if (API.driver != null) + return API.driver.itemDrivers(); + return null; + } + // ----------------------------------------------------------------------- // private Driver() { diff --git a/src/main/java/li/cil/oc/api/Machine.java b/src/main/java/li/cil/oc/api/Machine.java index 2fe890980..43fb6c572 100644 --- a/src/main/java/li/cil/oc/api/Machine.java +++ b/src/main/java/li/cil/oc/api/Machine.java @@ -3,6 +3,7 @@ package li.cil.oc.api; import li.cil.oc.api.machine.Architecture; import li.cil.oc.api.machine.MachineHost; +import java.util.Collection; import java.util.Collections; /** @@ -38,7 +39,7 @@ public final class Machine { /** * A list of all registered architectures. */ - public static Iterable> architectures() { + public static Collection> architectures() { if (API.machine != null) return API.machine.architectures(); return Collections.emptyList(); diff --git a/src/main/java/li/cil/oc/api/detail/DriverAPI.java b/src/main/java/li/cil/oc/api/detail/DriverAPI.java index 800e2c7de..1a0d1c470 100644 --- a/src/main/java/li/cil/oc/api/detail/DriverAPI.java +++ b/src/main/java/li/cil/oc/api/detail/DriverAPI.java @@ -7,6 +7,8 @@ import li.cil.oc.api.driver.Item; import net.minecraft.item.ItemStack; import net.minecraft.world.World; +import java.util.Collection; + public interface DriverAPI { /** * Registers a new driver for a block component. @@ -92,4 +94,26 @@ public interface DriverAPI { * @return a driver for the item, or null if there is none. */ Item driverFor(ItemStack stack); + + /** + * Get a list of all registered block drivers. + *

+ * This is intended to allow checking for particular drivers using more + * customized logic, and in particular to check for drivers with the + * {@link li.cil.oc.api.driver.EnvironmentAware} interface. + * + * @return the list of all registered block drivers. + */ + Collection blockDrivers(); + + /** + * Get a list of all registered item drivers. + *

+ * This is intended to allow checking for particular drivers using more + * customized logic, and in particular to check for drivers with the + * {@link li.cil.oc.api.driver.EnvironmentAware} interface. + * + * @return the list of all registered item drivers. + */ + Collection itemDrivers(); } diff --git a/src/main/java/li/cil/oc/api/detail/MachineAPI.java b/src/main/java/li/cil/oc/api/detail/MachineAPI.java index f435ebf91..282291c1a 100644 --- a/src/main/java/li/cil/oc/api/detail/MachineAPI.java +++ b/src/main/java/li/cil/oc/api/detail/MachineAPI.java @@ -4,6 +4,8 @@ import li.cil.oc.api.machine.Architecture; import li.cil.oc.api.machine.Machine; import li.cil.oc.api.machine.MachineHost; +import java.util.Collection; + public interface MachineAPI { /** * Register an architecture that can be used to create new machines. @@ -25,7 +27,7 @@ public interface MachineAPI { * a custom architecture also registers it, you may not see it in this list * until it also created a new machine using that architecture. */ - Iterable> architectures(); + Collection> architectures(); /** * Creates a new machine for the specified host. diff --git a/src/main/java/li/cil/oc/api/network/Component.java b/src/main/java/li/cil/oc/api/network/Component.java index ec032f727..abf29618f 100644 --- a/src/main/java/li/cil/oc/api/network/Component.java +++ b/src/main/java/li/cil/oc/api/network/Component.java @@ -3,6 +3,8 @@ package li.cil.oc.api.network; import li.cil.oc.api.machine.Callback; import li.cil.oc.api.machine.Context; +import java.util.Collection; + /** * Components are nodes that can be addressed computers via drivers. *

@@ -68,11 +70,13 @@ public interface Component extends Node { /** * The list of names of methods exposed by this component. *

- * Note: this does not return the callback annotations directly, because - * those may not contain the method's name (as it defaults to the name of - * the annotated method). + * This does not return the callback annotations directly, because those + * may not contain the method's name (as it defaults to the name of the + * annotated method). + *

+ * The returned collection is read-only. */ - Iterable methods(); + Collection methods(); /** * Get the annotation information of a method. diff --git a/src/main/scala/li/cil/oc/server/driver/Registry.scala b/src/main/scala/li/cil/oc/server/driver/Registry.scala index 9ac4ee11f..b87f50c56 100644 --- a/src/main/scala/li/cil/oc/server/driver/Registry.scala +++ b/src/main/scala/li/cil/oc/server/driver/Registry.scala @@ -11,6 +11,7 @@ import li.cil.oc.api.machine.Value import net.minecraft.item.ItemStack import net.minecraft.world.World +import scala.collection.convert.WrapAsJava._ import scala.collection.convert.WrapAsScala._ import scala.collection.mutable import scala.math.ScalaNumber @@ -76,6 +77,10 @@ private[oc] object Registry extends api.detail.DriverAPI { if (stack != null) items.find(_.worksWith(stack)).orNull else null + override def blockDrivers = blocks.toSeq + + override def itemDrivers = items.toSeq + def convert(value: Array[AnyRef]) = if (value != null) value.map(arg => convertRecursively(arg, new util.IdentityHashMap())) else null def convertRecursively(value: Any, memo: util.IdentityHashMap[AnyRef, AnyRef], force: Boolean = false): AnyRef = { diff --git a/src/main/scala/li/cil/oc/server/machine/Machine.scala b/src/main/scala/li/cil/oc/server/machine/Machine.scala index 02abce1bd..405fb57e5 100644 --- a/src/main/scala/li/cil/oc/server/machine/Machine.scala +++ b/src/main/scala/li/cil/oc/server/machine/Machine.scala @@ -889,7 +889,7 @@ object Machine extends MachineAPI { } } - override def architectures = scala.collection.convert.WrapAsJava.asJavaIterable(checked) + override def architectures = checked.toSeq override def create(host: MachineHost) = new Machine(host)