User management
Every player is considered a user in the API. We use the SocialUser
class to ensure platform abstraction and add some useful methods.
Getting a SocialUser
from a Player
You can get the SocialUser
instance of a Player
by referencing its UUID and using the built-in UserManager
:
UUID playerUuid = player.getUniqueId();
SocialUser user = Social.get().getUserManager().getByUuid(uuid);
Now that we have the user instance, why don't we try putting it to use?
Sending messages
The SocialUser
class implements Adventure's Audience. This means that you'll have access to all of its methods.
For example, we can send a Component with Audience#sendMessage
:
Of course, if you're using social's API you'll probably want to send messages with social's text processors. We have created a simple SocialUser#sendParsableMessage
method for this:
var message = "Hello, <gold>$(nickname)</gold>! :raw_smile:";
user.sendParsableMessage(message);
Accessing the Player
instance from a SocialUser
You can access the Player
instance from a SocialUser
with the method SocialUser::player
. Do note that this method returns an Optional
because some users might be dummies or offline. Here's an example of obtaining the Player
instance safely:
user.player().ifPresent(player -> {
// Code that uses the player instance
});
Dummies
Dummies are non-existant users that might be used to perform certain actions that require a SocialUser
without reflecting them on a player. You can create a dummy with SocialUser#dummy
:
SocialUser dummy = SocialUser.dummy();
// You can also create a dummy with a specific main channel:
SocialUser dummy = SocialUser.dummy(channel);
You can check if a SocialUser
is actually a dummy by using Java's instanceof
:
Warning
Dummies are a workaround we provide for very specific cases. It's better to avoid using them unnecessarily. Ignoring this warning will most likely end up causing exceptions in runtime.