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) ) ));
addFeature
的第一个参数有助于确定何时生成结构。 对于地上的房屋,您可以使用SURFACE_STRUCTURES
,对于洞穴,您可以使用 RAW_GENERATION
。
addFeature
的第二个参数是ConfiguredFeature,可以通过Biome.configureFeature
创建。 后者接收功能的实例,功能的config类,装饰器和装饰器配置的实例。
装饰器代表世界如何选择放置要素。 CHANCE_HEIGHTMAP
可通过查看高度图来工作,而NOISE_HEIGHTMAP_32
可使用噪声来工作。 要选择正确的装饰器,请签出与您自己风格相似的香草功能。 装饰器配置将分支此分支; 对于CHANCE_HEIGHTMAP
,您将传入''ChanceHeightmapDecorator的实例。