Features and loot
These types write a feature’s blocks into the world, count the blocks of a chunk section,
recompute what a feature rolled in a chunk, find where strongholds generate, tell vanilla’s
Superflat and Single Biome presets from other worlds, add a mod’s
features to biomes, and add its items to loot tables, the last two behind a switch the mod
reads from its settings. Use WorldGenBlocks to write any feature’s blocks, SectionCounts
in place of vanilla’s palette count, which allocates on each call, FeatureRandoms in place of
copying vanilla’s decoration seeding, Strongholds in place of /locate’s search, which can
load chunks, WorldGenPresets in place of an instanceof test, which also matches a mod’s
subclass, and BiomeFeatures and LootAdditions in place of each loader’s own biome modifiers and loot events, which differ
between Fabric and Forge.
At a glance
Section titled “At a glance”| Type | Summary | Status | Since | Side | Thread |
|---|---|---|---|---|---|
BiomeFeatures |
Adds placed features to biomes. | Experimental | 2.1.0 | both | setup |
FeatureRandoms |
The randoms world generation handed placed features. | Experimental | 2.13.0 | server | server |
LootAdditions |
Adds a pool to a loot table, or a chance to replace a brushed archaeology item. | Experimental | 2.1.0 | both | setup |
SectionCounts |
Block counts of a chunk section. | Experimental | 2.13.0 | server | any |
Strongholds |
The chunks where a level’s strongholds generate. | Experimental | 2.10.0 | server | any |
WorldGenBlocks |
Block writes and survival checks for world features. | Experimental | 2.10.0 | server | any |
WorldGenPresets |
Whether a level is one of vanilla’s Superflat or Single Biome world presets. | Experimental | 2.14.0 | server | any |
Quick start
Section titled “Quick start”The placed feature is the mod’s data/mymod/worldgen/placed_feature/maple.json. With
mapleEnabled, a BooleanSupplier that reads the mod’s settings, in your mod’s common
initializer:
ResourceKey<PlacedFeature> maple = RegistryLookups.placedFeatureKey(id("maple"));BiomeFeatures.add(maple, BiomeTags.IS_FOREST, GenerationStep.Decoration.VEGETAL_DECORATION, mapleEnabled);BiomeFeatures
Section titled “BiomeFeatures”Experimental · Since 2.1.0 · Side: both · Thread: setup
BiomeFeatures adds a mod’s placed features, such as its trees, to the biomes a tag names,
behind a switch the mod reads from its settings. A biome tag of the mod’s own lets data packs
choose the biomes; its file is data/mymod/tags/worldgen/biome/has_maples.json. With maple
and mapleEnabled as in the quick start:
BiomeFeatures.add(maple, RegistryLookups.biomeTagKey(id("has_maples")), GenerationStep.Decoration.VEGETAL_DECORATION, mapleEnabled);- A feature key with no placed feature in the server’s data fails when the server starts.
- A mod ships no Forge biome modifier JSON: Stope’s Forge jar carries the one biome modifier every mod’s features go through.
WorldGenBlocks
Section titled “WorldGenBlocks”Experimental · Since 2.10.0 · Side: server · Thread: any
WorldGenBlocks sets a feature’s blocks with the update flags vanilla’s features use: tree
flags for logs and leaves, as in the
Blueprint example,
and no neighbor updates for the rest. With level from the feature’s context, at, a
position the feature fills, and ANDESITE_ORE, the mod’s block handle:
BlockState ore = ANDESITE_ORE.get().defaultBlockState();if (level.getFluidState(at).is(FluidTags.WATER)) ore = WorldGenBlocks.waterlogged(ore);WorldGenBlocks.place(level, at, ore);With plant a block state from a data pack’s rule:
if (WorldGenBlocks.canSurvive(level, at, plant)) WorldGenBlocks.place(level, at, plant);SectionCounts
Section titled “SectionCounts”Experimental · Since 2.13.0 · Side: server · Thread: any
SectionCounts counts a chunk section’s block states, or those on a fixed grid of its
blocks, into an array the caller keeps. In a scanner class built once per scan, with oreIndex, the mod’s table from block state ID to a
column of counts (0 for every state it does not track), oreColumns its number of columns,
and chunk the chunk being scanned:
// Fields, made once: neither the array nor the key is made per chunk or per section.final int[] counts = new int[oreColumns];final ToIntFunction<BlockState> column = state -> oreIndex[Block.getId(state)];
void scan(LevelChunk chunk) { Arrays.fill(counts, 0); for (LevelChunkSection section : chunk.getSections()) { if (section.hasOnlyAir()) continue; SectionCounts.count(section, column, counts); }}For an estimate from one block in eight, in the same loop:
SectionCounts.count(section, column, counts, 2);FeatureRandoms
Section titled “FeatureRandoms”Experimental · Since 2.13.0 · Side: server · Thread: server
FeatureRandoms rebuilds the random world generation handed a placed feature in a chunk, so
a command can say what the feature rolled there. Its Javadoc says when a placed feature’s
modifiers must be replayed first.
In a command on the server thread, with level the command’s ServerLevel, chunkPos the
ChunkPos asked about, DEPOSITS, the mod’s placed feature key, added at top layer
modification with no placement modifiers, and MyFeatures#rollDeposit, the mod’s own method
that makes the roll its feature’s Feature#place makes, returning its Deposit record:
Optional<Deposit> rolled = FeatureRandoms .forChunk(level, chunkPos, DEPOSITS, GenerationStep.Decoration.TOP_LAYER_MODIFICATION) .map(MyFeatures::rollDeposit);Strongholds
Section titled “Strongholds”Experimental · Since 2.10.0 · Side: server · Thread: any
Strongholds gives the chunks where a level’s strongholds generate, for a feature that places
something by them. With NEAR the distance in chunks a feature
counts as near, in a feature’s place, with context its FeaturePlaceContext:
ChunkPos here = new ChunkPos(context.origin());List<ChunkPos> strongholds = Strongholds.ringChunks(context.level());boolean nearStronghold = false;for (int i = 0; i < strongholds.size() && !nearStronghold; i++) { nearStronghold = strongholds.get(i).getChessboardDistance(here) <= NEAR;}if (!nearStronghold) return false;WorldGenPresets
Section titled “WorldGenPresets”Experimental · Since 2.14.0 · Side: server · Thread: any
WorldGenPresets tells vanilla’s Superflat and Single Biome worlds from every other, for a
rule that should not apply in them. In
your mod’s common initializer, with findSafe, the mod’s own search near a spot, and the
WorldSpawnCallback
event:
WorldSpawnCallback.EVENT.register((level, spawn) -> WorldGenPresets.isSuperflat(level) || WorldGenPresets.isSingleBiome(level) ? spawn : findSafe(level, spawn).orElse(spawn));LootAdditions
Section titled “LootAdditions”Experimental · Since 2.1.0 · Side: both · Thread: setup
LootAdditions adds pools to loot tables and replacement items to archaeology loot, each
behind a switch the mod reads from its settings. With MAPLE_SAPLING, the sapling’s
Registered handle, and mapleEnabled as in the quick start:
LootAdditions.addPool(BuiltInLootTables.VILLAGE_PLAINS_HOUSE, () -> LootPool.lootPool() .add(LootItem.lootTableItem(MAPLE_SAPLING.get())) .when(LootItemRandomChanceCondition.randomChance(0.25f)), mapleEnabled);A mod’s own loot table can hold the odds, so one call adds all of it to a chest. With
data/mymod/loot_tables/chests/maple.json, which rolls a maple sapling one time in four:
LootAdditions.addTableRoll(BuiltInLootTables.SIMPLE_DUNGEON, id("chests/maple"), mapleEnabled);LootAdditions.replaceBrushed(BuiltInLootTables.DESERT_PYRAMID_ARCHAEOLOGY, 0.05f, random -> new ItemStack(MAPLE_SAPLING.get()), mapleEnabled);- A mod ships no Forge loot modifier JSON: Stope’s Forge jar carries the one global loot modifier every added pool goes through.
- A
LootAdditions#replaceBrushedaddition whose roll hits but whose item is empty passes the brush on to the next addition for that table. - Brushed replacements work the same on Fabric and Forge.
Edge cases
Section titled “Edge cases”Two Forge differences follow from Forge’s global loot modifiers:
- On Forge a pool applies to the table the game rolled. A pool added to a table that another
table reaches through a
loot_tableentry rolls on Fabric but not on Forge. Add pools to the table the game rolls; chest tables are rolled directly. - A data pack that replaces
data/forge/loot_modifiers/global_loot_modifiers.jsonwith"replace": trueand leaves outstope:poolsturns off every added pool on Forge.
See also
Section titled “See also”- Tree shapes: planning a tree’s logs and leaves before a feature writes them, and drawing a shape for review.
- Registration and ids:
RegistryLookups, which gives the feature keys these calls take. - Block behavior: saplings that grow a mod’s trees, and the wood set whose logs and leaves they place.
- Config files and settings screens: the settings these switches read.
- Server lifecycle events: a new world’s spawn point and a player’s spawn spot, which a preset check often guards.