User Tools

Site Tools


zh_cn:tutorial:features

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Next revision
Previous revision
zh_cn:tutorial:features [2020/01/08 12:18] – created lightcolourzh_cn:tutorial:features [2022/08/18 03:37] (current) solidblock
Line 1: Line 1:
-===== 在您的世界中生成Feature ===== +FIXME 本页有一段时间没有更新了可能未来版不可用请参考[[tutorial:features|英文页面]]进行更新。
-岩石树木,矿石和池塘都是Feature的示例。 它们是对世界的简单补充,它们的生成取决于它们的配置方式。 在本教程中,我们将研究如何在我们的世界中随机生成一个简单的石螺旋Feature +
-==== 创建Feature类 ==== +
-一个简单的Feature如下所示: +
-<code java [enable_line_numbers="true"]+
-public class StoneSpiralFeature extends Feature<DefaultFeatureConfig> {+
  
-    public StoneSpiralFeature(Function<Dynamic<?>, ? extends DefaultFeatureConfig> config) { +===== 添加地物 [1.17] =====
-        super(config); +
-    }+
  
-    @Override +岩石、树木、矿石、池塘都是地物的例子,是对世界的简单补充生成,并根据配置的方式生成。本教程中,我们将研究如何随机生成简单的石头螺旋地物。
-    public boolean generate(IWorld world, ChunkGenerator<? extends ChunkGeneratorConfig> chunkGenerator, Random random, BlockPos pos, DefaultFeatureConfig config) { +
-        BlockPos topPos = world.getTopPosition(Heightmap.Type.WORLD_SURFACE, pos); +
-        Direction offset = Direction.NORTH;+
  
-        for (int y = 1; y < 16; y++) { +往生物群系中地物特征需要3个步骤。 
-            offset = offset.rotateYClockwise(); +  * 创建地物 
-            world.setBlockState(topPos.up(y).offset(offset), Blocks.STONE.getDefaultState(), 3); +  * 配置地物 
-        }+  * 使用 [[https://github.com/FabricMC/fabric/pull/1097|Fabric API 中的 Biome Modification API]] 以往生物群系中添加地物。
  
-        return true;+注意 Biome Modification API 标记为实验性。如果 API 不起作用,考虑使用[[?rev=1599388928|mixin版本]]。 
 + 
 +==== 创建地物 ==== 
 +一个简单的地物如下所示: 
 +<code java> 
 +public class StoneSpiralFeature extends Feature<DefaultFeatureConfig>
 +  public StoneSpiralFeature(Codec<DefaultFeatureConfig> configCodec) { 
 +    super(configCodec); 
 +  } 
 +  
 +  @Override 
 +  public boolean generate(FeatureContext<DefaultFeatureConfig> context) { 
 +    BlockPos topPos = context.getWorld().getTopPosition(Heightmap.Type.OCEAN_FLOOR_WG, context.getOrigin()); 
 +    Direction offset = Direction.NORTH; 
 +  
 +    for (int y = 0; y < 15; y++) { 
 +      offset = offset.rotateYClockwise(); 
 +      context.getWorld().setBlockState(topPos.up(y).offset(offset), Blocks.STONE.getDefaultState(), 3);
     }     }
 + 
 +    return true;
 +  }
 } }
 </code> </code>
  
-构造函数采用''Function<Dynamic<? extends DefaultFeatureConfig>>'',这是数据修复程序配置实例的工厂。 您可以直接在超级调用中或在实例化功能时为默认配置功能传递''DefaultFeatureConfig :: deserialize'' 
  
-当块决定生成Feature时将调用``generate``。 如果将功能配为产生每块,则也会为正在生成个块调用此功能。 在将功能配置为以每个生物群落以一定速率生成的情况下,仅在世界想要生成结构情况下才调用``generate``+在我们的实现中我们从世界最高位的方块构建一简单15岩石螺旋
  
-我们实现中,我们将从世界的最高位开始构建简单的16块高的石头螺旋+''Feature<DefaultFeatureConfig>'' 构造器需要一个 ''Codec<DefaultFeatureConfig>''。你可以通过直接构造器的 super 调用或者在实例化地物时传入 ''DefaultFeatureConfig.CODEC''。 
 + 
 +区块决定生成地物时会调用 ''generate''。如果地物配置为在每个区块生成则每个区块被生成时都会调用一次。如果地物被配置为在每个生物群系以特定的概率生成,''generate'' 只会在世界需要生成结构实例中调用。 
 + 
 +我们的地物目前使用 ''DefaultFeatureConfig'',这意味着暂时还不能配。但是,你般都应该尝试让你的地物可以配置,以允许我们为不同的东西使用它,同时也可以在需要时通过数据包去改变。我们的地物的简单的配置如下所示
  
 <code java> <code java>
-@Override +public record SpiralFeatureConfig(IntProvider heightBlockStateProvider blockimplements FeatureConfig 
-public boolean generate(IWorld worldChunkGenerator<? extends ChunkGeneratorConfig> chunkGenerator, Random random, BlockPos pos, DefaultFeatureConfig config) { +  public static final Codec<SpiralFeatureConfig> CODEC RecordCodecBuilder.create(instance -> instance.group( 
-    BlockPos topPos world.getTopPosition(Heightmap.Type.WORLD_SURFACE, pos); +    IntProvider.VALUE_CODEC.fieldOf("height").forGetter(SpiralFeatureConfig::height), 
-    Direction offset = Direction.NORTH;+    BlockStateProvider.TYPE_CODEC.fieldOf("block").forGetter(SpiralFeatureConfig::block) 
 +  ).apply(instance, instance.stable(SpiralFeatureConfig::new))); 
 +
 +</code>
  
-    for (int y = 1; y < 16; y++) { +注意我们为高度使用了 ''IntProvider'',并为方块使用了 ''BlockStateProvider'',而不是直接使用一个 ''int'' 或 ''BlockState''。这是因为前者更加强大。比如说,你可以配置地物使用 ''UniformIntProvider'',这样其高度可以变化。
-        offset = offset.rotateYClockwise(); +
-        world.setBlockState(topPos.up(y).offset(offset), Blocks.STONE.getDefaultState(), 3); +
-    }+
  
 +现在,让我们的地物使用 ''SpiralFeatureConfig''(类名也改变了,因为地物现在不总是替换石头):
 +<code java>
 +public class SpiralFeature extends Feature<SpiralFeatureConfig> {
 +  public SpiralFeature(Codec<SpiralFeatureConfig> configCodec) {
 +    super(configCodec);
 +  }
 + 
 +  @Override
 +  public boolean generate(FeatureContext<SpiralFeatureConfig> context) {
 +    BlockPos pos = context.getOrigin();
 +    SpiralFeatureConfig config = context.getConfig();
 + 
 +    Direction offset = Direction.NORTH;
 +    int height = config.height().get(context.getRandom());
 + 
 +    for (int y = 0; y < height; y++) {
 +      offset = offset.rotateYClockwise();
 +      BlockPos blockPos = pos.up(y).offset(offset);
 + 
 +      context.getWorld().setBlockState(blockPos, config.block().getBlockState(context.getRandom(), blockPos), 3);
 +    }
 + 
     return true;     return true;
 +  }
 } }
 </code> </code>
- +游戏许多其他内容一样,地物也可以注册,也没有你需要担心的特别地构建器或机制。
-==== 注册一个Feature ==== +
-可以像注册游戏的其他大多数内容一样注册Feature您不必担心任何特殊的构建器或机制。+
 <code java> <code java>
-private static final Feature<DefaultFeatureConfigLAVA_HOLE = Registry.register( +public class ExampleMod implements ModInitializer { 
- Registry.FEATURE, +  private static final Feature<SpiralFeatureConfigSPIRAL new SpiralFeature(SpiralFeatureConfig.CODEC); 
- new Identifier("tutorial", "stone_spiral"), +  
- new StoneSpiralFeature(DefaultFeatureConfig::deserialize) +  @Override 
-);+  public void onInitialize() { 
 +    Registry.register(Registry.FEATURE, new Identifier("tutorial", "spiral"), SPIRAL); 
 +  } 
 +}
 </code> </code>
  
-==== 向生物群落添加Feature ==== 
-生物群系有一种称为''addFeature''的方法,用于将Feature添加到生物群落的生成过程中。 您可以在每个生物群落类(例如''ForestBiome''或''SavannaBiome'')中查看此方法的更详细用法。 
  
-我们可以遍历''Registry.BIOME''我们的Feature添加到每个生物群系+==== 配置地物 ==== 
 +地物添加到生物群系之前,我们需要为地物提供配置。确保已配置的地物像地物一样都已经注册 
 <code java> <code java>
-Registry.BIOME.forEach(biome -> biome.addFeature+public class ExampleMod implements ModInitializer { 
-        GenerationStep.Feature.RAW_GENERATION, +  public static final ConfiguredFeature<?, ?> STONE_SPIRAL = SPIRAL.configure(new SpiralFeatureConfig(ConstantIntProvider.create(15), new SimpleBlockStateProvider(Blocks.STONE.getDefaultState()))) 
- Biome.configureFeature+      .decorate(Decorator.HEIGHTMAP.configure(new HeightmapDecoratorConfig(Heightmap.Type.OCEAN_FLOOR_WG))) 
- LAVA_HOLE, +      .spreadHorizontally() 
- new DefaultFeatureConfig(), +      .applyChance(5); 
- Decorator.CHANCE_HEIGHTMAP+  
- new ChanceDecoratorConfig(100) +  @Override 
- +  public void onInitialize() { 
-));+    [...] 
 +  
 +    RegistryKey<ConfiguredFeature<?, ?>> stoneSpiral = RegistryKey.of(Registry.CONFIGURED_FEATURE_KEY
 +        new Identifier("tutorial", "stone_spiral")); 
 +    Registry.register(BuiltinRegistries.CONFIGURED_FEATURE, stoneSpiral.getValue(), STONE_SPIRAL); 
 +  } 
 +}
 </code> </code>
  
-''addFeature''第一参数有助于确定何时生成结构。 对于地上房屋,您可以使用''SURFACE_STRUCTURES''对于洞穴,您可以使用'' RAW_GENERATION''+注意,螺旋高度(15 方块)和需要放置方块(石头)现在已经可以配置。 
 + 
 +装饰器(由 ''decorate'' 添加或者像 ''spreadHorizontally'' 和 ''applyChange'' 那样的辅助方法)负责如何放置以及何处放置你的地物要选择正确的装饰器,检查原版的类似地物以及你自己的。 
 + 
 +==== 将地物添加到生物群系 ==== 
 +我们使用 Biome Modification API。 
 + 
 +<code java> 
 +public class ExampleMod implements ModInitializer { 
 +  [...] 
 + 
 +  @Override 
 +  public void onInitialize() { 
 +    [...] 
 +    BiomeModifications.addFeature(BiomeSelectors.all(), GenerationStep.Feature.UNDERGROUND_ORES, stoneSpiral); 
 +  } 
 +
 +</code>
  
-''addFeature''的第个参数是ConfiguredFeature,可以通过''Biome.configureFeature''创建。 后者接收功能的实例,功能的config类,装饰器和装饰器配置的实例+''addFeature'' 的第个参数确定结构生成在什么生物群系中
  
-装饰器代表世界如选择放置要素。 ''CHANCE_HEIGHTMAP''通过查看高度图来工作,而''NOISE_HEIGHTMAP_32''可使用噪声来工作。 要选择正确的装饰器请签出与您自己风格相似的香草功能。 装饰器配置将分支此分支; 对于''CHANCE_HEIGHTMAP'',您将传入''ChanceHeightmapDecorator的实例+第二个参数帮助你确定结构时生成对于地上的房子,以用 ''SURFACE_STRUCTURES'',对于洞穴,可以用 ''RAW_GENERATION''
  
 === 结果 === === 结果 ===
 {{https://i.imgur.com/Kr59o0B.png}} {{https://i.imgur.com/Kr59o0B.png}}
zh_cn/tutorial/features.1578485898.txt.gz · Last modified: 2020/01/08 12:18 by lightcolour