Solved [HELP] Kill entities with custom name, NPE

Discussion in 'Spigot Plugin Development' started by Seeyko, Apr 18, 2017.

  1. Hello all !

    I got a NullPointerExeption in my code, when a hit a custom villager with the name "VilBleu", he loose some of his health, and when this health goes less to 0, he and his armorstand get remove from the world. But i don't know why i get a null pointer exeption...

    So here the code :
    Code (Text):


    for (Entity entity : Bukkit.getWorld("FK").getEntities()) {
                   String entity1 = entity.getCustomName();
                   if(entity1.equals("§6Roi §9Bleu")){
                entity.remove();
                   new Sounds(player).playSound(Sound.IRONGOLEM_DEATH);

                   }
                   if(entity1.equals("VilBleu")){
                     entity.remove();

                   }  
                   
                }
     
     
  2. Do you check if the entity has a customName? Maybe there are some entities without a customName. Also, you should use the World#getNearbyEntities is more efficient
     
  3. Thanks, i know have this code :
    Code (Text):


    for (Entity entity : Bukkit.getWorld("FK").getEntities()) {
                   String entity1 = entity.getCustomName();
                       if(entity.isCustomNameVisible()){

                   if(entity1.equals("§6Roi §9Bleu") ){
                entity.remove();
                   player.getWorld().playSound(entity.getLocation(), Sound.WITHER_DEATH, 3.0F, 0.533F);

                   }
                   else if(entity1.equals("VilBleu")){
                     entity.remove();

                   }
                       }
     

    But it's only remove "Roi Bleu", VilBleu did'nt get remove... i try with and without the "else" but non works... dit i have to recreate a for loop ?

    And what did getNearbyEntities?

    Edit: I have try with getNearbyEntities like this :
    Code (Text):

    for (Entity entity : Bukkit.getWorld("FK").getEntities()) {
                   String entity1 = entity.getCustomName();
                       if(entity.isCustomNameVisible()){

                   if(entity1.equals("§6Roi §9Bleu") ){
                entity.remove();
                   List<Entity> villager = entity.getNearbyEntities(5, 5, 5);
                     villager.remove(EntityType.VILLAGER);
                   
                   }
                     
                       }
    }
     
    but it does'nt work... how can i remove a villager in a radius of 10 or 20 block from a player i don't care because the different villager will be at minimum 100 block of distance ?
     
    #3 Seeyko, Apr 18, 2017
    Last edited: Apr 18, 2017


  4. Try doing something like this

    Code (Text):
               
               
                for(Entity entity:entity.getNearbyEntities(5, 5, 5))
                {
                    if(entity instanceof Villager)
                    {
                        entity.remove();
                    }
                }
    Removing an EntityType from an Entity list won't do anything
     
  5. Hello/bonjour,

    Surely the NPE you are getting is thrown because there is an entity with no custom name. Thus you should always verify that the entity has one before proceeding to compare it to the values you are looking for. The simplest way of doing so is verifying that the name returned by Entity#getCustomName() is NOT null.

    Code (Text):
    String customName = entity.getCustomName();
    if (customName != null) {
       // The entity HAS a custom name
       // You may now safely compare it to whatever value you expect
    }
    Also I would recommend renaming the "entity1" variable to something like "customName", "name" or "entityName" to avoid confusions.
     
  6. Yes thanks ! I try the both method and i didn't ger error on both, but i prefer your method ;)

    Thanks both of you men !
    And yes, i have to rename my variable with more clear name, sometimes i'm lost ^^
    Bye / Bonsoir ;)
     
  7. Mark the post as solved if you no need more help
     
  8. Done !
    Thanks Parozz, i didn't quote you in the last post but your method works too, next is just a choise of preference !
     
  9. Don't worry. If you need any help, i might try to help as much as i can