tutorial:transfer-api_item_storage
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
tutorial:transfer-api_item_storage [2022/02/04 16:40] – [Dealing with Inventory/SidedInventory and PlayerInventory] 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 63: | Line 63: | ||
To learn how to use '' | To learn how to use '' | ||
- | ===== Dealing with Inventory/ | + | ==== Dealing with Inventory/ |
Sometimes, it is necessary to convert some '' | Sometimes, it is necessary to convert some '' | ||
'' | '' | ||
Line 69: | Line 69: | ||
[[https:// | [[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.1643992843.txt.gz · Last modified: 2022/02/04 16:40 by technici4n