Class ItemAccessItemHandler
java.lang.Object
net.neoforged.neoforge.transfer.ItemAccessResourceHandler<ItemResource>
net.neoforged.neoforge.transfer.item.ItemAccessItemHandler
- All Implemented Interfaces:
ResourceHandler<ItemResource>
Base implementation of an item
ResourceHandler backed by an ItemAccess.
The stacks are stored in a ItemContainerContents data component.
To use this class, register a new DataComponentType which holds an ItemContainerContents for your item.
Then reference that component from your ICapabilityProvider passed to RegisterCapabilitiesEvent.registerItem(net.neoforged.neoforge.capabilities.ItemCapability<T, C>, net.neoforged.neoforge.capabilities.ICapabilityProvider<net.minecraft.world.item.ItemStack, C, T>, net.minecraft.world.level.ItemLike...) to create an instance of this class.
-
Field Summary
FieldsModifier and TypeFieldDescriptionprotected final DataComponentType<ItemContainerContents> protected final ItemFields inherited from class net.neoforged.neoforge.transfer.ItemAccessResourceHandler
itemAccess, size -
Constructor Summary
ConstructorsConstructorDescriptionItemAccessItemHandler(ItemAccess itemAccess, DataComponentType<ItemContainerContents> component, int size) -
Method Summary
Modifier and TypeMethodDescriptionprotected intgetAmountFrom(ItemResource accessResource, int index) Retrieves the amount stored in the current contents of the item access.protected intgetCapacity(int index, ItemResource resource) Return the maximum capacity of this handler for the passed resource.protected ItemContainerContentsgetContents(ItemResource accessResource) Retrieves theItemContainerContentsfrom the current resource of the item access.protected ItemResourcegetResourceFrom(ItemResource accessResource, int index) Retrieves the resource stored in the current contents of the item access.protected ItemStackgetStackFromContents(ItemContainerContents contents, int slot) Retrieves a copy of a single stack from the underlying data component, returningItemStack.EMPTYif the component does not have a slot present.booleanisValid(int index, ItemResource resource) Returntrueif the passed non-empty resource can fit in this handler,falseotherwise.protected ItemResourceupdate(ItemResource accessResource, int index, ItemResource newResource, int newAmount) Returns a resource with updated resource and amount.Methods inherited from class net.neoforged.neoforge.transfer.ItemAccessResourceHandler
extract, getAmountAsLong, getCapacityAsLong, getResource, insert, sizeMethods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface net.neoforged.neoforge.transfer.ResourceHandler
extract, getAmountAsInt, getCapacityAsInt, insert
-
Field Details
-
validItem
-
component
-
-
Constructor Details
-
ItemAccessItemHandler
public ItemAccessItemHandler(ItemAccess itemAccess, DataComponentType<ItemContainerContents> component, int size)
-
-
Method Details
-
getContents
Retrieves theItemContainerContentsfrom the current resource of the item access. -
getStackFromContents
Retrieves a copy of a single stack from the underlying data component, returningItemStack.EMPTYif the component does not have a slot present.- Parameters:
contents- the existing contentsslot- the target slot- Returns:
- a copy of the stack in the target slot
-
getResourceFrom
Description copied from class:ItemAccessResourceHandlerRetrieves the resource stored in the current contents of the item access.- Specified by:
getResourceFromin classItemAccessResourceHandler<ItemResource>
-
getAmountFrom
Description copied from class:ItemAccessResourceHandlerRetrieves the amount stored in the current contents of the item access.- Specified by:
getAmountFromin classItemAccessResourceHandler<ItemResource>- See Also:
-
update
protected ItemResource update(ItemResource accessResource, int index, ItemResource newResource, int newAmount) Description copied from class:ItemAccessResourceHandlerReturns a resource with updated resource and amount.- Specified by:
updatein classItemAccessResourceHandler<ItemResource>- Parameters:
accessResource- current resource, before the updateindex- the index at which the resource and amount should be updatednewResource- the new resource, never empty: empty is indicated by a 0 amountnewAmount- the new amount- Returns:
accessResourceupdated with the new resource and amount, orItemResource.EMPTYif the new resource or amount cannot be stored
-
isValid
Description copied from class:ItemAccessResourceHandlerReturntrueif the passed non-empty resource can fit in this handler,falseotherwise.The result of this function is used in the provided implementations of:
ItemAccessResourceHandler.getCapacityAsLong(int, T), to report a capacity of0for invalid items;ItemAccessResourceHandler.insert(int, T, int, TransactionContext), to reject items that cannot fit in this handler.
- Specified by:
isValidin interfaceResourceHandler<ItemResource>- Overrides:
isValidin classItemAccessResourceHandler<ItemResource>- Parameters:
index- The index to check.resource- The resource to check. Must be non-empty.- Returns:
- whether the given resource is generally allowed to be contained at the given index, irrespective of the current amount or resource currently at that index
- See Also:
-
getCapacity
Description copied from class:ItemAccessResourceHandlerReturn the maximum capacity of this handler for the passed resource. If the passed resource is empty, an estimate should be returned.- Specified by:
getCapacityin classItemAccessResourceHandler<ItemResource>- Returns:
- The maximum capacity of this handler for the passed resource.
- See Also:
-