This is an old revision of the document!
Table of Contents
在您的世界中生成Feature
岩石,树木,矿石和池塘都是Feature的示例。 它们是对世界的简单补充,它们的生成取决于它们的配置方式。 在本教程中,我们将研究如何在我们的世界中随机生成一个简单的石螺旋Feature。
创建Feature类
一个简单的Feature如下所示:
public class StoneSpiralFeature extends Feature<DefaultFeatureConfig> { public StoneSpiralFeature(Function<Dynamic<?>, ? extends DefaultFeatureConfig> config) { 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++) { offset = offset.rotateYClockwise(); world.setBlockState(topPos.up(y).offset(offset), Blocks.STONE.getDefaultState(), 3); } return true; } }
构造函数采用Function<Dynamic<? extends DefaultFeatureConfig»
,这是数据修复程序配置实例的工厂。 您可以直接在超级调用中或在实例化功能时为默认配置功能传递DefaultFeatureConfig :: deserialize
。
当块决定生成Feature时,将调用``generate``。 如果将功能配置为产生每个块,则也会为正在生成的每个块调用此功能。 在将功能配置为以每个生物群落以一定速率生成的情况下,仅在世界想要生成结构的情况下才调用``generate``。
在我们的实现中,我们将从世界的最高位置开始构建一个简单的16块高的石头螺旋:
@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++) { offset = offset.rotateYClockwise(); world.setBlockState(topPos.up(y).offset(offset), Blocks.STONE.getDefaultState(), 3); } return true; }
注册一个Feature
可以像注册游戏中的其他大多数内容一样注册Feature,而且您不必担心任何特殊的构建器或机制。
private static final Feature<DefaultFeatureConfig> LAVA_HOLE = Registry.register( Registry.FEATURE, new Identifier("tutorial", "stone_spiral"), new StoneSpiralFeature(DefaultFeatureConfig::deserialize) );
向生物群落添加Feature
生物群系有一种称为addFeature
的方法,用于将Feature添加到生物群落的生成过程中。 您可以在每个生物群落类(例如ForestBiome
或SavannaBiome
)中查看此方法的更详细用法。
我们可以遍历Registry.BIOME
以将我们的Feature添加到每个生物群系中。
Registry.BIOME.forEach(biome -> biome.addFeature( GenerationStep.Feature.RAW_GENERATION, Biome.configureFeature( LAVA_HOLE, new DefaultFeatureConfig(), Decorator.CHANCE_HEIGHTMAP, new ChanceDecoratorConfig(100) ) ));
The first argument of addFeature
helps determine when the structure is generated. For above-ground houses you may go with SURFACE_STRUCTURES
, and for caves, you might go with RAW_GENERATION
.
The second argument of addFeature
is a ConfiguredFeature, which you can create through Biome.configureFeature
. The latter takes in an instance of your feature, an instance of your feature's config class, a decorator, and a decorator config.
The Decorator represents how the world chooses to place your Feature. CHANCE_HEIGHTMAP
works by looking at a heightmap, whereas NOISE_HEIGHTMAP_32
works with noise. To choose the correct Decorator, check out vanilla Features with a similar style to your own. The decorator config branches off this; in the case of CHANCE_HEIGHTMAP
, you would pass in an instance of ChanceHeightmapDecorator
.