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 [2021/08/13 01:46] – Convert most <code> blocks to <yarncode> daomephstatutorial:fluids [2026/02/23 14:26] (current) – fixed reference infinitychances
Line 5: Line 5:
  
 ===== Making an abstract fluid ===== ===== Making an abstract fluid =====
-Vanilla fluids extend ''<yarn net.minecraft.class_3609>'', and so shall we. +Vanilla fluids extend ''net.minecraft.world.level.material.FlowingFluid'', and so shall we. 
-<yarncode java [enable_line_numbers="true"]> +<code java [enable_line_numbers="true"]> 
-public abstract class TutorialFluid extends class_3609 {+public abstract class TutorialFluid extends FlowingFluid {
  /**  /**
  * @return whether the given fluid an instance of this fluid  * @return whether the given fluid an instance of this fluid
  */  */
  @Override  @Override
- public boolean method_15780(class_3611 fluid) { + public boolean isSame(Fluid fluid) { 
- return fluid == method_15751() || fluid == method_15750();+ return fluid == getSource() || fluid == getFlowing();
  }  }
  
  /**  /**
- * @return whether the fluid infinite like water+ * @return whether the fluid is infinite (which means can be infinitely created like water). In vanilla, it depends on the game rule.
  */  */
  @Override  @Override
- protected boolean method_15737() {+ protected boolean canConvertToSource() {
  return false;  return false;
  }  }
  
  /**  /**
- * Perform actions when fluid flows into a replaceable block. Water drops+ * Perform actions when the fluid flows into a replaceable block. Water drops
  * the block's loot table. Lava plays the "block.lava.extinguish" sound.  * the block's loot table. Lava plays the "block.lava.extinguish" sound.
  */  */
  @Override  @Override
- protected void method_15730(class_1936 worldclass_2338 pos, class_2680 state) { + protected void beforeDestroyingBlock(LevelAccessor levelBlockPos pos, BlockState state) { 
- final class_2586 blockEntity = state.method_31709() ? world.method_8321(pos) : null; + final BlockEntity entity = state.hasBlockEntity() ? level.getBlockEntity(pos) : null; 
- class_2248.method_9610(state, world, pos, blockEntity);+ Block.dropResources(state, level, pos, entity);
  }  }
  
  /**  /**
- * Lava returns true if its FluidState is above a certain height and the+ * Lava returns true if it'FluidState is above a certain height and the
  * Fluid is Water.  * Fluid is Water.
  
Line 41: Line 41:
  */  */
  @Override  @Override
- protected boolean method_15777(class_3610 fluidStateclass_1922 blockViewclass_2338 blockPosclass_3611 fluid, class_2350 direction) {+ protected boolean canBeReplacedWith(FluidState stateBlockGetter levelBlockPos posFluid fluid, Direction direction) {
  return false;  return false;
  }  }
Line 50: Line 50:
  */  */
  @Override  @Override
- protected int method_15733(class_4538 worldView) {+ protected int getSlopeFindDistance(LevelReader reader) {
  return 4;  return 4;
  }  }
  
  /**  /**
- * Water returns 1. Lava returns 2 in the Overworld and 1 in the Nether.+ * Water returns 1. Lava returns 2 in the overworld and 1 in the nether. 
 + * @return How many levels a fluid loses per block.
  */  */
  @Override  @Override
- protected int method_15739(class_4538 worldView) {+ protected int getDropOff(LevelReader reader) {
  return 1;  return 1;
  }  }
Line 64: Line 65:
  /**  /**
  * Water returns 5. Lava returns 30 in the Overworld and 10 in the Nether.  * Water returns 5. Lava returns 30 in the Overworld and 10 in the Nether.
 + * Seems to return the delay before it updates its state.
  */  */
  @Override  @Override
- public int method_15789(class_4538 worldView) {+ public int getTickDelay(LevelReader reader) {
  return 5;  return 5;
  }  }
Line 74: Line 76:
  */  */
  @Override  @Override
- protected float method_15784() {+ protected float getExplosionResistance() {
  return 100.0F;  return 100.0F;
  }  }
 } }
-</yarncode>+</code>
  
 ===== Implementation ===== ===== 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. 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.
  
-<yarncode java [enable_line_numbers="true"]>+<code java [enable_line_numbers="true"]>
 public abstract class AcidFluid extends TutorialFluid { public abstract class AcidFluid extends TutorialFluid {
  @Override  @Override
- public class_3611 method_15751() { + public Fluid getSource() { 
- return YOUR_STILL_FLUID_HERE;+ return YOUR_SOURCE_FLUID_HERE;
  }  }
  
  @Override  @Override
- public class_3611 method_15750() {+ public Fluid getFlowing() {
  return YOUR_FLOWING_FLUID_HERE;  return YOUR_FLOWING_FLUID_HERE;
  }  }
  
  @Override  @Override
- public class_1792 method_15774() {+ public Item getBucket() {
  return YOUR_BUCKET_ITEM_HERE;  return YOUR_BUCKET_ITEM_HERE;
  }  }
  
  @Override  @Override
- protected class_2680 method_15790(class_3610 fluidState) { + protected BlockState createLegacyBlock(FluidState state) { 
- return YOUR_FLUID_BLOCK_HERE.method_9564().method_11657(class_2741.field_12538method_15741(fluidState));+ return YOUR_FLUID_BLOCK_HERE.defaultBlockState().setValue(BlockStateProperties.LEVELgetLegacyLevel(state));
  }  }
  
  public static class Flowing extends AcidFluid {  public static class Flowing extends AcidFluid {
  @Override  @Override
- protected void method_15775(class_2689.class_2690<class_3611class_3610> builder) { + protected void createFluidStateDefinition(StateDefinition.Builder<FluidFluidState> builder) { 
- super.method_15775(builder); + super.createFluidStateDefinition(builder); 
- builder.method_11667(field_15900);+ builder.add(LEVEL);
  }  }
  
  @Override  @Override
- public int method_15779(class_3610 fluidState) { + public boolean isSource(FluidState state) { 
- return fluidState.method_11654(field_15900);+ return false;
  }  }
  
  @Override  @Override
- public boolean method_15793(class_3610 fluidState) { + public int getAmount(FluidState state) { 
- return false;+ return state.getValue(LEVEL);
  }  }
  }  }
Line 125: Line 127:
  public static class Still extends AcidFluid {  public static class Still extends AcidFluid {
  @Override  @Override
- public int method_15779(class_3610 fluidState) { + public boolean isSource(FluidState state) { 
- return 8;+ return true;
  }  }
  
  @Override  @Override
- public boolean method_15793(class_3610 fluidState) { + public int getAmount(FluidState state) { 
- return true;+ return 8;
  }  }
  }  }
 } }
-</yarncode>+</code>
  
 Next, we'll make static instances of still and flowing acid variants, and an acid bucket. In your ''ModInitializer'': Next, we'll make static instances of still and flowing acid variants, and an acid bucket. In your ''ModInitializer'':
  
-<yarncode java [enable_line_numbers="true"]> +<code java [enable_line_numbers="true"]> 
-public static class_3609 STILL_ACID+public static FlowingFluid ACID_SOURCE
-public static class_3609 FLOWING_ACID; +public static FlowingFluid FLOWING_ACID; 
-public static class_1792 ACID_BUCKET;+public static Item ACID_BUCKET;
    
 @Override @Override
 public void onInitialize() { public void onInitialize() {
- STILL_ACID class_2378.method_10230(class_2378.field_11154new class_2960(MOD_ID, "acid"), new AcidFluid.Still()); + ACID_SOURCE Registry.register(BuiltInRegistries.FLUIDIdentifier.fromNamespaceAndPath("tutorial", "acid"), new AcidFluid.Still()) 
- FLOWING_ACID = class_2378.method_10230(class_2378.field_11154new class_2960(MOD_ID, "flowing_acid"), new AcidFluid.Flowing()); + FLOWING_ACID =  Registry.register(BuiltInRegistries.FLUIDIdentifier.fromNamespaceAndPath("tutorial", "flowing_acid"), new AcidFluid.Flowing()); 
- ACID_BUCKET = class_2378.method_10230(class_2378.field_11142new class_2960(MOD_ID, "acid_bucket"),  + ACID_BUCKET = Registry.register(BuiltInRegistries.ITEMIdentifier.fromNamespaceAndPath("tutorial", "acid_bucket"),  
-        new class_1755(STILL_ACID, new class_1792.class_1793().method_7896(class_1802.field_8550).method_7889(1)));+        new BucketItem(ACID_SOURCE, properties), new Item.Properties().craftRemainder(Items.BUCKET).stacksTo(1);
    
  // ...  // ...
Line 155: Line 157:
    
 // ... // ...
-</yarncode>+</code>
  
-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:+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/fluid/water.json'' file and write the identifiers of your fluids in there:
 <code json [enable_line_numbers="true"]> <code json [enable_line_numbers="true"]>
 { {
Line 163: Line 165:
  "values":  "values":
  [  [
- "your_mod_id:acid", + "tutorial:acid", 
- "your_mod_id:flowing_acid"+ "tutorial:flowing_acid"
  ]  ]
 } }
Line 170: Line 172:
  
 ===== Making a fluid block ===== ===== Making a fluid block =====
-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 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'':+Next we need to create a block which will represent acid in the world. ''net.minecraft.world.level.block.LiquidBlock'' is the class we need to use. In your ''ModInitializer'':
  
-<yarncode java [enable_line_numbers="true"]> +<code java [enable_line_numbers="true"]> 
-public static class_2248 ACID;+public static Block ACID;
  
 @Override @Override
 public void onInitialize() { public void onInitialize() {
- ACID = class_2378.method_10230(class_2378.field_11146new class_2960(MOD_ID, "acid"), new class_2404(STILL_ACIDFabricBlockSettings.method_9630(class_2246.field_10382)){});+ ACID = Registry.register(BuiltInRegistries.BLOCKIdentifier.fromNamespaceAndPath("tutorial", "acid"), new LiquidBlock(ACID_SOURCEBlockBehaviour.Properties.ofFullCopy(Blocks.WATER)));
   
  // ...  // ...
  
-</yarncode>+</code>
  
 Now that we have these static objects, we can go back to ''AcidFluid'' and fill in the overridden methods: Now that we have these static objects, we can go back to ''AcidFluid'' and fill in the overridden methods:
  
-<yarncode java [enable_line_numbers="true"]>+<code java [enable_line_numbers="true"]>
 public abstract class AcidFluid extends TutorialFluid { public abstract class AcidFluid extends TutorialFluid {
  @Override  @Override
- public class_3611 method_15751() { + public Fluid getSource() { 
- return TutorialMod.STILL_ACID;+ return TutorialMod.ACID_SOURCE;
  }  }
- +
  @Override  @Override
- public class_3611 method_15750() {+ public Fluid getFlowing() {
  return TutorialMod.FLOWING_ACID;  return TutorialMod.FLOWING_ACID;
  }  }
- +
  @Override  @Override
- public class_1792 method_15774() {+ public Item getBucket() {
  return TutorialMod.ACID_BUCKET;  return TutorialMod.ACID_BUCKET;
  }  }
- +
  @Override  @Override
- protected class_2680 method_15790(class_3610 fluidState) { + protected BlockState createLegacyBlock(FluidState state) { 
- // method_15741 converts the LEVEL_1_8 of the fluid state to the LEVEL_15 the fluid block uses + return TutorialMod.ACID.defaultBlockState().setValue(BlockStateProperties.LEVELgetLegacyLevel(state));
- return TutorialMod.ACID.method_9564().method_11657(class_2741.field_12538method_15741(fluidState));+
  }  }
  
  public static class Flowing extends AcidFluid {  public static class Flowing extends AcidFluid {
  @Override  @Override
- protected void method_15775(class_2689.class_2690<class_3611class_3610> builder) { + protected void createFluidStateDefinition(StateDefinition.Builder<FluidFluidState> builder) { 
- super.method_15775(builder); + super.createFluidStateDefinition(builder); 
- builder.method_11667(field_15900);+ builder.add(LEVEL);
  }  }
  
  @Override  @Override
- public int method_15779(class_3610 fluidState) { + public boolean isSource(FluidState state) { 
- return fluidState.method_11654(field_15900);+ return false;
  }  }
  
  @Override  @Override
- public boolean method_15793(class_3610 fluidState) { + public int getAmount(FluidState state) { 
- return false;+ return state.getValue(LEVEL);
  }  }
  }  }
Line 228: Line 229:
  public static class Still extends AcidFluid {  public static class Still extends AcidFluid {
  @Override  @Override
- public int method_15779(class_3610 fluidState) { + public boolean isSource(FluidState state) { 
- return 8;+ return true;
  }  }
  
  @Override  @Override
- public boolean method_15793(class_3610 fluidState) { + public int getAmount(FluidState state) { 
- return true;+ return 8;
  }  }
  }  }
-  +
-</yarncode>+</code>
  
 ===== 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''.+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]]).
  
-<yarncode java [enable_line_numbers="true"]>+<code java [enable_line_numbers="true"]> 
 +@Environment(EnvType.CLIENT)
 public class TutorialModClient implements ClientModInitializer { public class TutorialModClient implements ClientModInitializer {
  
  @Override  @Override
  public void onInitializeClient() {  public void onInitializeClient() {
- setupFluidRendering(TutorialMod.STILL_ACID, TutorialMod.FLOWING_ACID, new class_2960("minecraft", "water"), 0x4CC248)+ FluidRenderHandlerRegistry.INSTANCE.register(TutorialMod.ACID_SOURCE, TutorialMod.FLOWING_ACID, SimpleFluidRenderHandler.coloredWater(0x4CC248));
- BlockRenderLayerMap.INSTANCE.putFluids(class_1921.method_23583(), TutorialMod.STILL_ACID, TutorialMod.FLOWING_ACID);+
  
- // ...+ BlockRenderLayerMap.putFluids(ChunkSectionLayer.TRANSLUCENT, TutorialMod.ACID_SOURCE, TutorialMod.FLOWING_ACID);
  }  }
 +}
 +</code>
 +===== Adding custom textures to your fluid =====
 +To create a fluid with custom textures, you will need at least 2 new textures, with an optional 3rd, overlay texture that is shown when you look at the fluid behind glass or leaves. All textures go in the block textures folder.
  
- public static void setupFluidRendering(final class_3611 stillfinal class_3611 flowing, final class_2960 textureFluidId, final int color) { +The first texture should be named YOUR_FLUID_NAME_still.pngand can optionally be animated. 
- final class_2960 stillSpriteId = new class_2960(textureFluidId.method_12836()"block/" + textureFluidId.method_12832() + "_still"); +The second texture should be named YOUR_FLUID_NAME_flowing.pngand should have double the size in the dimensions. For example, if your still texture has frames of 16x16, the flowing should have frames of 32x32. It can also be animated
- final class_2960 flowingSpriteId = new class_2960(textureFluidId.method_12836()"block/" + textureFluidId.method_12832() + "_flow");+The third will be named YOUR_FLUID_NAME_overlay.pngand should not be animated.
  
- // If they're not already presentadd the sprites to the block atlas +Using our acid example, the client initializer will look like this: 
- ClientSpriteRegistryCallback.event(class_1059.field_5275).register((atlasTexture, registry) -{ +<code java [enable_line_numbers="true"]
- registry.register(stillSpriteId); +@Environment(EnvType.CLIENT
- registry.register(flowingSpriteId); +public class TutorialModClient implements ClientModInitializer {
- });+
  
- final class_2960 fluidId = class_2378.field_11154.method_10221(still); + @Override 
- final class_2960 listenerId = new class_2960(fluidId.method_12836(), fluidId.method_12832() "_reload_listener");+ public void onInitializeClient() { 
 + FluidRenderHandlerRegistry.INSTANCE.register(TutorialMod.ACID_SOURCE, TutorialMod.FLOWING_ACID, new SimpleFluidRenderHandler( 
 + Identifier.fromNamespaceAndPath("tutorial", "block/acid_still"), 
 + Identifier.fromNamespaceAndPath("tutorial", "block/acid_flowing")
 + Identifier.fromNamespaceAndPath("tutorial", "block/acid_overlay") /*optional tint can go after the overlay*/);
  
- final class_1058[] fluidSprites = { null, null }; + BlockRenderLayerMap.putFluids(ChunkSectionLayer.TRANSLUCENTTutorialMod.ACID_SOURCETutorialMod.FLOWING_ACID);
- +
- ResourceManagerHelper.get(class_3264.field_14188).registerReloadListener(new SimpleSynchronousResourceReloadListener() { +
- @Override +
- public class_2960 getFabricId() { +
- return listenerId; +
-+
- +
- /** +
- * Get the sprites from the block atlas when resources are reloaded +
- */ +
- @Override +
- public void method_14491(class_3300 resourceManager) { +
- final Function<class_2960class_1058> atlas = class_310.method_1551().method_1549(class_1059.field_5275); +
- fluidSprites[0] = atlas.apply(stillSpriteId); +
- fluidSprites[1] = atlas.apply(flowingSpriteId); +
-+
- }); +
- +
- // The FluidRenderer gets the sprites and color from a FluidRenderHandler during rendering +
- final FluidRenderHandler renderHandler = new FluidRenderHandler() +
-+
- @Override +
- public class_1058[] getFluidSprites(class_1920 viewclass_2338 pos, class_3610 state) { +
- return fluidSprites; +
-+
- +
- @Override +
- public int getFluidColor(class_1920 view, class_2338 pos, class_3610 state) { +
- return color; +
-+
- }; +
- +
- FluidRenderHandlerRegistry.INSTANCE.register(still, renderHandler); +
- FluidRenderHandlerRegistry.INSTANCE.register(flowing, renderHandler);+
  }  }
 } }
-</yarncode>+</code>
  
-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+You can refer to vanilla's assets ((''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'' \\ ''assets/minecraft/textures/block/water_overlay.png'')) as a template for your own textures.
- +
-===== Generation in the world ===== +
-To make lakes of acid generate in the world, you can create a ''<yarn net.minecraft.class_3085>'' in your ''ModInitializer'' and then add it to the biomes you want it to generate in: +
- +
-<code java [enable_line_numbers="true"]> +
-public static LakeFeature ACID_LAKE; +
- +
-@Override +
-public void onInitialize() { +
- ACID_LAKE = Registry.register(Registry.FEATURE, new Identifier(MOD_ID, "acid_lake"), new LakeFeature(SingleStateFeatureConfig::deserialize)); +
-  +
- // generate in swamps, similar to water lakes, but with a chance of 40 (the higher the number, the lower the generation chance) +
- Biomes.SWAMP.addFeature( +
- GenerationStep.Feature.LOCAL_MODIFICATIONS, +
- ACID_LAKE.configure(new SingleStateFeatureConfig(ACID.getDefaultState())) +
- .createDecoratedFeature(Decorator.WATER_LAKE.configure(new ChanceDecoratorConfig(40))) +
- ); +
-+
-</code>+
  
tutorial/fluids.1628819165.txt.gz · Last modified: 2021/08/13 01:46 by daomephsta