Class SingleVariantItemStorage<T extends TransferVariant<?>>
- Type Parameters:
T
- The type of the stored transfer variant.
- All Implemented Interfaces:
Iterable<StorageView<T>>
,SingleSlotStorage<T>
,SlottedStorage<T>
,Storage<T>
,StorageView<T>
To expose the storage API for an item, you need to register a provider for your item, and pass it an instance of this class:
- You must override
getBlankResource()
, for examplereturn FluidVariant.blank();
for fluids. - You must override
getResource(ItemVariant)
andgetAmount(ItemVariant)
. Generally you will read the resource and the amount from the NBT of the item variant. - You must override
getCapacity(TransferVariant)
to set the capacity of your storage. - You must override
getUpdatedVariant(net.fabricmc.fabric.api.transfer.v1.item.ItemVariant, T, long)
. It is used to change the resource and the amount of the item variant. Generally you will copy the NBT, modify it, and then create a new variant from that. Copying the NBT instead of recreating it from scratch is important to keep custom names or enchantments. - You may also override
canInsert(T)
andcanExtract(T)
if you want to restrict insertion and/or extraction.
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionprotected boolean
canExtract
(T resource) Returntrue
if the passed non-blank variant can be extracted,false
otherwise.protected boolean
Returntrue
if the passed non-blank variant can be inserted,false
otherwise.long
extract
(T extractedResource, long maxAmount, TransactionContext transaction) Try to extract up to some amount of a resource from this storage.long
protected abstract long
getAmount
(ItemVariant currentVariant) Return the current amount by reading the NBT of the passed variant.protected abstract T
Return the blank resource.long
protected abstract long
getCapacity
(T variant) Return the capacity of this storage for the passed resource.protected abstract T
getResource
(ItemVariant currentVariant) Return the current resource by reading the NBT of the passed variant.protected abstract ItemVariant
getUpdatedVariant
(ItemVariant currentVariant, T newResource, long newAmount) Return an updated variant with new resource and amount.long
insert
(T insertedResource, 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.boolean
Return false if callingStorage.extract(T, long, net.fabricmc.fabric.api.transfer.v1.transaction.TransactionContext)
will absolutely always return 0, or true otherwise or in doubt.boolean
Return false if callingStorage.insert(T, long, net.fabricmc.fabric.api.transfer.v1.transaction.TransactionContext)
will absolutely always return 0, or true otherwise or in doubt.toString()
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
getVersion, nonEmptyIterator, nonEmptyViews
Methods inherited from interface net.fabricmc.fabric.api.transfer.v1.storage.StorageView
getUnderlyingView
-
Constructor Details
-
SingleVariantItemStorage
-
-
Method Details
-
getBlankResource
Return the blank resource. -
getResource
Return the current resource by reading the NBT of the passed variant. -
getAmount
Return the current amount by reading the NBT of the passed variant. -
getCapacity
Return the capacity of this storage for the passed resource. An estimate should be returned if the passed resource is blank. -
getUpdatedVariant
protected abstract ItemVariant getUpdatedVariant(ItemVariant currentVariant, T newResource, long newAmount) Return an updated variant with new resource and amount. Implementors should generally convert the passedcurrentVariant
to a stack, then edit the NBT of the stack so it contains the correct resource and amount.When the new amount is 0, it is recommended that the sub-NBTs corresponding to the resource and amount be removed, for example using
ItemStack.removeSubNbt(java.lang.String)
, so that newly-crafted containers can stack with emptied containers.- Parameters:
currentVariant
- Variant to which the modification should be applied.newResource
- Resource that should be contained in the returned variant.newAmount
- Amount that should be contained in the returned variant.- Returns:
- A modified variant containing the new resource and amount.
-
canInsert
Returntrue
if the passed non-blank variant can be inserted,false
otherwise. -
canExtract
Returntrue
if the passed non-blank variant can be extracted,false
otherwise. -
supportsInsertion
public boolean supportsInsertion()Description copied from interface:Storage
Return false if callingStorage.insert(T, long, net.fabricmc.fabric.api.transfer.v1.transaction.TransactionContext)
will absolutely always return 0, or true otherwise or in doubt.Note: This function is meant to be used by pipes or other devices that can transfer resources to know if they should interact with this storage at all.
- Specified by:
supportsInsertion
in interfaceStorage<T extends TransferVariant<?>>
-
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:
insertedResource
- 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.
-
supportsExtraction
public boolean supportsExtraction()Description copied from interface:Storage
Return false if callingStorage.extract(T, long, net.fabricmc.fabric.api.transfer.v1.transaction.TransactionContext)
will absolutely always return 0, or true otherwise or in doubt.Note: This function is meant to be used by pipes or other devices that can transfer resources to know if they should interact with this storage at all.
- Specified by:
supportsExtraction
in interfaceStorage<T extends TransferVariant<?>>
-
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:
extractedResource
- 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 estimated upper bound on the number of resources that could be stored if this view has a blank resource.
-
toString
-