Class SingleVariantStorage<T extends TransferVariant<?>>
- All Implemented Interfaces:
Iterable<StorageView<T>>
,SingleSlotStorage<T>
,SlottedStorage<T>
,Storage<T>
,StorageView<T>
,TransactionContext.CloseCallback
,TransactionContext.OuterCloseCallback
- Direct Known Subclasses:
SingleFluidStorage
,SingleItemStorage
getCapacity(TransferVariant)
,
and probably SnapshotParticipant.onFinalCommit()
as well for markDirty()
and similar calls.
canInsert(T)
and canExtract(T)
can be used for more precise control over which variants 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()
.
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.
- See Also:
-
Field Summary
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionprotected boolean
canExtract
(T variant) protected boolean
protected ResourceAmount<T>
Return a new nonnull object containing the current state of this participant.long
extract
(T extractedVariant, long maxAmount, TransactionContext transaction) Try to extract up to some amount of a resource from this storage.long
protected abstract T
Return the blank variant.long
protected abstract long
getCapacity
(T variant) Return the maximum capacity of this storage for the passed transfer variant.long
insert
(T 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
(ResourceAmount<T> snapshot) Roll back to a state previously created bySnapshotParticipant.createSnapshot()
.toString()
void
writeNbt
(NbtCompound nbt) Simple implementation of writing to NBT.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, 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
getSlot, getSlotCount, iterator
Methods inherited from interface net.fabricmc.fabric.api.transfer.v1.storage.SlottedStorage
getSlots
Methods inherited from interface net.fabricmc.fabric.api.transfer.v1.storage.Storage
exactView, getVersion, nonEmptyIterator, nonEmptyViews, simulateExtract, simulateInsert, supportsExtraction, supportsInsertion
Methods inherited from interface net.fabricmc.fabric.api.transfer.v1.storage.StorageView
getUnderlyingView
-
Field Details
-
variant
-
amount
public long amount
-
-
Constructor Details
-
SingleVariantStorage
public SingleVariantStorage()
-
-
Method Details
-
getBlankVariant
Return the blank variant.Note: this is called very early in the constructor. If fields need to be accessed from this function, make sure to re-initialize
variant
yourself. -
getCapacity
Return the maximum capacity of this storage for the passed transfer variant. If the passed variant is blank, an estimate should be returned. -
canInsert
- Returns:
true
if the passed non-blank variant can be inserted,false
otherwise.
-
canExtract
- Returns:
true
if the passed non-blank variant can be extracted,false
otherwise.
-
writeNbt
Simple implementation of writing to NBT. Other formats are allowed, this is just a convenient suggestion. -
insert
Description copied from interface:Storage
Try to insert up to some amount of a resource into this storage.- Specified by:
insert
in interfaceStorage<T extends TransferVariant<?>>
- 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<T extends TransferVariant<?>>
- Specified by:
extract
in interfaceStorageView<T extends TransferVariant<?>>
- Parameters:
extractedVariant
- 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.
-
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<T extends TransferVariant<?>>
-
getResource
- Specified by:
getResource
in interfaceStorageView<T extends TransferVariant<?>>
- Returns:
- The resource stored in this view. May not be blank if
StorageView.isResourceBlank()
isfalse
.
-
getAmount
public long getAmount()- Specified by:
getAmount
in interfaceStorageView<T extends TransferVariant<?>>
- Returns:
- The amount of
StorageView.getResource()
stored in this view.
-
getCapacity
public long getCapacity()- Specified by:
getCapacity
in interfaceStorageView<T extends TransferVariant<?>>
- 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.
-
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<ResourceAmount<T extends TransferVariant<?>>>
-
readSnapshot
Description copied from class:SnapshotParticipant
Roll back to a state previously created bySnapshotParticipant.createSnapshot()
.- Specified by:
readSnapshot
in classSnapshotParticipant<ResourceAmount<T extends TransferVariant<?>>>
-
toString
-