tutorial:fluids
Differences
This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
| tutorial:fluids [2021/07/28 12:52] – Correct method_15741 to getBlockStateLevel super | tutorial: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 '' | + | Vanilla fluids extend '' |
| <code java [enable_line_numbers=" | <code java [enable_line_numbers=" | ||
| - | public abstract class TutorialFluid extends | + | public abstract class TutorialFluid extends |
| /** | /** | ||
| * @return whether the given fluid an instance of this fluid | * @return whether the given fluid an instance of this fluid | ||
| */ | */ | ||
| @Override | @Override | ||
| - | public boolean | + | public boolean |
| - | return fluid == getStill() || fluid == getFlowing(); | + | return fluid == getSource() || fluid == getFlowing(); |
| } | } | ||
| - | + | ||
| /** | /** | ||
| - | * @return whether the fluid infinite like water | + | * @return whether the fluid is infinite |
| */ | */ | ||
| @Override | @Override | ||
| - | protected boolean | + | protected boolean |
| 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' | * the block' | ||
| */ | */ | ||
| @Override | @Override | ||
| - | protected void beforeBreakingBlock(WorldAccess world, BlockPos pos, BlockState state) { | + | protected void beforeDestroyingBlock(LevelAccessor level, BlockPos pos, BlockState state) { |
| - | final BlockEntity | + | final BlockEntity |
| - | Block.dropStacks(state, | + | Block.dropResources(state, |
| } | } | ||
| - | + | ||
| /** | /** | ||
| - | * Lava returns true if its FluidState is above a certain height and the | + | * Lava returns true if it' |
| * Fluid is Water. | * Fluid is Water. | ||
| * | * | ||
| Line 41: | Line 41: | ||
| */ | */ | ||
| @Override | @Override | ||
| - | protected boolean canBeReplacedWith(FluidState | + | protected boolean canBeReplacedWith(FluidState |
| return false; | return false; | ||
| } | } | ||
| - | + | ||
| /** | /** | ||
| * Possibly related to the distance checks for flowing into nearby holes? | * Possibly related to the distance checks for flowing into nearby holes? | ||
| Line 50: | Line 50: | ||
| */ | */ | ||
| @Override | @Override | ||
| - | protected int getFlowSpeed(WorldView worldView) { | + | protected int getSlopeFindDistance(LevelReader reader) { |
| return 4; | return 4; | ||
| } | } | ||
| - | + | ||
| /** | /** | ||
| - | * Water returns 1. Lava returns 2 in the Overworld | + | * Water returns 1. Lava returns 2 in the overworld |
| + | * @return How many levels a fluid loses per block. | ||
| */ | */ | ||
| @Override | @Override | ||
| - | protected int getLevelDecreasePerBlock(WorldView worldView) { | + | protected int getDropOff(LevelReader reader) { |
| return 1; | return 1; | ||
| } | } | ||
| - | + | ||
| /** | /** | ||
| * 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 getTickRate(WorldView worldView) { | + | public int getTickDelay(LevelReader reader) { |
| return 5; | return 5; | ||
| } | } | ||
| - | + | ||
| /** | /** | ||
| * Water and Lava both return 100.0F. | * Water and Lava both return 100.0F. | ||
| */ | */ | ||
| @Override | @Override | ||
| - | protected float getBlastResistance() { | + | protected float getExplosionResistance() { |
| return 100.0F; | return 100.0F; | ||
| } | } | ||
| Line 86: | Line 88: | ||
| public abstract class AcidFluid extends TutorialFluid { | public abstract class AcidFluid extends TutorialFluid { | ||
| @Override | @Override | ||
| - | public Fluid getStill() { | + | public Fluid getSource() { |
| - | return | + | return |
| } | } | ||
| - | + | ||
| @Override | @Override | ||
| public Fluid getFlowing() { | public Fluid getFlowing() { | ||
| - | return | + | return YOUR_FLOWING_FLUID_HERE; |
| } | } | ||
| - | + | ||
| @Override | @Override | ||
| - | public Item getBucketItem() { | + | public Item getBucket() { |
| - | return | + | return YOUR_BUCKET_ITEM_HERE; |
| } | } | ||
| - | + | ||
| @Override | @Override | ||
| - | protected BlockState | + | protected BlockState |
| - | return | + | return YOUR_FLUID_BLOCK_HERE.defaultBlockState().setValue(BlockStateProperties.LEVEL, getLegacyLevel(state)); |
| } | } | ||
| - | + | ||
| public static class Flowing extends AcidFluid { | public static class Flowing extends AcidFluid { | ||
| @Override | @Override | ||
| - | protected void appendProperties(StateManager.Builder< | + | protected void createFluidStateDefinition(StateDefinition.Builder< |
| - | super.appendProperties(builder); | + | super.createFluidStateDefinition(builder); |
| builder.add(LEVEL); | builder.add(LEVEL); | ||
| } | } | ||
| - | + | ||
| @Override | @Override | ||
| - | public | + | public |
| - | return | + | return |
| } | } | ||
| - | + | ||
| @Override | @Override | ||
| - | public | + | public |
| - | return | + | return |
| } | } | ||
| } | } | ||
| - | + | ||
| public static class Still extends AcidFluid { | public static class Still extends AcidFluid { | ||
| @Override | @Override | ||
| - | public | + | public |
| - | return | + | return |
| } | } | ||
| - | + | ||
| @Override | @Override | ||
| - | public | + | public |
| - | return | + | return |
| } | } | ||
| } | } | ||
| Line 140: | Line 142: | ||
| <code java [enable_line_numbers=" | <code java [enable_line_numbers=" | ||
| - | public static | + | public static |
| - | public static | + | public static |
| public static Item ACID_BUCKET; | public static Item ACID_BUCKET; | ||
| + | |||
| @Override | @Override | ||
| public void onInitialize() { | public void onInitialize() { | ||
| - | STILL_ACID | + | ACID_SOURCE |
| - | FLOWING_ACID = Registry.register(Registry.FLUID, | + | FLOWING_ACID = Registry.register(BuiltInRegistries.FLUID, Identifier.fromNamespaceAndPath(" |
| - | ACID_BUCKET = Registry.register(Registry.ITEM, | + | ACID_BUCKET = Registry.register(BuiltInRegistries.ITEM, Identifier.fromNamespaceAndPath(" |
| - | + | | |
| + | |||
| // ... | // ... | ||
| } | } | ||
| + | |||
| // ... | // ... | ||
| </ | </ | ||
| - | To make a custom fluid behave more like water or lava, you must add it to a corresponding fluid tag: For water, make a '' | + | To make a custom fluid behave more like water or lava, you must add it to a corresponding fluid tag: For water, make a '' |
| <code json [enable_line_numbers=" | <code json [enable_line_numbers=" | ||
| { | { | ||
| Line 162: | Line 165: | ||
| " | " | ||
| [ | [ | ||
| - | "your_mod_id: | + | "tutorial: |
| - | "your_mod_id: | + | "tutorial: |
| ] | ] | ||
| } | } | ||
| Line 169: | Line 172: | ||
| ===== Making a fluid block ===== | ===== Making a fluid block ===== | ||
| - | Next we need to create a block which will represent acid in the world. '' | + | Next we need to create a block which will represent acid in the world. '' |
| <code java [enable_line_numbers=" | <code java [enable_line_numbers=" | ||
| Line 176: | Line 179: | ||
| @Override | @Override | ||
| public void onInitialize() { | public void onInitialize() { | ||
| - | ACID = Registry.register(Registry.BLOCK, | + | ACID = Registry.register(BuiltInRegistries.BLOCK, Identifier.fromNamespaceAndPath(" |
| // ... | // ... | ||
| Line 187: | Line 190: | ||
| public abstract class AcidFluid extends TutorialFluid { | public abstract class AcidFluid extends TutorialFluid { | ||
| @Override | @Override | ||
| - | public Fluid getStill() { | + | public Fluid getSource() { |
| - | return TutorialMod.STILL_ACID; | + | return TutorialMod.ACID_SOURCE; |
| } | } | ||
| - | + | ||
| @Override | @Override | ||
| public Fluid getFlowing() { | public Fluid getFlowing() { | ||
| return TutorialMod.FLOWING_ACID; | return TutorialMod.FLOWING_ACID; | ||
| } | } | ||
| - | + | ||
| @Override | @Override | ||
| - | public Item getBucketItem() { | + | public Item getBucket() { |
| return TutorialMod.ACID_BUCKET; | return TutorialMod.ACID_BUCKET; | ||
| } | } | ||
| - | + | ||
| @Override | @Override | ||
| - | protected BlockState | + | protected BlockState |
| - | // 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.LEVEL, getLegacyLevel(state)); |
| - | return TutorialMod.ACID.getDefaultState().with(Properties.LEVEL_15, method_15741(fluidState)); | + | |
| } | } | ||
| - | } | + | |
| + | public static class Flowing extends AcidFluid { | ||
| + | @Override | ||
| + | protected void createFluidStateDefinition(StateDefinition.Builder< | ||
| + | super.createFluidStateDefinition(builder); | ||
| + | builder.add(LEVEL); | ||
| + | } | ||
| + | |||
| + | @Override | ||
| + | public boolean isSource(FluidState state) { | ||
| + | return false; | ||
| + | } | ||
| + | |||
| + | @Override | ||
| + | public int getAmount(FluidState state) { | ||
| + | return state.getValue(LEVEL); | ||
| + | } | ||
| + | } | ||
| + | |||
| + | public static class Still extends AcidFluid { | ||
| + | @Override | ||
| + | public boolean isSource(FluidState state) { | ||
| + | return true; | ||
| + | } | ||
| + | |||
| + | @Override | ||
| + | public int getAmount(FluidState state) { | ||
| + | return 8; | ||
| + | } | ||
| + | } | ||
| + | } | ||
| </ | </ | ||
| ===== Rendering setup ===== | ===== Rendering setup ===== | ||
| - | For your fluids to have textures or be tinted with a color, you will need to register a '' | + | For your fluids to have textures or be tinted with a color, you will need to register a '' |
| <code java [enable_line_numbers=" | <code java [enable_line_numbers=" | ||
| + | @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, | + | FluidRenderHandlerRegistry.INSTANCE.register(TutorialMod.ACID_SOURCE, TutorialMod.FLOWING_ACID, |
| - | BlockRenderLayerMap.INSTANCE.putFluids(RenderLayer.getTranslucent(), TutorialMod.STILL_ACID, TutorialMod.FLOWING_ACID); | + | |
| - | + | BlockRenderLayerMap.putFluids(ChunkSectionLayer.TRANSLUCENT, TutorialMod.ACID_SOURCE, TutorialMod.FLOWING_ACID); | |
| - | // ... | + | |
| - | } | + | |
| - | + | ||
| - | public static void setupFluidRendering(final Fluid still, final Fluid flowing, final Identifier textureFluidId, | + | |
| - | final Identifier stillSpriteId = new Identifier(textureFluidId.getNamespace(), | + | |
| - | final Identifier flowingSpriteId = new Identifier(textureFluidId.getNamespace(), | + | |
| - | + | ||
| - | // If they' | + | |
| - | ClientSpriteRegistryCallback.event(SpriteAtlasTexture.BLOCK_ATLAS_TEXTURE).register((atlasTexture, | + | |
| - | registry.register(stillSpriteId); | + | |
| - | registry.register(flowingSpriteId); | + | |
| - | }); | + | |
| - | + | ||
| - | final Identifier fluidId = Registry.FLUID.getId(still); | + | |
| - | final Identifier listenerId = new Identifier(fluidId.getNamespace(), | + | |
| - | + | ||
| - | final Sprite[] fluidSprites = { null, null }; | + | |
| - | + | ||
| - | ResourceManagerHelper.get(ResourceType.CLIENT_RESOURCES).registerReloadListener(new SimpleSynchronousResourceReloadListener() { | + | |
| - | @Override | + | |
| - | public Identifier getFabricId() { | + | |
| - | return listenerId; | + | |
| - | } | + | |
| - | + | ||
| - | /** | + | |
| - | * Get the sprites from the block atlas when resources are reloaded | + | |
| - | */ | + | |
| - | @Override | + | |
| - | public void apply(ResourceManager resourceManager) { | + | |
| - | final Function< | + | |
| - | 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 Sprite[] getFluidSprites(BlockRenderView view, BlockPos pos, FluidState state) { | + | |
| - | return fluidSprites; | + | |
| - | } | + | |
| - | + | ||
| - | @Override | + | |
| - | public int getFluidColor(BlockRenderView view, BlockPos pos, FluidState state) { | + | |
| - | return color; | + | |
| - | } | + | |
| - | }; | + | |
| - | + | ||
| - | FluidRenderHandlerRegistry.INSTANCE.register(still, | + | |
| - | FluidRenderHandlerRegistry.INSTANCE.register(flowing, | + | |
| } | } | ||
| } | } | ||
| </ | </ | ||
| + | ===== 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. | ||
| - | If you want to use your own fluid textures, you can refer to vanilla' | + | The first texture should be named YOUR_FLUID_NAME_still.png, and can optionally be animated. |
| - | + | The second texture should be named YOUR_FLUID_NAME_flowing.png, and should have double the size in the dimensions. For example, if your still texture has frames | |
| - | ===== Generation | + | The third will be named YOUR_FLUID_NAME_overlay.png, and should not be animated. |
| - | To make lakes of acid generate in the world, you can create a '' | + | |
| + | Using our acid example, the client initializer will look like this: | ||
| <code java [enable_line_numbers=" | <code java [enable_line_numbers=" | ||
| - | public | + | @Environment(EnvType.CLIENT) |
| + | public | ||
| - | @Override | + | @Override |
| - | public void onInitialize() { | + | public void onInitializeClient() { |
| - | ACID_LAKE = Registry.register(Registry.FEATURE, new Identifier(MOD_ID, "acid_lake" | + | FluidRenderHandlerRegistry.INSTANCE.register(TutorialMod.ACID_SOURCE, |
| - | + | Identifier.fromNamespaceAndPath(" | |
| - | // generate in swamps, similar to water lakes, but with a chance of 40 (the higher the number, the lower the generation chance) | + | Identifier.fromNamespaceAndPath(" |
| - | Biomes.SWAMP.addFeature( | + | Identifier.fromNamespaceAndPath(" |
| - | GenerationStep.Feature.LOCAL_MODIFICATIONS, | + | |
| - | ACID_LAKE.configure(new SingleStateFeatureConfig(ACID.getDefaultState())) | + | BlockRenderLayerMap.putFluids(ChunkSectionLayer.TRANSLUCENT, TutorialMod.ACID_SOURCE, |
| - | .createDecoratedFeature(Decorator.WATER_LAKE.configure(new ChanceDecoratorConfig(40))) | + | } |
| - | ); | + | |
| } | } | ||
| </ | </ | ||
| + | |||
| + | You can refer to vanilla' | ||
tutorial/fluids.1627476744.txt.gz · Last modified: 2021/07/28 12:52 by super