User Tools

Site Tools





Blocks and Block Entities



World Generation





Tutorials for Minecraft 1.14



Generating Structures


All code used in this tutorial is available here: fabric-structure-example-repo


This page is old, and will need to be rewritten once 1.16 is released, accounting new structure registry changes.

We’re going to look at registering and placing structures in your world.

To view examples of 1.14 vanilla structures in action, IglooGenerator & IglooFeature are a good place to start.

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.

Creating a Feature

To create a basic feature, we recommend creating a class that extends AbstractTempleFeature<DefaultFeatureConfig>. Various vanilla structures, such as Shipwrecks, Igloos, and Temples, use AbstractTempleFeature as a base. You will have to override the following methods:

  • shouldStartAt: return true for testing purposes.
  • getName: name of your structure
  • getRadius: radius of your structure, used for placement
  • getSeeedModifier

You can pass DefaultFeatureConfig::deserialize into your constructor for testing.

For getStructureStartFactory, most vanilla structures make a class that extends StructureStart inside their Feature class:

  1. public static class MyStructureStart extends StructureStart {
  2. public MyStructureStart (StructureFeature<?> feature, int chunkX, int chunkZ, BlockBox box, int references, long seed) {
  3. super(feature, chunkX, chunkZ, box, references, seed);
  4. }
  5. @Override
  6. public void initialize(ChunkGenerator<?> chunkGenerator, StructureManager structureManager, int chunkX, int chunkZ, Biome biome) {
  7. DefaultFeatureConfig defaultFeatureConfig = chunkGenerator.getStructureConfig(biome, MyMainclass.myFeature);
  8. int x = chunkX * 16;
  9. int z = chunkZ * 16;
  10. BlockPos startingPos = new BlockPos(x, 0, z);
  11. Rotation rotation = Rotation.values()[this.random.nextInt(Rotation.values().length)];
  12. MyGenerator.addParts(structureManager, startingPos, rotation, this.children, this.random, defaultFeatureConfig);
  13. this.setBoundingBoxFromChildren();
  14. }
  15. }

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.
  • Use structure files and a Generator. These are rather powerful at this point and are highly recommended.

Creating a Generator

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:

  • An Identifier that points to your structure file; use “igloo/top” if you need an example.
  • Some sort of setup method - addParts is a good name:
public static void addParts(StructureManager structureManager_1, BlockPos blockPos_1, Rotation rotation_1, 
    List<StructurePiece> list_1, Random random_1, DefaultFeatureConfig featureConfig)

In your addParts method, you can choose which structure pieces are added to your generation process. You can add a piece like this:

list_1.add(new MyGenerator.Piece(structureManager_1, identifier, blockPos, rotation_1));

where the identifier is the path we created recently.

We're now going to create the Piece we just referenced; make a class called Piece that extends SimpleStructurePiece within your generator class.

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 arguments, so it's not an override. We also have 2 constructors: 1 for our own pieces, and one for registry. A basic template would be:

  1. public static class Piece extends SimpleStructurePiece {
  2. private Rotation rotation;
  3. private Identifier template;
  5. public Piece(StructureManager structureManager_1, Identifier identifier_1, BlockPos blockPos_1, Rotation rotation_1) {
  6. super(MyModClass.myStructurePieceType, 0);
  8. this.pos = blockPos_1;
  9. this.rotation = rotation_1;
  10. this.template = identifier_1;
  12. this.setStructureData(structureManager_1);
  13. }
  15. public Piece(StructureManager structureManager_1, CompoundTag compoundTag_1) {
  16. super(MyModClass.myStructurePieceType, compoundTag_1);
  17. this.identifier = new Identifier(compoundTag_1.getString("Template"));
  18. this.rotation = Rotation.valueOf(compoundTag_1.getString("Rot"));
  19. this.setStructureData(structureManager_1);
  20. }
  22. @Override
  23. protected void toNbt(CompoundTag compoundTag_1) {
  24. super.toNbt(compoundTag_1);
  25. compoundTag_1.putString("Template", this.template.toString());
  26. compoundTag_1.putString("Rot",;
  27. }
  29. public void setStructureData(StructureManager structureManager) {
  30. Structure structure_1 = structureManager.getStructureOrBlank(this.identifier);
  31. StructurePlacementData structurePlacementData_1 = (new StructurePlacementData()).setRotation(this.rotation).setMirrored(Mirror.NONE).setPosition(pos).addProcessor(BlockIgnoreStructureProcessor.IGNORE_STRUCTURE_BLOCKS);
  32. this.setStructureData(structure_1, this.pos, structurePlacementData_1);
  33. }
  35. @Override
  36. protected void handleMetadata(String string_1, BlockPos blockPos_1, IWorld iWorld_1, Random random_1, MutableIntBoundingBox mutableIntBoundingBox_1) {
  38. }
  40. @Override
  41. public boolean generate(IWorld iWorld_1, Random random_1, MutableIntBoundingBox mutableIntBoundingBox_1, ChunkPos chunkPos_1) {
  43. }
  44. }

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.

We set the StructurePieceType to MyModClass.myStructurePiece type; this is the variable that holds your registered structure piece. We'll handle that after we finish the generate function, which sets the position of your structure and generates it:

  1. @Override
  2. public boolean generate(IWorld iWorld_1, Random random_1, MutableIntBoundingBox mutableIntBoundingBox_1, ChunkPos chunkPos_1) {
  3. int yHeight = iWorld_1.getTop(Heightmap.Type.WORLD_SURFACE_WG, this.pos.getX() + 8, this.pos.getZ() + 8);
  4. this.pos = this.pos.add(0, yHeight - 1, 0);
  5. return super.generate(iWorld_1, random_1, mutableIntBoundingBox_1, chunkPos_1);
  6. }

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
  • StructureFeature<DefaultFeatureConfig>
  • 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:

public static final StructurePieceType myStructurePieceType = Registry.register(Registry.STRUCTURE_PIECE, "my_piece", MyGenerator.Piece::new);

Registering feature:

public static final StructureFeature<DefaultFeatureConfig> myFeature = Registry.register(Registry.FEATURE, "my_feature", new MyFeature());

Registering structure:

public static final StructureFeature<?> myStructure = Registry.register(Registry.STRUCTURE_FEATURE, "my_structure", myFeature);

To put your feature in the features list, you can use:

Feature.STRUCTURES.put("My Awesome Feature", myFeature);

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:

  1. for(Biome biome : Registry.BIOME) {
  2. if(biome.getCategory() != Biome.Category.OCEAN && biome.getCategory() != Biome.Category.RIVER) {
  3. biome.addStructureFeature(myFeature.configure(FeatureConfig.DEFAULT));
  4. biome.addFeature(GenerationStep.Feature.SURFACE_STRUCTURES, myFeature.configure(FeatureConfig.DEFAULT).createDecoratedFeature(Decorator.CHANCE_PASSTHROUGH.configure(new ChanceDecoratorConfig(0))));
  5. }
  6. }

ChanceDecoratorConfig's argument is basically how many chunks it will skip over before generating. 0 is every chunk, 1 is every other, and 100 is every 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.

tutorial/structures.txt · Last modified: 2020/06/03 21:43 by ffrann