See if entity has a custom name?

Discussion in 'Plugin Development' started by Kepler_, Jun 8, 2013.

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

    Kepler_

    Is there a way to see if an entity has a custom name? The only way I could think of was to check the current name to the default name for that entity, but I don't know how to get the default name.

    Thanks if you can help!
     
  2. Offline

    kreashenz

    if(entity.hasCustomName())
     
  3. Offline

    Kepler_

    There isn't a hasCustomName method for the type of LivingEntity. is there a different type of entity i need to cast it to?

    (i'm using event.getEntity() in the CreatureSpawnEvent)
     
  4. Offline

    kreashenz

    Kepler_ I don't think you can check it.. Cast it to something, and then try.
     
  5. Offline

    Technius

    Kepler_
    The name is null if there is no name set.
     
    Kepler_ likes this.
  6. Offline

    Kepler_

    Thanks! That worked. Heres my code:

    Code:
    boolean hasCustomName = entity.getCustomName() != null;
     
  7. You are now checking if it's not equal to a string with the content "null"... Do it like this:
    Code:java
    1. boolean hasCustomName = entity.getCustomName() != null;

    Your code would always return true unless the name above it's head is actual "null".
     
  8. Offline

    Kepler_

    Hellsing
    That's what I meant to type, I just retyped it wrong in the post. Thanks for catching that :D


    I have one more small problem. In the CreatureSpawnEvent, event.getEntity().getCustomName() always returns null, even if the mob was spawned with a renamed spawn egg. I think this happens because the mobs are given a name some time after this event has been triggered. Is there another event I can use that is triggered after a mob is renamed (or something like that) to check if a mob has a custom name?
     
  9. Offline

    Technius

    Kepler_ You could try scheduling a task so that it runs one tick later.
     
  10. Offline

    Kepler_

    I could, but that seems like a pretty unelegant solution.
     
  11. Offline

    Technius

    Kepler_
    Some situations call for some hacks and trickery. I believe this is one of them.
     
  12. Offline

    Kepler_

    How do you make a synchronous task that waits a tick? I think the code to do it has changed since ive last used tasks and the new code looks simpler.
     
  13. Code:java
    1. Bukkit.getScheduler().runTaskLater(plugin, new Runnable()
    2. {
    3. // This is being run 1 tick after the runTaskLater() call... 20L would be 20 tick = 1 second
    4. }, 1L)
     
  14. Offline

    Kepler_

Thread Status:
Not open for further replies.

Share This Page