Class ItemAccessEnergyHandler

java.lang.Object
net.neoforged.neoforge.transfer.energy.ItemAccessEnergyHandler
All Implemented Interfaces:
EnergyHandler

public class ItemAccessEnergyHandler extends Object implements EnergyHandler
A simple implementation of an EnergyHandler backed by an ItemAccess. It can store any level of energy up to a given capacity, and has per-operation insertion and extraction limits. The energy level is stored in an Integer data component.

To use this class, register a new DataComponentType which holds an Integer 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 Details

    • itemAccess

      protected final ItemAccess itemAccess
    • validItem

      protected final Item validItem
    • energyComponent

      protected final DataComponentType<Integer> energyComponent
    • capacity

      protected final int capacity
    • maxInsert

      protected final int maxInsert
    • maxExtract

      protected final int maxExtract
  • Constructor Details

  • Method Details

    • getAmountFrom

      protected int getAmountFrom(ItemResource accessResource)
      Retrieves the amount stored in the current contents of the item access.
    • update

      protected ItemResource update(ItemResource accessResource, int newAmount)
      Returns a resource with updated amount.
      Parameters:
      accessResource - current resource, before the update
      newAmount - the new amount
      Returns:
      accessResource updated with the new amount, or ItemResource.EMPTY if the new amount cannot be stored
    • getAmountAsLong

      public long getAmountAsLong()
      Description copied from interface: EnergyHandler
      Returns the amount of energy currently stored, as a long.

      In general, energy handlers can report long amounts. However, if the handler is known to only support amounts up to Integer.MAX_VALUE, or if the caller prefers to deal in ints only, the int-returning overload can be used instead.

      The returned amount must be non-negative.

      Specified by:
      getAmountAsLong in interface EnergyHandler
      Returns:
      the amount as a long
      See Also:
    • getCapacityAsLong

      public long getCapacityAsLong()
      Description copied from interface: EnergyHandler
      Returns the capacity of the handler, irrespective of the current amount, as a long.

      In general, energy handlers can report long capacities. However, if the handler is known to only support capacities up to Integer.MAX_VALUE, or if the caller prefers to deal in ints only, the int-returning overload can be used instead.

      This function serves as a hint on the maximum amount the energy handler might contain, for example the handler can be considered full if amount >= capacity. Note that the returned capacity may overestimate the actual allowed amount, and it might be smaller than the current amount. The only way to know if a handler will accept a resource, is to try to insert it.

      Specified by:
      getCapacityAsLong in interface EnergyHandler
      Returns:
      the capacity, as a long
      See Also:
    • insert

      public int insert(int amount, TransactionContext transaction)
      Description copied from interface: EnergyHandler
      Inserts up to the given amount of energy into the handler.

      Changes to the handler are made in the context of a transaction.

      Specified by:
      insert in interface EnergyHandler
      Parameters:
      amount - The maximum amount of energy to insert. Must be non-negative.
      transaction - The transaction that this operation is part of.
      Returns:
      The amount that was inserted. Between 0 (inclusive, nothing was inserted) and amount (inclusive, everything was inserted).
    • extract

      public int extract(int amount, TransactionContext transaction)
      Description copied from interface: EnergyHandler
      Extracts up to the given amount of energy from the handler.

      Changes to the handler are made in the context of a transaction.

      Specified by:
      extract in interface EnergyHandler
      Parameters:
      amount - The maximum amount of energy to extract. Must be non-negative.
      transaction - The transaction that this operation is part of.
      Returns:
      The amount that was extracted. Between 0 (inclusive, nothing was extracted) and amount (inclusive, everything was extracted).