Solved Get the name of a right-clicked player

Discussion in 'Spigot Plugin Development' started by ChickenSaysBak, May 6, 2017.

  1. Hi, I'm having trouble getting the name of a right-clicked player, I've done a lot of googling, and most people find something that worked for them, but when I try it's not working for me. So basically what is happening is when I try to use event.getRightClicked(); (after casting to player) instead of it saying their name is "ChickenSaysBak" (for example) it will instead say that it is "CraftPlayer{name=ChickenSaysBak}" and I have tried soo many things to try to get rid of the craftplayer part but nothing is working.

    Here are a couple things that I tried:

    Code (Text):
        @EventHandler
        public void onPlayerClick(PlayerInteractEntityEvent event) {

            Player player = event.getPlayer();

            Player clickedPlayer = (Player) event.getRightClicked();

            player.sendMessage("Test: You clicked " + clickedPlayer.getName());
    Code (Text):
        @EventHandler
        public void onPlayerClick(PlayerInteractEntityEvent event) {

            Player player = event.getPlayer();

            Entity entity = event.getRightClicked();
           
            if (entity instanceof Player) {
               
                Player clickedPlayer = (Player) entity;
               
                player.sendMessage("Test: You clicked " + clickedPlayer.getName());
               
            }
    Help is appreciated! :)
     
  2. The second one should work. Did you register your events? Is the plugin loading?
     
  3. GG... I found the solution right after posting this. Well, just like all the other posts were saying, they needed to add .getName() to the end. My full code is using a hashmap, not the test message used in the code above^ I didn't realize you could add .getName() to the second part of the hashmap, I had just watched a tutorial on how do use these and the person used regular player objects for both. But anyways, I hope this helps anyone else with the same problem xD