User Tools

Site Tools


zh_cn:tutorial:structures

Differences

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

Link to this comparison view

Next revision
Previous revision
Last revisionBoth sides next revision
zh_cn:tutorial:structures [2020/02/04 10:41] – created lightcolourzh_cn:tutorial:structures [2021/07/24 16:34] – [创建一个生成器] not done solidblock
Line 1: Line 1:
-====== 生成结构 ======+====== 添加结构特征 [1.16.3] ====== 
 +现在,往世界中注册并放置一些结构。
  
-===== 特征 =====+入药查看原版结构的实例,可以从简单的''IglooFeature''和''IglooGenerator''开始。然而,制作新结构的上升的标准是使用拼图结构,这样更易于使用并可以让你少打一些代码。[[zh_cn:tutorial:jigsaw|这里有关于拼图结构和制作方法的教程。]]
  
-All code used in this tutorial is available here: [[https://github.com/Draylar/fabric-structure-example-repo|fabric-structure-example-repo]]+对于大多数基本的结构,你需要地物(feature)和生成器(generator)。地物处理注册结构并在生成世界时加载的过程。生成器处理方块的放置,或者在结构文件中加载(如果选择这样做)。
  
-==== Introduction ====+注意本教程依赖标记为实验性的[[https://github.com/FabricMC/fabric/pull/1097|Fabric API中的生物群系修改API]]。如果API不起作用,考虑使用[[?rev=1599808070|mixin版本]]。
  
-We’re going to look at registering and placing structures in your world.+===== 创建地物 ===== 
 +要创建基本的地物(feature),我们推荐创建扩展''StructureFeature<DefaultFeatureConfig>''的类。大多数原版结构,如沉船、雪屋、神殿都是以''StructureFeature<DefaultFeatureConfig>''为基础,
  
-To view examples of 1.14 vanilla structures in action, IglooGenerator & IglooFeature are a good place to start.+你需要覆写''getStructureStartFactory''方法。对于''getStructureStartFactory'',大多数原版的结构在其地物类中创建扩展''StructureStart''的类。
  
-You are going to need a Feature and Generator for the most basic structure. The feature handles the process of registering the structure and loading it in when the world is generating-- it answers questions such as ‘should I spawn here?’ and ‘what is my name?’ The generator handles the placement of blocks or loading in a structure file if you choose to do so.+<code java> 
 +public class MyFeature extends StructureFeature<DefaultFeatureConfig>
 +  public MyFeature(Codec<DefaultFeatureConfig> codec) { 
 +    super(codec); 
 +  }
  
-==== Creating a Feature ====+  @Override 
 +  public StructureFeature.StructureStartFactory<DefaultFeatureConfig> getStructureStartFactory() { 
 +    return Start::new; 
 +  }
  
-To create a basic feature, we recommend creating a class that extends AbstractTempleFeature<DefaultFeatureConfig>. Various vanilla structuressuch as ShipwrecksIgloosand Templesuse AbstractTempleFeature as a base. You will have to override the following methods:+  public static class Start extends StructureStart<DefaultFeatureConfig> 
 +    public Start(StructureFeature<DefaultFeatureConfig> feature, int chunkX, int chunkZ, BlockBox box, int references, 
 +        long seed) { 
 +      super(feature, chunkXchunkZboxreferencesseed); 
 +    }
  
-  * shouldStartAt: return true for testing purposes. +    // 世界尝试在新的结构中生成时调用,同时也是地物和结构之间的“空隙(gap)”。 
-  * getName: name of your structure +    public void init(DynamicRegistryManager registryManager, ChunkGenerator chunkGenerator, StructureManager manager, int chunkX, 
-  * getRadius: radius of your structure, used for placement +        int chunkZ, Biome biome, DefaultFeatureConfig config) { 
-  * getSeeedModifier +      int x = chunkX * 16; 
- +      int z = chunkZ * 16; 
-You can pass DefaultFeatureConfig::deserialize into your constructor for testing. +      int y = chunkGenerator.getHeight(x, z, Heightmap.Type.WORLD_SURFACE_WG); 
- +      BlockPos pos = new BlockPos(x, y, z); 
-For getStructureStartFactory, most vanilla structures make a class that extends StructureStart inside their Feature class: +      BlockRotation rotation = BlockRotation.random(this.random); 
- +      MyGenerator.addPieces(managerpos, rotation, this.children); 
-<code java [enable_line_numbers="true"]> +      this.setBoundingBoxFromChildren();
-public static class MyStructureStart extends StructureStart { +
-    public MyStructureStart (StructureFeature<?> structureFeature_1int int_1, int int_2, Biome biome_1, MutableIntBoundingBox mutableIntBoundingBox_1, int int_3, long long_1) { +
-        super(structureFeature_1, int_1, int_2, biome_1, mutableIntBoundingBox_1, int_3, long_1); +
-    } +
-    @Override +
-    public void initialize(ChunkGenerator<?> chunkGenerator, StructureManager structureManager, int chunkX, int chunkZ, Biome biome) { +
-        DefaultFeatureConfig defaultFeatureConfig = chunkGenerator.getStructureConfig(biome, MyMainclass.myFeature); +
-        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(structureManagerstartingPos, rotation, this.children, this.random, defaultFeatureConfig); +
-        this.setBoundingBoxFromChildren();+
     }     }
 +  }
 } }
 </code> </code>
-     
-This is called when the world attempts to spawn in a new structure, and is the gap between your Feature and Generator. The reference to the variable in your main class doesn't exist yet, but we'll create it at the end. You can also just set the config equal to a new DefaultFeatureConfig. You can return this in getStructureStartFactory with return MyStructureStart::new. 
  
-This is where structure files and generating straight from a generate method part ways. There are two ways to go about this:+===== 创建生成器 ===== 
 +你可能已经注意到,我们需要创建生成器。
  
-  * If you want, you can simply override generate in your Feature class and use setBlockState to place blocks directly in the world. This is a valid option and was popular pre-1.13. +This is where structure files and generating straight from ''generate'' method part ways有两种方式实现这样做:
-  * Use structure files and a Generator. These are rather powerful at this point and are highly recommended.+
  
-==== Creating Generator ====+  * If you want, you can simply override ''generate'' in your piece class and use ''addBlock'' to place blocks directly in the world. This is valid option and was popular pre-1.13. 
 +  * Use structure files. These are rather powerful at this point and are highly recommended.
  
-As you have probably noticed, we need to create a generator. We'll name it MyGenerator, and it's referenced in the initialize method of your StructureStart class. It doesn't need to override anything, but does require the following:+本教程中,使用结构文件,不需要override任何内容,但确实需要: 
 +  * 指向结构文件的标识符(identifier,例如''"igloo/top"''。 
 +  * 一些安装方法——例如''addPieces''就很不错。
  
-  * An Identifier that points to your structure file; use "igloo/top" if you need an example. +<code java> 
-  * Some sort of setup method - addParts is a good name:+public class MyGenerator { 
 +  private static final Identifier IGLOO_TOP = new Identifier("igloo/top");
  
-<code java> +  public static void addPieces(StructureManager manager, BlockPos posBlockRotation rotation, List<StructurePiece> pieces) { 
-public static void addParts(StructureManager structureManager_1, BlockPos blockPos_1Rotation rotation_1 +    pieces.add(new MyPiece(manager, posIGLOO_TOProtation)); 
-    List<StructurePiece> list_1Random random_1DefaultFeatureConfig featureConfig+  }
-    +
 } }
 </code> </code>
-     + 
-In your addParts method, you can choose which structure pieces are added to your generation processYou can add a piece like this:+在''addPieces''方法中,你可以选择往你的生成过程中添加哪些结构。 
 + 
 +我们现在//在生成器类中//创建我们刚刚提到的结构,创建名为''MyPiece''的类,继承''SimpleStructurePiece''。 
 + 
 +Override required methodsand add a constructor that takes in a ''StructureManager'', ''BlockPos'', ''Identifier'' and ''Rotation''
 +**toNbt isn't required but is available if you need it**. 
 +We're also implementing ''initializeStructureData'', which is not an override. 
 +We also have 2 constructors: 1 for our own pieces, and one for registry. 
 +A basic template would be:
  
 <code java> <code java>
-list_1.add(new MyGenerator.Piece(structureManager_1, identifier, blockPos, rotation_1)); +public static class MyPiece extends SimpleStructurePiece { 
-</code> +  private final BlockRotation rotation; 
-   +  private final Identifier template;
-where the identifier is the path we created recently.+
  
-We're now going to create the Piece we just referencedmake a class called Piece that extends SimpleStructurePiece //within your generator class//.+  public MyPiece(StructureManager structureManager, CompoundTag compoundTag) { 
 +    super(ExampleMod.MY_PIECE, compoundTag); 
 +    this.template = new Identifier(compoundTag.getString("Template")); 
 +    this.rotation = BlockRotation.valueOf(compoundTag.getString("Rot")); 
 +    this.initializeStructureData(structureManager); 
 +  }
  
-Override required methods, and add a constructor that takes in a StructureManager, Identifier, BlockPos, and Rotation. **toNbt isn't required but is available if you need it**. We're also implementing our own setStructureData with different argumentsso it's not an overrideWe also have 2 constructors: 1 for our own piecesand one for registryA basic template would be:+  public MyPiece(StructureManager structureManager, BlockPos posIdentifier templateBlockRotation rotation) { 
 +    super(ExampleMod.MY_PIECE0); 
 +    this.pos = pos; 
 +    this.rotation = rotation; 
 +    this.template = template;
  
-<code java [enable_line_numbers="true"]> +    this.initializeStructureData(structureManager); 
-public static class Piece extends SimpleStructurePiece { +  } 
-    private Rotation rotation; + 
-    private Identifier template; +  private void initializeStructureData(StructureManager structureManager) { 
-     +    Structure structure structureManager.getStructureOrBlank(this.template); 
-    public Piece(StructureManager structureManager_1, Identifier identifier_1, BlockPos blockPos_1, Rotation rotation_1) { +    StructurePlacementData placementData = (new StructurePlacementData()
-        super(MyModClass.myStructurePieceType, 0); +      .setRotation(this.rotation
-         +      .setMirror(BlockMirror.NONE
-        this.pos blockPos_1; +      .addProcessor(BlockIgnoreStructureProcessor.IGNORE_STRUCTURE_BLOCKS); 
-        this.rotation = rotation_1; +    this.setStructureData(structure, this.pos, placementData); 
-        this.template = identifier_1; +  
-         + 
-        this.setStructureData(structureManager_1); +  protected void toNbt(CompoundTag tag) { 
-    +    super.toNbt(tag); 
-     +    tag.putString("Template", this.template.toString()); 
-    public Piece(StructureManager structureManager_1, CompoundTag compoundTag_1{ +    tag.putString("Rot", this.rotation.name()); 
-        super(MyModClass.myStructurePieceType, compoundTag_1); +  
-        this.identifier = new Identifier(compoundTag_1.getString("Template")); + 
-        this.rotation = Rotation.valueOf(compoundTag_1.getString("Rot")); +  @Override 
-        this.setStructureData(structureManager_1); +  protected void handleMetadata(String metadata, BlockPos posServerWorldAccess serverWorldAccess, Random random
-    +      BlockBox boundingBox) { 
-     +  }
-    @Override +
-    protected void toNbt(CompoundTag compoundTag_1) { +
-        super.toNbt(compoundTag_1); +
-        compoundTag_1.putString("Template", this.template.toString()); +
-        compoundTag_1.putString("Rot", this.rotation.name()); +
-    +
-     +
-    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, this.pos, structurePlacementData_1); +
-    } +
-     +
-    @Override +
-    protected void handleMetadata(String string_1, BlockPos blockPos_1IWorld iWorld_1, Random random_1MutableIntBoundingBox mutableIntBoundingBox_1) { +
-         +
-    } +
-     +
-    @Override +
-    public boolean generate(IWorld iWorld_1, Random random_1, MutableIntBoundingBox mutableIntBoundingBox_1, ChunkPos chunkPos_1) { +
-       +
-    }+
 } }
 </code> </code>
          
-handleMetadata is where you look at data blocks within your structure and do tasks based on what you find. In vanilla structures, data blocks are placed above chests so they can be filled with loot in this method.+''handleMetadata'' is where you look at data blocks within your structure and can do tasks based on what you find. 
 +This can be good for dynamic stuff such as placing certain mobs based on what mod is on and so on.
  
-We set the StructurePieceType to MyModClass.myStructurePiece type; this is the variable that holds your registered structure pieceWe'll handle that after we finish the generate function, which sets the position of your structure and generates it:+In vanilla structures, data blocks are placed above chests so they can be filled with loot in this method.  
 +HOWEVER, you do not need to use datablocks to place chests with lootInstead, use this command to set a north facing chest with a loottable.  
 +Save this chest into your structure's nbt file and it will generate loot when opened for the first time(Don't open the chest before saving to the nbt file!) 
 +<code>/setblock ~ ~ ~ minecraft:chest[facing=north]{LootTable:"modid:loottable"}</code>
  
-<code java [enable_line_numbers="true"]> +We set the ''StructurePieceType'' to ''ExampleMod.MY_PIECE''; this is the variable that holds your registered structure piece
-@Override +
-public boolean generate(IWorld iWorld_1, Random random_1, MutableIntBoundingBox mutableIntBoundingBox_1, ChunkPos chunkPos_1) { +
-    int yHeight = iWorld_1.getTop(Heightmap.Type.WORLD_SURFACE_WG, this.pos.getX() + 8, this.pos.getZ() + 8); +
-    this.pos = this.pos.add(0, yHeight - 1, 0); +
-    return super.generate(iWorld_1, random_1, mutableIntBoundingBox_1, chunkPos_1); +
-+
-</code> +
-     +
-In this case, we simply get the y position of the highest block in the middle of our chunk and generate the structure off that position+
  
-==== Registering Features ====+==== 注册功能 ====
  
-The last step is to register our features. We're going to need to register:+最后一步是注册我们的功能。 我们需要注册:
  
   * StructurePieceType   * StructurePieceType
Line 147: Line 138:
   * StructureFeature<?>   * StructureFeature<?>
  
-We're also going to need to add the structure to the STRUCTURES list and add it to each biome as a feature and generation step.+我们还需要将结构添加到“结构”列表中,并将其添加到每个生物群系中,作为功能部件和生成步骤。
  
-Registering piece type:+注册件类型:
 <code java> <code java>
 public static final StructurePieceType myStructurePieceType = Registry.register(Registry.STRUCTURE_PIECE, "my_piece", MyGenerator.Piece::new); public static final StructurePieceType myStructurePieceType = Registry.register(Registry.STRUCTURE_PIECE, "my_piece", MyGenerator.Piece::new);
 </code> </code>
      
-Registering feature:+注册功能:
 <code java> <code java>
 public static final StructureFeature<DefaultFeatureConfig> myFeature = Registry.register(Registry.FEATURE, "my_feature", new MyFeature()); public static final StructureFeature<DefaultFeatureConfig> myFeature = Registry.register(Registry.FEATURE, "my_feature", new MyFeature());
 </code> </code>
      
-Registering structure:+注册结构:
 <code java> <code java>
 public static final StructureFeature<?> myStructure = Registry.register(Registry.STRUCTURE_FEATURE, "my_structure", myFeature); public static final StructureFeature<?> myStructure = Registry.register(Registry.STRUCTURE_FEATURE, "my_structure", myFeature);
 </code> </code>
      
-To put your feature in the features list, you can use:+要将功能放入功能列表,可以使用:
 <code java> <code java>
 Feature.STRUCTURES.put("My Awesome Feature", myFeature); Feature.STRUCTURES.put("My Awesome Feature", myFeature);
 </code> </code>
      
-For testing, it's a good idea to register your feature to every biome and set the spawn rate to 100% so you can be sure it's spawning and working. You probably don't want your structure floating in the water, so we'll also filter that out. Add it to every biome by iterating over the biome list and adding it as a feature and generation step:+对于测试,将功能注册到每个生物群系并将生成率设置为100%是个好主意,这样您就可以确保其生成并正常工作。 您可能不希望您的结构漂浮在水中,因此我们也将其过滤掉。 通过遍历生物群系列表并将其添加为特征和生成步骤,将其添加到每个生物群系:
  
 <code java [enable_line_numbers="true"]> <code java [enable_line_numbers="true"]>
Line 180: Line 171:
 </code> </code>
  
-ChanceDecoratorConfig's argument is basically how many chunks it will skip over before generating. is every chunk, is every other, and 100 is every 100.+ChanceDecoratorConfig的参数基本上是在生成之前将跳过多少个块。 0是每个块,1是彼此,并且100是每100
  
-You need to add your structure as a feature so your biome knows it exists, and then as a generation step so it's actually generated.+您需要将结构添加为特征,以便您的生物群系知道其存在,然后作为生成步骤,以使其实际生成。
  
-Load into your world, and if all went well, you should be met with a //lot// of Igloos.+加载到您的世界中,如果一切顺利,应该会遇到//很多//的雪屋。