weaz19
weaz19

Reputation: 21

How to detect if a player has played before

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:

How can I check if the player has already played, so I can customize this behaviour?

Upvotes: 1

Views: 4155

Answers (1)

LeoColman
LeoColman

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

Related Questions