Class SingleVariantItemStorage<T extends TransferVariant<?>>
- Type Parameters:
T- The type of the stored transfer 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.
- 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
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprotected booleancanExtract(T resource) Returntrueif the passed non-blank variant can be extracted,falseotherwise.protected booleanReturntrueif the passed non-blank variant can be inserted,falseotherwise.longextract(T extractedResource, long maxAmount, TransactionContext transaction) Try to extract up to some amount of a resource from this storage.longprotected abstract longgetAmount(ItemVariant currentVariant) Return the current amount by reading the NBT of the passed variant.protected abstract TReturn the blank resource.longprotected abstract longgetCapacity(T variant) Return the capacity of this storage for the passed resource.protected abstract TgetResource(ItemVariant currentVariant) Return the current resource by reading the NBT of the passed variant.protected abstract ItemVariantgetUpdatedVariant(ItemVariant currentVariant, T newResource, long newAmount) Return an updated variant with new resource and amount.longinsert(T insertedResource, long maxAmount, TransactionContext transaction) Try to insert up to some amount of a resource into this storage.booleanReturntrueif theStorageView.getResource()contained in this storage view is blank, orfalseotherwise.booleanReturn 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.booleanReturn 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, waitMethods inherited from interface java.lang.Iterable
forEach, spliteratorMethods inherited from interface net.fabricmc.fabric.api.transfer.v1.storage.base.SingleSlotStorage
getSlot, getSlotCount, iteratorMethods inherited from interface net.fabricmc.fabric.api.transfer.v1.storage.SlottedStorage
getSlotsMethods inherited from interface net.fabricmc.fabric.api.transfer.v1.storage.Storage
exactView, getVersion, nonEmptyIterator, nonEmptyViews, simulateExtract, simulateInsertMethods 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 passedcurrentVariantto 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
Returntrueif the passed non-blank variant can be inserted,falseotherwise. -
canExtract
Returntrueif the passed non-blank variant can be extracted,falseotherwise. -
supportsInsertion
public boolean supportsInsertion()Description copied from interface:StorageReturn 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:
supportsInsertionin interfaceStorage<T extends TransferVariant<?>>
-
insert
Description copied from interface:StorageTry to insert up to some amount of a resource into this storage.- Specified by:
insertin 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:StorageReturn 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:
supportsExtractionin interfaceStorage<T extends TransferVariant<?>>
-
extract
Description copied from interface:StorageTry to extract up to some amount of a resource from this storage.- Specified by:
extractin interfaceStorage<T extends TransferVariant<?>>- Specified by:
extractin 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:StorageViewReturntrueif theStorageView.getResource()contained in this storage view is blank, orfalseotherwise.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:
isResourceBlankin interfaceStorageView<T extends TransferVariant<?>>
-
getResource
- Specified by:
getResourcein 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:
getAmountin interfaceStorageView<T extends TransferVariant<?>>- Returns:
- The amount of
StorageView.getResource()stored in this view.
-
getCapacity
public long getCapacity()- Specified by:
getCapacityin 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
-