User Tools

Site Tools


zh_cn:tutorial:features

This is an old revision of the document!


添加地形特征 [1.17]

岩石、树木、矿石、池塘都是地形特征的例子,是对世界的简单补充生成,并根据配置的方式生成。本教程中,我们将研究如何随机生成简单的石头螺旋地形。

往生物群系中添加地形特征需要3个步骤。

注意生物群系修改API标记为实验性。如果API不起作用,考虑使用mixin版本

创建地形特征

一个简单的地形特征如下所示:

public class StoneSpiralFeature extends Feature<DefaultFeatureConfig> {
  public StoneSpiralFeature(Codec<DefaultFeatureConfig> config) {
    super(config);
  }
 
  @Override
  public boolean generate(StructureWorldAccess world, ChunkGenerator generator, Random random, BlockPos pos,
      DefaultFeatureConfig config) {
    BlockPos topPos = world.getTopPosition(Heightmap.Type.WORLD_SURFACE, pos);
    Direction offset = Direction.NORTH;
 
    for (int y = 1; y <= 15; y++) {
      offset = offset.rotateYClockwise();
      world.setBlockState(topPos.up(y).offset(offset), Blocks.STONE.getDefaultState(), 3);
    }
 
    return true;
  }
}

Feature<DefaultFeatureConfig>构造器采用Codec<DefaultFeatureConfig>。你可以直接在构造器的超级调用中或者实例化特性时为默认的配置特性传入DefaultFeatureConfig.CODEC

区块决定生成地形特征时调用generate。如果地形特征配置为在每个区块生成,则每个区块被生成时都会调用一次。在特征被配置为以每个生物群系特定的概率生成的情况下,generate只会在世界想要生成结构的实例中调用。

在我们的实现中,我们从世界最高位置的方块构建一个简单的15个方块高的岩石螺旋。

地形特征可以像游戏其他内容一样被注册,且你不需要担心没有特定的构建器(builder)和机制(mechanic)。

public class ExampleMod implements ModInitializer {
  private static final Feature<DefaultFeatureConfig> STONE_SPIRAL = new StoneSpiralFeature(DefaultFeatureConfig.CODEC);
 
  @Override
  public void onInitialize() {
    Registry.register(Registry.FEATURE, new Identifier("tutorial", "stone_spiral"), STONE_SPIRAL);
  }
}

配置地形特征

我们需要为地形特征提供配置。确保注册配置的地形特征和地形特征。

public class ExampleMod implements ModInitializer {
  public static final ConfiguredFeature<?, ?> STONE_SPIRAL_CONFIGURED = STONE_SPIRAL.configure(FeatureConfig.DEFAULT)
      .decorate(Decorator.CHANCE.configure(new ChanceDecoratorConfig(5)));
 
  @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_CONFIGURED);
  }
}

Decorator表示世界如何选择放置该特征。要选择正确的Decorator,检查原版的地形特征,你自己的应该要类似。装饰器配置是从这个分支出来的,在CHANCE的情况下,你应该传入ChanceDecoratorConfig的实例。

向生物群系添加特征地形

我们使用生物群系修改API。

public class ExampleMod implements ModInitializer {
  [...]
 
  @Override
  public void onInitialize() {
    [...]
    BiomeModifications.addFeature(BiomeSelectors.all(), GenerationStep.Feature.UNDERGROUND_ORES, stoneSpiral);
  }
}

addFeature的第一个参数确定结构生成在什么生物群系中。

第二个参数帮助你确定结构何时生成。对于地上的房子,可以用SURFACE_STRUCTURES,对于洞穴,可以用RAW_GENERATION

结果

zh_cn/tutorial/features.1626052629.txt.gz · Last modified: 2021/07/12 01:17 by solidblock