How to find if an entity is a player.

Discussion in 'Plugin Development' started by darknesschaos, Jan 15, 2011.

Thread Status:
Not open for further replies.
  1. Offline

    darknesschaos

    Sorry if this is an incredibly simple issue, but by reading the javadocs I could not find how I could do this if it were possible.

    So my question to you guys (and gals) happens to be how do I figure out if an entity (lets say damaged is the event) is a player, and if they are a player, what player.

    Thanks for the help!
     
  2. Offline

    The_Guest

    Currently there is no way. A lot of other people have this problem and they haven't got responses either.
     
  3. Offline

    darknesschaos

    That bites, that would be quite useful. But I understand that they have A LOT on their plate due to legal issues, hMod dieing and so forth.
     
  4. Offline

    axel3200

    This should work (I haven't tested it)

    Code:
    public boolean isPlayer(Entity entity) {
    
        if (entity instanceof Player) {
    
            return true
    
        }
    
        return false
    
    }
    
    As for how to get the name, I have no clue.
     
  5. Offline

    AaronLLF

    Code:
    public String playerName;
    public boolean isPlayer(Entity entity, Player player) {
    
        if (entity.getType() == Human) {
            playerName = player.getDisplayName();
            return true;
    
        }
    
        return false;
    
    }
    
    Or maybe replace Human with Player, and also try it with "s.
    BTW, I haven't gotten much to explore the Bukkit API, but I've seen the entity.getType() function somewhere.
     
  6. Offline

    The_Guest

    Oh sorry, I meant that you can find out if it is a player, but you can't get the Player from it.

    Here is the easiest way to get a boolean answer.

    Code:
    if ((Entity damagee instanceof Player) == true)
    {
    //Do stuff
    }
     
  7. Offline

    axel3200

    You can just do
    Code:
    if (damagee instanceof Player) {
    //do stuff
    }
    the == true is redundant and the Entity shouldn't be there.
     
  8. Offline

    darknesschaos

    Finding the Player from it is quite helpful, however, also knowing that it is a player has its benefits as well.
     
  9. Offline

    axel3200

    Solution posted here.
     
Thread Status:
Not open for further replies.

Share This Page