Reputation: 21
I have a plugin that sends a message to a player when he joins:
@EventHandler
public void onPlayerJoin(PlayerJoinEvent p){
Player a = p.getPlayer();
a.sendMessage(ChatColor.BLUE + "Welcome message");
}
What I want is to send to the player two possible messages when he joins:
One if it's the first time the player joined the server, like "Welcome to the server"
The other is if the player have already joined the server before. How can that be done?
How can I check if the player has already played, so I can customize this behaviour?
Upvotes: 1
Views: 4155
Reputation: 7143
The Player
object has a method to determine if the player has already played on the server before:
yourPlayer.hasPlayedBefore();
This method returns a boolean which is true if the player has already played on the server, and false otherwise.
You can customize the welcome messages as the player joins the server with code that looks like this:
@EventHandler
public void onPlayerJoin(PlayerJoinEvent p) {
Player a = p.getPlayer();
boolean hasPlayed = a.hasPlayedBefore();
if (hasPlayed) {
a.sendMessage("Welcome back to the server!");
} else {
a.sendMessage("Welcome to our server! This is your first time playing!"
}
}
The boolean is saved with your players, so it will always be the situation of the player, either if he have played before or not. Even if the server restarts or the world changes (as long as the players folder is not deleted)
Upvotes: 2