tutorial:transfer-api_item_storage
Differences
This shows you the differences between two versions of the page.
| Next revision | Previous revision | ||
| tutorial:transfer-api_item_storage [2022/02/04 11:17] – created technici4n | tutorial:transfer-api_item_storage [2022/02/04 17:37] (current) – external edit 127.0.0.1 | ||
|---|---|---|---|
| Line 1: | Line 1: | ||
| - | ====== Fabric Transfer API: Understanding | + | ====== Fabric Transfer API: Item transfer with Storage< |
| //This article is part of a series on the Fabric Transfer API. [[tutorial: | //This article is part of a series on the Fabric Transfer API. [[tutorial: | ||
| Line 5: | Line 5: | ||
| ===== Brief overview ===== | ===== Brief overview ===== | ||
| - | **The Item Transfer API is exactly the same as the Fluid Transfer API, but with '' | + | The core message is: **The Item Transfer API is used exactly the same as the Fluid Transfer API, but with '' |
| ==== ItemVariant ==== | ==== ItemVariant ==== | ||
| Line 19: | Line 19: | ||
| ItemVariant magicIron = ItemVariant.of(Items.IRON_INGOT, | ItemVariant magicIron = ItemVariant.of(Items.IRON_INGOT, | ||
| // Creating an item variant from an ItemStack | // Creating an item variant from an ItemStack | ||
| - | ItemStack | + | ItemStack |
| - | stack.setCustomName(new LiteralText(" | + | specialStack.setCustomName(new LiteralText(" |
| - | ItemVariant specialIngot = ItemVariant.of(stack); | + | ItemVariant specialIngot = ItemVariant.of(specialStack); |
| // Further modifications to the ItemStack will not affect the ItemVariant since all the data is copied by of(...). | // 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 | // Creating an item stack from an item variant | ||
| Line 30: | Line 30: | ||
| Variants are always compared with '' | Variants are always compared with '' | ||
| <code java> | <code java> | ||
| - | waterVariant.equals(waterVariant); // returns true | + | ironVariant.equals(ironVariant); // returns true |
| - | waterVariant.equals(magicWater); // returns false | + | ironVariant.equals(magicIron); // returns false |
| - | // You can easily test if a variant has some fluid: | + | // You can easily test if a variant has some item: |
| - | waterVariant.isOf(Fluids.WATER); // returns true | + | ironVariant.isOf(Items.IRON_INGOT); // returns true |
| - | magicWater.isOf(Fluids.WATER); // returns true | + | magicIron.isOf(Items.IRON_INGOT); |
| + | // The matches function can be used to compare an item variant with an item stack: | ||
| + | ironVariant.matches(specialStack); | ||
| + | specialIngot.matches(specialStack); // returns true | ||
| </ | </ | ||
| Line 41: | Line 44: | ||
| // NBT | // NBT | ||
| NbtCompound compound = variant.toNbt(); | NbtCompound compound = variant.toNbt(); | ||
| - | FluidVariant | + | ItemVariant |
| // Network packets | // Network packets | ||
| variant.toPacket(buf); | variant.toPacket(buf); | ||
| - | FluidVariant | + | ItemVariant |
| </ | </ | ||
| + | |||
| + | ==== Finding Storage< | ||
| + | '' | ||
| + | It is queried and exposed through the '' | ||
| + | <code java> | ||
| + | // Finding an instance | ||
| + | @Nullable | ||
| + | Storage< | ||
| + | |||
| + | // To expose an instance, use one of the ItemStorage.SIDED.registerFor* functions | ||
| + | </ | ||
| + | |||
| + | To learn how to use '' | ||
| + | |||
| + | ==== Dealing with Inventory/ | ||
| + | Sometimes, it is necessary to convert some '' | ||
| + | '' | ||
| + | The side '' | ||
| + | |||
| + | [[https:// | ||
| + | |||
| + | **A word of caution**: To be able to support transactions, | ||
| + | |||
| + | ===== How to implement Storage< | ||
| + | This section describes various ways to create and customize '' | ||
| + | |||
| + | ==== SingleVariantStorage, | ||
| + | The helpers described on the [[transfer-api/ | ||
| + | |||
| + | '' | ||
| + | |||
| + | ==== Use SimpleInventory ==== | ||
| + | A nice way to obtain a '' | ||
| + | |||
| + | Example: | ||
| + | <code java> | ||
| + | public class MyBlockEntity extends BlockEntity { | ||
| + | // ... [other code] | ||
| + | |||
| + | // This is the internal inventory, it can be used directly, or to create Slots, etc... | ||
| + | private final SimpleInventory inventory = new SimpleInventory(3) { // 3 here is the slot count | ||
| + | @Override | ||
| + | public int getMaxCountPerStack() { | ||
| + | return 4; // Optional: customize the maximum count in each slot. | ||
| + | } | ||
| + | |||
| + | @Override | ||
| + | public boolean isValid(int slot, ItemStack stack) { | ||
| + | // Optional: restrict which stacks are allowed in which slot. | ||
| + | if (slot == 1) { | ||
| + | return stack.isOf(Items.COAL); | ||
| + | } | ||
| + | return true; | ||
| + | } | ||
| + | |||
| + | @Override | ||
| + | public void markDirty() { | ||
| + | // Don't forget to schedule the block entity for saving after a modification! | ||
| + | MyBlockEntity.this.markDirty(); | ||
| + | } | ||
| + | }; | ||
| + | // It's better for performance to store the result of InventoryStorage.of(...), | ||
| + | // It may also be nicer to use than the SimpleInventory in some cases. | ||
| + | public final InventoryStorage inventoryWrapper = InventoryStorage.of(inventory, | ||
| + | |||
| + | // Don't forget to implement fromNbt/ | ||
| + | |||
| + | // Don't forget to expose inventoryWrapper through ItemStorage.SIDED. | ||
| + | |||
| + | // [...] Other code omitted | ||
| + | } | ||
| + | </ | ||
| + | |||
| + | This is a powerful implementation technique due to the low amount of code that is required, and it can be taken even further: | ||
| + | * Multiple '' | ||
| + | * (Advanced) You can have your '' | ||
| + | |||
| + | ==== Use SingleStackStorage ==== | ||
| + | Another (more complicated) option is to store a stack or a list of stacks somewhere, and also a [[https:// | ||
| + | |||
| + | ==== (Discouraged) Directly implement Inventory or SidedInventory ==== | ||
| + | For compatibility with existing vanilla inventories, | ||
| + | * Performance: | ||
| + | * Code size: this will require more code than using '' | ||
| + | * Flexibility: | ||
| + | * Correctness: | ||
tutorial/transfer-api_item_storage.1643973457.txt.gz · Last modified: 2022/02/04 11:17 by technici4n