User Tools

Site Tools


zh_cn: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
zh_cn:tutorial:fluids [2019/12/19 10:19] lightcolourzh_cn:tutorial:fluids [2023/05/04 11:31] (current) – [渲染设置] solidblock
Line 1: Line 1:
 ====== 创建流体 ====== ====== 创建流体 ======
-===== Overview ===== +===== 概述 ===== 
-在这里,我们将介绍自定义流体的创建。 如果计划创建多个流体,建议创建一个抽象的基本流体类,在其中设置必要的默认值,这些默认值将在其子类中共享。 我们还将使其像湖泊一样在世界上产生。 +在这里,我们将介绍自定义流体的创建。如果计划创建多个流体,建议创建一个抽象的基本流体类,在其中设置必要的默认值,这些默认值将在其子类中共享。我们还将使其像湖泊一样在世界。 
-===== 使抽象流畅 ===== +===== 创建抽象流体 ===== 
-香草流体扩展**net.minecraft.fluid.BaseFluid**类,我们的抽象流体将扩展。 可能是这样的: +原版流体继承了 ''<yarn net.minecraft.class_3609>'',我们也应如此。 
-<code java> +<yarncode java [enable_line_numbers="true"]
-public abstract class BasicFluid extends BaseFluid +public abstract class TutorialFluid extends class_3609 
-+ /** 
-    /** +  * @return 给定的流体是否为该流体的实例? 
-     * @return does it produce infinite fluid (like water)? +  */ 
-     */ + @Override 
-    @Override + public boolean method_15780(class_3611 fluid) { 
-    protected boolean isInfinite() + return fluid == getStill() || fluid == getFlowing(); 
-    +
-        return false; +  
-    } + /** 
- +  * @return 流体是否可以像无限刷水的方法一样无限生成?在原版,这取决于游戏规则。 
-    // make it transparent +  */ 
-    @Override + @Override 
-    protected BlockRenderLayer getRenderLayer() + protected boolean method_15737() 
-    { + return false; 
-        return BlockRenderLayer.TRANSLUCENT+ } 
-    +  
- + /** 
-    /** +  流体流入一个可替换的方块时的行为。 
-     * +  水会掉落方块的战利品表。熔岩会播放“block.lava.extinguish”音效。 
-     * @return an associated item that "holds" this fluid +  */ 
-     */ + @Override 
-    @Override + protected void method_15730(class_1936 world, class_2338 pos, class_2680 state
-    public abstract Item getBucketItem(); + final BlockEntity blockEntity = state.getBlock().hasBlockEntity() ? world.getBlockEntity(pos) : null
- + Block.dropStacks(state, world, pos, blockEntity); 
-    /** + } 
-     +  
-     @return a blockstate of the associated {@linkplain net.minecraft.block.FluidBlock} with {@linkplain net.minecraft.block.FluidBlock#LEVEL} + /** 
-     */ +  * 熔岩在其 FluidState 高于指定的高度且该流体为水时返回 true。 
-    @Override +  *  
-    protected abstract BlockState toBlockState(FluidState var1); +  * @return 给定的流体能否流入它的 FluidState? 
- +  */ 
-    /** + @Override 
-     * + protected boolean method_15777(class_3610 fluidState, class_1922 blockView, class_2338 blockPos, class_3611 fluid, class_2350 direction) { 
-     * @return flowing static instance of this fluid + return false
-     */ +
-    @Override +  
-    public abstract Fluid getFlowing(); + /** 
- +  或许与流入周围附近凹洞的距离检查有关? 
-    /** +  水返回4。熔岩在主世界返回2,而在下界返回4。 
-     +  */ 
-     * @return still static instance of this fluid + @Override 
-     */ + protected int method_15733(class_4538 worldView) { 
-    @Override + return 4
-    public abstract Fluid getStill(); +
- +  
-    // how much does the height of the fluid block decreases + /** 
-    @Override +  * 返回每次流动一格,其等级减少的数值。水返回1,熔岩在主世界返回2,在下界返回1。 
-    protected int getLevelDecreasePerBlock(ViewableWorld world) +  *
-    + @Override 
-        return 1+ protected int method_15739(class_4538 worldView) { 
-    + return 1
- +
-    /** +  
-     *  + /** 
-     @return update rate + * 返回每流一格需要花费的时间(按刻计算)。水返回5。熔岩在主世界返回30,在下界返回10。 
-     */ + *
-    @Override + @Override 
-    public int getTickRate(ViewableWorld world) + public int method_15789(class_4538 worldView) { 
-    + return 5
-        return 5+
-    +  
- + /** 
-    @Override +  返回爆炸抗性。水和熔岩都返回100.0F。 
-    protected float getBlastResistance() +  */ 
-    { + @Override 
-        return 100; + protected float method_15784() 
-    } + return 100.0F
- + }
-    /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); +
 } }
-</code>+</yarncode>
  
-===== Implementation ===== +===== 实现 ===== 
-Now let's make an actual fluid; it will have a //still// and //flowing// variants; will name it "Acid":+现在让我们制作一个拥有静止和流动两个变种的实际流体。在此教程中,我们将其称为“酸”。缺失的引用稍后补全。
  
-<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; +
-        } + }
- +
-        /** +
-         * @return height of the fluid block +
-         */ +
-        @Override +
-        public int getLevel(FluidState fluidState) +
-        { +
-            return fluidState.get(LEVEL); +
-        } +
- +
-        @Override +
-        protected void appendProperties(StateFactory.Builder<Fluid, FluidState> stateFactoryBuilder) +
-        { +
-            super.appendProperties(stateFactoryBuilder); +
-            stateFactoryBuilder.add(LEVEL)+
-        +
-    }+
 } }
-</code+</yarncode
- +接下来,我们将制作静态和动态酸变体的静态实例,以及一个酸桶。在您的 ''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"]
- +public static class_3609 STILL_ACID
-<code java> +public static class_3609 FLOWING_ACID
- +public static class_1792 ACID_BUCKET
-     +  
-    public static Acid stillAcid+@Override 
-    public static Acid flowingAcid+public void onInitialize() { 
-     + STILL_ACID class_2378.method_10230(class_7923.field_41173, new class_2960("tutorial", "acid"), new AcidFluid.Still()); 
-    public static BucketItem acidBucket+ 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"),  
-    @Override +        new class_1755(STILL_ACID, new class_1792.class_1793().method_7896(class_1802.field_8550).method_7889(1))); 
-    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()); +// ... 
-         +</yarncode>
-        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.json" and write identifiers of your fluids in there: +为了使自定义流体表现得像水或熔岩,您必须将其添加到相应的流体标签中:对于水,制作 ''data/minecraft/tags/fluids/water.json'' 文件,并在其中写入流体 id: 
-<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 ==== 
-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: 
  
-<code java> +==== 制作一个流体方块 ==== 
-public class BaseFluidBlock extends FluidBlock +接下来,我们需要在世界中创建表示酸的方块。''<yarn net.minecraft.class_2404>'' 是我们需要使用的类,但由于其构造器受保护,我们不能直接构造它。一种解决方法是制作子类或者匿名子类。这里我们展示后一种方式。在您的 ''ModInitializer'' 中: 
-+<yarncode java [enable_line_numbers="true"]
-    public BaseFluidBlock(BaseFluid fluid, Settings settings) +public static class_2248 ACID;
-    { +
-        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("tutorial", "acid"), new class_2404(STILL_ACID, FabricBlockSettings.method_9630(class_2246.field_10382)){}); 
 +  
 + // ... 
 +}  
 +</yarncode>
  
-<code java> +既然我们有了这些静态对象,我们回到 ''AcidFluid'' 并补全被重写的方法: 
-    ... +<yarncode java [enable_line_numbers="true"]
-     +public abstract class AcidFluid extends TutorialFluid { 
-    public static FluidBlock acid;+ @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 将流体状态的 LEVEL_1_8 转换为流体方块使用的 LEVEL_15 
 + return TutorialMod.ACID.method_9564().method_11657(class_2741.field_12538, method_15741(fluidState)); 
 + }
  
-    @Override + public static class Flowing extends AcidFluid { 
-    public void onInitialize() + @Override 
-    + protected void method_15775(class_2689.class_2690<class_3611, class_3610> builder) { 
-     + super.method_15775(builder); 
-        ... + builder.method_11667(field_15900); 
-         + }
-        acid = new BaseFluidBlock(stillAcid, FabricBlockSettings.of(Material.WATER).dropsNothing().build()); +
-        Registry.register(Registry.BLOCK, new Identifier(MODID, "acid_block"), acid); +
-        +
-</code>+
  
-Now when we have these static objects, we go back to **Acid** class and complete the overridden methods:+ @Override 
 + public int method_15779(class_3610 fluidState) { 
 + return fluidState.method_11654(field_15900); 
 + }
  
-<code java> + @Override 
-public abstract class Acid extends BasicFluid + public boolean method_15793(class_3610 fluidState) { 
-+ return false
-    @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 + public static class Still extends AcidFluid { 
-    public Fluid getFlowing() + @Override 
-    + public int method_15779(class_3610 fluidState) { 
-        return Mod.flowingAcid+ return 8
-    }+ }
  
-    @Override + @Override 
-    public Fluid getStill() + public boolean method_15793(class_3610 fluidState) { 
-    + return true
-        return Mod.stillAcid+ } 
-    }+
 +}   
 +</yarncode> 
 +===== 渲染设置 ===== 
 +为了让流体拥有纹理,或者与一个颜色绑定,你需要为其注册一个''FluidRenderHandler''。这里,我们重用水的纹理,并仅仅改变用于其上的颜色。为确保纹理渲染为半透明的,你可以使用 Fabric 的 ''BlockRenderLayerMap''(参见 [[blockappearance]])。
  
-    @Override +<yarncode java [enable_line_numbers="true"]> 
-    public boolean matchesType(Fluid fluid_1+@Environment(EnvType.CLIENT
-    { +public class TutorialModClient implements ClientModInitializer {
-        return fluid_1==Mod.flowingAcid || fluid_1==Mod.stillAcid; +
-    } +
-     +
-    ... +
-     +
-}     +
-</code>+
  
-Now we can assert that the Acid class is complete.+ @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 
 + ));
  
-===== Rendering setup =====+ BlockRenderLayerMap.INSTANCE.putFluids(class_1921.method_23583(), TutorialMod.STILL_ACID, TutorialMod.FLOWING_ACID);
  
-Time to do client-side things. In your **ClientModInitializer** you need to specify locations of sprites for your fluids and define their renderingI will reuse water textures and just change the color applied to them.+ //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")); 
 + //});
  
-<code java> + // ... 
-    @Override + } 
-    public void onInitializeClient() +} 
-    { +</yarncode>
-         +
-        // adding the sprites to the block texture atlas +
-        ClientSpriteRegistryCallback.event(SpriteAtlasTexture.BLOCK_ATLAS_TEX).register((spriteAtlasTexture, registry) -> { +
-         +
-            Identifier stillSpriteLocation = new Identifier("block/water_still"); +
-            Identifier dynamicSpriteLocation = new Identifier("block/water_flow"); +
-            // here I tell to use only 16x16 area of the water texture +
-            FabricSprite stillAcidSprite = new FabricSprite(stillSpriteLocation, 16, 16); +
-            // same, but 32 +
-            FabricSprite dynamicAcidSprite = new FabricSprite(dynamicSpriteLocation, 32, 32); +
-         +
-            registry.register(stillAcidSprite); +
-            registry.register(dynamicAcidSprite); +
-             +
-             +
-            // this renderer is responsible for drawing fluids in a world +
-            FluidRenderHandler acidRenderHandler = new FluidRenderHandler() +
-            { +
-                // return the sprites: still sprite goes first into the array, flowing/dynamic goes last +
-                @Override +
-                public Sprite[] getFluidSprites(ExtendedBlockView extendedBlockView, BlockPos blockPos, FluidState fluidState) +
-                { +
-                    return new Sprite[] {stillAcidSprite, dynamicAcidSprite}; +
-                }+
  
-                // apply light green color +如果你需要使用你自己的流体纹理,你可以参考原版资源包((''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''))作为一个模板。
-                @Override +
-                public int getFluidColor(ExtendedBlockView view, BlockPos pos, FluidState state) +
-                { +
-                    return 0x4cc248; +
-                } +
-            };+
  
-            // registering the same renderer for both fluid variants is intentional+===== 在世界中生成 ===== 
 +为使得酸湖在世界中生成,你可以在你的''ModInitializer''中创建一个''net.minecraft.world.gen.feature.LakeFeature'',然后将其添加到你需要让它生成的生物群系中:
  
-            FluidRenderHandlerRegistry.INSTANCE.register(Mod.stillAcid, acidRenderHandler); +<code java [enable_line_numbers="true"]> 
-            FluidRenderHandlerRegistry.INSTANCE.register(Mod.flowingAcid, acidRenderHandler); +// ...
-        });+
  
-</code>+public static LakeFeature ACID_LAKE;
  
-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 =====+@Override 
 +public void onInitialize() 
 +
 + // ... 
 +  
 + ACID_LAKE Registry.register(Registry.FEATURE, new Identifier(MOD_ID, "acid_lake"), new LakeFeature(SingleStateFeatureConfig::deserialize)); 
 +  
 + // 在沼泽中生成,类似于水湖,但是概率为40(数字越高,生成几率越低) 
 + Biomes.SWAMP.addFeature( 
 + GenerationStep.Feature.LOCAL_MODIFICATIONS, 
 + ACID_LAKE.configure(new SingleStateFeatureConfig(ACID.getDefaultState())) 
 + .createDecoratedFeature(Decorator.WATER_LAKE.configure(new ChanceDecoratorConfig(40))) 
 + ); 
 +  
 + // ... 
 +}
  
-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()))); +
- +
-</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> </code>
-This is the end of the tutorial. 
  
zh_cn/tutorial/fluids.1576750765.txt.gz · Last modified: 2019/12/19 10:19 by lightcolour