Skip to content

Commit 6a3a10a

Browse files
committed
updated userManager
1 parent 537f4ce commit 6a3a10a

File tree

1 file changed

+18
-26
lines changed

1 file changed

+18
-26
lines changed

source/plugin/offline-userplayer-data.rst

+18-26
Original file line numberDiff line numberDiff line change
@@ -2,32 +2,21 @@
22
Offline Player Data
33
===================
44

5-
.. warning::
6-
7-
These docs were written for SpongeAPI 7 and are likely out of date.
8-
`If you feel like you can help update them, please submit a PR! <https://github.com/SpongePowered/SpongeDocs>`__
9-
10-
115
.. javadoc-import::
12-
org.spongepowered.api.entity.living.player.Player
6+
org.spongepowered.api.entity.living.player.server.ServerPlayer
137
org.spongepowered.api.entity.living.player.User
148
org.spongepowered.api.profile.GameProfileManager
15-
org.spongepowered.api.service.ServiceManager
16-
org.spongepowered.api.service.user.UserStorageService
9+
org.spongepowered.api.user.UserManager
1710

1811
It may be necessary for plugins to access player data even when the player is offline.
19-
You might think that ``Sponge.getServer().getPlayer()`` returning a :javadoc:`Player` can be used for this.
20-
But since ``Player`` objects only exist for online players, another solution must be used.
12+
You might think that ``Sponge.server().player()`` returning a :javadoc:`ServerPlayer` can be used for this.
13+
But since all ``Player`` objects only exist for online players,
14+
another solution must be used.
2115

2216
Some plugins store the relevant data themselves and associate the user by using the :javadoc:`GameProfileManager`.
2317
But writing different code for offline and online users is not necessary.
24-
The :javadoc:`ServiceManager` natively provides a service known as the :javadoc:`UserStorageService` which is capable
25-
of returning :javadoc:`User` instances for ``Player``\s who are currently offline.
26-
Since the ``Player`` interface extends ``User`` most methods you call on a ``Player`` are also available.
27-
28-
For example:
29-
30-
* ``#hasPermission(String permission)`` is available from both instances.
18+
The :javadoc:`UserManager` is capable of returning :javadoc:`User` instances for ``Player``\s who are currently
19+
offline.
3120

3221
Code Example
3322
------------
@@ -38,23 +27,26 @@ Here's an example for a utility method that can be used to get a ``User``:
3827
3928
import java.util.Optional;
4029
import java.util.UUID;
30+
import java.util.concurrent;
4131
4232
import org.spongepowered.api.Sponge;
4333
import org.spongepowered.api.entity.living.player.User;
44-
import org.spongepowered.api.service.user.UserStorageService;
34+
import org.spongepowered.api.user.UserManager;
4535
46-
public Optional<User> getUser(UUID uuid) {
47-
Optional<UserStorageService> userStorage = Sponge.getServiceManager().provide(UserStorageService.class);
48-
return userStorage.get().get(uuid);
36+
public CompletableFuture<Optional<User>> getUser(UUID uuid) {
37+
UserManager userManager = Sponge.server().userManager();
38+
return userManager.load(uuid);
4939
}
5040
51-
This code will get the ``UserStorageService`` from the ``ServiceManager`` and then retrieve the ``User`` from there.
41+
This code will get the ``UserManager`` and then retrieve the ``User`` from there.
5242

5343
.. note::
5444

55-
The ``UserStorageService`` can only return ``User``\s who previously were connected.
45+
The ``UserManager`` will use local cached versions of the user, however will contact Mojang servers if
46+
the user has not previously joined, hence why the load returns a ``CompletableFuture``. If the user
47+
cannot be found in either cache or Mojang then the ``CompletableFuture`` will return ``Optional.empty``
5648

5749
.. note::
5850

59-
This solution will not return ``Player`` instances. This makes it safe to store the returned ``User`` objects,
60-
but you will need to use the ``User.getPlayer()`` method to retrieve the online entity.
51+
You can check if the player is loaded in cache with the ``exists(UUID playerUuid)`` method found in
52+
``UserManager``

0 commit comments

Comments
 (0)