Class SingleVariantItemStorage<T extends TransferVariant<?>>

java.lang.Object
net.fabricmc.fabric.api.transfer.v1.storage.base.SingleVariantItemStorage<T>
Type Parameters:
T - The type of the stored transfer variant.
All Implemented Interfaces:
Iterable<StorageView<T>>, SingleSlotStorage<T>, SlottedStorage<T>, Storage<T>, StorageView<T>

public abstract class SingleVariantItemStorage<T extends TransferVariant<?>> extends Object implements SingleSlotStorage<T>
Base implementation of a fixed-capacity "continuous" storage for item-provided storage APIs. The item may not change, so the data has to be stored in the components of the stacks. This can be used for example to implement portable fluid tanks, fluid-containing jetpacks, and so on... Continuous here means that they can store any integer amount between 0 and the capacity, unlike buckets or bottles.

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:

  • Constructor Details

  • Method Details

    • getBlankResource

      protected abstract T getBlankResource()
      Return the blank resource.
    • getResource

      protected abstract T getResource(ItemVariant currentVariant)
      Return the current resource by reading the components of the passed variant.
    • getAmount

      protected abstract long getAmount(ItemVariant currentVariant)
      Return the current amount by reading the components of the passed variant.
    • getCapacity

      protected abstract long getCapacity(T variant)
      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 passed currentVariant to a stack, then edit the components of the stack so it contains the correct resource and amount.

      When the new amount is 0, it is recommended that the components corresponding to the resource and amount be removed, so that newly-crafted containers can stack with emptied containers. If a custom item is used, this means setting it to the default as specified in Item.Settings.component(net.minecraft.component.DataComponentType<T>, T); if a vanilla item is used, this means removing the said component.

      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

      protected boolean canInsert(T resource)
      Return true if the passed non-blank variant can be inserted, false otherwise.
    • canExtract

      protected boolean canExtract(T resource)
      Return true if the passed non-blank variant can be extracted, false otherwise.
    • supportsInsertion

      public boolean supportsInsertion()
      Description copied from interface: Storage
      Return false if calling Storage.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 interface Storage<T extends TransferVariant<?>>
    • insert

      public long insert(T insertedResource, long maxAmount, TransactionContext transaction)
      Description copied from interface: Storage
      Try to insert up to some amount of a resource into this storage.
      Specified by:
      insert in interface Storage<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 calling Storage.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 interface Storage<T extends TransferVariant<?>>
    • extract

      public long extract(T extractedResource, long maxAmount, TransactionContext transaction)
      Description copied from interface: Storage
      Try to extract up to some amount of a resource from this storage.
      Specified by:
      extract in interface Storage<T extends TransferVariant<?>>
      Specified by:
      extract in interface StorageView<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
      Return true if the StorageView.getResource() contained in this storage view is blank, or false 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 interface StorageView<T extends TransferVariant<?>>
    • getResource

      public T getResource()
      Specified by:
      getResource in interface StorageView<T extends TransferVariant<?>>
      Returns:
      The resource stored in this view. May not be blank if StorageView.isResourceBlank() is false.
    • getAmount

      public long getAmount()
      Specified by:
      getAmount in interface StorageView<T extends TransferVariant<?>>
      Returns:
      The amount of StorageView.getResource() stored in this view.
    • getCapacity

      public long getCapacity()
      Specified by:
      getCapacity in interface StorageView<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

      public String toString()
      Overrides:
      toString in class Object