Item Api Lookup
public interface ItemApiLookup<A, C>
Content copied to clipboard
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.
{@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 * }}
{@code * public final class MyApi { * public static final ItemApiLookupFLUID_CONTAINER_ITEM = ItemApiLookup.get(new Identifier("mymod:fluid_container"), FluidContainer.class, Void.class);
* }}
{@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!"); * } * }}
{@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 * });}
{@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>
Content copied to clipboard
Functions
contextClass
Link copied to clipboard
get
Link copied to clipboard
registerFallback
Link copied to clipboard
abstract void registerFallback(ItemApiLookup.ItemApiProvider<A, C> fallbackProvider)
Content copied to clipboard
registerForItems
Link copied to clipboard
abstract void registerForItems(ItemApiLookup.ItemApiProvider<A, C> provider, Array<ItemConvertible> items)
Content copied to clipboard
registerSelf
Link copied to clipboard
Inheritors
ItemApiLookupImpl
Link copied to clipboard
Sources
jvm source
Link copied to clipboard