Get world by name?

I’m trying to update my plugin to API8/9 and trying to figure out how to do
Is getting all the worlds and filtering/finding by properties().name() == worldName the way to go now, or is there a better one?

Due to the fact you shouldn’t be saving by worldname anymore, but instead it’s resource key, there isn’t a getWorld(String name)

However personally I do know it is a lot easier to use the world name for commands, so yes looping though is probably the best way

Your way of


will only get you the currently loaded worlds.

You can do

.map(key -> key.value())
.filter(worldName -> worldName.equalsIgnoreCase(yourWorldName))

to get a world by its key name (not its actual name) even when offline

But because you didn’t say what the use was. I’ll warn you now. You should not be using display names as persistent forms of identification. Instead use UUID or ResourceKeys

Also == doesn’t work for strings. == means memory equal. As strings are immutable, it is highly unlikely that it will be true. The only time it would be true is you create a string, store it in a variable and then compare that variable to itself, or something similar

1 Like

My use case is reading position and some world identifier from the config (probably the name, as I think it is the easiest options for the admins), and then listening for block interactions and checking whether the world and position matches.
I suppose storing just the world name and comparing it is the way to go, on the previous API I just stored the World object, but I suppose it might have been bad practice even at that time.
As for the ==, I forgot to mention that I use kotlin :slight_smile:

1 Like

Ah Koltlin, I personally never liked it, but fair enough.

As for storage, if I was a admin who changed the world name of one of my worlds, I rather it be the resource key as I don’t need to go though each and every one of my configs. But that’s me personally (I’m weird as it is - I use Linux as a daily driver :wink: )

As for storing the world object in the previous… Pretty sure there was a TypeTokens of world, so that probably caught your bad practise. Don’t quote me on that though

1 Like

btw, I use Arch :slight_smile:

Good point about changing every config file, probably I should provide both options (by name / by resource key).

The main question is solved, another question from user perspective (one who is not up to date with latest updates), how do I get resource key of a world?

I tried Groovy and Kotlin multiple times for whole large projects. I hated every bit about the Kotlin. Groovy was ok… Just … Java XD

User wise, I believe by default is just minecraft:<world name when updated/created as key format> so it’s just something you pick up. But to be honest, I’m out of the admin game and have been for a while

Hm, I’m not sure I get this part - <world name when updated/created as key format> :sweat_smile:
What does “updated/created as key format” mean? Does it mean the resource key for the default world should be minecraft:world?

Yep. However if they had it as Hello World it would be hello_world

Weird, this is the first thing I’ve tried, and it gives me Optional.empty: println(Sponge.server().worldManager().world(ResourceKey.minecraft("world"))).
I’m doing it during StartedEngineEvent<Server>, so everything should be loaded already. It is the very default world, no changes/modifications

See this RegionGuard/ at a7191e88ce5580d83f2b4eb47becba0c4b6cff95 · SawFowl/RegionGuard · GitHub
It works without fail.

1 Like