====== Creating a fluid ====== ===== Overview ===== Here we'll cover creation of a custom fluid. If you plan to create several fluids, it is recommended to make an abstract basic fluid class where you'll set necessary defaults that will be shared in its subclasses. We'll also make it generate in the world like lakes. ===== Making an abstract fluid ===== Vanilla fluids extend '''', and so shall we. public abstract class TutorialFluid extends class_3609 { /** * @return whether the given fluid an instance of this fluid */ @Override public boolean method_15780(class_3611 fluid) { return fluid == method_15751() || fluid == method_15750(); } /** * @return whether the fluid is infinite (which means can be infinitely created like water). In vanilla, it depends on the game rule. */ @Override protected boolean method_15737() { return false; } /** * Perform actions when the fluid flows into a replaceable block. Water drops * the block's loot table. Lava plays the "block.lava.extinguish" sound. */ @Override protected void method_15730(class_1936 world, class_2338 pos, class_2680 state) { final class_2586 blockEntity = state.method_31709() ? world.method_8321(pos) : null; class_2248.method_9610(state, world, pos, blockEntity); } /** * Lava returns true if it's FluidState is above a certain height and the * Fluid is Water. * * @return whether the given Fluid can flow into this FluidState */ @Override protected boolean method_15777(class_3610 fluidState, class_1922 blockView, class_2338 blockPos, class_3611 fluid, class_2350 direction) { return false; } /** * Possibly related to the distance checks for flowing into nearby holes? * Water returns 4. Lava returns 2 in the Overworld and 4 in the Nether. */ @Override protected int method_15733(class_4538 worldView) { return 4; } /** * Water returns 1. Lava returns 2 in the Overworld and 1 in the Nether. */ @Override protected int method_15739(class_4538 worldView) { return 1; } /** * Water returns 5. Lava returns 30 in the Overworld and 10 in the Nether. */ @Override public int method_15789(class_4538 worldView) { return 5; } /** * Water and Lava both return 100.0F. */ @Override protected float method_15784() { return 100.0F; } } ===== Implementation ===== Now let's make an actual fluid which will have still and flowing variants. For this tutorial, we will call it Acid. The missing references will be filled in shortly. public abstract class AcidFluid extends TutorialFluid { @Override public class_3611 method_15751() { return YOUR_STILL_FLUID_HERE; } @Override public class_3611 method_15750() { return YOUR_FLOWING_FLUID_HERE; } @Override public class_1792 method_15774() { return YOUR_BUCKET_ITEM_HERE; } @Override protected class_2680 method_15790(class_3610 fluidState) { return YOUR_FLUID_BLOCK_HERE.method_9564().method_11657(class_2741.field_12538, method_15741(fluidState)); } public static class Flowing extends AcidFluid { @Override protected void method_15775(class_2689.class_2690 builder) { super.method_15775(builder); builder.method_11667(field_15900); } @Override public int method_15779(class_3610 fluidState) { return fluidState.method_11654(field_15900); } @Override public boolean method_15793(class_3610 fluidState) { return false; } } public static class Still extends AcidFluid { @Override public int method_15779(class_3610 fluidState) { return 8; } @Override public boolean method_15793(class_3610 fluidState) { return true; } } } Next, we'll make static instances of still and flowing acid variants, and an acid bucket. In your ''ModInitializer'': public static class_3609 STILL_ACID; public static class_3609 FLOWING_ACID; public static class_1792 ACID_BUCKET; @Override public void onInitialize() { STILL_ACID = class_2378.method_10230(class_7923.field_41173, new class_2960("tutorial", "acid"), new AcidFluid.Still()); FLOWING_ACID = class_2378.method_10230(class_7923.field_41173, new class_2960("tutorial", "flowing_acid"), new AcidFluid.Flowing()); ACID_BUCKET = class_2378.method_10230(class_7923.field_41178, new class_2960("tutorial", "acid_bucket"), new class_1755(STILL_ACID, new class_1792.class_1793().method_7896(class_1802.field_8550).method_7889(1))); // ... } // ... To make a custom fluid behave more like water or lava, you must add it to a corresponding fluid tag: For water, make a ''data/minecraft/tags/fluids/water.json'' file and write the identifiers of your fluids in there: { "replace": false, "values": [ "tutorial:acid", "tutorial:flowing_acid" ] } ===== Making a fluid block ===== Next we need to create a block which will represent acid in the world. '''' is the class we need to use, but since its constructor is protected, we can't construct it directly. Some ways to use it are to make a subclass or an anonymous subclass. Here we will be showing the latter. In your ''ModInitializer'': public static class_2248 ACID; @Override public void onInitialize() { ACID = class_2378.method_10230(class_7923.field_41175, new class_2960(MOD_ID, "acid"), new class_2404(STILL_ACID, FabricBlockSettings.method_9630(class_2246.field_10382)){}); // ... } Now that we have these static objects, we can go back to ''AcidFluid'' and fill in the overridden methods: public abstract class AcidFluid extends TutorialFluid { @Override public class_3611 method_15751() { return TutorialMod.STILL_ACID; } @Override public class_3611 method_15750() { return TutorialMod.FLOWING_ACID; } @Override public class_1792 method_15774() { return TutorialMod.ACID_BUCKET; } @Override protected class_2680 method_15790(class_3610 fluidState) { // method_15741 converts the LEVEL_1_8 of the fluid state to the LEVEL_15 the fluid block uses return TutorialMod.ACID.method_9564().method_11657(class_2741.field_12538, method_15741(fluidState)); } public static class Flowing extends AcidFluid { @Override protected void method_15775(class_2689.class_2690 builder) { super.method_15775(builder); builder.method_11667(field_15900); } @Override public int method_15779(class_3610 fluidState) { return fluidState.method_11654(field_15900); } @Override public boolean method_15793(class_3610 fluidState) { return false; } } public static class Still extends AcidFluid { @Override public int method_15779(class_3610 fluidState) { return 8; } @Override public boolean method_15793(class_3610 fluidState) { return true; } } } ===== Rendering setup ===== For your fluids to have textures or be tinted with a color, you will need to register a ''FluidRenderHandler'' for them. Here, we will reuse water's textures and just change the tint color applied to them. To make sure the textures are rendered as translucent, you can use Fabric's ''BlockRenderLayerMap'' (see [[blockappearance]]). @Environment(EnvType.CLIENT) public class TutorialModClient implements ClientModInitializer { @Override public void onInitializeClient() { FluidRenderHandlerRegistry.INSTANCE.register(TutorialMod.STILL_ACID, TutorialMod.FLOWING_ACID, new SimpleFluidRenderHandler( new class_2960("minecraft:block/water_still"), new class_2960("minecraft:block/water_flow"), 0x4CC248 )); BlockRenderLayerMap.INSTANCE.putFluids(class_1921.method_23583(), TutorialMod.STILL_ACID, TutorialMod.FLOWING_ACID); //if you want to use custom textures they needs to be registered. //In this example this is unnecessary because the vanilla water textures are already registered. //To register your custom textures use this method. //ClientSpriteRegistryCallback.event(PlayerScreenHandler.BLOCK_ATLAS_TEXTURE).register((atlasTexture, registry) -> { // registry.register(new Identifier("tutorial:block/custom_fluid_still")); // registry.register(new Identifier("tutorial:block/custom_fluid_flowing")); //}); // ... } } If you want to use your own fluid textures, you can refer to vanilla's assets ((''assets/minecraft/blockstates/water.json''\\ ''assets/minecraft/models/block/water.json''\\ ''assets/minecraft/textures/block/water_still.png''\\ ''assets/minecraft/textures/block/water_still.png.mcmeta''\\ ''assets/minecraft/textures/block/water_flow.png''\\ ''assets/minecraft/textures/block/water_flow.png.mcmeta'')) as a template. ===== Generation in the world ===== TODO Update to 1.19.4