tutorial:jigsaw
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
tutorial:jigsaw [2020/08/14 18:26] – created emmanuelmess | tutorial:jigsaw [2021/12/30 05:47] (current) – Fix link directing at Forge version, not Fabric redgrapefruit | ||
---|---|---|---|
Line 2: | Line 2: | ||
Jigsaws are good for advanced structures such as dungeons & villages, and allow you to spend more time on actually building content vs. messing with procedural generation code. | Jigsaws are good for advanced structures such as dungeons & villages, and allow you to spend more time on actually building content vs. messing with procedural generation code. | ||
- | A repository with finished code can be found [[https:// | + | A repository with the finished code from this tutorial |
+ | A full example with structure files and more can be found [[https:// | ||
===== Creating a StructureFeature ===== | ===== Creating a StructureFeature ===== | ||
Line 27: | Line 28: | ||
</ | </ | ||
- | //getName// is the name of your structure. It's used for a few things, including: | + | Finally, |
- | * Serializing and Deserializing your structure from a chunk | + | |
- | * Locating your structure | + | |
- | * Labeling your structure | + | |
- | Vanilla convention is a properly capitalized title. "Igloo," " | + | |
<code java [enable_line_numbers=" | <code java [enable_line_numbers=" | ||
@Override | @Override | ||
- | public | + | public |
- | return | + | return |
} | } | ||
</ | </ | ||
- | The exact usage for // | + | ==== Creating |
- | <code java [enable_line_numbers=" | + | |
- | @Override | + | '' |
- | public | + | <code java [enable_line_numbers=" |
- | | + | public static class Start extends StructureStart< |
+ | |||
+ | Start(StructureFeature< | ||
+ | super(feature, | ||
+ | } | ||
+ | |||
+ | | ||
+ | public | ||
+ | | ||
+ | |||
+ | | ||
} | } | ||
</ | </ | ||
- | Finally, // | + | Now all we have to do is add our starting piece in our '' |
<code java [enable_line_numbers=" | <code java [enable_line_numbers=" | ||
@Override | @Override | ||
- | public | + | public |
- | | + | |
+ | | ||
+ | |||
+ | boolean randomYPos = false; | ||
+ | boolean calculateMaxYFromPiecePositions = false; | ||
+ | |||
+ | ExamplePiece.init(); | ||
+ | |||
+ | StructurePoolBasedGenerator.method_30419(config.getStartPool(), | ||
+ | pos, children, random, calculateMaxYFromPiecePositions, | ||
+ | |||
+ | setBoundingBoxFromChildren(); | ||
} | } | ||
</ | </ | ||
+ | |||
+ | The Identifier is the starting pool to select from, the int is the size of the entire structure (with 7 being "7 squares out"), and the 3rd argument is a factory for the piece we'll register in a second. | ||
Our finalized '' | Our finalized '' | ||
<code java [enable_line_numbers=" | <code java [enable_line_numbers=" | ||
- | import com.mojang.datafixers.Dynamic; | + | public class ExampleFeature extends StructureFeature< |
- | import net.minecraft.world.gen.chunk.ChunkGenerator; | + | |
- | import net.minecraft.world.gen.feature.DefaultFeatureConfig; | + | |
- | import net.minecraft.world.gen.feature.StructureFeature; | + | |
- | + | ||
- | import java.util.Random; | + | |
- | import java.util.function.Function; | + | |
- | + | ||
- | public class ExampleFeature extends StructureFeature< | + | |
- | public ExampleFeature(Function<Dynamic<?>, | + | public ExampleFeature(Codec<StructurePoolFeatureConfig> codec) { |
- | super(config); | + | super(codec); |
} | } | ||
@Override | @Override | ||
- | public | + | public |
- | return | + | return |
} | } | ||
@Override | @Override | ||
- | | + | |
- | return | + | return |
} | } | ||
- | | + | public |
- | | + | |
- | return " | + | |
- | } | + | |
- | | + | Start(StructureFeature< |
- | public | + | super(feature, |
- | | + | } |
+ | |||
+ | | ||
+ | public | ||
+ | | ||
+ | | ||
+ | |||
+ | boolean randomYPos = false; | ||
+ | boolean calculateMaxYFromPiecePositions = false; | ||
+ | |||
+ | ExamplePiece.init(); | ||
+ | |||
+ | StructurePoolBasedGenerator.addPieces(config.startPool, | ||
+ | pos, children, random, calculateMaxYFromPiecePositions, | ||
+ | |||
+ | setBoundingBoxFromChildren(); | ||
+ | } | ||
} | } | ||
} | } | ||
</ | </ | ||
- | ==== Creating a StructureStart class ===== | + | ===== Creating a Piece ===== |
- | '' | + | This portion |
- | <code java [enable_line_numbers=" | + | <code java [enable_line_numbers=" |
- | public class ExampleStructureStart | + | public class ExamplePiece |
- | | + | |
- | super(feature, x, z, biome, box, int_3, seed); | + | super(TutorialJigsaws.EXAMPLE_PIECE, structureManager_1, structurePoolElement_1, blockPos_1, int_1, blockRotation_1, mutableIntBoundingBox_1); |
} | } | ||
- | | + | public |
- | | + | |
- | + | ||
} | } | ||
} | } | ||
</ | </ | ||
+ | |||
+ | Where '' | ||
+ | |||
+ | |||
+ | In a static block at the top of our class, we're going to register our structure pools using '' | ||
+ | <code java [enable_line_numbers=" | ||
+ | public static void init() { } | ||
+ | |||
+ | static { | ||
+ | StructurePoolBasedGenerator.REGISTRY.add( | ||
+ | new StructurePool( | ||
+ | TutorialJigsaws.BASE_POOL, | ||
+ | new Identifier(" | ||
+ | ImmutableList.of( | ||
+ | Pair.of(new LegacySinglePoolElement(" | ||
+ | Pair.of(new LegacySinglePoolElement(" | ||
+ | ), | ||
+ | StructurePool.Projection.RIGID | ||
+ | ) | ||
+ | ); | ||
+ | |||
+ | StructurePoolBasedGenerator.REGISTRY.add( | ||
+ | new StructurePool( | ||
+ | TutorialJigsaws.COLOR_POOL, | ||
+ | new Identifier(" | ||
+ | ImmutableList.of( | ||
+ | Pair.of(new LegacySinglePoolElement(" | ||
+ | Pair.of(new LegacySinglePoolElement(" | ||
+ | Pair.of(new LegacySinglePoolElement(" | ||
+ | Pair.of(new LegacySinglePoolElement(" | ||
+ | ), | ||
+ | StructurePool.Projection.RIGID | ||
+ | ) | ||
+ | ); | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | Here, we're registering 2 pools (base & color) and then adding their respective children to them. The StructurePool constructor is as follows: | ||
+ | * registry name of the pool, same as target pool at top of a jigsaw | ||
+ | * @Draylar if you know what this one does | ||
+ | * a list of pool elements | ||
+ | * the projection type of the pool | ||
+ | |||
+ | For the list of elements, we add Pairs((com.mojang.datafixers.util)) of pool elements and integers. The string passed into the element is the location of the structure in the data directory, and the int is the weight of the element within the entire target pool. Using 1 for each element ensures each one will be picked evenly. | ||
+ | |||
+ | The projection is how the pool is placed in the world. Rigid means it will be placed directly as is, and terrain matching means it will be bent to sit on top of the terrain. The latter may be good for a wheat field structure that moves with the terrain shape, whereas the first would be better for houses with solid floors. | ||
+ | |||
+ | |||
+ | ==== Jigsaws and pieces ==== | ||
To understand what happens here, we'll have to dive into jigsaws (https:// | To understand what happens here, we'll have to dive into jigsaws (https:// | ||
Line 114: | Line 187: | ||
The jigsaw menu consists of 3 fields: | The jigsaw menu consists of 3 fields: | ||
- | * target pool | + | |
- | * attachment type | + | * target pool |
- | * turns into | + | * attachment type |
+ | * turns into | ||
{{ https:// | {{ https:// | ||
Line 135: | Line 209: | ||
This jigsaw will ask for any other jigsaw that: | This jigsaw will ask for any other jigsaw that: | ||
- | * is in the // | + | |
- | * has an attachment type of // | + | * has an attachment type of // |
It then turns into white concrete to match the rest of the platform. | It then turns into white concrete to match the rest of the platform. | ||
Line 156: | Line 230: | ||
The setup is complete! We now have 6 total squares. Let's briefly recap the goal: | The setup is complete! We now have 6 total squares. Let's briefly recap the goal: | ||
- | * have a white or black square selected as the center for our structure | + | |
- | * have a pool of the 4 other colors | + | * have a pool of the 4 other colors |
- | * branch off from the center square with our 4 extra colors | + | * branch off from the center square with our 4 extra colors |
- | Let's head back to our '' | + | Let's head back to our '' |
<code java [enable_line_numbers=" | <code java [enable_line_numbers=" | ||
private static final Identifier BASE_POOL = new Identifier(" | private static final Identifier BASE_POOL = new Identifier(" | ||
Line 168: | Line 242: | ||
Remember: every jigsaw ends up searching through the color pool, but we still have a base pool! This is to keep our black & white squares out of the outside generated squares. It's also going to be our origin pool, where we randomly select 1 structure from to begin our generation. | Remember: every jigsaw ends up searching through the color pool, but we still have a base pool! This is to keep our black & white squares out of the outside generated squares. It's also going to be our origin pool, where we randomly select 1 structure from to begin our generation. | ||
- | In a static block at the bottom of our class, we're going to register our structure | + | ===== Registering Everything ===== |
+ | We'll need to register our structure | ||
<code java [enable_line_numbers=" | <code java [enable_line_numbers=" | ||
- | static | + | public |
- | StructurePoolBasedGenerator.REGISTRY.add( | + | " |
- | new StructurePool( | + | new ExampleFeature(StructurePoolFeatureConfig.CODEC), |
- | BASE_POOL, | + | |
- | new Identifier(" | + | |
- | ImmutableList.of( | + | |
- | Pair.of(new SinglePoolElement(" | + | |
- | Pair.of(new SinglePoolElement(" | + | |
- | ), | + | |
- | StructurePool.Projection.RIGID | + | |
- | ) | + | |
- | | + | |
- | + | ||
- | StructurePoolBasedGenerator.REGISTRY.add( | + | |
- | new StructurePool( | + | |
- | COLOR_POOL, | + | |
- | new Identifier(" | + | |
- | ImmutableList.of( | + | |
- | Pair.of(new SinglePoolElement(" | + | |
- | Pair.of(new SinglePoolElement(" | + | |
- | Pair.of(new SinglePoolElement(" | + | |
- | Pair.of(new SinglePoolElement(" | + | |
- | ), | + | |
- | StructurePool.Projection.RIGID | + | |
- | ) | + | |
- | | + | |
- | } | + | |
- | </ | + | |
- | + | ||
- | Here, we're registering 2 pools (base & color) and then adding their respective children to them. The StructurePool constructor is as follows: | + | |
- | * registry name of the pool, same as target pool at top of a jigsaw | + | |
- | * @Draylar if you know what this one does | + | |
- | * a list of pool elements | + | |
- | * the projection type of the pool | + | |
- | + | ||
- | For the list of elements, we add Pairs((com.mojang.datafixers.util)) of pool elements and integers. The string passed into the element is the location of the structure in the data directory, and the int is the weight of the element within the entire target pool. Using 1 for each element ensures each one will be picked evenly. | + | |
- | + | ||
- | The projection is how the pool is placed in the world. Rigid means it will be placed directly as is, and terrain matching means it will be bent to sit on top of the terrain. The latter may be good for a wheat field structure that moves with the terrain shape, whereas the first would be better for houses with solid floors. | + | |
- | + | ||
- | Now all we have to do is add our starting piece in our '' | + | |
- | <code java [enable_line_numbers=" | + | |
- | @Override | + | |
- | public void initialize(ChunkGenerator<?> | + | |
- | StructurePoolBasedGenerator.addPieces(BASE_POOL, | + | |
- | setBoundingBoxFromChildren(); | + | |
- | } | + | |
- | </ | + | |
- | The Identifier is the starting pool to select from, the int is the size of the entire structure (with 7 being "7 squares out"), and the 3rd argument is a factory for the piece we'll register in a second. | + | |
- | + | ||
- | ==== Creating a Piece ==== | + | |
- | This portion is very simple. A piece represents one section or element in your full structure. You'll need to create a basic piece class, and we'll register it later: | + | |
- | <code java [enable_line_numbers=" | + | |
- | public class ExamplePiece extends PoolStructurePiece { | + | |
- | + | ||
- | ExamplePiece(StructureManager structureManager_1, | + | |
- | super(ExampleMod.EXAMPLE_PIECE, structureManager_1, | + | |
- | } | + | |
- | + | ||
- | public ExamplePiece(StructureManager manager, CompoundTag tag) { | + | |
- | | + | |
- | } | + | |
- | } | + | |
- | </ | + | |
- | + | ||
- | Where '' | + | |
- | + | ||
- | ==== Registering Everything ==== | + | |
- | We'll need to register our structure as both a feature //and// a structure feature, and also register our piece. Registering your structure as a StructureFeature is optional, and is used for saving it to the chunk. If the world is stopped half-way through your structure loading, having this registered will allow it to continue after the world is re-opened. If it is not registered to a structure feature and this happens, the structure will stop half-way through (which would mostly only occur in larger, multiple chunk wide structures). | + | |
- | <code java [enable_line_numbers=" | + | |
- | public static final StructureFeature< | + | |
- | Registry.FEATURE, | + | |
- | new Identifier(" | + | |
- | new ExampleFeature(DefaultFeatureConfig:: | + | |
); | ); | ||
- | public static final StructureFeature< | + | public static final StructurePieceType EXAMPLE_PIECE = StructurePieceType.register( |
- | Registry.STRUCTURE_FEATURE, | + | |
- | new Identifier(" | + | " |
- | EXAMPLE_FEATURE | + | |
- | ); | + | |
- | + | ||
- | public static final StructurePieceType EXAMPLE_PIECE = Registry.register( | + | |
- | Registry.STRUCTURE_PIECE, | + | |
- | new Identifier(" | + | |
- | ExamplePiece:: | + | |
); | ); | ||
</ | </ | ||
- | ==== Spawning Our Structure ==== | + | ===== Spawning Our Structure |
Finally, we'll have to spawn our structure. A basic example which adds it to every biome is: | Finally, we'll have to spawn our structure. A basic example which adds it to every biome is: | ||
<code java [enable_line_numbers=" | <code java [enable_line_numbers=" | ||
- | Registry.BIOME.forEach(biome -> { | + | public static final ConfiguredStructureFeature< |
- | biome.addFeature(GenerationStep.Feature.RAW_GENERATION, EXAMPLE_FEATURE.configure(new DefaultFeatureConfig()).createDecoratedFeature(Decorator.NOPE.configure(DecoratorConfig.DEFAULT))); | + | = FEATURE.configure(new StructurePoolFeatureConfig(BASE_POOL, |
- | biome.addStructureFeature(EXAMPLE_FEATURE.configure(new DefaultFeatureConfig())); | + | |
- | }); | + | static |
+ | | ||
+ | new ImmutableMap.Builder< | ||
+ | | ||
+ | | ||
+ | .build() | ||
+ | | ||
+ | } | ||
+ | |||
+ | @Override | ||
+ | public void onInitialize() { | ||
+ | Registry.BIOME.forEach(biome -> { | ||
+ | | ||
+ | }); | ||
+ | } | ||
</ | </ | ||
- | + | ===== Finished! | |
- | === Finished! === | + | |
As you can see, we have a single white square in the center, with boxes going off the edges. Note that the radius in this screenshot was increased to 14 instead of the 7 used in the tutorial. | As you can see, we have a single white square in the center, with boxes going off the edges. Note that the radius in this screenshot was increased to 14 instead of the 7 used in the tutorial. | ||
{{ https:// | {{ https:// | ||
- | === Jigsaw Tips === | + | ===== Jigsaw Tips ===== |
Ideally, you do not want structure pieces to be bigger than 32x32x32, so breaking them into chunk-sized pieces is the best option. | Ideally, you do not want structure pieces to be bigger than 32x32x32, so breaking them into chunk-sized pieces is the best option. | ||
You cannot generate other structure pieces of the same pool through jigsaws. So, if you have a piece in pool A and you try to generate another piece, you will have to have another pool. | You cannot generate other structure pieces of the same pool through jigsaws. So, if you have a piece in pool A and you try to generate another piece, you will have to have another pool. |
tutorial/jigsaw.1597429584.txt.gz · Last modified: 2020/08/14 18:26 by emmanuelmess