From 4b70a026d211c9d94c6afe3f899b23d53cf2cc70 Mon Sep 17 00:00:00 2001 From: Vexatos Date: Mon, 14 Nov 2016 22:09:13 +0100 Subject: [PATCH] Allow multiple environment providers to return environments for the same ItemStack. --- src/main/java/li/cil/oc/api/Driver.java | 21 ++++++++++++++++++- .../java/li/cil/oc/api/detail/DriverAPI.java | 15 +++++++++++++ .../integration/nei/CallbackDocHandler.scala | 2 +- .../li/cil/oc/server/driver/Registry.scala | 3 +++ 4 files changed, 39 insertions(+), 2 deletions(-) diff --git a/src/main/java/li/cil/oc/api/Driver.java b/src/main/java/li/cil/oc/api/Driver.java index e805b65ee..9d3836f23 100644 --- a/src/main/java/li/cil/oc/api/Driver.java +++ b/src/main/java/li/cil/oc/api/Driver.java @@ -14,6 +14,7 @@ import net.minecraft.world.World; import net.minecraftforge.common.util.ForgeDirection; import java.util.Collection; +import java.util.Set; /** * This API allows registering new drivers with the mod. @@ -219,13 +220,31 @@ public final class Driver { * * @param stack the item stack to get the environment type for. * @return the type of environment associated with the stack, or null. + * @deprecated Use {@link #environmentsFor(ItemStack)} instead. */ + @Deprecated public static Class environmentFor(ItemStack stack) { if (API.driver != null) return API.driver.environmentFor(stack); return null; } + /** + * Looks up the environments associated with the specified item stack. + *

+ * This will use the registered {@link EnvironmentProvider}s to find + * environment types for the specified item stack. If none can be + * found, returns an empty Set. + * + * @param stack the item stack to get the environment type for. + * @return the type of environment associated with the stack, or an empty Set, or null if the API is not present. + */ + public static Set> environmentsFor(ItemStack stack) { + if (API.driver != null) + return API.driver.environmentsFor(stack); + return null; + } + /** * Get an inventory implementation providing access to an item inventory. *

@@ -282,4 +301,4 @@ public final class Driver { private Driver() { } -} \ No newline at end of file +} 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 8c1d1b4f7..4cec1fa65 100644 --- a/src/main/java/li/cil/oc/api/detail/DriverAPI.java +++ b/src/main/java/li/cil/oc/api/detail/DriverAPI.java @@ -14,6 +14,7 @@ import net.minecraft.world.World; import net.minecraftforge.common.util.ForgeDirection; import java.util.Collection; +import java.util.Set; public interface DriverAPI { /** @@ -169,9 +170,23 @@ public interface DriverAPI { * * @param stack the item stack to get the environment type for. * @return the type of environment associated with the stack, or null. + * @deprecated Use {@link #environmentsFor(ItemStack)} instead. */ + @Deprecated Class environmentFor(ItemStack stack); + /** + * Looks up the environments associated with the specified item stack. + *

+ * This will use the registered {@link EnvironmentProvider}s to find + * environment types for the specified item stack. If none can be + * found, returns an empty Set. + * + * @param stack the item stack to get the environment type for. + * @return the type of environment associated with the stack, or an empty Set. + */ + Set> environmentsFor(ItemStack stack); + /** * Get an inventory implementation providing access to an item inventory. *

diff --git a/src/main/scala/li/cil/oc/integration/nei/CallbackDocHandler.scala b/src/main/scala/li/cil/oc/integration/nei/CallbackDocHandler.scala index ce9a282e3..5c9711ae9 100644 --- a/src/main/scala/li/cil/oc/integration/nei/CallbackDocHandler.scala +++ b/src/main/scala/li/cil/oc/integration/nei/CallbackDocHandler.scala @@ -25,7 +25,7 @@ class CallbackDocHandler(pages: Option[Array[String]]) extends PagedUsageHandler if (input == "item") { ingredients.collect { case stack: ItemStack if stack.getItem != null => - val callbacks = getCallbacks(api.Driver.environmentFor(stack)).toBuffer + val callbacks = api.Driver.environmentsFor(stack).flatMap(getCallbacks).toBuffer // TODO remove in OC 1.7 if (callbacks.isEmpty) { 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 c787f4106..5ebb89d66 100644 --- a/src/main/scala/li/cil/oc/server/driver/Registry.scala +++ b/src/main/scala/li/cil/oc/server/driver/Registry.scala @@ -136,12 +136,15 @@ private[oc] object Registry extends api.detail.DriverAPI { if (stack != null) items.find(_.worksWith(stack)).orNull else null + @Deprecated override def environmentFor(stack: ItemStack): Class[_] = { environmentProviders.map(provider => provider.getEnvironment(stack)).collectFirst { case clazz: Class[_] => clazz }.orNull } + override def environmentsFor(stack: ItemStack): util.Set[Class[_]] = environmentProviders.map(_.getEnvironment(stack)).filter(_ != null).toSet[Class[_]] + override def inventoryFor(stack: ItemStack, player: EntityPlayer): IInventory = { inventoryProviders.find(provider => provider.worksWith(stack, player)). map(provider => provider.getInventory(stack, player)).