Minestom wiki
Search…
Generation

Basics

Each Instance has an optional Generator that is responsible for generating areas of various sizes.
The area size is abstracted as a GenerationUnit representing an aggregate of sections, the generator then has the ability to place blocks (and biomes) using relative and absolute coordinates. The dynamically sized area allows the API to be more flexible, and allows the instance to choose whether to generate full chunks at once or section by section without changing the generator.
Generation tasks are currently forwarded to the common JDK pool. Virtual threads will be used once Project Loom is integrated into the mainline.

Your first flat world

Here is the naive way of generating a flat world from y=0 to y=40
1
Instance instance = ...;
2
instance.setGenerator(unit -> {
3
final Point start = unit.absoluteStart();
4
final Point size = unit.size();
5
for (int x = 0; x < size.blockX(); x++) {
6
for (int z = 0; z < size.blockZ(); z++) {
7
for (int y = 0; y < Math.min(40 - start.blockY(), size.blockY()); y++) {
8
unit.modifier().setBlock(start.add(x, y, z), Block.STONE);
9
}
10
}
11
}
12
});
Copied!
GenerationUnit#absoluteStart returns the lowest coordinate of the unit which is useful for absolute placements. Now, we can heavily simplify the code by using one of our hand-optimized methods:
1
Instance instance = ...;
2
instance.setGenerator(unit ->
3
unit.modifier().fillHeight(0, 40, Block.STONE));
Copied!

Modifying over unit borders

Modification over the border of a GenerationUnit cannot be done without extra steps. GenerationUnits cannot be resized during generation, instead we need to create a new GenerationUnit that encloses the area around our target blocks. We can do this through the GenerationUnit#fork methods.
Forked units are designed to be placed into the instance whenever it is possible to do so. This eliminates any section bordering issues that may arise.
There are two fork methods, both useful in their own ways. Here is a simple example of adding a structure (snowman):
1
Instance instance = ...;
2
instance.setGenerator(unit -> {
3
Random random = ...;
4
Point start = unit.absoluteStart();
5
6
// Create a snow carpet for the snowmen
7
unit.modifier().fillHeight(-64, -60, Block.SNOW);
8
9
// Exit out if unit is not the bottom unit, and exit 5 in 6 times otherwise
10
if (start.y() > -64 || random.nextInt(6) != 0) {
11
return;
12
}
13
14
// Lets fork this section to add our tall snowman.
15
// We add two extra sections worth of space to this fork to fit the snowman.
16
GenerationUnit fork = unit.fork(start, start.add(16, 32, 16));
17
18
// Now we add the snowman to the fork
19
fork.modifier().fill(start, start.add(3, 19, 3), Block.POWDER_SNOW);
20
fork.modifier().setBlock(start.add(1, 19, 1), Block.JACK_O_LANTERN);
21
});
Copied!
Adding structures using forks is trivial. However, for this GenerationUnit#fork method, you must know how large these structures are beforehand. To alleviate this condition, there is an alternative GenerationUnit#fork method that gives access to a direct Block.Setter. This Block.Setter automatically adjusts the fork's size depending on the blocks you set using it. Here is the same example written with the Block.Setter utility:
1
Instance instance = ...;
2
instance.setGenerator(unit -> {
3
Random random = ...;
4
Point start = unit.absoluteStart();
5
6
// Create a snow carpet for the snowmen
7
unit.modifier().fillHeight(-64, -60, Block.SNOW);
8
9
// Exit out if unit is not the bottom unit or 5 in 6 times
10
if (start.y() > -64 || random.nextInt(6) != 0) {
11
return;
12
}
13
14
// Add the snowman
15
unit.fork(setter -> {
16
for (int x = 0; x < 3; x++) {
17
for (int y = 0; y < 19; y++) {
18
for (int z = 0; z < 3; z++) {
19
setter.setBlock(start.add(x, y, z), Block.POWDER_SNOW);
20
}
21
}
22
}
23
setter.setBlock(start.add(1, 19, 1), Block.JACK_O_LANTERN);
24
});
25
});
Copied!
These examples will generate a flat snow world with chunky snowmen scattered throughout, cleanly applying the snowmen whenever it is possible to do so.
Example with missing terrain for clarity:

Heightmaps with JNoise

This example shows a simply approach to building heightmaps using JNoise, this can be expanded to other noise implementations as well.
1
// Noise used for the height
2
JNoise noise = JNoise.newBuilder()
3
.fastSimplex()
4
.setFrequency(0.005) // Low frequency for smooth terrain
5
.build();
6
7
// Set the Generator
8
instance.setGenerator(unit -> {
9
Point start = unit.absoluteStart();
10
for (int x = 0; x < unit.size().x(); x++) {
11
for (int z = 0; z < unit.size().z(); z++) {
12
Point bottom = start.add(x, 0, z);
13
14
synchronized (noise) { // Synchronization is necessary for JNoise
15
double height = noise.getNoise(bottom.x(), bottom.z()) * 16;
16
// * 16 means the height will be between -16 and +16
17
unit.modifier().fill(bottom, bottom.add(1, 0, 1).withY(height), Block.STONE);
18
}
19
}
20
}
21
});
Copied!
Here's and example of what that looks like: