Class ItemStackResourceHandler
- All Implemented Interfaces:
ValueIOSerializable,ResourceHandler<ItemResource>
- Direct Known Subclasses:
CarriedSlotWrapper,LivingEntityEquipmentWrapper.SlotWrapper,VanillaContainerWrapper.SlotWrapper
ItemStack.
Implementors should at least override getStack() and setStack(net.minecraft.world.item.ItemStack),
and probably SnapshotJournal.onRootCommit(T) as well for setChanged() and similar calls.
isValid(net.neoforged.neoforge.transfer.item.ItemResource) can be used for more precise control over which items may be stored.
getCapacity(ItemResource) can be overridden to change the maximum capacity depending on the item resource.
-
Field Summary
Fields -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprotected ItemStackReturn a new nonnull object containing the current state of this journal.voiddeserialize(ValueInput input) intextract(int index, ItemResource 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 intgetCapacity(ItemResource resource) Return the maximum capacity of this handler for the passed item resource.longgetCapacityAsLong(int index, ItemResource 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 ItemStackgetStack()Return the stack of this handler.intinsert(int index, ItemResource resource, int amount, TransactionContext transaction) Inserts up to the given amount of a resource into the handler at the given index.booleanisValid(int index, ItemResource resource) 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.protected booleanisValid(ItemResource resource) Returntrueif the passed non-empty item resource can fit in this handler,falseotherwise.protected voidrevertToSnapshot(ItemStack snapshot) Roll back to a state previously created bySnapshotJournal.createSnapshot().voidserialize(ValueOutput output) protected abstract voidSet the stack of this handler.final intsize()Returns the current number of indices in this resource handler.toString()Methods inherited from class net.neoforged.neoforge.transfer.transaction.SnapshotJournal
onRootCommit, releaseSnapshot, updateSnapshotsMethods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, waitMethods inherited from interface net.neoforged.neoforge.transfer.ResourceHandler
extract, getAmountAsInt, getCapacityAsInt, insert
-
Field Details
-
VALUE_IO_KEY
- See Also:
-
-
Constructor Details
-
ItemStackResourceHandler
public ItemStackResourceHandler()
-
-
Method Details
-
getStack
Return the stack of this handler. It will be modified directly sometimes to avoid needless copies. However, any mutation of the stack will directly be followed by a call tosetStack(net.minecraft.world.item.ItemStack). This means that either returning the backing stack directly or a copy is safe. If returning a copy, consider overridinggetAmountAsLong(int)andgetResource(int)to avoid unnecessary copies.- Returns:
- The current stack.
-
setStack
Set the stack of this handler. -
isValid
Returntrueif the passed non-empty item resource can fit in this handler,falseotherwise.The result of this function is used in the provided implementations of:
isValid(int, ItemResource), to report which items are valid;getCapacityAsLong(int, ItemResource), to report a capacity of0for invalid items;insert(int, ItemResource, int, TransactionContext), to reject items that cannot fit in this handler.
-
getCapacity
Return the maximum capacity of this handler for the passed item resource. If the passed item resource is empty, an estimate should be returned.If the capacity should be limited by the max stack size of the item, this function must take it into account. Additionally, the empty resource should be special-cased to return the intended maximum capacity of the handler, as it will otherwise report a max stack size of 1. For example, a handler with a maximum count of 4, or less for items that have a smaller max stack size, should override this to return
resource.isEmpty() ? 4 : Math.min(resource.getMaxStackSize(), 4).- Returns:
- The maximum capacity of this handler for the passed item resource.
-
size
public final 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<ItemResource>- Returns:
- The size of the resource handler. Can be
0if the handler currently has no indices.
-
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<ItemResource>- 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<ItemResource>- 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:
-
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<ItemResource>- 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<ItemResource>- 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<ItemResource>- 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
Description copied from interface:ResourceHandlerReturns 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
insertit.- Specified by:
isValidin interfaceResourceHandler<ItemResource>- 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
-
createSnapshot
Description copied from class:SnapshotJournalReturn a new nonnull object containing the current state of this journal.nullmay not be returned, or an exception will be thrown!- Specified by:
createSnapshotin classSnapshotJournal<ItemStack>
-
revertToSnapshot
Description copied from class:SnapshotJournalRoll back to a state previously created bySnapshotJournal.createSnapshot().- Specified by:
revertToSnapshotin classSnapshotJournal<ItemStack>
-
serialize
- Specified by:
serializein interfaceValueIOSerializable
-
deserialize
- Specified by:
deserializein interfaceValueIOSerializable
-
toString
-