Says entity exists and doesn't exist?

Discussion in 'Plugin Development' started by shohouku, Jan 31, 2020.

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

    shohouku

    So I'm trying to check if a certain entity exists but it's saying it exists and doesn't exist at the same time.
    Code:
    Location location = new Location(Bukkit.getWorld("world"), -4.762, 89, -19.306);
    for(Entity goblin1 : location.getWorld().getNearbyEntities(location, 100, 100, 100)) {
    if(goblin1.getName() != null)
    if(goblin1.getName().equals("Goblin")) {
    System.out.println("Goblin exists");
    } else {
    System.out.println("Goblin doesn't exist");
    }                                         
    }
     
  2. Offline

    bowlerguy66

    @shohouku When you call the for loop for all of the nearby entities to your location it will be getting you, the player entity, and whatever your target entity is. When it finds you, it will say that the "goblin doesn't exist" and then when it finds the goblin it say it will exist. This can be fixed by adding an entity type check before your name checks, something like:
    Code:
    if(!(goblin1 instanceof YourEntity)) {
      continue;
    }
     
  3. Offline

    Strahan

  4. Offline

    Xp10d3

    In an off topic question, is it possible to check the name of the entity instead?
     
  5. Offline

    bowlerguy66

    @Xp10d3 Not sure what you mean by the name of the entity. If you want the custom name given to the entity you can use (entity).getCustomName() (make sure to check if it's null beforehand). If you want the type of the entity you can use (entity).getType() and use that to compare with the EntityType enum.
     
  6. Offline

    Xp10d3

    Never mind. Just wondering if it's possible to get the name of the entity; probably not thinking straight xD Ignore this...
     
Thread Status:
Not open for further replies.

Share This Page