'Player player doesnt work (Minecraft Java Dev)

I'm writing the core plugin for my Minecraft server and I was implementing the PlayerJoin and Player leave functions when I came across this issue.

When I try to compile or package it says this:

Incompatible types. Found: 'java.lang.String', required: 'org.bukkit.entity.Player'

And I don't know what to do with it.

Heres the code:

import org.bukkit.ChatColor;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerJoinEvent;

public class PlayerJoin implements Listener {

    @EventHandler
    void onPlayerJoin(PlayerJoinEvent e){
        Player player = e.getPlayer().getDisplayName();
        e.setJoinMessage(ChatColor.GREEN + "[+] " + ChatColor.GRAY + "Welcome back adventurer! You may now continue your quest " + ChatColor.GREEN + player.getDisplayName());
    }
}


Solution 1:[1]

The e.getPlayer() method already return the player. You are using the getDisplayName() too early, and you just should remove it such as you invoke it when changing the join message.

The method getDisplayName() return a string, which can't be changed to Player. That's why you get this issue.

To conclude, you will have this code:

public class PlayerJoin implements Listener {

    @EventHandler
    void onPlayerJoin(PlayerJoinEvent e){
        Player player = e.getPlayer();
        e.setJoinMessage(ChatColor.GREEN + "[+] " + ChatColor.GRAY + "Welcome back adventurer! You may now continue your quest " + ChatColor.GREEN + player.getDisplayName());
    }
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Elikill58