ItemApiLookup

public interface ItemApiLookup<A, C>

An object that allows retrieving APIs from item stacks. Instances of this interface can be obtained through get.

When trying to find an API for an item stack, the provider registered for the item of the stack will be queried if it exists. If it doesn't exist, or if it returns {@code null}, the fallback providers will be queried in order.

Usage ExampleLet us reuse {@code FluidContainer} from the BlockApiLookup example. We will query {@code FluidContainer} instances from the stack directly. We need no context, so we will use {@code Void}.
{@code * public interface FluidContainer { * boolean containsFluids(); // return true if not empty * }}
We need to create the ItemApiLookup:
{@code * public final class MyApi { * public static final ItemApiLookupFLUID_CONTAINER_ITEM = ItemApiLookup.get(new Identifier("mymod:fluid_container"), FluidContainer.class, Void.class);
 * }}
API instances are easy to access:
{@code * FluidContainer container = MyApi.FLUID_CONTAINER_ITEM.find(itemStack, null); // Void is always null * if (container != null) { * // Do something with the container * if (container.containsFluids()) { * System.out.println("It contains fluids!"); * } * }}
For the query to return a useful result, we must expose the API:
{@code * // If the item directly implements the interface, registerSelf can be used. * public class InfiniteWaterItem implements FluidContainer { * @Override * public boolean containsFluids() { * return true; // This item always contains fluids! * } * } * MyApi.FLUID_CONTAINER_ITEM.registerSelf(INFINITE_WATER_ITEM); * * // Otherwise, registerForItems can be used. * MyApi.FLUID_CONTAINER_ITEM.registerForItems((itemStack, ignored) -> { * // return a FluidContainer for your item, or null if there is none * // the second parameter is Void in this case, so it's always null and can be ignored * }, ITEM_INSTANCE, ANOTHER_ITEM_INSTANCE); // register as many items as you want * * // General fallback, to interface with anything, for example another ItemApiLookup. * MyApi.FLUID_CONTAINER_ITEM.registerFallback((itemStack, ignored) -> { * // return something if available, or null * });}
Generic context typesNote that {@code FluidContainer} and {@code Void} were completely arbitrary in this example. We can define any {@code ItemApiLookup<A, C>}, where {@code A} is the type of the queried API, and {@code C} is the type of the additional context (the void parameter in the previous example). If no context is necessary, {@code Void} should be used, and {@code null} instances should be passed, like we did in the example.

Parameters

<A>

The type of the API.

<C>

The type of the additional context object.

Types

ItemApiProvider
Link copied to clipboard
public interface ItemApiProvider<A, C>

Functions

apiClass
Link copied to clipboard
abstract Class<AapiClass()
Return the API class of this lookup.
contextClass
Link copied to clipboard
abstract Class<CcontextClass()
Return the context class of this lookup.
find
Link copied to clipboard
@Nullable()
abstract A find(ItemStack itemStack, C context)
Attempt to retrieve an API from an item stack.
get
Link copied to clipboard
static ItemApiLookup<A, Cget<A, C>(Identifier lookupId, Class<A> apiClass, Class<C> contextClass)
Retrieve the ItemApiLookup associated with an identifier, or create it if it didn't exist yet.
registerFallback
Link copied to clipboard
abstract void registerFallback(ItemApiLookup.ItemApiProvider<A, C> fallbackProvider)
Expose the API for all queries: the fallbacks providers will be invoked if no object was found using the regular providers.
registerForItems
Link copied to clipboard
abstract void registerForItems(ItemApiLookup.ItemApiProvider<A, C> provider, Array<ItemConvertible> items)
Expose the API for the passed items.
registerSelf
Link copied to clipboard
abstract void registerSelf(Array<ItemConvertible> items)
Expose the API for the passed items directly implementing it.

Inheritors

ItemApiLookupImpl
Link copied to clipboard

Sources

jvm source
Link copied to clipboard