getting the name of an entity?

Discussion in 'Plugin Development' started by 1337, Jan 22, 2011.

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

    1337

    how would i get the name of an entity from this code?
    Code:
    public void onEntityDamageByEntity(EntityDamageByEntityEvent event) {
            if(event.getEntity() instanceof Player) {
                // get the name of the player who was damaged
    // get the name of the person who damaged him
            }
        }
     
  2. Offline

    matejdro

    You have to cast it to Player:

    Code:
    Player player = (Player) event.getEntity();
    String name = player.getName();
     
     
  3. Offline

    genre

    And what if the entity is not a player? My understanding is that that method will be called any time an entity, which could be a block, for example, damages an another entity.
     
  4. Offline

    Lycake

    not all entities have names so this will not work in general
     
  5. Offline

    matejdro

    Player is only player

    http://javadoc.lukegb.com/Bukkit/inherits.html (scroll down to org.bukkit.entity.Player)
     
  6. Offline

    1337

    you can use if entity instanceof player
     
  7. Offline

    Plague

    That is the proper way, but for quite some time this does not always work, it is an accepted bug, so hopefully it will soon.
     
Thread Status:
Not open for further replies.

Share This Page