Getting Player name when clicked on.

Discussion in 'Plugin Development' started by zanginator, Feb 1, 2014.

Thread Status:
Not open for further replies.
  1. Hi,
    Sorry if this has been already answered but I can not find the answer.

    I am trying to make a party/grouping system and the only hurdle left is to get the player name.

    So when I (right) click on a player I want to be able to get their displayname, but I am forever getting null returned to me.

    Code:java
    1. @EventHandler
    2. public void onPlayerInteract(PlayerInteractEntityEvent e) {
    3. Entity entity = e.getRightClicked();
    4. Player playersel = (Player) e.getPlayer(); //This part is non-functional
    5. if (!(entity instanceof Player)) return;
    6. if (e.getPlayer().getItemInHand().getType().equals(Material.DIAMOND_SWORD)) return;
    7. menu.show(e.getPlayer(), playersel);
    8. }


    With this method I can get my menu to show, but if I switch to PlayerInteractEvent the menu doesn't even appear.

    So to be clear, I do not need the player that caused the interact, I need the target player of the interact.

    Can anyone help?

    -zanginator
     
  2. It opens a custom inventory giving the player the option to add or remove the selected player to or from a party.
     
  3. zanginator mind giving me your whole class? Then I will try to solve it for you.
     
  4. Code:java
    1. **Omitted**
    2.  
    3. public class PartySystem extends JavaPlugin implements Listener {
    4.  
    5. private PartySystemInv menu;
    6.  
    7.  
    8. public void onEnable() {
    9. menu = new PartySystemInv(this);
    10. Bukkit.getServer().getPluginManager().registerEvents(this, this);
    11. }
    12.  
    13. @EventHandler
    14. public void onPlayerInteract(PlayerInteractEntityEvent e) {
    15. Entity entity = e.getRightClicked();
    16. Player playersel = (Player) e.getPlayer();
    17. if(!(entity instanceof Player)) return;
    18. if (e.getPlayer().getItemInHand().getType().equals(Material.DIAMOND_SWORD)) return;
    19. menu.show(e.getPlayer(), playersel);
    20. }
    21. }


    All I need is to grab the name of the player interacted with.
     
  5. Offline

    sgavster

    Code:java
    1. if(e.getRightClicked() instanceof Player) {
    2. Player p = (Player) e.getRightClicked();
    3. String playerName = p.getName();
    4. }
     
  6. Thanks you both :D
     
Thread Status:
Not open for further replies.

Share This Page