1.14.4 Spawn entity if it doesn't exist in area?

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

  1. So regardless if there is a goblin it will spawn an entity anyways because there are entities in the location, what I want is to spawn only 1 if there isn't any goblins. How can I do that?

    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 instanceof Player) {
    System.out.println(goblin1.getName());
    if(goblin1.getName().equals("Goblin")) {
    System.out.println("do not spawn!");
    } else if(!goblin1.getName().equals("Goblin")){
    System.out.println("Spawn!");
    }
    }
    }          
     
  2. This is a more elegant way of doing this:
    Code (Java):
    final Collection<Entity> entities = world.getNearbyEntities(player.getLocation(), 100, 100, 100, (e) -> e.getType() == EntityType.PLAYER && e.getName().equals("Goblin"));
     
    Now you should just be able to check if the list is Empty, and spawn if is is:
    Code (Text):
    if (entities.isEmpty()) {
        // spawn goblin
    }