Class SingleStackStorage

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

public abstract class SingleStackStorage extends SnapshotParticipant<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.

  • Constructor Details Link icon

    • SingleStackStorage Link icon

      public SingleStackStorage()
  • Method Details Link icon

    • getStack Link icon

      protected abstract 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 Link icon

      protected abstract void setStack(ItemStack stack)
      Set the stack of this storage.
    • canInsert Link icon

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

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

      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 Link icon

      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<ItemVariant>
    • getResource Link icon

      public 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 Link icon

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

      public 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 estimated upper bound on the number of resources that could be stored if this view has a blank resource.
    • insert Link icon

      public 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 non-negative integer not greater than maxAmount: the amount that was inserted.
    • extract Link icon

      public 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 non-negative integer not greater than maxAmount: the amount that was extracted.
    • createSnapshot Link icon

      protected 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<ItemStack>
    • readSnapshot Link icon

      protected void readSnapshot(ItemStack snapshot)
      Description copied from class: SnapshotParticipant
      Roll back to a state previously created by SnapshotParticipant.createSnapshot().
      Specified by:
      readSnapshot in class SnapshotParticipant<ItemStack>
    • toString Link icon

      public String toString()
      Overrides:
      toString in class Object