Delete WorldProperties before the World is loaded

When the server starts there are a few worlds I would like to delete before they are loaded. server.deleteWorld() accepts an WorldProperties argument. server.getAllWorldProperties() returns all world properties.

I was thinking I could use server.getAllWorldProperties() to delete some of the worlds (I also delete these worlds when the server shuts down, but the worlds won’t get deleted if the server crashes for example). So I threw this in real quick:

Server server = ...;
void onServerAboutToStart(GameAboutToStartServerEvent event) {
    Log.info("Total worlds: " + server.getWorldProperties().size()); // prints "0" to console

    for (WorldProperties world : server.getWorldProperties()) {
            if (world.getName().startsWith("foo")) {
                server.deleteWorld(world);
            }
        }
}

The problem is that it seems like server.getAllWorldProperties() returns an empty collection during GameAboutToStartServerEvent, so this won’t work. I assume that WorldProperties are not loaded until GameStartingServerEvent is ran, but by then it is already too late because the world has already been loaded. I would then have to unload it and then delete it, which seems like a waste of time.

Is there a way to access and delete the world properties before the worlds are loaded? Or do I have to manually use IO to delete the folders world/foo_world and config/sponge/worlds/minecraft/overworld/foo_world?

It sounds like you are wanting to create a world that doesn’t save to disk?
If so, you can call WorldProperties#setSerializationBehavior(SerializationBehaviors.NONE) on the properties when creating the world. This will make it never persist to disk.

Thank you for the suggestion. You are correct that I’m trying to create a world that does not save to disk.

I was unaware of the method to set the serialisation behaviour. I tried it now but it seems that the “foo_world” folders are still being created, world/foo_world and config/sponge/worlds/minecraft/overworld/foo_world, and not removed when the server shuts down. It might not be saving any changes made to the world but the initial (generated) state of the world still seems to be saved to disk.

WorldProperties worldProperties = server.createWorldProperties("foo_world", WorldArchetypes.THE_VOID);
worldProperties.setSerializationBehavior(SerializationBehaviors.NONE);
server.loadWorld(worldProperties);