GetOfflinePlayer

Hello Community,

I have a problem, getting an PlayerObject when the Player was not online at all or is not online at the moment.

In Bukkit there is the Method: Bukkit.getOfflinePlayer(String name)

But I could not find anything similar in Sponge!

Do you have an advice for me?

Bye
Forward

In the UserStorageService, there’s a method to get a User by UUID or String. Note that User is both at the same time a Player and can be an offline Player’s data representation.

Thanks a lot gabizou!

How do I use the UserStorageService? What are the main commands?

Forward

It’s accessed through the Service API, so it would look like this:

UserStorageService userStorage = Sponge.getServiceManager().provide(UserStorageService.class);

Huge Thanks! :slight_smile:

Ok now I’m facing another problem:

if I’m trying to get a User, who wasn’t online at all. The UserStorageService.getUser(String name) fails to get an User. And the resulting Optional isn’t present.

I stumbled over another Method in the UserStorageService class: getOrCreate(GameProfile arg0)

But I don’t know how to initialize a new GameProfile Object.

When I could create this new GameProfile, i could easily get the UUID by: GameProfile.getUniqueID()

Forward

Ok I’m back again. I mad some progress, beacause I could get the UUID of a Player who wasn’t online on the Server at all by the Method:

service.getOrCreate(Sponge.getGame().getServer().getGameProfileManager().get(playername, false).get()).getUniqueId();

So I can get a GameProfile of an User who wasn’t online at all. But how do I get the User instance out of this GameProfile?

Oh I got it. Shame on me xD

Forward

This doesn’t work for me. It says that I have to provide java.lang.Class<T> instead of java.lang.Class<org.spongepowered.api.service.user.UserStorageService>

provide returns an Optional so it should be

Optional<UserStorageService> userStorage = Sponge.getServiceManager().provide(UserStorageService.class);

Alternatively you could use provideUnchecked which throws an unchecked exception if the service does not exist.

1 Like

Optional<User> userOptional = userStorage.get(uuid); doesn’t work.
Is there anything that I have to do first?

Make sure your uuid object is in fact an instance of UUID and not String because the get(String) searches by name not UUID.
If uuid is a string, just do UUID.fromString(uuid);

It’s an actual UUID

OK, are you sure the player UUID actually existed on the server? The service will look for known players (those who have player data in the playerdata folder), players on the whitelist and players on the ban list.

Since it’s optinal, shouldn’t it just give a non-present value if the player doesn’t exist on the server?

Yes, if a player name or uuid is not known to the server, it will return Optional.empty()

Weird. it just just throws a NullPointerException no matter whether the playerdata exists or not. Does it matter where the UserStorageService is being initialized? (post- or pre- GameInitialization etc.)

You can’t use the service before the world data has loaded. Therefore wait until GameStartedServerEvent

1 Like