Package net.neoforged.neoforge.transfer
Class ResourceHandlerUtil
java.lang.Object
net.neoforged.neoforge.transfer.ResourceHandlerUtil
Utility methods for dealing with
ResourceHandler.-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic <T extends Resource>
booleancontains(ResourceHandler<T> handler, T resource) Returnstrueif the resource handler contains the given resource,falseotherwise.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 aResourceHandlerthat is not empty and matches the given filter.static <T extends Resource>
@Nullable TfindExtractableResource(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.static <T extends Resource>
intgetRedstoneSignalFromResourceHandler(ResourceHandler<T> handler) Calculates the redstone signal strength based on the given resource handler's content.static <T extends Resource>
intindexOf(ResourceHandler<T> handler, T resource) Returns the first index that contains the given resource, or -1 if no index contains it.static <T extends Resource>
intinsertStacking(@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.static booleanDetermines if either the given resource or amount is classified as empty: if eitherResource.isEmpty()istrue, or the amount is zero (or negative) then the resource is considered empty.static booleanisEmpty(ResourceHandler<? extends Resource> handler) Checks if aResourceHandleris empty.static <T extends Resource>
booleanisFull(ResourceHandler<T> handler) Checks if aResourceHandleris full.static <T extends Resource>
booleanisValid(ResourceHandler<T> handler, T resource) Returns whether the given resourceis validfor any index of the given resource handler.static <T extends Resource>
intmove(@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.static <T extends Resource>
@Nullable ResourceStack<T> moveFirst(@Nullable ResourceHandler<T> from, @Nullable ResourceHandler<T> to, Predicate<T> filter, int amount, @Nullable TransactionContext transaction) Similar tomove(net.neoforged.neoforge.transfer.ResourceHandler<T>, net.neoforged.neoforge.transfer.ResourceHandler<T>, java.util.function.Predicate<T>, int, net.neoforged.neoforge.transfer.transaction.TransactionContext), but transfers only the first resource that matches the filter and can be successfully transferred.
-
Constructor Details
-
ResourceHandlerUtil
private ResourceHandlerUtil()
-
-
Method Details
-
isEmpty
Determines if either the given resource or amount is classified as empty: if eitherResource.isEmpty()istrue, 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:
trueif eitherResource.isEmpty()returnstrue, or the amount is<= 0.
-
isEmpty
Checks if aResourceHandleris empty.A
ResourceHandleris 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- theResourceHandlerto check for emptiness- Returns:
trueif theResourceHandleris empty,falseotherwise
-
isFull
Checks if aResourceHandleris full.A
IResourceHandleris 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- theResourceHandlerto check- Returns:
trueif theResourceHandleris full,falseotherwise
-
isValid
Returns whether the given resourceis validfor any index of the given resource handler.- Parameters:
handler- theResourceHandlerto checkresource- the resource to check. Must be non-empty.- Returns:
trueif 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- theResourceHandlerto insert the resource into. can benull, 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 innullwill 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 aResourceHandlerthat is not empty and matches the given filter.- Type Parameters:
T- The type of resource handled by the handler- Parameters:
handler- TheResourceHandlerto extract the resource from. Can benull, which makes this method a no-op.filter- The first non-empty resource for which this filter returnstruewill be extracted.amount- The desired amount of the resource to extracttransaction- The transaction context for the operation. Passing innullwill 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
nullif 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 returnstruewill 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 innullwill 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
nullif 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) Similar tomove(net.neoforged.neoforge.transfer.ResourceHandler<T>, net.neoforged.neoforge.transfer.ResourceHandler<T>, java.util.function.Predicate<T>, int, net.neoforged.neoforge.transfer.transaction.TransactionContext), but transfers only the first resource that matches the filter and can be successfully transferred.- 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 returnstruewill 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 innullwill 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
nullif nothing was transferred
-
contains
Returnstrueif the resource handler contains the given resource,falseotherwise.- Parameters:
handler- The handler to check for the resource.resource- The resource to check for. Must be non-empty.- Returns:
trueif the resource handler contains the given resource,falseotherwise
-
indexOf
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 returnstruewill be considered.transaction- The transaction that this operation is part of. This method will always use a nested transaction that will be rolled back.nullcan 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
nullotherwise.
-