Class StacksResourceHandler<S,T extends Resource>
- Type Parameters:
S- The type of stack used to store the contents of this handler.T- The type of resource this handler manages.
- All Implemented Interfaces:
ValueIOSerializable,ResourceHandler<T>
- Direct Known Subclasses:
FluidStacksResourceHandler,ItemStacksResourceHandler,ResourceStacksResourceHandler
ResourceHandler backed by a list of stacks.
This implementation is generic in the type of transferred resources T,
and in the type of stack S used to store the contents of the handler.
As a result of this flexibility, this base implementation comes with the following methods will typically be overridden:
- (required)
getResourceFrom(S),getAmountFrom(S), andgetStackFrom(T, int)to convert between amounts, resources and stacks. - (required)
copyOf(S)to copy stacks for snapshotting support. - (recommended)
matches(S, T)to optimize the frequent operation of checking whether a resource and a stack match. - (optional)
isValid(int, T)to limit which resources are allowed in this handler; by default any resource is allowed. - (required)
getCapacity(int, T)to specify the capacity of this handler. - (recommended)
onContentsChanged(int, S)to react to changes in this handler, for example to triggersetChanged().
- See Also:
-
Nested Class Summary
Nested Classes -
Field Summary
FieldsModifier and TypeFieldDescriptionprotected final com.mojang.serialization.Codec<NonNullList<S>> protected final Sprivate final ArrayList<StacksResourceHandler<S, T>.StackJournal> protected NonNullList<S> static final String -
Constructor Summary
ConstructorsModifierConstructorDescriptionprotectedStacksResourceHandler(int size, S emptyStack, com.mojang.serialization.Codec<S> stackCodec) protectedStacksResourceHandler(NonNullList<S> stacks, S emptyStack, com.mojang.serialization.Codec<S> stackCodec) -
Method Summary
Modifier and TypeMethodDescriptionprotected abstract SCreates a copy of a stack, for use as a snapshot.Copies all the contents of this handler to a mutable fixed-sizeNonNullList.voiddeserialize(ValueInput input) intextract(int index, T resource, int amount, TransactionContext transaction) Extracts up to the given amount of a resource from the handler at the given index.longgetAmountAsLong(int index) Returns the amount of the currently stored resource at the given index, as along.protected abstract intgetAmountFrom(S stack) Retrieves the amount from a stack.protected abstract intgetCapacity(int index, T resource) Return the maximum capacity of this handler for the passed resource.longgetCapacityAsLong(int index, T resource) 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 along.getResource(int index) Returns the resource at the given index, which may be empty.protected abstract TgetResourceFrom(S stack) Retrieves the resource from a stack.protected abstract SgetStackFrom(T resource, int amount) Creates a stack from a resource and an amount.intinsert(int index, T resource, int amount, TransactionContext transaction) Inserts up to the given amount of a resource into the handler at the given index.booleanReturntrueif the passed non-empty resource can fit in this handler,falseotherwise.protected booleanChecks if the passed resource corresponds to the stack.private NonNullList<S> mutableCopyOf(Collection<S> list) Creates aNonNullListthat is a fixed-size mutable copy of the given collection.protected voidonContentsChanged(int index, S previousContents) Called after the contents of the handler changed.voidserialize(ValueOutput output) voidDirectly overwrites the contents of the handler.protected voidsetStacks(NonNullList<S> stacks) Changes the list of stacks.intsize()Returns the current number of indices in this resource handler.private voidMethods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface net.neoforged.neoforge.transfer.ResourceHandler
extract, getAmountAsInt, getCapacityAsInt, insert
-
Field Details
-
VALUE_IO_KEY
- See Also:
-
emptyStack
-
stacks
-
codec
-
snapshotJournals
-
-
Constructor Details
-
StacksResourceHandler
-
StacksResourceHandler
protected StacksResourceHandler(NonNullList<S> stacks, S emptyStack, com.mojang.serialization.Codec<S> stackCodec)
-
-
Method Details
-
mutableCopyOf
Creates aNonNullListthat is a fixed-size mutable copy of the given collection. -
setStacks
Changes the list of stacks. Can change the size of the handler.- Parameters:
stacks- The new list of stacks. A shallow copy will be made.
-
updateStacksSize
private void updateStacksSize() -
serialize
- Specified by:
serializein interfaceValueIOSerializable
-
deserialize
- Specified by:
deserializein interfaceValueIOSerializable
-
set
Directly overwrites the contents of the handler.Note that this method can be used as an
IndexModifier, for usage inResourceHandlerSlot.- Parameters:
index- index to changeresource- new resource at the indexamount- new amount at the index- Throws:
IllegalArgumentException- if either the amount is negative; or if the resource is non-empty for a 0 amount
-
getResourceFrom
Retrieves the resource from a stack. In the case of anItemStackanItemResourcewould be returned for example. -
getAmountFrom
-
getStackFrom
Creates a stack from a resource and an amount.If the stack would be empty, consider returning
emptyStackinstead of creating a new empty stack instance. -
copyOf
Creates a copy of a stack, for use as a snapshot.If using an immutable stack type such as
ResourceStack, it can be returned as is. In the case of a mutable stack type such as an item or fluid stack, a copy should be returned. -
matches
Checks if the passed resource corresponds to the stack.- Parameters:
stack- the stack, usually the current stored valueresource- the resource, usually the received value in insert or extract- Returns:
trueif the stack and resource match;falseotherwise.
-
isValid
Returntrueif the passed non-empty resource can fit in this handler,falseotherwise.The result of this function is used in the provided implementations of:
getCapacityAsLong(int, T), to report a capacity of0for invalid items;insert(int, T, int, TransactionContext), to reject items that cannot fit in this handler.
- Specified by:
isValidin interfaceResourceHandler<S>- 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
-
getCapacity
Return the maximum capacity of this handler for the passed resource. If the passed resource is empty, an estimate should be returned.- Returns:
- The maximum capacity of this handler for the passed resource.
-
onContentsChanged
Called after the contents of the handler changed.For changes that happen through
set(int, T, int), this method is called immediately. For changes that happen throughinsert(int, T, int, net.neoforged.neoforge.transfer.transaction.TransactionContext)orextract(int, T, int, net.neoforged.neoforge.transfer.transaction.TransactionContext), this function will be called at the end of the transaction, once per index that changed.- Parameters:
index- the index where the change happenedpreviousContents- the stack before the change
-
copyToList
Copies all the contents of this handler to a mutable fixed-sizeNonNullList. -
size
public int size()Description copied from interface:ResourceHandlerReturns 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:
sizein interfaceResourceHandler<S>- Returns:
- The size of the resource handler. Can be
0if the handler currently has no indices.
-
getResource
Description copied from interface:ResourceHandlerReturns the resource at the given index, which may be empty.If the resource is empty, the stored amount must be 0.
- Specified by:
getResourcein interfaceResourceHandler<S>- 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:ResourceHandlerReturns the amount of the currently stored resource at the given index, as along.In general, resource handlers can report
longamounts. However, if the handler is known to only support amounts up toInteger.MAX_VALUE, or if the caller prefers to deal inints 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:
getAmountAsLongin interfaceResourceHandler<S>- Parameters:
index- The index to get the amount from.- Returns:
- the amount at the given index, as a long
- See Also:
-
getCapacityAsLong
Description copied from interface:ResourceHandlerReturns 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 along.In general, resource handlers can report
longcapacities. However, if the handler is known to only support capacities up toInteger.MAX_VALUE, or if the caller prefers to deal inints 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 toinsertit.- Specified by:
getCapacityAsLongin interfaceResourceHandler<S>- 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:
-
insert
Description copied from interface:ResourceHandlerInserts 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:
insertin interfaceResourceHandler<S>- 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) andamount(inclusive, everything was inserted). - See Also:
-
extract
Description copied from interface:ResourceHandlerExtracts 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:
extractin interfaceResourceHandler<S>- 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) andamount(inclusive, everything was extracted). - See Also:
-