Adding custom world generator

I looked at SpongeDocs, but there is nothing about world generator API. I couldn’t find any existing world generator plugin for sponge.
I also lookes at SpongeAPI source and I found these classes:
WorldGeneratorModifier
WorldGenerator
GeneratorType.

I want to completely replace vanilla world generation, so I thought implementing GeneratorType and WorldGenerator would be ok. But I can’t find any way to register GeneratorType.
It’s possible to register WorldGeneratorModifier, but since I want to replace everything - I would need to clear populator list (and possibly remove some populators added by other plugins, if it is/will be possible to use multiple WorldGeneratorModifiers).

What is the right way to completely replace world generator with sponge?

Writing something for the docs is on my TODO list, give me a bit and I’ll write up something basic for here.

1 Like

There is one WGen example in the Cookbook, a collection of demonstration plugins.

I hope that helps tide you over until Deamon has a chance to prepare some Docs!
Good luck with the Tall Worlds mod, I hope to see it on a Sponge Server some day.

1 Like

@Barteks2x Alright here it is, sorry it took a bit. At some point A form of this will probably make it into the actual documentation but here is my start on it. Ask any questions that you have.

WorldGen documentation gist

1 Like

@Inscrutable In this example plugin no new world generators are created. To use my custom generator I would seed to add command to create new world with that generator or use it in existing world. And when world generator onfiguration is implemented (something like setting generator in bukkit.yml for bukkit?) it wouldn’t work. But it should be good enough for testing.

@Deamon Nice explanation. As I understand it, to replace everything I need to implement WorldGeneratorModifier and replace BiomeGenerator, BaseGeneratorPopulator and all Populators with my own implementations.
Are you planning to add some other way to replace world generator? Maybe adding custom WorldType with it’s own world generator?

There is also WorldGenerator class that isn’t mentioned in the WorldGen documentation. World class has setWorldGenerator method. As currently there is no configuration for modifiers yet, I think I can use this as well.

You also wrote that Chunk size may not be 16x256x16 with other implementations.
If it’s higher - it’s probably not an issue.
To make difference it must be lower, so together with ChunkLayout class it seems to be some kind of support for 3d chunks implementations. I’m almost sure that they will be incompatible with custom world generation in SpongeAPI (maybe except biome generation). As an example, Tall Worlds mod uses 4 generation stages instead of 2.
And plugins will most likely ignore chunk y coordinate as long as there are no other implementations.

As I understand it, to replace everything I need to implement WorldGeneratorModifier and replace BiomeGenerator, BaseGeneratorPopulator and all Populators with my own implementations.

Thats correct.

Are you planning to add some other way to replace world generator? Maybe adding custom WorldType with it’s own world generator?

As I mentioned at the bottom its possible that we may add the ability to create some sort of custom GeneratorType (essentially WorldType), but at this point I’m not too sure what the use of this would be. The entire point of WorldGeneratorModifiers is to allow multiple mods to touch the world generator at the same time.

There is also WorldGenerator class that isn’t mentioned in the WorldGen documentation. World class has setWorldGenerator method. As currently there is no configuration for modifiers yet, I think I can use this as well.

You could, but I would heavily discourage this as you would not be compatible with other plugins wanting to make small changes or additions to the world generator. Additionally it may not work quite as you are thinking as it is cloned on set and then never referenced again if you were thinking of using it for anything more than setting the WGen components into the world.

If you’re only using that as a temporary method then you may as well write it in modifier form anyway and inject them using something like this for now until the configuration is added:

@Subscribe
public void onWorldLoad(WorldLoadEvent event)
{
    World world = event.getWorld();
    if (world.getName().equalsIgnoreCase("name")) {
        DataContainer arguments = new MemoryDataContainer();
        WorldGenerator worldGenerator = world.getWorldGenerator();
        modifyWorldGenerator(world.getCreationSettings(), arguments, worldGenerator);
        world.setWorldGenerator(worldGenerator);
    }
}

And then as soon as the configuration is added you simply delete that event subscription.

You also wrote that Chunk size may not be 16x256x16 with other implementations.
If it’s higher - it’s probably not an issue. To make difference it must be lower, so together with ChunkLayout class it seems to be some kind of support for 3d chunks implementations. I’m
almost sure that they will be incompatible with custom world generation in SpongeAPI (maybe except biome generation).

Sure it’ll be incompatible with plugins which build in the assumptions about the standard layout. However the point is that we are also not excluding detecting and building in support for these alternate systems.

As an example, Tall Worlds mod uses 4 generation stages instead of 2.

I’m curious what are the 4 stages for their generation?

In Tall Worlds mod the 4 stages are:
Terrain (stone abd water blocks, no caves etc),
Surface (dirt/grass/sand blocks - biome blocks),
Structures (caves, ravines etc…),
(lighting, doesn’t generate terrain - so I didn’t count it as terrain generation stage),
Features (populator).

We need it because of the way biome block generation works (it needs information about blocks above before generating structures). In vanilla the first 3 are done in the first stage. Note: this mod is open source.