tutorial:transfer-api_simpletank
Differences
This shows you the differences between two versions of the page.
| Next revision | Previous revision | ||
| tutorial:transfer-api_simpletank [2021/10/29 20:46] – created technici4n | tutorial:transfer-api_simpletank [2023/02/22 08:22] (current) – Minor change in wording redgrapefruit | ||
|---|---|---|---|
| Line 1: | Line 1: | ||
| ===== Fabric Transfer API: Creating a simple tank ===== | ===== Fabric Transfer API: Creating a simple tank ===== | ||
| //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: | ||
| + | |||
| + | === 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() // returns Fluids.WATER | ||
| + | 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 '' | ||
| + | <code java> | ||
| + | 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: | ||
| + | <code java> | ||
| + | // NBT | ||
| + | NbtCompound compound = variant.toNbt(); | ||
| + | FluidVariant variant = FluidVariant.fromNbt(compound); | ||
| + | // Network packets | ||
| + | variant.toPacket(buf); | ||
| + | FluidVariant variant = FluidVariant.fromPacket(buf); | ||
| + | </ | ||
| + | |||
| + | CAUTION: make sure that you know the base understanding of [[tutorial: | ||
| Let's see how we can make a block entity contain some fluid: | Let's see how we can make a block entity contain some fluid: | ||
| + | |||
| <code java> | <code java> | ||
| - | public class MyTankBlockEntity extends BlockEntity { | + | // Make sure you implement ExtendedScreenHandlerFactory because we need to write the pos of this entity... |
| + | public class MyTankBlockEntity extends BlockEntity | ||
| + | private final DefaultedList< | ||
| // This field is going to contain the amount, and the fluid variant (more on that in a bit). | // This field is going to contain the amount, and the fluid variant (more on that in a bit). | ||
| public final SingleVariantStorage< | public final SingleVariantStorage< | ||
| Line 16: | Line 53: | ||
| // Here, you can pick your capacity depending on the fluid variant. | // Here, you can pick your capacity depending on the fluid variant. | ||
| // For example, if we want to store 8 buckets of any fluid: | // For example, if we want to store 8 buckets of any fluid: | ||
| - | return 8 * FluidConstants.BUCKET; | + | return |
| } | } | ||
| Line 23: | Line 60: | ||
| // Called after a successful insertion or extraction, markDirty to ensure the new amount and variant will be saved properly. | // Called after a successful insertion or extraction, markDirty to ensure the new amount and variant will be saved properly. | ||
| markDirty(); | markDirty(); | ||
| + | if (!world.isClient) { | ||
| + | var buf = PacketByteBufs.create(); | ||
| + | // Write your data here. | ||
| + | | ||
| + | | ||
| + | }); | ||
| + | } | ||
| } | } | ||
| }; | }; | ||
| Line 42: | Line 86: | ||
| Alright, now we can contain some fluid, and we are properly saving it if it changes. | Alright, now we can contain some fluid, and we are properly saving it if it changes. | ||
| - | Now, we must register ensure that other mods can properly interact with our tank: | + | Now, we must register |
| <code java> | <code java> | ||
| BlockEntityType< | BlockEntityType< | ||
| Line 48: | Line 92: | ||
| // Put this in your mod initializer, | // Put this in your mod initializer, | ||
| FluidStorage.SIDED.registerForBlockEntity((myTank, | FluidStorage.SIDED.registerForBlockEntity((myTank, | ||
| - | </ | ||
| - | |||
| - | === 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() // returns Fluids.WATER | ||
| - | 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 '' | ||
| - | <code java> | ||
| - | 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: | ||
| - | <code java> | ||
| - | // NBT | ||
| - | NbtCompound compound = variant.toNbt(); | ||
| - | FluidVariant variant = FluidVariant.fromNbt(compound); | ||
| - | // Network packets | ||
| - | variant.toPacket(buf); | ||
| - | FluidVariant variant = FluidVariant.fromPacket(buf); | ||
| </ | </ | ||
tutorial/transfer-api_simpletank.1635540402.txt.gz · Last modified: 2021/10/29 20:46 by technici4n