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:32] – removed "draft" designation miirtutorial:chunkgenerator [2024/05/31 18:46] (current) – [Registering and Codecs] miir
Line 52: Line 52:
  
     /* this method builds the shape of the terrain. it places stone everywhere, which will later be overwritten with grass, terracotta, snow, sand, etc     /* this method builds the shape of the terrain. it places stone everywhere, which will later be overwritten with grass, terracotta, snow, sand, etc
-     by the buildSurface method. it also is responsible for putting the water in oceans. */+     by the buildSurface method. it also is responsible for putting the water in oceans. it returns a CompletableFuture-- you'll likely want this to be delegated to worker threads. */
     @Override     @Override
     public CompletableFuture<Chunk> populateNoise(Executor executor, Blender blender, NoiseConfig noiseConfig, StructureAccessor structureAccessor, Chunk chunk) {     public CompletableFuture<Chunk> populateNoise(Executor executor, Blender blender, NoiseConfig noiseConfig, StructureAccessor structureAccessor, Chunk chunk) {
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>
 +
 +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.1670257967.txt.gz · Last modified: 2022/12/05 16:32 by miir