Class SingleVariantStorage<T extends TransferVariant<?>>

java.lang.Object
net.fabricmc.fabric.api.transfer.v1.transaction.base.SnapshotParticipant<ResourceAmount<T>>
net.fabricmc.fabric.api.transfer.v1.storage.base.SingleVariantStorage<T>
All Implemented Interfaces:
Iterable<StorageView<T>>, SingleSlotStorage<T>, SlottedStorage<T>, Storage<T>, StorageView<T>, TransactionContext.CloseCallback, TransactionContext.OuterCloseCallback
Direct Known Subclasses:
SingleFluidStorage, SingleItemStorage

public abstract class SingleVariantStorage<T extends TransferVariant<?>> extends SnapshotParticipant<ResourceAmount<T>> implements SingleSlotStorage<T>
A storage that can store a single transfer variant at any given time. Implementors should at least override 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().

See Also:
  • Field Details

    • variant

      public T extends TransferVariant<?> variant
    • amount

      public long amount
  • Constructor Details

    • SingleVariantStorage

      public SingleVariantStorage()
  • Method Details

    • getBlankVariant

      protected abstract T 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

      protected abstract long getCapacity(T variant)
      Return the maximum capacity of this storage for the passed transfer variant. If the passed variant is blank, an estimate should be returned.
    • canInsert

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

      protected boolean canExtract(T variant)
      Returns:
      true if the passed non-blank variant can be extracted, false otherwise.
    • writeNbt

      public void writeNbt(NbtCompound nbt)
      Simple implementation of writing to NBT. Other formats are allowed, this is just a convenient suggestion.
    • insert

      public long insert(T insertedVariant, 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:
      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

      public long extract(T extractedVariant, 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:
      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
      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.
    • createSnapshot

      protected ResourceAmount<T> 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 class SnapshotParticipant<ResourceAmount<T extends TransferVariant<?>>>
    • readSnapshot

      protected void readSnapshot(ResourceAmount<T> snapshot)
      Description copied from class: SnapshotParticipant
      Roll back to a state previously created by SnapshotParticipant.createSnapshot().
      Specified by:
      readSnapshot in class SnapshotParticipant<ResourceAmount<T extends TransferVariant<?>>>
    • toString

      public String toString()
      Overrides:
      toString in class Object