zh_cn: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 | ||
zh_cn:tutorial:fluids [2020/11/15 01:16] – [进行] solidblock | zh_cn:tutorial:fluids [2023/05/04 11:22] – [创建抽象流体] solidblock | ||
---|---|---|---|
Line 3: | Line 3: | ||
在这里,我们将介绍自定义流体的创建。如果计划创建多个流体,建议创建一个抽象的基本流体类,在其中设置必要的默认值,这些默认值将在其子类中共享。我们还将使其像湖泊一样在世界中生成。 | 在这里,我们将介绍自定义流体的创建。如果计划创建多个流体,建议创建一个抽象的基本流体类,在其中设置必要的默认值,这些默认值将在其子类中共享。我们还将使其像湖泊一样在世界中生成。 | ||
===== 创建抽象流体 ===== | ===== 创建抽象流体 ===== | ||
- | 原版流体扩展了'' | + | 原版流体继承了 '' |
- | <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 == getStill() || fluid == getFlowing(); | 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 | + | * 流体流入一个可替换的方块时的行为。 |
- | * the block' | + | * 水会掉落方块的战利品表。熔岩会播放“block.lava.extinguish”音效。 |
*/ | */ | ||
@Override | @Override | ||
- | protected void beforeBreakingBlock(WorldAccess | + | protected void method_15730(class_1936 |
- | { | + | |
final BlockEntity blockEntity = state.getBlock().hasBlockEntity() ? world.getBlockEntity(pos) : null; | final BlockEntity blockEntity = state.getBlock().hasBlockEntity() ? world.getBlockEntity(pos) : null; | ||
Block.dropStacks(state, | Block.dropStacks(state, | ||
Line 37: | Line 33: | ||
/** | /** | ||
- | * Lava returns true if its FluidState | + | * 熔岩在其 |
- | * Fluid is Water. | + | |
* | * | ||
- | * @return | + | * @return |
*/ | */ | ||
@Override | @Override | ||
- | protected boolean | + | protected boolean |
- | { | + | |
return false; | return false; | ||
} | } | ||
/** | /** | ||
- | * Possibly related to the distance checks for flowing into nearby holes? | + | * 或许与流入周围附近凹洞的距离检查有关? |
- | * Water returns | + | * 水返回4。熔岩在主世界返回2,而在下界返回4。 |
*/ | */ | ||
@Override | @Override | ||
- | protected int getFlowSpeed(WorldView | + | protected int method_15733(class_4538 |
- | { | + | |
return 4; | return 4; | ||
} | } | ||
/** | /** | ||
- | * Water returns | + | * 返回每次流动一格,其等级减少的数值。水返回1,熔岩在主世界返回2,在下界返回1。 |
*/ | */ | ||
@Override | @Override | ||
- | protected int getLevelDecreasePerBlock(WorldView | + | protected int method_15739(class_4538 |
- | { | + | |
return 1; | return 1; | ||
} | } | ||
/** | /** | ||
- | * Water returns | + | * 返回每流一格需要花费的时间(按刻计算)。水返回5。熔岩在主世界返回30,在下界返回10。 |
*/ | */ | ||
@Override | @Override | ||
- | public int getTickRate(WorldView | + | public int method_15789(class_4538 |
- | { | + | |
return 5; | return 5; | ||
} | } | ||
/** | /** | ||
- | * Water and Lava both return | + | * 返回爆炸抗性。水和熔岩都返回100.0F。 |
*/ | */ | ||
@Override | @Override | ||
- | protected float getBlastResistance() | + | protected float method_15784() { |
- | { | + | |
return 100.0F; | return 100.0F; | ||
} | } | ||
} | } | ||
- | </code> | + | </yarncode> |
===== 进行 ===== | ===== 进行 ===== | ||
Line 198: | Line 188: | ||
==== 制作一个流体方块 ==== | ==== 制作一个流体方块 ==== | ||
- | 接下来,我们需要创建一个代表世界酸的图块。 '' | + | 接下来,我们需要在世界中创建表示酸的方块。'' |
- | <code java> | + | <code java [enable_line_numbers=" |
- | public | + | // ... |
+ | |||
+ | public | ||
+ | |||
+ | // ... | ||
+ | |||
+ | @Override | ||
+ | public void onInitialize() | ||
{ | { | ||
- | public BaseFluidBlock(BaseFluid fluid, Settings settings) | + | // ... |
- | { | + | |
- | super(fluid, settings); | + | ACID = Registry.register(Registry.BLOCK, new Identifier(MOD_ID, |
- | } | + | |
+ | // ... | ||
} | } | ||
- | </ | ||
- | 现在创建一个静态块实例: | + | // ... |
- | <code java> | + | |
- | | + | |
- | + | ||
- | public static FluidBlock acid; | + | |
- | + | ||
- | @Override | + | |
- | public void onInitialize() | + | |
- | { | + | |
- | + | ||
- | ... | + | |
- | + | ||
- | acid = new BaseFluidBlock(stillAcid, | + | |
- | Registry.register(Registry.BLOCK, | + | |
- | } | + | |
</ | </ | ||
- | 现在,当我们有了这些静态对象时,我们返回** Acid **类并完成重写的方法: | ||
- | <code java> | ||
- | public abstract class Acid extends BasicFluid | ||
- | { | ||
- | @Override | ||
- | public Item getBucketItem() | ||
- | { | ||
- | return Mod.acidBucket; | ||
- | } | ||
- | | ||
- | @Override | ||
- | protected BlockState toBlockState(FluidState fluidState) | ||
- | { | ||
- | // | ||
- | return Mod.acid.getDefaultState().with(FluidBlock.LEVEL, | ||
- | } | ||
- | |||
- | @Override | ||
- | public Fluid getFlowing() | ||
- | { | ||
- | return Mod.flowingAcid; | ||
- | } | ||
- | | + | 既然我们有了这些静态对象,我们回到'' |
- | public Fluid getStill() | + | <code java [enable_line_numbers=" |
- | { | + | public abstract class AcidFluid extends TutorialFluid |
- | return | + | { |
- | } | + | @Override |
- | + | public Fluid getStill() | |
- | @Override | + | { |
- | public | + | return |
- | { | + | } |
- | return | + | |
- | } | + | @Override |
- | + | public Fluid getFlowing() | |
- | ... | + | { |
- | + | return | |
+ | } | ||
+ | |||
+ | @Override | ||
+ | public Item getBucketItem() | ||
+ | { | ||
+ | return TutorialMod.ACID_BUCKET; | ||
+ | } | ||
+ | |||
+ | @Override | ||
+ | protected BlockState toBlockState(FluidState fluidState) | ||
+ | { | ||
+ | // 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, | ||
+ | } | ||
+ | |||
+ | // ... | ||
} | } | ||
</ | </ | ||
+ | ===== 渲染设置 ===== | ||
+ | 为了让流体拥有纹理,或者与一个颜色绑定,你需要为其注册一个'' | ||
- | 现在我们可以断言Acid类已完成。 | + | <code java [enable_line_numbers=" |
- | ===== Rendering setup ===== | + | public class TutorialModClient implements ClientModInitializer |
+ | { | ||
+ | // ... | ||
+ | |||
+ | @Override | ||
+ | public void onInitializeClient() | ||
+ | { | ||
+ | // ... | ||
+ | |||
+ | setupFluidRendering(TutorialMod.STILL_ACID, | ||
+ | BlockRenderLayerMap.INSTANCE.putFluids(RenderLayer.getTranslucent(), | ||
+ | |||
+ | // ... | ||
+ | } | ||
+ | |||
+ | // ... | ||
+ | |||
+ | public static void setupFluidRendering(final Fluid still, final Fluid flowing, final Identifier textureFluidId, | ||
+ | { | ||
+ | final Identifier stillSpriteId | ||
+ | final Identifier flowingSpriteId | ||
+ | |||
+ | // If they' | ||
+ | ClientSpriteRegistryCallback.event(SpriteAtlasTexture.BLOCK_ATLAS_TEX).register((atlasTexture, | ||
+ | { | ||
+ | registry.register(stillSpriteId); | ||
+ | registry.register(flowingSpriteId); | ||
+ | }); | ||
+ | |||
+ | final Identifier fluidId | ||
+ | final Identifier listenerId | ||
+ | |||
+ | final Sprite[] fluidSprites | ||
+ | |||
+ | 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] | ||
+ | fluidSprites[1] | ||
+ | } | ||
+ | }); | ||
+ | |||
+ | // The FluidRenderer gets the sprites and color from a FluidRenderHandler during rendering | ||
+ | final FluidRenderHandler renderHandler | ||
+ | { | ||
+ | @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, | ||
+ | } | ||
+ | |||
+ | // ... | ||
+ | } | ||
+ | </ | ||
- | 是时候做客户端的事情了。 在** ClientModInitializer **中,您需要为流体指定精灵的位置并定义其渲染。 我将重复使用水纹理,只是更改应用于它们的颜色。 | + | 如果你需要使用你自己的流体纹理,你可以参考原版资源包(('' |
- | <code java> | + | |
- | @Override | + | |
- | public void onInitializeClient() | + | |
- | { | + | |
- | + | ||
- | | + | |
- | ClientSpriteRegistryCallback.event(SpriteAtlasTexture.BLOCK_ATLAS_TEX).register((spriteAtlasTexture, | + | |
- | + | ||
- | Identifier stillSpriteLocation = new Identifier(" | + | |
- | Identifier dynamicSpriteLocation = new Identifier(" | + | |
- | | + | |
- | FabricSprite stillAcidSprite = new FabricSprite(stillSpriteLocation, | + | |
- | | + | |
- | FabricSprite dynamicAcidSprite = new FabricSprite(dynamicSpriteLocation, | + | |
- | + | ||
- | 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/ | + | |
- | @Override | + | |
- | public Sprite[] getFluidSprites(ExtendedBlockView extendedBlockView, | + | |
- | { | + | |
- | return new Sprite[] {stillAcidSprite, | + | |
- | } | + | |
- | // apply light green color | + | ===== 在世界中生成 ===== |
- | | + | 为使得酸湖在世界中生成,你可以在你的'' |
- | public int getFluidColor(ExtendedBlockView view, BlockPos pos, FluidState state) | + | |
- | { | + | |
- | return 0x4cc248; | + | |
- | } | + | |
- | }; | + | |
- | | + | <code java [enable_line_numbers=" |
+ | // ... | ||
- | FluidRenderHandlerRegistry.INSTANCE.register(Mod.stillAcid, | + | public static LakeFeature ACID_LAKE; |
- | FluidRenderHandlerRegistry.INSTANCE.register(Mod.flowingAcid, | + | |
- | }); | + | |
- | </code> | + | // ... |
- | 然后剩下要做的就是创建必要的Json文件和纹理,但是您现在应该知道该怎么做。 | + | @Override |
+ | public void onInitialize() | ||
+ | { | ||
+ | // ... | ||
+ | |||
+ | ACID_LAKE = Registry.register(Registry.FEATURE, | ||
+ | |||
+ | // 在沼泽中生成,类似于水湖,但是概率为40(数字越高,生成几率越低) | ||
+ | Biomes.SWAMP.addFeature( | ||
+ | GenerationStep.Feature.LOCAL_MODIFICATIONS, | ||
+ | ACID_LAKE.configure(new SingleStateFeatureConfig(ACID.getDefaultState())) | ||
+ | .createDecoratedFeature(Decorator.WATER_LAKE.configure(new ChanceDecoratorConfig(40))) | ||
+ | ); | ||
+ | |||
+ | // ... | ||
+ | } | ||
- | ===== Generation in a world ===== | + | // ... |
- | + | ||
- | 要在世界上产生酸湖,可以使用在ModInitializer中创建的** net.minecraft.world.gen.feature.LakeFeature **: | + | |
- | <code java> | + | |
- | + | ||
- | LakeFeature acidFeature = Registry.register(Registry.FEATURE, | + | |
- | + | ||
- | </code> | + | |
- | 然后将其放入所需的生物群系中以生成: | + | |
- | <code java> | + | |
- | | + | |
- | Biomes.FOREST.addFeature(GenerationStep.Feature.LOCAL_MODIFICATIONS, | + | |
</ | </ | ||
- | 本教程到此结束。 | ||
zh_cn/tutorial/fluids.txt · Last modified: 2023/05/04 11:31 by solidblock