Class ResourceHandlerUtil

java.lang.Object
net.neoforged.neoforge.transfer.ResourceHandlerUtil

public final class ResourceHandlerUtil extends Object
Utility methods for dealing with ResourceHandler.
  • Constructor Details

    • ResourceHandlerUtil

      private ResourceHandlerUtil()
  • Method Details

    • isEmpty

      public static boolean isEmpty(Resource resource, int amount)
      Determines if either the given resource or amount is classified as empty: if either Resource.isEmpty() is true, or the amount is zero (or negative) then the resource is considered empty.
      Parameters:
      resource - The resource to check.
      amount - An amount to check.
      Returns:
      true if either Resource.isEmpty() returns true, or the amount is <= 0.
    • isEmpty

      public static boolean isEmpty(ResourceHandler<? extends Resource> handler)
      Checks if a ResourceHandler is empty.

      A ResourceHandler is considered empty if it has an amount of zero at all its indices.

      A handler of size zero will always be considered empty.

      Parameters:
      handler - the ResourceHandler to check for emptiness
      Returns:
      true if the ResourceHandler is empty, false otherwise
    • isFull

      public static <T extends Resource> boolean isFull(ResourceHandler<T> handler)
      Checks if a ResourceHandler is full.

      A IResourceHandler is considered full if all of its indices contain resources with amounts greater than or equal to their respective capacity.

      A handler of size zero is always considered full.

      Parameters:
      handler - the ResourceHandler to check
      Returns:
      true if the ResourceHandler is full, false otherwise
    • isValid

      public static <T extends Resource> boolean isValid(ResourceHandler<T> handler, T resource)
      Returns whether the given resource is valid for any index of the given resource handler.
      Parameters:
      handler - the ResourceHandler to check
      resource - the resource to check. Must be non-empty.
      Returns:
      true if the resource is valid in any index of the handler.
      See Also:
    • getRedstoneSignalFromResourceHandler

      public static <T extends Resource> int getRedstoneSignalFromResourceHandler(ResourceHandler<T> handler)
      Calculates the redstone signal strength based on the given resource handler's content. This value is between 0 and 15.

      This method is based on AbstractContainerMenu.getRedstoneSignalFromContainer(Container).

      Type Parameters:
      T - the type of resource handled by the handler
      Parameters:
      handler - the resource handler to calculate the signal from
      Returns:
      the redstone signal strength
    • insertStacking

      public static <T extends Resource> int insertStacking(@Nullable ResourceHandler<T> handler, T resource, int amount, @Nullable TransactionContext transaction)
      Tries to insert up to some amount of a resource into the handler, using stacking logic: resources will be inserted into filled indices first, then empty indices.
      Parameters:
      handler - the ResourceHandler to insert the resource into. can be null, which makes this method a no-op.
      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. Passing in null will open a root transaction, and commit it at the end of the method.
      Returns:
      the amount of the resource that was inserted
    • extractFirst

      public static <T extends Resource> @Nullable ResourceStack<T> extractFirst(@Nullable ResourceHandler<T> handler, Predicate<T> filter, int amount, @Nullable TransactionContext transaction)
      Extracts the first resource from a ResourceHandler that is not empty and matches the given filter.
      Type Parameters:
      T - The type of resource handled by the handler
      Parameters:
      handler - The ResourceHandler to extract the resource from. Can be null, which makes this method a no-op.
      filter - The first non-empty resource for which this filter returns true will be extracted.
      amount - The desired amount of the resource to extract
      transaction - The transaction context for the operation. Passing in null will open a root transaction, whereas passing in a transaction will allow you to make the final decision to commit based on the results of this method.
      Returns:
      the resource and amount that was extracted or null if nothing was extracted
    • move

      public static <T extends Resource> int move(@Nullable ResourceHandler<T> from, @Nullable ResourceHandler<T> to, Predicate<T> filter, int amount, @Nullable TransactionContext transaction)
      Move resources matching a given filter between two resource handlers, and return the amount that was successfully moved.

      Usage Example

      
       // Source
       IResourceHandler<FluidResource> source;
       // Target
       IResourceHandler<FluidResource> target;
       // Move exactly one bucket of water:
       try (Transaction transaction = Transaction.open(null)) {
           int waterMoved = ResourceHandlerUtil.move(source, target, fr -> fr.is(Fluids.WATER), FluidType.BUCKET_VOLUME, transaction);
           if (waterMoved == FluidType.BUCKET_VOLUME) {
               // Only commit if exactly one bucket was moved.
               transaction.commit();
           }
           // Leaving this try-block will keep changes only when the transaction was committed.
       }
       
      Parameters:
      from - The source handler. May be null.
      to - The target handler. May be null.
      filter - The filter for transferred resources. Only resources for which this filter returns true will be transferred. This filter will never be tested with an empty resource.
      amount - The maximum amount that will be transferred.
      transaction - The transaction that this operation is part of. Passing in null will open a root transaction, and commit it at the end of the method. Passing in a transaction will allow the caller to make the final decision to commit or not, based on the results of this method.
      Returns:
      the resource and amount that was transferred or null if nothing was transferred
      Throws:
      IllegalStateException - If no transaction is passed and a transaction is already active on the current thread.
    • moveFirst

      public static <T extends Resource> @Nullable ResourceStack<T> moveFirst(@Nullable ResourceHandler<T> from, @Nullable ResourceHandler<T> to, Predicate<T> filter, int amount, @Nullable TransactionContext transaction)
      Type Parameters:
      T - the type of resource to move.
      Parameters:
      from - The source handler. May be null.
      to - The target handler. May be null.
      filter - The filter for transferred resources. Only resources for which this filter returns true will be transferred. This filter will never be tested with an empty resource.
      amount - The maximum amount that will be transferred.
      transaction - The transaction context for the operation. Passing in null will open a root transaction, whereas passing in a transaction will allow you to make the final decision to commit based on the results of this method.
      Returns:
      the resource and amount that was transferred or null if nothing was transferred
    • contains

      public static <T extends Resource> boolean contains(ResourceHandler<T> handler, T resource)
      Returns true if the resource handler contains the given resource, false otherwise.
      Parameters:
      handler - The handler to check for the resource.
      resource - The resource to check for. Must be non-empty.
      Returns:
      true if the resource handler contains the given resource, false otherwise
    • indexOf

      public static <T extends Resource> int indexOf(ResourceHandler<T> handler, T resource)
      Returns the first index that contains the given resource, or -1 if no index contains it.
      Parameters:
      handler - The handler to check for the resource.
      resource - The resource to find. Must be non-empty.
      Returns:
      the first index that contains the given resource, or -1 if no index contains it
    • findExtractableResource

      public static <T extends Resource> @Nullable T findExtractableResource(ResourceHandler<T> handler, Predicate<T> filter, @Nullable TransactionContext transaction)
      Checks if the given resource handler has at least one resource that matches the given filter and can be extracted.
      Type Parameters:
      T - The type of resource handled by the handler.
      Parameters:
      handler - The handler to check for resources.
      filter - A filter that will be applied to each non-empty resource in the handler. Only resources for which this filter returns true will be considered.
      transaction - The transaction that this operation is part of. This method will always use a nested transaction that will be rolled back. null can be passed to conveniently have this method open its own root transaction.
      Returns:
      The first non-empty resource that matches the filter and is extractable, or null otherwise.