🗺️Using Coordinates

"How can I get a unique ID for every world / server / realm / etc to store my data under"

Coordinates are Java records that contain an ID based on the connection, and a generated human-readable name. Some may also contain extra data such as a singleplayer world's directory name, or a multiplayer server's address.

Coordinate Type
Example ID
User-friendly Name

Singleplayer / LAN host

singleplayer/New World

Singleplayer: New World

LAN (remote player)

lan/Herobrine - New World

LAN:

Multiplayer Server

multiplayer/mc_hypixel_net

Multiplayer:

Realm Server

Minecraft Realms: Builder's Palace

User-friendly names for Singleplayer, Multiplayer and Realms coordinates are localized based on the user's current language.

A common use point might be on server connect, such as in Fabric's ClientPlayConnectionEvents#JOIN:

ClientPlayConnectionEvents.JOIN.register((handler, sender, client) -> {
    Coordinate.getCurrent().ifPresent(coordinate -> {
        String id = coordinate.id();
        
        // load data, check server id, etc
    });
});

If you're making a mod specifically to work with a server, you can easily refine a coordinate and check it's address:

if (coord instanceof Coordinate.Multiplayer multi
    && multi.address().contains("hypixel.net")) {
    // hypixel specific stuff
}

Last updated