tutorial:transfer-api
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
tutorial:transfer-api [2021/10/29 20:45] – technici4n | tutorial:transfer-api [2023/07/19 13:38] (current) – technici4n | ||
---|---|---|---|
Line 1: | Line 1: | ||
- | ====== | + | ====== |
- | This tutorial gives a brief overview of how the Fabric Transfer API works, and how you can use it for your own mods. | + | |
- | For any question, please go to the ''# | + | |
- | ===== Creating a simple tank ===== | + | All Transfer API classes are marked as '' |
- | Let's see how we can make a block entity contain some fluid: | + | |
- | <code java> | + | |
- | public class MyTankBlockEntity extends BlockEntity { | + | |
- | // This field is going to contain | + | |
- | public final SingleVariantStorage< | + | |
- | @Override | + | |
- | protected FluidVariant getBlankVariant() { | + | |
- | return FluidVariant.blank(); | + | |
- | } | + | |
- | @Override | + | ==== Transactions ==== |
- | protected long getCapacity(FluidVariant variant) { | + | The Fabric Transfer API relies heavily |
- | // Here, you can pick your capacity depending | + | |
- | // For example, if we want to store 8 buckets | + | |
- | return 8 * FluidConstants.BUCKET; | + | |
- | } | + | |
- | @Override | + | - [[tutorial: |
- | protected void onFinalCommit() { | + | |
- | // Called after a successful insertion or extraction, markDirty | + | |
- | markDirty(); | + | |
- | } | + | |
- | }; | + | |
- | @Override | + | ==== The Fluid Transfer API ==== |
- | public NbtCompound writeNbt(NbtCompound tag) { | + | The Fabric Fluid Transfer API is how fluid-containing blocks such as machines, pipes, and tanks communicate with each other. It's what allows all mods to be compatible with each other as far as fluid transfer is concerned. (If you are coming from Forge, it serves a purpose similar to '' |
- | tag.put(" | + | |
- | tag.putLong(" | + | |
- | return super.writeNbt(tag); | + | |
- | } | + | |
- | @Override | + | |
- | public void readNbt(NbtCompound tag) { | + | |
- | super.readNbt(tag); | + | |
- | fluidStorage.variant = FluidVariant.fromNbt(tag.getCompound(" | + | |
- | fluidStorage.amount = tag.getLong(" | + | |
- | } | + | |
- | } | + | |
- | </ | + | |
- | Alright, now we can contain some fluid, and we are properly saving it if it changes. | + | - [[tutorial: |
- | Now, we must register ensure that other mods can properly interact with our tank: | + | - [[tutorial:transfer-api/ |
- | <code java> | + | - [[tutorial: |
- | BlockEntityType<MyTankBlockEntity> MY_TANK = // see block entity | + | |
- | // Put this in your mod initializer, | + | ==== The Item Transfer API ==== |
- | FluidStorage.SIDED.registerForBlockEntity((myTank, direction) -> myTank.fluidStorage, | + | The Fabric Item Transfer API is used by item-containing blocks such as chests, machines, or storage drawers to communicate with other. (If you are coming from Forge, it serves a purpose similar to '' |
- | </code> | + | - [[tutorial: |
- | === But wait, what is a FluidVariant ? === | + | // |
- | A '' | + | |
- | <code java> | + | |
- | // Creating a fluid variant from a fluid, without an NBT tag. | + | |
- | FluidVariant waterVariant = FluidVariant.of(Fluids.WATER); | + | |
- | waterVariant.getFluid() | + | |
- | waterVariant.copyNbt() // returns a copy of the optional nbt tag, in this case null | + | |
- | // Creating a fluid variant from a fluid, with an NBT tag. | + | |
- | NbtCompound customTag = new NbtCompound(); | + | |
- | customTag.putBoolean(" | + | |
- | FluidVariant magicWater = FluidVariant.of(Fluids.WATER, | + | |
- | </ | + | |
- | Variants are always compared with '' | + | ==== Fluid-containing items ==== |
- | <code java> | + | - [Not finished] [[tutorial: |
- | waterVariant.equals(waterVariant); | + | |
- | waterVariant.equals(magicWater); | + | |
- | // You can easily test if a variant has some fluid: | + | |
- | waterVariant.isOf(Fluids.WATER); | + | |
- | magicWater.isOf(Fluids.WATER); | + | |
- | </ | + | |
- | They can easily be serialized to and from NBT or network packets: | + | ==== The TechReborn Energy API ==== |
- | <code java> | + | - [Not yet written] 1. [[tutorial:energy-blockentity|how to create an energy storage |
- | // NBT | + | |
- | NbtCompound compound | + | |
- | FluidVariant variant | + | |
- | // Network packets | + | |
- | variant.toPacket(buf); | + | |
- | FluidVariant variant | + | |
- | </ | + | |
- | + | ||
- | === More on Fabric API Lookup | + | |
- | To understand what the call to '' | + | |
- | The Fabric API Lookup system allows blocks to expose interfaces such as '' | + |
tutorial/transfer-api.1635540302.txt.gz · Last modified: 2021/10/29 20:45 by technici4n