tutorial:structures
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revisionNext revisionBoth sides next revision | ||
tutorial:structures [2020/05/09 20:17] – MyStructureStart constructor yanis48 | tutorial:structures [2022/08/16 20:48] – FME mineblock11 | ||
---|---|---|---|
Line 1: | Line 1: | ||
- | ====== Generating Structures ====== | + | DELETEME //**For 1.18.2, Fabric Structure API no longer exists and structures can be done entirely |
- | + | ||
- | ===== Features ===== | + | |
- | + | ||
- | All code used in this tutorial is available here: [[https:// | + | |
- | + | ||
- | ==== Introduction ==== | + | |
+ | ====== Adding Structure Features [1.16.3] ====== | ||
We’re going to look at registering and placing structures in your world. | We’re going to look at registering and placing structures in your world. | ||
- | To view examples of 1.14 vanilla structures in action, | + | To view examples of vanilla structures in action, |
- | You are going to need a Feature | + | You are going to need a feature |
+ | The feature handles the process of registering the structure and loading it in when the world is generating. | ||
+ | The generator handles the placement of blocks or loading in a structure file if you choose to do so. | ||
- | ==== Creating a Feature ==== | + | Note that this tutorial depends on [[https:// |
+ | If the API doesn' | ||
- | To create a basic feature, we recommend creating a class that extends | + | ===== Creating a Feature ===== |
+ | To create a basic feature, we recommend creating a class that extends | ||
+ | Various vanilla structures, such as shipwrecks, igloos, and temples, use '' | ||
- | * shouldStartAt: | + | You will have to override '' |
- | * getName: name of your structure | + | For '' |
- | * getRadius: radius of your structure, used for placement | + | |
- | * getSeeedModifier | + | |
- | You can pass DefaultFeatureConfig:: | + | <code java> |
+ | public class MyFeature extends StructureFeature< | ||
+ | public MyFeature(Codec< | ||
+ | super(codec); | ||
+ | } | ||
- | For getStructureStartFactory, most vanilla structures make a class that extends StructureStart inside their Feature class: | + | @Override |
+ | public StructureFeature.StructureStartFactory< | ||
+ | return Start::new; | ||
+ | } | ||
- | <code java [enable_line_numbers=" | + | |
- | public static class MyStructureStart | + | public |
- | public | + | |
- | super(feature, | + | super(feature, |
} | } | ||
- | @Override | + | |
- | public void initialize(ChunkGenerator<?> | + | // Called when the world attempts to spawn in a new structure, and is the gap between your feature and generator. |
- | | + | public void init(DynamicRegistryManager registryManager, |
- | | + | |
- | int z = chunkZ * 16; | + | int x = chunkX * 16; |
- | BlockPos | + | int z = chunkZ * 16; |
- | | + | int y = chunkGenerator.getHeight(x, |
- | MyGenerator.addParts(structureManager, startingPos, rotation, this.children, this.random, | + | |
- | this.setBoundingBoxFromChildren(); | + | |
+ | MyGenerator.addPieces(manager, pos, rotation, this.children); | ||
+ | this.setBoundingBoxFromChildren(); | ||
} | } | ||
+ | } | ||
} | } | ||
</ | </ | ||
- | | ||
- | 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' | ||
- | This is where structure files and generating straight from a generate method part ways. There are two ways to go about this: | + | ===== Creating |
+ | As you have probably noticed, we need to create a generator. | ||
- | * If you want, you can simply override generate in your Feature class and use setBlockState to place blocks directly in the world. | + | This is where structure files and generating straight from a '' |
- | * Use structure files and a Generator. These are rather powerful at this point and are highly recommended. | + | |
- | ==== Creating | + | * If you want, you can simply override '' |
+ | * 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' | + | In this tutorial, we' |
+ | It doesn' | ||
+ | * An identifier that points to your structure file; use ''" | ||
+ | * Some sort of setup method - '' | ||
- | * An Identifier | + | <code java> |
- | * Some sort of setup method - addParts is a good name: | + | public class MyGenerator { |
+ | private static final Identifier | ||
- | <code java> | + | |
- | public static void addParts(StructureManager | + | pieces.add(new MyPiece(manager, |
- | | + | } |
- | + | ||
} | } | ||
</ | </ | ||
- | | + | |
- | In your addParts | + | In your '' |
+ | |||
+ | We're now going to create the piece we just referenced; make a class called '' | ||
+ | |||
+ | Override required methods, and add a constructor that takes in a '' | ||
+ | **toNbt isn't required but is available if you need it**. | ||
+ | We're also implementing '' | ||
+ | We also have 2 constructors: | ||
+ | A basic template would be: | ||
<code java> | <code java> | ||
- | list_1.add(new MyGenerator.Piece(structureManager_1, | + | public static class MyPiece extends SimpleStructurePiece { |
- | </ | + | |
- | + | | |
- | 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//. | + | public MyPiece(StructureManager structureManager, |
+ | super(ExampleMod.MY_PIECE, | ||
+ | this.template = new Identifier(compoundTag.getString(" | ||
+ | this.rotation = BlockRotation.valueOf(compoundTag.getString(" | ||
+ | this.initializeStructureData(structureManager); | ||
+ | } | ||
- | Override required methods, and add a constructor that takes in a StructureManager, Identifier, BlockPos, | + | public MyPiece(StructureManager |
+ | super(ExampleMod.MY_PIECE, 0); | ||
+ | this.pos = pos; | ||
+ | this.rotation = rotation; | ||
+ | this.template = template; | ||
- | <code java [enable_line_numbers=" | + | |
- | public static class Piece extends SimpleStructurePiece { | + | } |
- | private Rotation rotation; | + | |
- | private Identifier template; | + | |
- | + | Structure | |
- | public Piece(StructureManager structureManager_1, | + | StructurePlacementData |
- | super(MyModClass.myStructurePieceType, | + | |
- | + | | |
- | | + | |
- | this.rotation = rotation_1; | + | this.setStructureData(structure, this.pos, |
- | this.template = identifier_1; | + | } |
- | + | ||
- | this.setStructureData(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 | + | |
- | super.toNbt(compoundTag_1); | + | |
- | compoundTag_1.putString(" | + | |
- | compoundTag_1.putString(" | + | |
- | } | + | |
- | + | ||
- | public void setStructureData(StructureManager structureManager) { | + | |
- | Structure | + | |
- | StructurePlacementData | + | |
- | this.setStructureData(structure_1, this.pos, | + | |
- | } | + | |
- | + | ||
- | @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 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: | + | protected void toNbt(CompoundTag tag) { |
+ | super.toNbt(tag); | ||
+ | tag.putString(" | ||
+ | tag.putString(" | ||
+ | } | ||
- | <code java [enable_line_numbers=" | + | |
- | @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); | + | } |
- | | + | |
- | | + | |
} | } | ||
</ | </ | ||
| | ||
- | In this case, we simply get the y position of the highest block in the middle of our chunk and generate the structure | + | '' |
+ | This can be good for dynamic stuff such as placing certain mobs based on what mod is on and so on. | ||
- | ==== Registering Features ==== | + | 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 loot. Instead, use this command to set a north facing chest with a loottable. | ||
+ | Save this chest into your structure' | ||
+ | < | ||
- | The last step is to register our features. | + | We set the '' |
- | * StructurePieceType | + | ===== Registering Structures ===== |
- | * StructureFeature< | + | The last step is to register our structures. We're going to need to register: |
- | * StructureFeature<?> | + | |
- | We're also going to need to add the structure | + | * structure |
+ | * piece | ||
+ | * configured | ||
- | Registering piece type: | ||
<code java> | <code java> | ||
- | public static final StructurePieceType | + | public class ExampleMod implements ModInitializer { |
+ | | ||
+ | private static final StructureFeature< | ||
+ | private static final ConfiguredStructureFeature<?, | ||
+ | |||
+ | @Override | ||
+ | public void onInitialize() { | ||
+ | | ||
+ | FabricStructureBuilder.create(new Identifier(" | ||
+ | .step(GenerationStep.Feature.SURFACE_STRUCTURES) | ||
+ | .defaultConfig(32, | ||
+ | .adjustsSurface() | ||
+ | .register(); | ||
+ | |||
+ | RegistryKey< | ||
+ | new Identifier(" | ||
+ | BuiltinRegistries.add(BuiltinRegistries.CONFIGURED_STRUCTURE_FEATURE, | ||
+ | } | ||
+ | } | ||
</ | </ | ||
- | | + | |
- | Registering | + | ===== Adding a configured |
+ | In this tutorial, we add our structure to all biomes. | ||
<code java> | <code java> | ||
- | public | + | public |
- | </ | + | [...] |
- | + | ||
- | Registering structure: | + | |
- | <code java> | + | public |
- | public | + | [...] |
- | </ | + | |
- | + | ||
- | To put your feature in the features list, you can use: | + | |
- | <code java> | + | |
- | Feature.STRUCTURES.put(" | + | |
- | </ | + | |
- | + | ||
- | 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: | + | |
- | <code java [enable_line_numbers=" | + | BiomeModifications.addStructure(BiomeSelectors.all(), myConfigured); |
- | for(Biome biome : Registry.BIOME) { | + | } |
- | if(biome.getCategory() != Biome.Category.OCEAN && biome.getCategory() != Biome.Category.RIVER) { | + | |
- | biome.addStructureFeature(myFeature.configure(FeatureConfig.DEFAULT)); | + | |
- | biome.addFeature(GenerationStep.Feature.SURFACE_STRUCTURES, myFeature.configure(FeatureConfig.DEFAULT).createDecoratedFeature(Decorator.CHANCE_PASSTHROUGH.configure(new ChanceDecoratorConfig(0)))); | + | |
- | } | + | |
} | } | ||
</ | </ | ||
- | ChanceDecoratorConfig' | + | ===== Result ===== |
+ | You should be met with igloos. | ||
+ | You can use below command to find your structure in the world. | ||
+ | |||
+ | < | ||
+ | /locate tutorial: | ||
+ | </ | ||
- | 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. | + | {{tutorial: |
- | Load into your world, and if all went well, you should be met with a //lot// of Igloos. |
tutorial/structures.txt · Last modified: 2022/11/05 12:06 by jab125