Skip to content

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.

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

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:

Grow maples in every forest biome
ResourceKey<PlacedFeature> maple = RegistryLookups.placedFeatureKey(id("maple"));
BiomeFeatures.add(maple, BiomeTags.IS_FOREST, GenerationStep.Decoration.VEGETAL_DECORATION,
mapleEnabled);

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:

Add maples to the biomes the mod's own tag lists
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.

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:

Fill one block of a vein, waterlogged where it replaces water
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:

Place a rule's plant only where it can stay
if (WorldGenBlocks.canSurvive(level, at, plant)) WorldGenBlocks.place(level, at, plant);

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:

Count tracked ores in every section of a chunk
// 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:

Count tracked ores on a grid of one block in eight
SectionCounts.count(section, column, counts, 2);

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:

Recompute the deposit a chunk rolled at generation
Optional<Deposit> rolled = FeatureRandoms
.forChunk(level, chunkPos, DEPOSITS, GenerationStep.Decoration.TOP_LAYER_MODIFICATION)
.map(MyFeatures::rollDeposit);

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:

Place only within a few chunks of a stronghold
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;

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:

Keep vanilla's spawn point in Superflat and Single Biome worlds
WorldSpawnCallback.EVENT.register((level, spawn) ->
WorldGenPresets.isSuperflat(level) || WorldGenPresets.isSingleBiome(level)
? spawn
: findSafe(level, spawn).orElse(spawn));

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:

Add maple saplings to plains village houses
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:

Add the mod's maple table to dungeon chests
LootAdditions.addTableRoll(BuiltInLootTables.SIMPLE_DUNGEON, id("chests/maple"), mapleEnabled);
Make 1 in 20 desert pyramid finds a maple sapling
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#replaceBrushed addition 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.

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_table entry 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.json with "replace": true and leaves out stope:pools turns off every added pool on Forge.