This is an old revision of the document!
−Table of Contents
Fabric Transfer API: Understanding Storage<ItemVariant>
This article is part of a series on the Fabric Transfer API. Link to the home page of the series.
This tutorial aims to give an overview of the Fabric Item Transfer API. Please make sure that you read the various fluid tutorials before reading this one, since most concepts used in the Fluid Storage API apply to the Item Transfer API.
Brief overview
The Item Transfer API is exactly the same as the Fluid Transfer API, but with ItemVariant
instead of FluidVariant
.
ItemVariant
Item variants represent the “type” of an item, i.e. the Minecraft `Item`, and also an optional NBT compound tag. They are immutable. The following examples are very similar to the FluidVariant
ones, since they are the same concept. Reading them is recommended though since ItemVariant
has a few nice functions to work with ItemStack
s.
// Creating an item variant from an item, without an NBT tag. ItemVariant ironVariant = ItemVariant.of(Items.IRON_INGOT); ironVariant.getItem() // returns Items.IRON_INGOT ironVariant.copyNbt() // returns a copy of the optional nbt tag, in this case null // Creating an item variant from an item, with an NBT tag. NbtCompound customTag = new NbtCompound(); customTag.putBoolean("magic", true); ItemVariant magicIron = ItemVariant.of(Items.IRON_INGOT, customTag); // Creating an item variant from an ItemStack ItemStack stack = new ItemStack(Items.IRON_INGOT); stack.setCustomName(new LiteralText("My Special Ingot")); ItemVariant specialIngot = ItemVariant.of(stack); // Further modifications to the ItemStack will not affect the ItemVariant since all the data is copied by of(...). // Creating an item stack from an item variant ItemStack size1 = specialIngot.toStack(); // no parameter -> count of the stack is 1 ItemStack anySize = specialIngot.toStack(32); // int parameter -> sets the count of the stack
Variants are always compared with .equals
, NEVER WITH ==
!
waterVariant.equals(waterVariant); // returns true waterVariant.equals(magicWater); // returns false // You can easily test if a variant has some fluid: waterVariant.isOf(Fluids.WATER); // returns true magicWater.isOf(Fluids.WATER); // returns true
They can easily be serialized to and from NBT or network packets:
// NBT NbtCompound compound = variant.toNbt(); FluidVariant variant = FluidVariant.fromNbt(compound); // Network packets variant.toPacket(buf); FluidVariant variant = FluidVariant.fromPacket(buf);