2
2
Offline Player Data
3
3
===================
4
4
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
-
11
5
.. javadoc-import ::
12
- org.spongepowered.api.entity.living.player.Player
6
+ org.spongepowered.api.entity.living.player.server.ServerPlayer
13
7
org.spongepowered.api.entity.living.player.User
14
8
org.spongepowered.api.profile.GameProfileManager
15
- org.spongepowered.api.service.ServiceManager
16
- org.spongepowered.api.service.user.UserStorageService
9
+ org.spongepowered.api.user.UserManager
17
10
18
11
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.
21
15
22
16
Some plugins store the relevant data themselves and associate the user by using the :javadoc: `GameProfileManager `.
23
17
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.
31
20
32
21
Code Example
33
22
------------
@@ -38,23 +27,26 @@ Here's an example for a utility method that can be used to get a ``User``:
38
27
39
28
import java.util.Optional ;
40
29
import java.util.UUID ;
30
+ import java.util.concurrent ;
41
31
42
32
import org.spongepowered.api.Sponge ;
43
33
import org.spongepowered.api.entity.living.player.User ;
44
- import org.spongepowered.api.service. user.UserStorageService ;
34
+ import org.spongepowered.api.user.UserManager ;
45
35
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);
49
39
}
50
40
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.
52
42
53
43
.. note ::
54
44
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 ``
56
48
57
49
.. note ::
58
50
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