How to get player's name

Discussion in 'Plugin Development' started by piddi24, Jan 1, 2021.

  1. Offline

    piddi24

    This is my class:

    Code:
    package me.hardboom.potiondeath;
    import org.bukkit.Bukkit;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.entity.PlayerDeathEvent;
    public class Potion {
        @EventHandler
        public void PlayerDeath(PlayerDeathEvent event) {
           
            Player player = Bukkit.getPlayer(null);
           
            
    
        }
    }
    [/CO
    Player player = Bukkit.getPlayer(null); does not work gives me an error:
    [CODE]The method getPlayer(String) is ambiguous for the type Bukkit
    Please help!
     
  2. Offline

    Mathias Eklund

    To get the player that died in the PlayerDeathEvent, use:
    Player player = event.getPlayer();

    then, to get the players name you can do:
    String name = player.getName();
     
  3. Offline

    Ago19

    Or in just one line
    String name = event.getPlayer().getName();
    If you only need the player's name and not the instance of the player this is way better, you use less memory
     

Share This Page