UtilityBiome:Chunk Features
A chunk feature is a piece of Java code that Utility Biome runs once on every newly generated chunk whose biome matches. Waterfalls, overhangs, boulders or small ruins can be made this way, things the vanilla world generator cannot produce from its JSON files alone. Better Biomes uses chunk features for many of its details, for example betterbiomes:waterfall and betterbiomes:overhang.
All types on this page are in the package com.cyberpiratepenguin.utilitybiome.api.
Registering a feature
Register features in your own setup(). A feature only affects chunks generated after it was registered. Chunks that already exist stay as they are.
@Override
protected void setup() {
UtilityBiomeAPI.registerChunkFeature(ChunkFeature.named("mymod:boulders")
.inBiomes("Zone1_Tier*/Tile.Plains_*")
.chance(0.1) // share of matching chunks
.order(10) // lower runs first
.placer((chunk, random) -> {
int x = random.nextInt(ChunkAccess.SIZE);
int z = random.nextInt(ChunkAccess.SIZE);
int y = chunk.height(x, z) + 1;
chunk.setBlock(x, y, z, "Rock_Stone_Cobble");
}));
}
| Setting | Default | Meaning |
|---|---|---|
named(id) |
required | Unique ID with your mod's namespace, mod:name. An ID without : is rejected.
|
inBiomes(globs...) |
all biomes | Globs on the biome at the centre of the chunk, written as Zone/Tile.Name or Zone/Custom.Name. * matches any number of characters, ? exactly one. The glob must match the whole name.
|
chance(p) |
1 | Share of matching chunks that get the feature, from 0 to 1. |
order(n) |
0 | Features run in ascending order, ties by ID. |
placer(...) |
required | The code that places the feature. Finishes the builder and returns the ChunkFeature.
|
What a placer may do
A ChunkPlacer gets a ChunkAccess for the new chunk and a random generator.
- Only this chunk. Local coordinates are 0–31 for x and z and 0–319 for y. Writing outside throws. To plan beyond the chunk, use the world lookups below.
- Not on the world thread. Placers run in parallel for several chunks. Only read and write
chunk, and do not change any shared state of your own. - All randomness from
random. It is derived from the world seed, the feature ID and the chunk position. The same world always gives the same result, which matters because Hytale regenerates chunks that were never saved. - All or nothing. Writes are collected and only applied when the placer returns. If it throws, nothing of it is applied, a warning goes to the server log, and the chunk loads normally. Other features still run.
ChunkAccess
| Method | Purpose |
|---|---|
chunkX(), chunkZ() |
Chunk position. Block x = chunkX * 32 + x.
|
height(x, z) |
Highest non-transparent block of the column. Leaves count. |
block(x, y, z) |
Block key without rotation, "Empty" for air.
|
isSolid(x, y, z) |
Whether the block is solid. |
fluid(x, y, z) |
Fluid key, or null.
|
biome(x, z) |
Zone and biome of the column, as a BiomeSample.
|
worldSeed() |
Seed of the world. Since 1.2.0. |
terrainHeight(worldX, worldZ) |
Natural terrain height at any world position, also outside this chunk. Since 1.2.0. |
biomeAt(worldX, worldZ) |
Zone and biome at any world position, also outside this chunk. Since 1.2.0. |
setBlock(x, y, z, key) |
Sets a block. Keys are written as in the assets, with rotation if needed: "Rock_Stone", "Rock_Ice_Icicles|Pitch=180".
|
setFluid(x, y, z, key) |
Sets a fluid at full level, for example "Water_Source".
|
setContainer(x, y, z, key, dropList) |
Sets a container (chest, barrel, pot ...) and fills it from a drop list, for example vanilla "Zone1_Undead_Tier1". The content is rolled once, when the chunk first loads. Since 1.1.0.
|
Constants: ChunkAccess.SIZE = 32, ChunkAccess.HEIGHT = 320.
Notes:
- Fluids live in their own layer next to the blocks. The block at that spot must be air or non-solid, otherwise the water disappears. Water set by a feature flows when the chunk loads.
- Inside a placer,
blockandfluidalready see your own writes.heightandisSolidshow the state before your placer ran. - Blocks placed by a chunk feature get their block entity, so chests have storage, crops grow and spawn markers work. In 1.0.0 this was missing. See Version History.
setContaineron a block that is not a container throws, so nothing of the placer is applied.
Looking beyond the chunk
A placer only writes its own chunk, but since 1.2.0 it can ask the world generator about any position. This lets a structure span several chunks: each chunk plans the whole structure from the same data and then builds only its own part.
worldSeed(): the seed of the world, for example to derive a layout that is the same in every chunk.terrainHeight(worldX, worldZ): the natural height from the generator's height noise, without caves, covers, prefabs and features. It is the same in every chunk and on every call, also for chunks that do not exist yet. Each call costs one noise evaluation, so sample sparingly.biomeAt(worldX, worldZ): zone and biome at any position, the same answer asbiome(x, z)gives inside the chunk.
terrainHeight can differ from height in the same column, because height reads the real blocks with trees, covers and earlier features. Better Biomes uses these lookups for its mountain villages, which stretch over many chunks.
.placer((chunk, random) -> {
// the same cell, and so the same plan, in every chunk it touches
int cellX = Math.floorDiv(chunk.chunkX() * ChunkAccess.SIZE, 256);
int cellZ = Math.floorDiv(chunk.chunkZ() * ChunkAccess.SIZE, 256);
long planSeed = chunk.worldSeed() ^ (cellX * 341873128712L + cellZ * 132897987541L);
int centreX = cellX * 256 + 128;
int centreZ = cellZ * 256 + 128;
int ground = chunk.terrainHeight(centreX, centreZ);
if (!chunk.biomeAt(centreX, centreZ).biome().startsWith("Plains_")) {
return;
}
// ... plan with planSeed and ground, then write only the blocks inside this chunk
})
Do not use the random of the placer for the shared plan: it differs per chunk.
These three methods only work in the running game. In unit tests with your own ChunkAccess they throw an UnsupportedOperationException unless you implement them.
For boxes, spheres, building sites and terrain checks, see Structures.
Checking features in the game
/ub featureslists all registered features, their chance, their biomes and how often each was placed since the server started./ub feature <id>teleports you to a spot where the feature was placed.- Every placement is written to the server log with its position and number of changes. Every 30 seconds at most, a summary line shows how many chunks were generated, how many features were placed or failed and the slowest time per chunk.
See Commands.
Test features
For developing Utility Biome itself, the environment variable UTILITYBIOME_TEST_FEATURES (any value) registers two test features at start:
ub:test_pillar: in 5 % of chunks, a white crystal pillar in the middle of the chunk with a water source on top.ub:test_broken: throws on purpose in 2 % of chunks, to check that nothing is applied and a warning is logged.
Never set it on a server people play on.