User Tools

Site Tools


tutorial:chunkgenerator

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
tutorial:chunkgenerator [2022/12/05 16:35] – added clarification to multithreaded chunk generation miirtutorial:chunkgenerator [2025/02/08 20:28] (current) – adds note to use Registries.CHUNK_GENERATOR on 1.19.3 and above 3xpl0173d
Line 97: Line 97:
 ==== Registering and Codecs ==== ==== Registering and Codecs ====
  
-Like other features in the game, ''ChunkGenerators'' have to be registered. However, you don't pass the registry a static instance-- instead, you give it a ''Codec'' instance. You can put whatever you want in the codec-- Mojang provides serialization codecs for a lot of useful objects, including entire registries. Then, all that's left is to register your chunk generator. Put the following line into your mod initializer ''onInitialize'' method:+Like other features in the game, ''ChunkGenerator''have to be registered. However, you don't pass the registry a static instance-- instead, you give it a ''Codec'' instance. You can put whatever you want in the codec-- Mojang provides serialization codecs for a lot of useful objects, including entire registries. The easiest way to do this is via ''RecordCodecBuilder''. Here's an example codec: 
 +<code java> 
 +    public static final Codec<ExampleChunkGenerator> CODEC = RecordCodecBuilder.create(instance -> 
 +        instance.group( 
 +                BiomeSource.CODEC.fieldOf("biome_source").forGetter(ExampleChunkGenerator::getBiomeSource), 
 +                Codec.INT.fieldOf("sea_level").forGetter(ExampleChunkGenerator::getSeaLevel), 
 +                Codec.INT.fieldOf("world_height").forGetter(ExampleChunkGenerator::getWorldHeight), 
 +                Identifier.fieldOf("custom_block").forGetter(ExampleChunkGenerator::getCustomBlockID) 
 +                ).apply(instance, ExampleChunkGenerator::new)); 
 +</code> 
 +Each line in the codec corresponds to a different field in your chunk generator that should be configurable. In this example, I pass a ''BiomeSource'' and a few ''int''s. Most objects you will want to add into your chunk generator will have a ''Codec'' defined for them. Primitives have theirs defined in the ''Codec'' class, and other classes (for example, ''BiomeSource'' or ''Identifier'') have theirs defined in their classes. 
 + 
 +To use the fields properly, you have to add them, along with getters, into your class: 
 + 
 +<code java> 
 +private final BiomeSource biomeSource; 
 +public BiomeSource getBiomeSource() { 
 +    return this.biomeSource; 
 +
 +private final int seaLevel; 
 +public int getSeaLevel() { 
 +    return this.seaLevel; 
 +
 +// and the same for the other fields 
 +</code> 
 + 
 +and then, create a constructor for your generator that uses each of those fields **in the same order that you listed them in the codec**: 
 +<code java> 
 +public ExampleChunkGenerator(BiomeSource biomeSource, int seaLevel, int worldHeight, Identifier customBlockID) { 
 +    super(biomeSource); 
 +    this.seaLevel = seaLevel; 
 +    this.worldHeight = worldHeight; 
 +    this.customBlock = Registries.BLOCK.getOrThrow(RegistryKey.of(RegistryKeys.BLOCK, defaultBlock)).getDefaultState(); 
 +    this.customBlockID = customBlockID; // this line is included because we need to have a getter for the ID specifically 
 +
 +</code> 
 + 
 + 
 +Then, all that's left is to register your chunk generator. Put the following line into your mod initializer ''onInitialize'' method:
 <code java> <code java>
 Registry.register(Registry.CHUNK_GENERATOR, new Identifier("wiki-example", "example"), ExampleChunkGenerator.CODEC); Registry.register(Registry.CHUNK_GENERATOR, new Identifier("wiki-example", "example"), ExampleChunkGenerator.CODEC);
 +</code>
 +//* For Minecraft Versions 1.19.3 and above, use ''Registries.CHUNK_GENERATOR'' instead of ''Registry.CHUNK_GENERATOR''
 +//
 +
 +Now, you can create json files to use your new chunk generator:
 +<code json>
 +// resources/data/wiki-example/dimension/example.json
 +{
 +  "type": "minecraft:overworld",
 +  "generator": {
 +    "type": "wiki-example:example",
 +    "custom_block": "minecraft:dirt",
 +    "sea_level": 0,
 +    "world_height": 384,
 +    "biome_source": // etc.
 </code> </code>
  
 And you're done! If you want to use your new generator, it might be helpful to wrap it with a [[tutorial:world_presets|WorldPreset]]. And you're done! If you want to use your new generator, it might be helpful to wrap it with a [[tutorial:world_presets|WorldPreset]].
tutorial/chunkgenerator.1670258118.txt.gz · Last modified: 2022/12/05 16:35 by miir