Class ComponentItemHandler

java.lang.Object
net.neoforged.neoforge.items.ComponentItemHandler
All Implemented Interfaces:
IItemHandler, IItemHandlerModifiable

public class ComponentItemHandler extends Object implements IItemHandlerModifiable
Variant of ItemStackHandler for use with data components.

The actual data storage is managed by a data component, and all changes will write back to that 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.

Since data components are immutable, this will not work nicely with vanilla's container methods which expect the stack to be mutable. Use ItemHandlerCopySlot to get around this issue.

  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    protected final net.minecraft.core.component.DataComponentType<net.minecraft.world.item.component.ItemContainerContents>
     
     
    protected final int
     
  • Constructor Summary

    Constructors
    Constructor
    Description
    ComponentItemHandler(MutableDataComponentHolder parent, net.minecraft.core.component.DataComponentType<net.minecraft.world.item.component.ItemContainerContents> component, int size)
    Creates a new ComponentItemHandler with target size.
  • Method Summary

    Modifier and Type
    Method
    Description
    net.minecraft.world.item.ItemStack
    extractItem(int slot, int amount, boolean simulate)
    Extracts an ItemStack from the given slot.
    protected net.minecraft.world.item.component.ItemContainerContents
    Retrieves the ItemContainerContents from the parent object's data component map.
    int
    getSlotLimit(int slot)
    Retrieves the maximum stack size allowed to exist in the given slot.
    int
    Returns the number of slots available
    protected net.minecraft.world.item.ItemStack
    getStackFromContents(net.minecraft.world.item.component.ItemContainerContents contents, int slot)
    Retrieves a copy of a single stack from the underlying data component, returning ItemStack.EMPTY if the component does not have a slot present.
    net.minecraft.world.item.ItemStack
    getStackInSlot(int slot)
    Returns the ItemStack in a given slot.
    net.minecraft.world.item.ItemStack
    insertItem(int slot, net.minecraft.world.item.ItemStack toInsert, boolean simulate)
    Inserts an ItemStack into the given slot and return the remainder.
    boolean
    isItemValid(int slot, net.minecraft.world.item.ItemStack stack)
    This function re-implements the vanilla function Container.canPlaceItem(int, ItemStack).
    protected void
    onContentsChanged(int slot, net.minecraft.world.item.ItemStack oldStack, net.minecraft.world.item.ItemStack newStack)
    void
    setStackInSlot(int slot, net.minecraft.world.item.ItemStack stack)
    Overrides the stack in the given slot.
    protected void
    updateContents(net.minecraft.world.item.component.ItemContainerContents contents, net.minecraft.world.item.ItemStack stack, int slot)
    Performs a copy and write operation on the underlying data component, changing the stack in the target slot.
    protected final void
    Throws UnsupportedOperationException if the provided slot index is invalid.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • parent

      protected final MutableDataComponentHolder parent
    • component

      protected final net.minecraft.core.component.DataComponentType<net.minecraft.world.item.component.ItemContainerContents> component
    • size

      protected final int size
  • Constructor Details

    • ComponentItemHandler

      public ComponentItemHandler(MutableDataComponentHolder parent, net.minecraft.core.component.DataComponentType<net.minecraft.world.item.component.ItemContainerContents> component, int size)
      Creates a new ComponentItemHandler with target size. If the existing component is smaller than the given size, it will be expanded on write.
      Parameters:
      parent - The parent component holder, such as an ItemStack
      component - The data component referencing the stored inventory of the item stack
      size - The number of slots. Must be less than 256 due to limitations of ItemContainerContents
  • Method Details

    • getSlots

      public int getSlots()
      Description copied from interface: IItemHandler
      Returns the number of slots available
      Specified by:
      getSlots in interface IItemHandler
      Returns:
      The number of slots available
    • getStackInSlot

      public net.minecraft.world.item.ItemStack getStackInSlot(int slot)
      Description copied from interface: IItemHandler
      Returns the ItemStack in a given slot. The result's stack size may be greater than the itemstack's max size. If the result is empty, then the slot is empty.

      IMPORTANT: This ItemStack MUST NOT be modified. This method is not for altering an inventory's contents. Any implementers who are able to detect modification through this method should throw an exception.

      SERIOUSLY: DO NOT MODIFY THE RETURNED ITEMSTACK

      Specified by:
      getStackInSlot in interface IItemHandler
      Parameters:
      slot - Slot to query
      Returns:
      ItemStack in given slot. Empty Itemstack if the slot is empty.
    • setStackInSlot

      public void setStackInSlot(int slot, net.minecraft.world.item.ItemStack stack)
      Description copied from interface: IItemHandlerModifiable
      Overrides the stack in the given slot. This method is used by the standard Forge helper methods and classes. It is not intended for general use by other mods, and the handler may throw an error if it is called unexpectedly.
      Specified by:
      setStackInSlot in interface IItemHandlerModifiable
      Parameters:
      slot - Slot to modify
      stack - ItemStack to set slot to (may be empty).
    • insertItem

      public net.minecraft.world.item.ItemStack insertItem(int slot, net.minecraft.world.item.ItemStack toInsert, boolean simulate)
      Description copied from interface: IItemHandler

      Inserts an ItemStack into the given slot and return the remainder. The ItemStack should not be modified in this function!

      Note: This behaviour is subtly different from IFluidHandler.fill(FluidStack, IFluidHandler.FluidAction)
      Specified by:
      insertItem in interface IItemHandler
      Parameters:
      slot - Slot to insert into.
      toInsert - ItemStack to insert. This must not be modified by the item handler.
      simulate - If true, the insertion is only simulated
      Returns:
      The remaining ItemStack that was not inserted (if the entire stack is accepted, then return an empty ItemStack). May be the same as the input ItemStack if unchanged, otherwise a new ItemStack. The returned ItemStack can be safely modified after.
    • extractItem

      public net.minecraft.world.item.ItemStack extractItem(int slot, int amount, boolean simulate)
      Description copied from interface: IItemHandler
      Extracts an ItemStack from the given slot.

      The returned value must be empty if nothing is extracted, otherwise its stack size must be less than or equal to amount and ItemStack.getMaxStackSize().

      Specified by:
      extractItem in interface IItemHandler
      Parameters:
      slot - Slot to extract from.
      amount - Amount to extract (may be greater than the current stack's max limit)
      simulate - If true, the extraction is only simulated
      Returns:
      ItemStack extracted from the slot, must be empty if nothing can be extracted. The returned ItemStack can be safely modified after, so item handlers should return a new or copied stack.
    • getSlotLimit

      public int getSlotLimit(int slot)
      Description copied from interface: IItemHandler
      Retrieves the maximum stack size allowed to exist in the given slot.
      Specified by:
      getSlotLimit in interface IItemHandler
      Parameters:
      slot - Slot to query.
      Returns:
      The maximum stack size allowed in the slot.
    • isItemValid

      public boolean isItemValid(int slot, net.minecraft.world.item.ItemStack stack)
      Description copied from interface: IItemHandler

      This function re-implements the vanilla function Container.canPlaceItem(int, ItemStack). It should be used instead of simulated insertions in cases where the contents and state of the inventory are irrelevant, mainly for the purpose of automation and logic (for instance, testing if a minecart can wait to deposit its items into a full inventory, or if the items in the minecart can never be placed into the inventory and should move on).

      • isItemValid is false when insertion of the item is never valid.
      • When isItemValid is true, no assumptions can be made and insertion must be simulated case-by-case.
      • The actual items in the inventory, its fullness, or any other state are not considered by isItemValid.
      Specified by:
      isItemValid in interface IItemHandler
      Parameters:
      slot - Slot to query for validity
      stack - Stack to test with for validity
      Returns:
      true if the slot can insert the ItemStack, not considering the current state of the inventory. false if the slot can never insert the ItemStack in any situation.
    • onContentsChanged

      protected void onContentsChanged(int slot, net.minecraft.world.item.ItemStack oldStack, net.minecraft.world.item.ItemStack newStack)
      Called from updateContents(net.minecraft.world.item.component.ItemContainerContents, net.minecraft.world.item.ItemStack, int) after the stack stored in a slot has been updated.

      Modifications to the stacks used as parameters here will not write-back to the stored data.

      Parameters:
      slot - The slot that changed
      oldStack - The old stack that was present in the slot
      newStack - The new stack that is now present in the slot
    • getContents

      protected net.minecraft.world.item.component.ItemContainerContents getContents()
      Retrieves the ItemContainerContents from the parent object's data component map.
    • getStackFromContents

      protected net.minecraft.world.item.ItemStack getStackFromContents(net.minecraft.world.item.component.ItemContainerContents contents, int slot)
      Retrieves a copy of a single stack from the underlying data component, returning ItemStack.EMPTY if the component does not have a slot present.

      Throws an exception if the slot is out-of-bounds for this capability.

      Parameters:
      contents - The existing contents from getContents()
      slot - The target slot
      Returns:
      A copy of the stack in the target slot
    • updateContents

      protected void updateContents(net.minecraft.world.item.component.ItemContainerContents contents, net.minecraft.world.item.ItemStack stack, int slot)
      Performs a copy and write operation on the underlying data component, changing the stack in the target slot.

      If the existing component is larger than getSlots(), additional slots will not be truncated.

      Parameters:
      contents - The existing contents from getContents()
      stack - The new stack to set to the slot
      slot - The target slot
    • validateSlotIndex

      protected final void validateSlotIndex(int slot)
      Throws UnsupportedOperationException if the provided slot index is invalid.