tutorial:fluids
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revisionNext revisionBoth sides next revision | ||
tutorial:fluids [2020/02/11 12:07] – Registry#add() -> Registry.register() upcraftlp | tutorial:fluids [2023/05/04 11:25] – [Implementation] use tutorial namespace solidblock | ||
---|---|---|---|
Line 5: | Line 5: | ||
===== Making an abstract fluid ===== | ===== Making an abstract fluid ===== | ||
- | Vanilla fluids extend '' | + | Vanilla fluids extend '' |
- | <code java [enable_line_numbers=" | + | <yarncode |
- | public abstract class TutorialFluid extends | + | public abstract class TutorialFluid extends |
- | { | + | |
/** | /** | ||
- | * @return | + | * @return |
*/ | */ | ||
@Override | @Override | ||
- | public boolean | + | public boolean |
- | { | + | return fluid == method_15751() || fluid == method_15750(); |
- | return fluid == getStill() || fluid == getFlowing(); | + | |
} | } | ||
- | + | ||
/** | /** | ||
- | * @return | + | * @return |
*/ | */ | ||
@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(IWorld | + | protected void method_15730(class_1936 |
- | { | + | final |
- | final | + | class_2248.method_9610(state, world, pos, blockEntity); |
- | Block.dropStacks(state, world.getWorld(), pos, blockEntity); | + | |
} | } | ||
- | + | ||
/** | /** | ||
- | * Lava returns true if its FluidState is above a certain height and the | + | * Lava returns true if it' |
* Fluid is Water. | * Fluid is Water. | ||
* | * | ||
- | * @return | + | * @return |
*/ | */ | ||
@Override | @Override | ||
- | protected boolean method_15777(FluidState | + | protected boolean method_15777(class_3610 |
- | { | + | |
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 55: | Line 50: | ||
*/ | */ | ||
@Override | @Override | ||
- | protected int method_15733(WorldView | + | protected int method_15733(class_4538 |
- | { | + | |
return 4; | return 4; | ||
} | } | ||
- | + | ||
/** | /** | ||
- | * Water returns 1. Lava returns | + | * Water returns 1. Lava returns |
*/ | */ | ||
@Override | @Override | ||
- | protected int getLevelDecreasePerBlock(WorldView | + | protected int method_15739(class_4538 |
- | { | + | |
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. | ||
*/ | */ | ||
@Override | @Override | ||
- | public int getTickRate(WorldView | + | public int method_15789(class_4538 |
- | { | + | |
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 method_15784() { |
- | { | + | |
return 100.0F; | return 100.0F; | ||
} | } | ||
} | } | ||
- | </code> | + | </yarncode> |
===== Implementation ===== | ===== Implementation ===== | ||
- | Now let's make an actual fluid which' | + | 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 [enable_line_numbers=" | + | <yarncode |
- | public abstract class AcidFluid extends TutorialFluid | + | public abstract class AcidFluid extends TutorialFluid { |
- | { | + | |
@Override | @Override | ||
- | public | + | public |
- | { | + | return YOUR_STILL_FLUID_HERE; |
- | return | + | |
} | } | ||
- | + | ||
@Override | @Override | ||
- | public | + | public |
- | { | + | return YOUR_FLOWING_FLUID_HERE; |
- | return | + | |
} | } | ||
- | + | ||
@Override | @Override | ||
- | public | + | public |
- | { | + | return YOUR_BUCKET_ITEM_HERE; |
- | return | + | |
} | } | ||
- | + | ||
@Override | @Override | ||
- | protected | + | protected |
- | { | + | return YOUR_FLUID_BLOCK_HERE.method_9564().method_11657(class_2741.field_12538, method_15741(fluidState)); |
- | // method_15741 converts the LEVEL_1_8 of the fluid state to the LEVEL_15 the fluid block uses | + | |
- | return | + | |
} | } | ||
- | + | ||
- | public static class Flowing extends AcidFluid | + | public static class Flowing extends AcidFluid { |
- | { | + | |
@Override | @Override | ||
- | protected void appendProperties(StateManager.Builder<Fluid, FluidState> builder) | + | protected void method_15775(class_2689.class_2690<class_3611, class_3610> builder) { |
- | { | + | super.method_15775(builder); |
- | super.appendProperties(builder); | + | builder.method_11667(field_15900); |
- | builder.add(LEVEL); | + | |
} | } | ||
- | + | ||
@Override | @Override | ||
- | public int getLevel(FluidState | + | public int method_15779(class_3610 |
- | { | + | return fluidState.method_11654(field_15900); |
- | return fluidState.get(LEVEL); | + | |
} | } | ||
- | + | ||
@Override | @Override | ||
- | public boolean | + | public boolean |
- | { | + | |
return false; | return false; | ||
} | } | ||
} | } | ||
- | + | ||
- | public static class Still extends AcidFluid | + | public static class Still extends AcidFluid { |
- | { | + | |
@Override | @Override | ||
- | public int getLevel(FluidState | + | public int method_15779(class_3610 |
- | { | + | |
return 8; | return 8; | ||
} | } | ||
- | + | ||
@Override | @Override | ||
- | public boolean | + | public boolean |
- | { | + | |
return true; | return true; | ||
} | } | ||
} | } | ||
} | } | ||
- | </code> | + | </yarncode> |
Next, we'll make static instances of still and flowing acid variants, and an acid bucket. In your '' | Next, we'll make static instances of still and flowing acid variants, and an acid bucket. In your '' | ||
- | <code java [enable_line_numbers=" | + | <yarncode |
- | // ... | + | public static |
- | + | public static | |
- | public static | + | public static |
- | public static | + | |
- | + | ||
- | public static | + | |
- | + | ||
- | // ... | + | |
@Override | @Override | ||
- | public void onInitialize() | + | public void onInitialize() { |
- | { | + | STILL_ACID = class_2378.method_10230(class_7923.field_41173, new class_2960(" |
- | // ... | + | FLOWING_ACID = class_2378.method_10230(class_7923.field_41173, new class_2960(" |
- | + | ACID_BUCKET = class_2378.method_10230(class_7923.field_41178, new class_2960(" | |
- | STILL_ACID = Registry.register(Registry.FLUID, new Identifier(MOD_ID, " | + | |
- | + | ||
- | FLOWING_ACID = Registry.register(Registry.FLUID, new Identifier(MOD_ID, " | + | |
- | + | ||
- | ACID_BUCKET = Registry.register(Registry.ITEM, new Identifier(MOD_ID, " | + | |
- | + | ||
// ... | // ... | ||
} | } | ||
+ | |||
// ... | // ... | ||
- | </code> | + | </yarncode> |
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 '' | ||
Line 194: | Line 163: | ||
" | " | ||
[ | [ | ||
- | "your_mod_id: | + | "tutorial: |
- | "your_mod_id: | + | "tutorial: |
] | ] | ||
} | } | ||
Line 201: | Line 170: | ||
===== 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=" | + | <yarncode |
- | // ... | + | public static |
- | + | ||
- | public static | + | |
- | + | ||
- | // ... | + | |
@Override | @Override | ||
- | public void onInitialize() | + | public void onInitialize() { |
- | { | + | ACID = class_2378.method_10230(class_7923.field_41175, new class_2960(MOD_ID, " |
- | // ... | + | |
- | + | ||
- | ACID = Registry.register(Registry.BLOCK, new Identifier(MOD_ID, " | + | |
// ... | // ... | ||
- | } | + | } |
- | + | </yarncode> | |
- | // ... | + | |
- | </code> | + | |
Now that we have these static objects, we can go back to '' | Now that we have these static objects, we can go back to '' | ||
- | <code java [enable_line_numbers=" | + | <yarncode |
- | public abstract class AcidFluid extends TutorialFluid | + | public abstract class AcidFluid extends TutorialFluid { |
- | { | + | |
@Override | @Override | ||
- | public | + | public |
- | { | + | |
return TutorialMod.STILL_ACID; | return TutorialMod.STILL_ACID; | ||
} | } | ||
- | + | ||
@Override | @Override | ||
- | public | + | public |
- | { | + | |
return TutorialMod.FLOWING_ACID; | return TutorialMod.FLOWING_ACID; | ||
} | } | ||
- | + | ||
@Override | @Override | ||
- | public | + | public |
- | { | + | |
return TutorialMod.ACID_BUCKET; | return TutorialMod.ACID_BUCKET; | ||
} | } | ||
- | + | ||
@Override | @Override | ||
- | protected | + | protected |
- | { | + | |
// method_15741 converts the LEVEL_1_8 of the fluid state to the LEVEL_15 the fluid block uses | // method_15741 converts the LEVEL_1_8 of the fluid state to the LEVEL_15 the fluid block uses | ||
- | return TutorialMod.ACID.getDefaultState().with(Properties.LEVEL_15, method_15741(fluidState)); | + | return TutorialMod.ACID.method_9564().method_11657(class_2741.field_12538, method_15741(fluidState)); |
} | } | ||
- | + | ||
- | // ... | + | public static class Flowing extends AcidFluid { |
- | } | + | @Override |
- | </code> | + | protected void method_15775(class_2689.class_2690< |
+ | 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; | ||
+ | } | ||
+ | } | ||
+ | } | ||
+ | </yarncode> | ||
===== 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=" | + | <yarncode |
- | public class TutorialModClient implements ClientModInitializer | + | public class TutorialModClient implements ClientModInitializer { |
- | { | + | |
- | // ... | + | |
- | + | ||
@Override | @Override | ||
- | public void onInitializeClient() | + | public void onInitializeClient() { |
- | { | + | FluidRenderHandlerRegistry.INSTANCE.register(TutorialMod.STILL_ACID, |
- | // ... | + | new class_2960(" |
- | + | new class_2960("minecraft: | |
- | setupFluidRendering(TutorialMod.STILL_ACID, | + | 0x4CC248 |
- | BlockRenderLayerMap.INSTANCE.putFluids(RenderLayer.getTranslucent(), TutorialMod.STILL_ACID, | + | )); |
- | + | ||
+ | BlockRenderLayerMap.INSTANCE.putFluids(class_1921.method_23583(), TutorialMod.STILL_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. | ||
+ | // | ||
+ | // registry.register(new Identifier(" | ||
+ | // registry.register(new Identifier(" | ||
+ | //}); | ||
// ... | // ... | ||
} | } | ||
- | |||
- | // ... | ||
- | |||
- | 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_TEX).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 = new Sprite[] { 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, | ||
- | } | ||
- | |||
- | // ... | ||
} | } | ||
- | </code> | + | </yarncode> |
If you want to use your own fluid textures, you can refer to vanilla' | If you want to use your own fluid textures, you can refer to vanilla' | ||
===== Generation in the world ===== | ===== Generation in the world ===== | ||
- | To make lakes of acid generate in the world, you can create a '' | + | TODO Update |
- | + | ||
- | <code java [enable_line_numbers=" | + | |
- | // ... | + | |
- | + | ||
- | public static LakeFeature ACID_LAKE; | + | |
- | + | ||
- | // ... | + | |
- | + | ||
- | @Override | + | |
- | public void onInitialize() | + | |
- | { | + | |
- | // ... | + | |
- | + | ||
- | ACID_LAKE = Registry.register(Registry.FEATURE, | + | |
- | + | ||
- | // 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))) | + | |
- | ); | + | |
- | + | ||
- | // ... | + | |
- | } | + | |
- | + | ||
- | // ... | + | |
</ | </ | ||
- |
tutorial/fluids.txt · Last modified: 2023/05/04 11:31 by solidblock