Fabric API

Fabric API

112M Downloads

Overriding vanilla registries

langvest opened this issue ยท 2 comments

commented

#override #replace #vanilla #default #registry #feature

Problem description

I'd like my mod to replace all the vanilla birch trees (or whatever) in the game with my custom ones (or whatever). To do this, I'm trying to override the vanilla configured feature minecraft:birch to my custom configured feature that I've prepared in advance.

NOTE: birch trees must be completely replaced in the game, not only during the world generation phase, but also in all other possible game events and situations (e.g. when growing a birch sapling).

I've written some code examples (see below), but none of them work. In the error message, Fabric suggests using the .set() method instead of the .register(), but this method does not exist in the net.minecraft.core.Registry class (Parchment mappings) or net.minecraft.util.registry.Registry class (Yarn mappings). So I have no clue what to do with this information.

Vanilla source code

NOTE: I'm using Minecraft 1.18.2 with Parchment mappings.

package net.minecraft.data.worldgen.features;

// ...

public class TreeFeatures {
	// ...
	public static final Holder<ConfiguredFeature<TreeConfiguration, ?>> BIRCH = FeatureUtils.register("birch", Feature.TREE, TreeFeatures.createBirch().build());
	// ...
}

My code

NOTE: I'm using Minecraft 1.18.2 with Parchment mappings.

Way 1 (according to the vanilla source code)

๐Ÿ› ๏ธ Code

FeatureUtils.register("birch", Feature.TREE, MyCustomTreeFeatures.createMyCustomBirch().build());

โŒ Error

Attempted to register ID ResourceKey[minecraft:worldgen/configured_feature / minecraft:birch] at different raw IDs (24, 184)! If you're trying to override an item, use .set(), not .register()!

Way 2 (according to Fabric Wiki tutorial)

๐Ÿ› ๏ธ Code

Registry.register(Registry.FEATURE, "birch", Feature.TREE);
Registry.register(BuiltinRegistries.CONFIGURED_FEATURE, "birch", new ConfiguredFeature<>(Feature.TREE, MyCustomTreeFeatures.createMyCustomBirch().build()));

โŒ Error

Attempted to register object net.minecraft.world.level.levelgen.feature.TreeFeature@2aaa5122 twice! (at raw IDs 1 and 61 )

Way 3 (according to Fabric Wiki tutorial without first line)

๐Ÿ› ๏ธ Code

// Registry.register(Registry.FEATURE, "birch", Feature.TREE);
Registry.register(BuiltinRegistries.CONFIGURED_FEATURE, "birch", new ConfiguredFeature<>(Feature.TREE, MyCustomTreeFeatures.createMyCustomBirch().build()));

โŒ Error

Attempted to register ID ResourceKey[minecraft:worldgen/configured_feature / minecraft:birch] at different raw IDs (24, 184)! If you're trying to override an item, use .set(), not .register()!

Way 4 (using datapack)

๐Ÿ› ๏ธ File added

File path: src/main/resources/pack.mcmeta
File content:

{
	"pack": {
		"pack_format": 9,
		"description": "My mod resources."
	}
}

๐Ÿ› ๏ธ File added

File path: src/main/resources/data/minecraft/worldgen/configured_feature/birch.json
File content: the original JSON code of the vanilla birch tree configured feature, but with a slight modification of the trunk height.

โŒ Does not work

Changes are not applied, nothing happens. Maybe it has something to do with other issues:
#303
#66

Question

Is there any way to solve this problem without using mixins or datapacks (if possible)?

NOTE: I'm looking for a compact, simple, clear solution, so I don't prioritize the use of mixins and datapacks. If there are no other ways, then I can use them.

Suggestion

Please, add to Fabric Wiki articles explaining how to override some vanilla stuff in registries.

My system Info

Minecraft version: 1.18.2
Mappings: Parchment
Parchment mappings version: 2022.11.06
Fabric API version: 0.76.0+1.18.2
Fabric loader version: 0.14.24
Fabric loom version: 1.4-SNAPSHOT
Java version: 17
Gradle version: 8.4
OS: Windows 10

commented

should be able to override the vanilla configured features by using json files in newer versions due to datagen changes in minecraft. In older versions, you would use mixins to modify the existing feature (or remove the addition of the features to biomes).

Fabric does not support replacing registry objects. if you want to modify biomes to remove or replace features, there is a biome api that lets you remove features from biomes and add other features. Make your own tree feature and replace the features from the biomes that have them via the biome modification api

commented

Yeah, use data packs here. (1.18.2 will not receive feature updates anyway.)

In newer versions you can use the dynamic registry API to modify the registry, I believe.