zh_cn:tutorial:structures
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
zh_cn:tutorial:structures [2021/07/24 16:22] – 拒绝机翻,从我做起! solidblock | zh_cn:tutorial:structures [2022/09/29 05:27] (current) – 已过时 solidblock | ||
---|---|---|---|
Line 1: | Line 1: | ||
- | ====== 添加结构特征 [1.16.3] ====== | ||
- | 现在,往世界中注册并放置一些结构。 | ||
- | 入药查看原版结构的实例,可以从简单的'' | ||
- | |||
- | 对于大多数基本的结构,你需要地物(feature)和生成器(generator)。地物处理注册结构并在生成世界时加载的过程。生成器处理方块的放置,或者在结构文件中加载(如果选择这样做)。 | ||
- | |||
- | 注意本教程依赖标记为实验性的[[https:// | ||
- | |||
- | ==== 创建特征 ==== | ||
- | |||
- | 要创建基本功能,我们建议创建一个扩展AbstractTempleFeature < | ||
- | |||
- | * shouldStartAt:出于测试目的,返回true。 | ||
- | * getName:您的结构的名称 | ||
- | * getRadius:结构的半径,用于放置 | ||
- | * getSeeedModifier | ||
- | |||
- | 您可以将DefaultFeatureConfig :: deserialize传递给构造函数进行测试。 | ||
- | |||
- | 对于getStructureStartFactory,大多数原始结构会创建一个在其Feature类中扩展StructureStart的类: | ||
- | |||
- | <code java [enable_line_numbers=" | ||
- | public static class MyStructureStart extends StructureStart { | ||
- | public MyStructureStart (StructureFeature<?> | ||
- | super(structureFeature_1, | ||
- | } | ||
- | @Override | ||
- | public void initialize(ChunkGenerator<?> | ||
- | DefaultFeatureConfig defaultFeatureConfig = chunkGenerator.getStructureConfig(biome, | ||
- | int x = chunkX * 16; | ||
- | int z = chunkZ * 16; | ||
- | BlockPos startingPos = new BlockPos(x, 0, z); | ||
- | Rotation rotation = Rotation.values()[this.random.nextInt(Rotation.values().length)]; | ||
- | MyGenerator.addParts(structureManager, | ||
- | this.setBoundingBoxFromChildren(); | ||
- | } | ||
- | } | ||
- | </ | ||
- | | ||
- | 当世界尝试以新结构生成时,这称为“要素”和“生成器”之间的差距。 在您的主类中对该变量的引用尚不存在,但我们将在最后创建它。 您也可以将配置设置为等于新的DefaultFeatureConfig。 您可以在getStructureStartFactory中使用返回MyStructureStart :: new返回此函数。 | ||
- | 这是结构文件和直接从generate方法生成的部分方式。 有两种解决方法: | ||
- | |||
- | | ||
- | | ||
- | |||
- | ==== 创建一个生成器 ==== | ||
- | |||
- | 您可能已经注意到,我们需要创建一个生成器。 我们将其命名为MyGenerator,并在StructureStart类的initialize方法中对其进行引用。 它不需要覆盖任何内容,但需要满足以下条件: | ||
- | |||
- | *指向您的结构文件的标识符; 如果需要示例,请使用'' | ||
- | | ||
- | |||
- | <code java> | ||
- | public static void addParts(StructureManager structureManager_1, | ||
- | List< | ||
- | | ||
- | } | ||
- | </ | ||
- | |||
- | 在addParts方法中,您可以选择将哪些结构件添加到生成过程中。 您可以添加如下内容: | ||
- | |||
- | <code java> | ||
- | list_1.add(new MyGenerator.Piece(structureManager_1, | ||
- | </ | ||
- | |||
- | 标识符是我们最近创建的路径。 | ||
- | |||
- | 现在,我们将创建我们刚刚引用的作品; 创建一个名为Piece的类,该类扩展了SimpleStructurePiece// | ||
- | |||
- | 覆盖必需的方法,并添加一个接受StructureManager,Identifier,BlockPos和Rotation的构造函数。 ** toNbt不是必需的,但在需要时可用**。 我们还将使用不同的参数实现自己的setStructureData,因此它不是替代。 我们还有2个构造函数:1个构造函数,一个构造函数。 一个基本的模板是: | ||
- | <code java [enable_line_numbers=" | ||
- | public static class Piece extends SimpleStructurePiece { | ||
- | private Rotation rotation; | ||
- | private Identifier template; | ||
- | | ||
- | public Piece(StructureManager structureManager_1, | ||
- | super(MyModClass.myStructurePieceType, | ||
- | | ||
- | this.pos = blockPos_1; | ||
- | this.rotation = rotation_1; | ||
- | this.template = identifier_1; | ||
- | | ||
- | this.setStructureData(structureManager_1); | ||
- | } | ||
- | | ||
- | public Piece(StructureManager structureManager_1, | ||
- | super(MyModClass.myStructurePieceType, | ||
- | this.identifier = new Identifier(compoundTag_1.getString(" | ||
- | this.rotation = Rotation.valueOf(compoundTag_1.getString(" | ||
- | this.setStructureData(structureManager_1); | ||
- | } | ||
- | | ||
- | @Override | ||
- | protected void toNbt(CompoundTag compoundTag_1) { | ||
- | super.toNbt(compoundTag_1); | ||
- | compoundTag_1.putString(" | ||
- | compoundTag_1.putString(" | ||
- | } | ||
- | | ||
- | public void setStructureData(StructureManager structureManager) { | ||
- | Structure structure_1 = structureManager.getStructureOrBlank(this.identifier); | ||
- | StructurePlacementData structurePlacementData_1 = (new StructurePlacementData()).setRotation(this.rotation).setMirrored(Mirror.NONE).setPosition(pos).addProcessor(BlockIgnoreStructureProcessor.IGNORE_STRUCTURE_BLOCKS); | ||
- | this.setStructureData(structure_1, | ||
- | } | ||
- | | ||
- | @Override | ||
- | protected void handleMetadata(String string_1, BlockPos blockPos_1, IWorld iWorld_1, Random random_1, MutableIntBoundingBox mutableIntBoundingBox_1) { | ||
- | | ||
- | } | ||
- | | ||
- | @Override | ||
- | public boolean generate(IWorld iWorld_1, Random random_1, MutableIntBoundingBox mutableIntBoundingBox_1, | ||
- | | ||
- | } | ||
- | } | ||
- | </ | ||
- | | ||
- | handleMetadata是查看结构中的数据块并根据发现的内容执行任务的地方。 在原版结构中,数据块放置在箱子上方,因此可以用这种方法将它们填满战利品。 | ||
- | |||
- | 我们将StructurePieceType设置为MyModClass.myStructurePiece类型; 这是保存您注册的结构件的变量。 在完成generate函数之后,我们将对其进行处理,该函数将设置结构的位置并生成它: | ||
- | |||
- | <code java [enable_line_numbers=" | ||
- | @Override | ||
- | public boolean generate(IWorld iWorld_1, Random random_1, MutableIntBoundingBox mutableIntBoundingBox_1, | ||
- | int yHeight = iWorld_1.getTop(Heightmap.Type.WORLD_SURFACE_WG, | ||
- | this.pos = this.pos.add(0, | ||
- | return super.generate(iWorld_1, | ||
- | } | ||
- | </ | ||
- | | ||
- | 在这种情况下,我们仅获取块中间最高块的y位置,并从该位置生成结构。 | ||
- | |||
- | ==== 注册功能 ==== | ||
- | |||
- | 最后一步是注册我们的功能。 我们需要注册: | ||
- | |||
- | * StructurePieceType | ||
- | * StructureFeature< | ||
- | * StructureFeature<?> | ||
- | |||
- | 我们还需要将结构添加到“结构”列表中,并将其添加到每个生物群系中,作为功能部件和生成步骤。 | ||
- | |||
- | 注册件类型: | ||
- | <code java> | ||
- | public static final StructurePieceType myStructurePieceType = Registry.register(Registry.STRUCTURE_PIECE, | ||
- | </ | ||
- | | ||
- | 注册功能: | ||
- | <code java> | ||
- | public static final StructureFeature< | ||
- | </ | ||
- | | ||
- | 注册结构: | ||
- | <code java> | ||
- | public static final StructureFeature<?> | ||
- | </ | ||
- | | ||
- | 要将功能放入功能列表,可以使用: | ||
- | <code java> | ||
- | Feature.STRUCTURES.put(" | ||
- | </ | ||
- | | ||
- | 对于测试,将功能注册到每个生物群系并将生成率设置为100%是个好主意,这样您就可以确保其生成并正常工作。 您可能不希望您的结构漂浮在水中,因此我们也将其过滤掉。 通过遍历生物群系列表并将其添加为特征和生成步骤,将其添加到每个生物群系: | ||
- | |||
- | <code java [enable_line_numbers=" | ||
- | for(Biome biome : Registry.BIOME) { | ||
- | if(biome.getCategory() != Biome.Category.OCEAN && biome.getCategory() != Biome.Category.RIVER) { | ||
- | biome.addStructureFeature(myFeature, | ||
- | biome.addFeature(GenerationStep.Feature.SURFACE_STRUCTURES, | ||
- | } | ||
- | } | ||
- | </ | ||
- | |||
- | ChanceDecoratorConfig的参数基本上是在生成之前将跳过多少个块。 0是每个块,1是彼此,并且100是每100。 | ||
- | |||
- | 您需要将结构添加为特征,以便您的生物群系知道其存在,然后作为生成步骤,以使其实际生成。 | ||
- | |||
- | 加载到您的世界中,如果一切顺利,应该会遇到// |