Class SingleStackStorage

java.lang.Object
net.fabricmc.fabric.api.transfer.v1.transaction.base.SnapshotParticipant<net.minecraft.item.ItemStack>
net.fabricmc.fabric.api.transfer.v1.item.base.SingleStackStorage
All Implemented Interfaces:
SingleSlotStorage<ItemVariant>, Storage<ItemVariant>, StorageView<ItemVariant>, TransactionContext.CloseCallback, TransactionContext.OuterCloseCallback

@Experimental public abstract class SingleStackStorage extends SnapshotParticipant<net.minecraft.item.ItemStack> implements SingleSlotStorage<ItemVariant>
An item variant storage backed by an ItemStack. Implementors should at least override getStack() and setStack(net.minecraft.item.ItemStack), and probably SnapshotParticipant.onFinalCommit() as well for markDirty() and similar calls.

canInsert(net.fabricmc.fabric.api.transfer.v1.item.ItemVariant) and canExtract(net.fabricmc.fabric.api.transfer.v1.item.ItemVariant) can be used for more precise control over which items 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(). getCapacity(ItemVariant) can be overridden to change the maximum capacity depending on the item 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.

  • Constructor Details

    • SingleStackStorage

      public SingleStackStorage()
  • Method Details

    • getStack

      protected abstract net.minecraft.item.ItemStack getStack()
      Return the stack of this storage. It will be modified directly sometimes to avoid needless copies. However, any mutation of the stack will directly be followed by a call to setStack(net.minecraft.item.ItemStack). This means that either returning the backing stack directly or a copy is safe.
      Returns:
      The current stack.
    • setStack

      protected abstract void setStack(net.minecraft.item.ItemStack stack)
      Set the stack of this storage.
    • canInsert

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

      protected boolean canExtract(ItemVariant itemVariant)
      Return true if the passed non-blank item variant can be extracted, false otherwise.
    • getCapacity

      protected int getCapacity(ItemVariant itemVariant)
      Return the maximum capacity of this storage for the passed item variant. If the passed item variant is blank, an estimate should be returned.

      If the capacity should be limited by the max count of the item, this function must take it into account. For example, a storage with a maximum count of 4, or less for items that have a smaller max count, should override this to return Math.min(itemVariant.getItem().getMaxCount(), 4);.

      Returns:
      The maximum capacity of this storage for the passed item variant.
    • isResourceBlank

      public final 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<ItemVariant>
    • getResource

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

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

      public final long getCapacity()
      Specified by:
      getCapacity in interface StorageView<ItemVariant>
      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.
    • insert

      public final long insert(ItemVariant 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<ItemVariant>
      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 nonnegative integer not greater than maxAmount: the amount that was inserted.
    • extract

      public final long extract(ItemVariant variant, 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<ItemVariant>
      Specified by:
      extract in interface StorageView<ItemVariant>
      Parameters:
      variant - 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 nonnegative integer not greater than maxAmount: the amount that was extracted.
    • createSnapshot

      protected final net.minecraft.item.ItemStack 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<net.minecraft.item.ItemStack>
    • readSnapshot

      protected final void readSnapshot(net.minecraft.item.ItemStack snapshot)
      Description copied from class: SnapshotParticipant
      Roll back to a state previously created by SnapshotParticipant.createSnapshot().
      Specified by:
      readSnapshot in class SnapshotParticipant<net.minecraft.item.ItemStack>