User Tools

Site Tools


tutorial:fluids

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
tutorial:fluids [2019/09/24 15:17] – more code style alexiytutorial:fluids [2023/05/04 11:31] (current) – [Rendering setup] solidblock
Line 1: Line 1:
 ====== Creating a fluid ====== ====== Creating a fluid ======
 +
 ===== Overview ===== ===== 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. 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 ===== ===== Making an abstract fluid =====
-Vanilla fluids extend **net.minecraft.fluid.BaseFluid** class, and so will our abstract fluidIt can be like this: +Vanilla fluids extend ''<yarn net.minecraft.class_3609>'', and so shall we
-<code java> +<yarncode java [enable_line_numbers="true"]
-public abstract class BasicFluid extends BaseFluid +public abstract class TutorialFluid extends class_3609 
-+ /** 
-    /** +  * @return whether the given fluid an instance of this fluid 
-     * @return does it produce infinite fluid (like water)? +  */ 
-     */ + @Override 
-    @Override + public boolean method_15780(class_3611 fluid) { 
-    protected boolean isInfinite() + return fluid == method_15751() || fluid == method_15750()
-    + }
-        return false+
-    }+
  
-    // make it transparent + /** 
-    @Override + * @return whether the fluid is infinite (which means can be infinitely created like water). In vanilla, it depends on the game rule. 
-    protected BlockRenderLayer getRenderLayer() + */ 
-    + @Override 
-        return BlockRenderLayer.TRANSLUCENT+ protected boolean method_15737() { 
-    }+ return false
 + }
  
-    /** + /** 
-     +  Perform actions when the fluid flows into a replaceable block. Water drops 
-     @return an associated item that "holdsthis fluid +  the block's loot table. Lava plays the "block.lava.extinguishsound. 
-     */ +  */ 
-    @Override + @Override 
-    public abstract Item getBucketItem();+ 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 
-     * @return a blockstate of the associated {@linkplain net.minecraft.block.FluidBlock} with {@linkplain net.minecraft.block.FluidBlock#LEVEL} + * Fluid is Water. 
-     */ +  *  
-    @Override +  * @return whether the given Fluid can flow into this FluidState 
-    protected abstract BlockState toBlockState(FluidState var1);+  */ 
 + @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? 
-     @return flowing static instance of this fluid +  Water returns 4. Lava returns 2 in the Overworld and 4 in the Nether. 
-     */ +  */ 
-    @Override + @Override 
-    public abstract Fluid getFlowing();+ protected int method_15733(class_4538 worldView
 + return 4; 
 + }
  
-    /** + /** 
-     +  Water returns 1. Lava returns 2 in the Overworld and 1 in the Nether. 
-     * @return still static instance of this fluid +  */ 
-     */ + @Override 
-    @Override + protected int method_15739(class_4538 worldView
-    public abstract Fluid getStill();+ return 1; 
 + }
  
-    // how much does the height of the fluid block decreases + /** 
-    @Override +  Water returns 5. Lava returns 30 in the Overworld and 10 in the Nether. 
-    protected int getLevelDecreasePerBlock(ViewableWorld world) +  */ 
-    { + @Override 
-        return 1; + public int method_15789(class_4538 worldView) { 
-    } + return 5; 
- + }
-    /** +
-     *  +
-     * @return update rate +
-     */ +
-    @Override +
-    public int getTickRate(ViewableWorld world) +
-    +
-        return 5; +
-    } +
- +
-    @Override +
-    protected float getBlastResistance() +
-    { +
-        return 100; +
-    } +
- +
-    // this seems to determine fluid's spread speed (higher value means faster) +
-    @Override +
-    protected int method_15733(ViewableWorld world) +
-    { +
-        return 4; +
-    } +
- +
-    // I don't know what this does, but it's present in the water fluid +
-    @Override +
-    protected void beforeBreakingBlock(IWorld world, BlockPos blockPos, BlockState blockState) { +
-        BlockEntity blockEntity = blockState.getBlock().hasBlockEntity() ? world.getBlockEntity(blockPos) : null; +
-        Block.dropStacks(blockState, world.getWorld(), blockPos, blockEntity); +
-    } +
- +
-    // also don't know what it does +
-    public boolean method_15777(FluidState fluidState, BlockView blockView, BlockPos blockPos, Fluid fluid, Direction direction) { +
-        return direction == Direction.DOWN; +
-    } +
- +
-    /** +
-     * +
-     * @return is given fluid instance of this fluid? +
-     */ +
-    @Override +
-    public abstract boolean matchesType(Fluid fluid);+
  
 + /**
 + * Water and Lava both return 100.0F.
 + */
 + @Override
 + protected float method_15784() {
 + return 100.0F;
 + }
 } }
-</code>+</yarncode>
  
 ===== Implementation ===== ===== Implementation =====
-Now let's make an actual fluid; it will have a //still// and //flowing// variantswill name it "Acid":+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.
  
-<code java> +<yarncode java [enable_line_numbers="true"]
-public abstract class Acid extends BasicFluid +public abstract class AcidFluid extends TutorialFluid 
-+ @Override 
-    @Override + public class_3611 method_15751() { 
-    public Item getBucketItem() + return YOUR_STILL_FLUID_HERE
-    { + }
-        return null; +
-    } +
-    @Override +
-    protected BlockState toBlockState(FluidState var1) +
-    +
-        return null+
-    }+
  
-    @Override + @Override 
-    public Fluid getFlowing() + public class_3611 method_15750() { 
-    + return YOUR_FLOWING_FLUID_HERE
-        return null+ }
-    }+
  
-    @Override + @Override 
-    public Fluid getStill() + public class_1792 method_15774() { 
-    + return YOUR_BUCKET_ITEM_HERE
-        return null+ }
-    }+
  
-    @Override + @Override 
-    public boolean matchesType(Fluid fluid) + protected class_2680 method_15790(class_3610 fluidState) { 
-    + return YOUR_FLUID_BLOCK_HERE.method_9564().method_11657(class_2741.field_12538, method_15741(fluidState))
-        return false+ }
-    }+
  
-    // still acid + public static class Flowing extends AcidFluid { 
-    public static class Still extends Acid + @Override 
-    {+ protected void method_15775(class_2689.class_2690<class_3611, class_3610> builder) { 
 + super.method_15775(builder); 
 + builder.method_11667(field_15900); 
 + }
  
-        @Override + @Override 
-        public boolean isStill(FluidState fluidState) + public int method_15779(class_3610 fluidState) { 
-        + return fluidState.method_11654(field_15900)
-            return true+ }
-        }+
  
-        /** + @Override 
-         * @return height of the fluid block + public boolean method_15793(class_3610 fluidState) { 
-         */ + return false
-        @Override +
-        public int getLevel(FluidState fluidState) + }
-        +
-            return 8+
-        +
-    }+
  
-    // flowing acid + public static class Still extends AcidFluid { 
-    public static class Flowing extends  Acid + @Override 
-    {+ public int method_15779(class_3610 fluidState) { 
 + return 8; 
 + }
  
-        @Override + @Override 
-        public boolean isStill(FluidState fluidState) + public boolean method_15793(class_3610 fluidState) { 
-        + return true
-            return false+ } 
-        }+
 +
 +</yarncode>
  
-        /** +Next, we'll make static instances of still and flowing acid variants, and an acid bucketIn your ''ModInitializer'':
-         * @return height of the fluid block +
-         */ +
-        @Override +
-        public int getLevel(FluidState fluidState) +
-        { +
-            return fluidState.get(LEVEL); +
-        }+
  
-        @Override +<yarncode java [enable_line_numbers="true"]> 
-        protected void appendProperties(StateFactory.Builder<Fluid, FluidState> stateFactoryBuilder) +public static class_3609 STILL_ACID; 
-        +public static class_3609 FLOWING_ACID; 
-            super.appendProperties(stateFactoryBuilder); +public static class_1792 ACID_BUCKET; 
-            stateFactoryBuilder.add(LEVEL); +  
-        } +@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))); 
 +  
 + // ...
 } }
-</code> +  
- +// ... 
-Next, we'll make static instances of still and flowing acid variants, and an acid bucketIn your **ModInitializer**: +</yarncode>
- +
-<code java> +
- +
-     +
-    public static Acid stillAcid; +
-    public static Acid flowingAcid; +
-     +
-    public static BucketItem acidBucket; +
- +
-    @Override +
-    public void onInitialize() +
-    { +
-     +
-        stillAcid = Registry.register(Registry.FLUID, new Identifier(MODID,"acid_still"), new Acid.Still()); +
-        flowingAcid = Registry.register(Registry.FLUID, new Identifier(MODID,"acid_flowing"), new Acid.Flowing()); +
-         +
-        acidBucket = new BucketItem(stillAcid, new Item.Settings().maxCount(1)); +
-        Registry.register(Registry.ITEM, new Identifier(MODID,"acid_bucket"), acidBucket); +
-    }     +
-</code>+
  
-To make the custom fluid behave like water or lava, you must add it to a corresponding fluid tag: make a file "data/minecraft/tags/fluids/water.jsonand write identifiers of your fluids in there: +To make 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: 
-<code json>+<code json [enable_line_numbers="true"]>
 { {
-  "replace": false, + "replace": false, 
-  "values":+ "values": 
-    "modid:acid_still", +
-    "modid:acid_flowing+ "tutorial:acid", 
-  ]+ "tutorial:flowing_acid
 + ]
 } }
 </code> </code>
  
-==== Making a fluid block ==== +===== Making a fluid block ===== 
-Next we need to create a block which will represent acid in the world. **net.minecraft.block.FluidBlock** is the class we need to use, but for "mojang" reasons its constructor is protected. The solution is well-known - make a subclass of it and change the visibility of the constructor:+Next we need to create a block which will represent acid in the world. ''<yarn net.minecraft.class_2404>'' is the class we need to use, but since its constructor is protected, we can't construct it directlySome ways to use it are to make a subclass or an anonymous subclass. Here we will be showing the latter. In your ''ModInitializer'':
  
-<code java> +<yarncode java [enable_line_numbers="true"]
-public class BaseFluidBlock extends FluidBlock +public static class_2248 ACID;
-+
-    public BaseFluidBlock(BaseFluid fluid, Settings settings) +
-    { +
-        super(fluid, settings); +
-    } +
-+
-</code>+
  
-Now make a static block instance:+@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)){}); 
 +  
 + // ... 
 +}  
 +</yarncode>
  
-<code java> +Now that we have these static objects, we can go back to ''AcidFluid'' and fill in the overridden methods:
-    ... +
-     +
-    public static FluidBlock acid;+
  
-    @Override +<yarncode java [enable_line_numbers="true"]> 
-    public void onInitialize() +public abstract class AcidFluid extends TutorialFluid { 
-    + @Override 
-     + public class_3611 method_15751() { 
-        ..+ return TutorialMod.STILL_ACID; 
-         + } 
-        acid = new BaseFluidBlock(stillAcid, FabricBlockSettings.of(Material.WATER).dropsNothing().build()); +  
-        Registry.register(Registry.BLOCKnew Identifier(MODID, "acid_block"), acid); + @Override 
-        + public class_3611 method_15750() { 
-</code>+ 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_12538method_15741(fluidState)); 
 + }
  
-Now when we have these static objects, we go back to **Acid** class and complete the overridden methods:+ public static class Flowing extends AcidFluid { 
 + @Override 
 + protected void method_15775(class_2689.class_2690<class_3611, class_3610> builder) { 
 + super.method_15775(builder); 
 + builder.method_11667(field_15900); 
 + }
  
-<code java> + @Override 
-public abstract class Acid extends BasicFluid + public int method_15779(class_3610 fluidState) { 
-+ return fluidState.method_11654(field_15900); 
-    @Override + }
-    public Item getBucketItem() +
-    +
-        return Mod.acidBucket; +
-    } +
-     +
-    @Override +
-    protected BlockState toBlockState(FluidState fluidState+
-    { +
-        //don't ask me what **method_15741** does... +
-        return Mod.acid.getDefaultState().with(FluidBlock.LEVEL, method_15741(fluidState)); +
-    }+
  
-    @Override + @Override 
-    public Fluid getFlowing() + public boolean method_15793(class_3610 fluidState) { 
-    + return false
-        return Mod.flowingAcid+
-    }+ }
  
-    @Override + public static class Still extends AcidFluid { 
-    public Fluid getStill() + @Override 
-    + public int method_15779(class_3610 fluidState) { 
-        return Mod.stillAcid+ return 8
-    }+ }
  
-    @Override + @Override 
-    public boolean matchesType(Fluid fluid_1) + public boolean method_15793(class_3610 fluidState) { 
-    + return true
-        return fluid_1==Mod.flowingAcid || fluid_1==Mod.stillAcid+
-    + } 
-     +  
-    ... +</yarncode>
-     +
-    +
-</code> +
- +
-Now we can assert that the Acid class is complete.+
  
 ===== Rendering setup ===== ===== 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]]).
  
-Time to do client-side thingsIn your **ClientModInitializer** you need to specify locations of sprites for your fluids and define their rendering. I will reuse water textures and just change the color applied to them.+<yarncode java [enable_line_numbers="true"]> 
 +@Environment(EnvType.CLIENT) 
 +public class TutorialModClient implements ClientModInitializer {
  
-<code java> + @Override 
-    @Override + public void onInitializeClient() { 
-    public void onInitializeClient() + FluidRenderHandlerRegistry.INSTANCE.register(TutorialMod.STILL_ACID, TutorialMod.FLOWING_ACID, new SimpleFluidRenderHandler( 
-    + new class_2960("minecraft:block/water_still"), 
-        Identifier stillSpriteLocation = new Identifier("block/water_still"); + new class_2960("minecraft:block/water_flow"), 
-        Identifier dynamicSpriteLocation = new Identifier("block/water_flow")+ 0x4CC248 
-        // here I tell to use only 16x16 area of the water texture + ));
-        FabricSprite stillAcidSprite = new FabricSprite(stillSpriteLocation16, 16); +
-        // same, but 32 +
-        FabricSprite dynamicAcidSprite = new FabricSprite(dynamicSpriteLocation, 32, 32)+
-         +
-        // adding the sprites to the block texture atlas +
-        ClientSpriteRegistryCallback.event(SpriteAtlasTexture.BLOCK_ATLAS_TEX).register((spriteAtlasTexture, registry) -> { +
-            registry.register(stillAcidSprite); +
-            registry.register(dynamicAcidSprite); +
-        });+
  
-        // this renderer is responsible for drawing fluids in a world + BlockRenderLayerMap.INSTANCE.putFluids(class_1921.method_23583(), TutorialMod.STILL_ACIDTutorialMod.FLOWING_ACID);
-        FluidRenderHandler acidRenderHandler = new FluidRenderHandler() +
-        { +
-            // return the sprites: still sprite goes first into the arrayflowing/dynamic goes last +
-            @Override +
-            public Sprite[] getFluidSprites(ExtendedBlockView extendedBlockViewBlockPos blockPos, FluidState fluidState) +
-            { +
-                return new Sprite[] {stillAcidSprite, dynamicAcidSprite}; +
-            }+
  
-            // apply light green color + //if you want to use custom textures they needs to be registered. 
-            @Override + //In this example this is unnecessary because the vanilla water textures are already registered. 
-            public int getFluidColor(ExtendedBlockView view, BlockPos posFluidState state) + //To register your custom textures use this method. 
-            + //ClientSpriteRegistryCallback.event(PlayerScreenHandler.BLOCK_ATLAS_TEXTURE).register((atlasTextureregistry-> 
-                return 0x4cc248+ //    registry.register(new Identifier("tutorial:block/custom_fluid_still"))
-            } + //    registry.register(new Identifier("tutorial:block/custom_fluid_flowing")); 
-        };+ //});
  
-        // registering the same renderer for both fluid variants is intentional+ // ... 
 +
 +
 +</yarncode>
  
-        FluidRenderHandlerRegistry.INSTANCE.register(Mod.stillAcid, acidRenderHandler); +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.
-        FluidRenderHandlerRegistry.INSTANCE.register(Mod.flowingAcid, acidRenderHandler); +
-</code> +
- +
-Then what's left to do is to create necessary Json files and textures, but you should know how to do that at this point. +
- +
-===== Generation in a world ===== +
- +
-To make acid lakes generate in the world, you can use **net.minecraft.world.gen.feature.LakeFeature**, which you create in the ModInitializer: +
-<code java> +
-         +
-        LakeFeature acidFeature = Registry.register(Registry.FEATURE, new Identifier(MODID,"acid_lake"), new LakeFeature(dynamic -> new LakeFeatureConfig(acid.getDefaultState())));+
  
 +===== Generation in the world =====
 +TODO Update to 1.19.4
 </code> </code>
-Then put it into desired biomes to generate: 
-<code java> 
-        // I tell it to generate like water lakes, with a rarity of 40 (the higher is the number, the lesser is the generation chance): 
-        Biomes.FOREST.addFeature(GenerationStep.Feature.LOCAL_MODIFICATIONS, Biome.configureFeature(acidFeature, new LakeFeatureConfig(acid.getDefaultState()), Decorator.WATER_LAKE, new LakeDecoratorConfig(40))); 
-</code> 
-This is the end of the tutorial. 
- 
tutorial/fluids.1569338253.txt.gz · Last modified: 2019/09/24 15:17 by alexiy