[Solved] Getting the world by the String of Get Default World Name returns NoSuchElementException

World world = Sponge.getServer().getWorld(Sponge.getServer().getDefaultWorldName()).get();

The line of code above should theoretically return The default world in a variable called world, but the problem is that it’s empty and returns a “NoSuchElementException”. Please tell me where I am goofing because I can’t figure out where i went wrong.

Hey ! I’ve noticed that you solved your own problem.

But just to create a resource for anyone else that might be having troubles, posting the solution also helps :slight_smile:

Sponge Server has the following methods

    /**
     * Gets a loaded {@link World} by its unique id ({@link UUID}), if it
     * exists.
     *
     * @param uniqueId UUID to lookup
     * @return The world, if found
     */
    Optional<World> getWorld(UUID uniqueId);

    /**
     * Gets a loaded {@link World} by name, if it exists.
     *
     * @param worldName Name to lookup
     * @return The world, if found
     */
    Optional<World> getWorld(String worldName);

    /**
     * Gets the properties of default world.
     *
     * @return The world properties
     */
    Optional<WorldProperties> getDefaultWorld();

    /**
     * Gets the default {@link World} name that the server creates and loads.
     *
     * @return The name
     */
    String getDefaultWorldName();

My guess is that the code was being called, before the world was loaded?

@drunkripper what was the solution or workaround you came up with?

1 Like

I just was calling the method in the Game Init Event, and i’ve moved it over to a new listener which is listening for Game Started Event where the worlds are loaded.

1 Like