Allow multiple environment providers to return environments for the same ItemStack.

This commit is contained in:
Vexatos 2016-11-14 22:09:13 +01:00
parent 1d5c69924f
commit 4b70a026d2
4 changed files with 39 additions and 2 deletions

View File

@ -14,6 +14,7 @@ import net.minecraft.world.World;
import net.minecraftforge.common.util.ForgeDirection; import net.minecraftforge.common.util.ForgeDirection;
import java.util.Collection; import java.util.Collection;
import java.util.Set;
/** /**
* This API allows registering new drivers with the mod. * 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. * @param stack the item stack to get the environment type for.
* @return the type of environment associated with the stack, or <tt>null</tt>. * @return the type of environment associated with the stack, or <tt>null</tt>.
* @deprecated Use {@link #environmentsFor(ItemStack)} instead.
*/ */
@Deprecated
public static Class<?> environmentFor(ItemStack stack) { public static Class<?> environmentFor(ItemStack stack) {
if (API.driver != null) if (API.driver != null)
return API.driver.environmentFor(stack); return API.driver.environmentFor(stack);
return null; return null;
} }
/**
* Looks up the environments associated with the specified item stack.
* <p/>
* 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<Class<?>> environmentsFor(ItemStack stack) {
if (API.driver != null)
return API.driver.environmentsFor(stack);
return null;
}
/** /**
* Get an inventory implementation providing access to an item inventory. * Get an inventory implementation providing access to an item inventory.
* <p/> * <p/>

View File

@ -14,6 +14,7 @@ import net.minecraft.world.World;
import net.minecraftforge.common.util.ForgeDirection; import net.minecraftforge.common.util.ForgeDirection;
import java.util.Collection; import java.util.Collection;
import java.util.Set;
public interface DriverAPI { public interface DriverAPI {
/** /**
@ -169,9 +170,23 @@ public interface DriverAPI {
* *
* @param stack the item stack to get the environment type for. * @param stack the item stack to get the environment type for.
* @return the type of environment associated with the stack, or <tt>null</tt>. * @return the type of environment associated with the stack, or <tt>null</tt>.
* @deprecated Use {@link #environmentsFor(ItemStack)} instead.
*/ */
@Deprecated
Class<?> environmentFor(ItemStack stack); Class<?> environmentFor(ItemStack stack);
/**
* Looks up the environments associated with the specified item stack.
* <p/>
* 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<Class<?>> environmentsFor(ItemStack stack);
/** /**
* Get an inventory implementation providing access to an item inventory. * Get an inventory implementation providing access to an item inventory.
* <p/> * <p/>

View File

@ -25,7 +25,7 @@ class CallbackDocHandler(pages: Option[Array[String]]) extends PagedUsageHandler
if (input == "item") { if (input == "item") {
ingredients.collect { ingredients.collect {
case stack: ItemStack if stack.getItem != null => 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 // TODO remove in OC 1.7
if (callbacks.isEmpty) { if (callbacks.isEmpty) {

View File

@ -136,12 +136,15 @@ private[oc] object Registry extends api.detail.DriverAPI {
if (stack != null) items.find(_.worksWith(stack)).orNull if (stack != null) items.find(_.worksWith(stack)).orNull
else null else null
@Deprecated
override def environmentFor(stack: ItemStack): Class[_] = { override def environmentFor(stack: ItemStack): Class[_] = {
environmentProviders.map(provider => provider.getEnvironment(stack)).collectFirst { environmentProviders.map(provider => provider.getEnvironment(stack)).collectFirst {
case clazz: Class[_] => clazz case clazz: Class[_] => clazz
}.orNull }.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 = { override def inventoryFor(stack: ItemStack, player: EntityPlayer): IInventory = {
inventoryProviders.find(provider => provider.worksWith(stack, player)). inventoryProviders.find(provider => provider.worksWith(stack, player)).
map(provider => provider.getInventory(stack, player)). map(provider => provider.getInventory(stack, player)).