Class SingleStackStorage
- All Implemented Interfaces:
Iterable<StorageView<ItemVariant>>
,SingleSlotStorage<ItemVariant>
,Storage<ItemVariant>
,StorageView<ItemVariant>
,TransactionContext.CloseCallback
,TransactionContext.OuterCloseCallback
ItemStack
.
Implementors should at least override getStack()
and setStack(net.minecraft.item.ItemStack)
,
and probably SnapshotParticipant.onFinalCommit()
as well for markDirty()
and similar calls.
canInsert(net.fabricmc.fabric.api.transfer.v1.item.ItemVariant)
and canExtract(net.fabricmc.fabric.api.transfer.v1.item.ItemVariant)
can be used for more precise control over which items may be inserted or extracted.
If one of these two functions is overridden to always return false, implementors may also wish to override
Storage.supportsInsertion()
and/or Storage.supportsExtraction()
.
getCapacity(ItemVariant)
can be overridden to change the maximum capacity depending on the item variant.
Experimental feature, we reserve the right to remove or change it without further notice. The transfer API is a complex addition, and we want to be able to correct possible design mistakes.
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionprotected boolean
canExtract
(ItemVariant itemVariant) Returntrue
if the passed non-blank item variant can be extracted,false
otherwise.protected boolean
canInsert
(ItemVariant itemVariant) Returntrue
if the passed non-blank item variant can be inserted,false
otherwise.protected ItemStack
Return a new nonnull object containing the current state of this participant.long
extract
(ItemVariant variant, long maxAmount, TransactionContext transaction) Try to extract up to some amount of a resource from this storage.long
long
protected int
getCapacity
(ItemVariant itemVariant) Return the maximum capacity of this storage for the passed item variant.protected abstract ItemStack
getStack()
Return the stack of this storage.long
insert
(ItemVariant insertedVariant, long maxAmount, TransactionContext transaction) Try to insert up to some amount of a resource into this storage.boolean
Returntrue
if theStorageView.getResource()
contained in this storage view is blank, orfalse
otherwise.protected void
readSnapshot
(ItemStack snapshot) Roll back to a state previously created bySnapshotParticipant.createSnapshot()
.protected abstract void
Set the stack of this storage.Methods inherited from class net.fabricmc.fabric.api.transfer.v1.transaction.base.SnapshotParticipant
afterOuterClose, onClose, onFinalCommit, releaseSnapshot, updateSnapshots
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
Methods inherited from interface java.lang.Iterable
forEach, spliterator
Methods inherited from interface net.fabricmc.fabric.api.transfer.v1.storage.base.SingleSlotStorage
iterator
Methods inherited from interface net.fabricmc.fabric.api.transfer.v1.storage.Storage
exactView, exactView, getVersion, simulateExtract, simulateInsert, supportsExtraction, supportsInsertion
Methods inherited from interface net.fabricmc.fabric.api.transfer.v1.storage.StorageView
getUnderlyingView
-
Constructor Details
-
SingleStackStorage
public SingleStackStorage()
-
-
Method Details
-
getStack
Return the stack of this storage. 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.item.ItemStack)
. This means that either returning the backing stack directly or a copy is safe.- Returns:
- The current stack.
-
setStack
Set the stack of this storage. -
canInsert
Returntrue
if the passed non-blank item variant can be inserted,false
otherwise. -
canExtract
Returntrue
if the passed non-blank item variant can be extracted,false
otherwise. -
getCapacity
Return the maximum capacity of this storage for the passed item variant. If the passed item variant is blank, an estimate should be returned.If the capacity should be limited by the max count of the item, this function must take it into account. For example, a storage with a maximum count of 4, or less for items that have a smaller max count, should override this to return
Math.min(itemVariant.getItem().getMaxCount(), 4);
.- Returns:
- The maximum capacity of this storage for the passed item variant.
-
isResourceBlank
public boolean isResourceBlank()Description copied from interface:StorageView
Returntrue
if theStorageView.getResource()
contained in this storage view is blank, orfalse
otherwise.This function is mostly useful when dealing with storages of arbitrary types. For transfer variant storages, this should always be equivalent to
getResource().isBlank()
.- Specified by:
isResourceBlank
in interfaceStorageView<ItemVariant>
-
getResource
- Specified by:
getResource
in interfaceStorageView<ItemVariant>
- Returns:
- The resource stored in this view. May not be blank if
StorageView.isResourceBlank()
isfalse
.
-
getAmount
public long getAmount()- Specified by:
getAmount
in interfaceStorageView<ItemVariant>
- Returns:
- The amount of
StorageView.getResource()
stored in this view.
-
getCapacity
public long getCapacity()- Specified by:
getCapacity
in interfaceStorageView<ItemVariant>
- Returns:
- The total amount of
StorageView.getResource()
that could be stored in this view, or an estimate of the number of resources that could be stored if this view has a blank resource.
-
insert
Description copied from interface:Storage
Try to insert up to some amount of a resource into this storage.- Specified by:
insert
in interfaceStorage<ItemVariant>
- Parameters:
insertedVariant
- The resource to insert. May not be blank.maxAmount
- The maximum amount of resource to insert. May not be negative.transaction
- The transaction this operation is part of.- Returns:
- A non-negative integer not greater than maxAmount: the amount that was inserted.
-
extract
Description copied from interface:Storage
Try to extract up to some amount of a resource from this storage.- Specified by:
extract
in interfaceStorage<ItemVariant>
- Specified by:
extract
in interfaceStorageView<ItemVariant>
- Parameters:
variant
- The resource to extract. May not be blank.maxAmount
- The maximum amount of resource to extract. May not be negative.transaction
- The transaction this operation is part of.- Returns:
- A non-negative integer not greater than maxAmount: the amount that was extracted.
-
createSnapshot
Description copied from class:SnapshotParticipant
Return a new nonnull object containing the current state of this participant.null
may not be returned, or an exception will be thrown!- Specified by:
createSnapshot
in classSnapshotParticipant<ItemStack>
-
readSnapshot
Description copied from class:SnapshotParticipant
Roll back to a state previously created bySnapshotParticipant.createSnapshot()
.- Specified by:
readSnapshot
in classSnapshotParticipant<ItemStack>
-