Class CombinedResourceHandler<T extends Resource>

java.lang.Object
net.neoforged.neoforge.transfer.CombinedResourceHandler<T>
All Implemented Interfaces:
ResourceHandler<T>
Direct Known Subclasses:
LivingEntityEquipmentWrapper.EquipmentTypeWrapper

public class CombinedResourceHandler<T extends Resource> extends Object implements ResourceHandler<T>
A resource handler that wraps multiple resource handlers, concatenating all their indices into one large handler.

The size of the wrapper will not change in response to changes in the size of wrapped handlers. This means that indices added to the wrapped handlers will not be visible and indices removed will appear empty and read-only. To adjust to size changes of the wrapped handlers, the wrapper must be recreated.

  • Field Details

    • handlers

      private final ResourceHandler<T extends Resource>[] handlers
      The wrapped handlers.
    • baseIndex

      private final int[] baseIndex
      For each wrapped handler, the index at which it starts in the combined handler.
    • sizeCache

      private final int sizeCache
      The total number of indices in the combined handler, which is the sum of the sizes of all wrapped handlers.
  • Constructor Details

  • Method Details

    • getHandlerIndex

      protected int getHandlerIndex(int index)
      Returns the index of the handler in handlers that contains the given index.
    • getHandlerFromIndex

      protected ResourceHandler<T> getHandlerFromIndex(int handlerIndex)
    • getSlotFromIndex

      protected int getSlotFromIndex(int index, int handlerIndex)
    • size

      public final int size()
      Description copied from interface: ResourceHandler
      Returns the current number of indices in this resource handler.

      This size provides a bound on the valid indices for this handler, see the documentation of ResourceHandler.

      Specified by:
      size in interface ResourceHandler<T extends Resource>
      Returns:
      The size of the resource handler. Can be 0 if the handler currently has no indices.
    • getResource

      public T getResource(int index)
      Description copied from interface: ResourceHandler
      Returns the resource at the given index, which may be empty.

      If the resource is empty, the stored amount must be 0.

      Specified by:
      getResource in interface ResourceHandler<T extends Resource>
      Parameters:
      index - The index to get the resource from.
      Returns:
      the resource at the given index, which may be empty
    • getAmountAsLong

      public long getAmountAsLong(int index)
      Description copied from interface: ResourceHandler
      Returns the amount of the currently stored resource at the given index, as a long.

      In general, resource 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. If the stored resource is empty, the amount must be 0.

      Specified by:
      getAmountAsLong in interface ResourceHandler<T extends Resource>
      Parameters:
      index - The index to get the amount from.
      Returns:
      the amount at the given index, as a long
      See Also:
    • getCapacityAsLong

      public long getCapacityAsLong(int index, T resource)
      Description copied from interface: ResourceHandler
      Returns the capacity of the handler at the given index and for the given resource, irrespective of the current amount or resource currently at that index, as a long.

      In general, resource 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 resource 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 ResourceHandler<T extends Resource>
      Parameters:
      index - The index to get the capacity for.
      resource - The resource to get the capacity for. May be empty to get the general capacity at the index.
      Returns:
      the capacity at the given index, as a long
      See Also:
    • isValid

      public boolean isValid(int index, T resource)
      Description copied from interface: ResourceHandler
      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.

      This function serves as a hint on whether the resource handler can contain the resource or not. The only way to know if a handler will accept a resource, is to try to insert it.

      Specified by:
      isValid in interface ResourceHandler<T extends Resource>
      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
    • insert

      public int insert(int index, T resource, int amount, TransactionContext transaction)
      Description copied from interface: ResourceHandler
      Inserts up to the given amount of a resource into the handler at the given index.

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

      Specified by:
      insert in interface ResourceHandler<T extends Resource>
      Parameters:
      index - The index to insert the resource into.
      resource - The resource to insert. Must be non-empty.
      amount - The maximum amount of the resource 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).
      See Also:
    • insert

      public int insert(T resource, int amount, TransactionContext transaction)
      Description copied from interface: ResourceHandler
      Inserts up to the given amount of a resource into the handler.

      This function is preferred to the index-specific overload since it lets the handler decide how to distribute the resource.

      This method is expected to be more efficient than callers trying to find a suitable index for insertion themselves.

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

      Specified by:
      insert in interface ResourceHandler<T extends Resource>
      Parameters:
      resource - The resource to insert. Must be non-empty.
      amount - The maximum amount of the resource 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).
      See Also:
    • extract

      public int extract(int index, T resource, int amount, TransactionContext transaction)
      Description copied from interface: ResourceHandler
      Extracts up to the given amount of a resource from the handler at the given index.

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

      Specified by:
      extract in interface ResourceHandler<T extends Resource>
      Parameters:
      index - The index to extract the resource from.
      resource - The resource to extract. Must be non-empty.
      amount - The maximum amount of the resource 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).
      See Also:
    • extract

      public int extract(T resource, int amount, TransactionContext transaction)
      Description copied from interface: ResourceHandler
      Tries to extract up to the given amount of a resource from the handler.

      This function is preferred to the index-specific overload since it lets the handler decide how to find indices that contain the resource.

      This method is expected to be more efficient than callers trying to find indices that contain the resource themselves.

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

      Specified by:
      extract in interface ResourceHandler<T extends Resource>
      Parameters:
      resource - The resource to extract. Must be non-empty.
      amount - The maximum amount of the resource 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).
      See Also: