Solved Says entity exists and doesn't exist?

Discussion in 'Spigot Plugin Development' started by kinukin, Feb 1, 2020.

  1. 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 (Text):
    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. FrostedSnowman

    Resource Staff

    You may have accidentally made a mistake in your if-statement block logic. Currently, your code will detect entities whose name is and isn't Goblin, essentially all entities will have some output here.

    If I'm correct, your intentions may be to place the else statement on the outer if statement:
    Code (Java):
    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"); //technically a goblin could exist, but this just means the current element in the collection does not have a name of "Goblin"
        }
    }
    If you are only attempting to find 1 "Goblin" just exit out of the loop once you've found one.
     
  3. It doesn't detect if no goblins exist however it does detect if goblins do exist.
     
  4. FrostedSnowman

    Resource Staff

    In that case, you want to break out of the loop once you've found one. You can have some outer Entity variable that can be used to cache the entity you found. Outside of the loop, if the variable is null, no goblin was found. If it's not, then the cached entity is the goblin.

    Here is a generic example:
    Code (Java):
    Object found = null;
    Collection<Object> elements = getElements();

    for (Object element : elements) {
         if (element.foo()) {
              found = element;
              break;
         }
    }

    if (found != null) {
          //element was found
    }
     
    • Like Like x 1
  5. How can I check how much goblins there are? For example: If I had two goblins then do whatever.
     
  6. FrostedSnowman

    Resource Staff

    It's essentially the same concept except instead of caching the entity, you have an integer that you increment, and you wouldn't break out of the loop.

    You can also use the stream API to accomplish this:
    Code (Java):
    elements.stream().filter(element -> element.foo("example_condition")).count();
     
  7. How do I reset the Object to be null again after there is no more goblins?