Class EmptyItemFluidStorage

java.lang.Object
net.fabricmc.fabric.api.transfer.v1.fluid.base.EmptyItemFluidStorage
All Implemented Interfaces:
InsertionOnlyStorage<FluidVariant>, Storage<FluidVariant>

@Experimental public final class EmptyItemFluidStorage extends Object implements InsertionOnlyStorage<FluidVariant>
Base implementation of a fluid storage for an empty item. The empty item can be filled with an exact amount of some fluid to yield a full item instead. The default behavior is to copy the NBT from the empty item to the full item, however there is a second constructor that allows customizing the mapping.

For example, an empty bucket could be registered to accept exactly 81000 droplets of water and turn into a water bucket, like that:


 FluidStorage.combinedItemApiProvider(Items.BUCKET) // Go through the combined API provider to make sure other mods can provide storages for empty buckets.
             .register(context -> {// Register a provider for the bucket, returning a new storage every time:
                 return new EmptyItemFluidStorage(
                     context, // Pass the context.
                     Items.WATER_BUCKET, // The result after fluid is inserted.
                     Fluids.WATER, // Which fluid to accept.
                     FluidConstants.BUCKET // How much fluid to accept.
                 );
             });
 
(This is just for illustration purposes! In practice, Fabric API already registers storages for most buckets, and it is inefficient to have one storage registered per fluid so Fabric API has a storage that accepts any fluid with a corresponding full bucket).

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

    • EmptyItemFluidStorage

      public EmptyItemFluidStorage(ContainerItemContext context, net.minecraft.item.Item fullItem, net.minecraft.fluid.Fluid insertableFluid, long insertableAmount)
      Create a new instance.
      Parameters:
      context - The current context.
      fullItem - The new item after a successful fill operation.
      insertableFluid - The fluid that can be inserted. Fluid variant NBT is ignored.
      insertableAmount - The amount of fluid that can be inserted.
    • EmptyItemFluidStorage

      public EmptyItemFluidStorage(ContainerItemContext context, Function<ItemVariant,ItemVariant> emptyToFullMapping, net.minecraft.fluid.Fluid insertableFluid, long insertableAmount)
      Create a new instance, with a custom mapping function. The mapping function allows customizing how the NBT of the full item depends on the NBT of the empty item. The default behavior with the other constructor is to just copy the full NBT.
      Parameters:
      context - The current context.
      emptyToFullMapping - A function mapping the empty item variant, to the variant that should be used for the full item.
      insertableFluid - The fluid that can be inserted. Fluid variant NBT is ignored on insertion.
      insertableAmount - The amount of fluid that can be inserted.
      See Also:
  • Method Details

    • insert

      public long insert(FluidVariant resource, 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<FluidVariant>
      Parameters:
      resource - 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.
    • iterator

      public Iterator<StorageView<FluidVariant>> iterator(TransactionContext transaction)
      Description copied from interface: Storage
      Iterate through the contents of this storage, for the scope of the passed transaction. Every visited StorageView represents a stored resource and an amount. The iterator doesn't guarantee that a single resource only occurs once during an iteration.

      The returned iterator and any view it returns are only valid for the scope of to the passed transaction. They should not be used once that transaction is closed.

      More precisely, as soon as the transaction is closed, hasNext() must return false, and any call to next() must throw a NoSuchElementException.

      Storage.insert(T, long, net.fabricmc.fabric.api.transfer.v1.transaction.TransactionContext) and Storage.extract(T, long, net.fabricmc.fabric.api.transfer.v1.transaction.TransactionContext) may be called safely during iteration. Extractions should be visible to an open iterator, but insertions are not required to. In particular, inventories with a fixed amount of slots may wish to make insertions visible to iterators, but inventories with a dynamic or very large amount of slots should not do that to ensure timely termination of the iteration.

      Specified by:
      iterator in interface Storage<FluidVariant>
      Parameters:
      transaction - The transaction to which the scope of the returned iterator is tied.
      Returns:
      An iterator over the contents of this storage.