Checking if entities are nearby, and if there are none spawning an entity

Discussion in 'Spigot Help' started by ZachThePwn, Jul 8, 2018.

  1. Hey, I am trying to make scheduled mob spawners that spawn my custom mobs whenever there are none nearby. I have been trying to get it working for the past 2 days and can't seem to figure it out. The wither skull is spawned and removed, but then nothing else is happened. I tested it and it is always triggering the
    "if(nESize > 0) " even if no mobs are within the 3 blocks. i also had my player move away from it and it still triggers that part only. Please help i'm very lost at what's even the issue. (The custom entities work already, so if it makes it easier just ignore that in the code as I can easily add it back)
    Code (Text):
    public void spawnMobs() {
            getServer().getScheduler().scheduleSyncRepeatingTask(this, new Runnable() {
                public void run() {
                    ItemStack leatherhelmet1 = new ItemStack(Material.LEATHER_HELMET);
                    ItemMeta leathermeta1 = leatherhelmet1.getItemMeta();
                    leathermeta1.setDisplayName(ChatColor.WHITE + "Tier 1 Helmet");
                    ArrayList<String> lore1 = new ArrayList<String>();
                    lore1.add(ChatColor.WHITE + "Health : " + ChatColor.LIGHT_PURPLE + "40");
                    leathermeta1.setLore(lore1);
                    leatherhelmet1.setItemMeta(leathermeta1);
                     
                    ItemStack woodensword1 = new ItemStack(Material.WOOD_SWORD);
                    ItemMeta woodmeta1 = woodensword1.getItemMeta();
                    woodmeta1.setDisplayName(ChatColor.WHITE + "Tier 1 Sword");
                    ArrayList<String> wlore1 = new ArrayList<String>();
                    wlore1.add(ChatColor.WHITE + "Damage : " + ChatColor.RED + "20");
                    woodmeta1.setLore(wlore1);
                    woodensword1.setItemMeta(woodmeta1);
                       
                    int x = 70;
                    int y = 68;
                    int z = 191;
                    /*boolean decide = false;
                    onSpawn(null, decide);*/
                    World world = Bukkit.getWorld("world");
                    Location loc = new Location(world, x, y, z);
                    EntityType skull = EntityType.WITHER_SKULL;
                    Entity skullspawn = Bukkit.getWorld("world").spawnEntity(loc, skull);
                    List<Entity> nearbyEntities = (List<Entity>) skullspawn.getNearbyEntities(3, 3, 3);
                    int nESize = nearbyEntities.size();
                    if(nESize > 0) {
                        skullspawn.remove();
                        return;
                    } else if (nESize < 1){
                        skullspawn.remove();
                        LivingEntity zombie = (LivingEntity) Bukkit.getWorld("world").spawnEntity(loc, EntityType.ZOMBIE);
                        zombie.setCustomName(ChatColor.WHITE + "Tier 1 Zombie");
                        zombie.getEquipment().setHelmet(leatherhelmet1);
                        zombie.getEquipment().setItemInMainHand(woodensword1);
                        zombie.getAttribute(Attribute.GENERIC_MAX_HEALTH).setBaseValue(getEntityMaxHealth(zombie));
                        zombie.setHealth(getEntityMaxHealth(zombie));
                        zombie.getAttribute(Attribute.GENERIC_MOVEMENT_SPEED).setBaseValue(0.30);
                    }  
                                         
                     
                     
                             
                }
            }, 20L, 20L);
        }
     
     
  2. Because you use a fixed location? Just add some debug statements to find where it is checking & what it finds there.
     
  3. I want it to be a fixed location, I'm going to have separate spawn locations for entities and if they have some nearby I don't want any to spawn
     
  4. Debug statements still.
     
  5. What is an example of one I would need? I can try to figure out the rest if I know one of them. I'm still very new to plugin development and this is my first project.
     
  6. Everything. Then narrow down the scope as you figure out what's wrong.

    gather experience like that, figuring out how to properly debug will be a great asset. I am sure that someone can point you to the right direction but that would defeat the purpose of debugging as you got told about the issue rather then narrow down on it yourself.

    I won't give you the hints on it, if someone else wants to they are welcome to do exactly that .
     
  7. I am very lost I need help
     
  8. Code (Text):
    List<Entity> nearbyEntities = (List<Entity>) skullspawn.getNearbyEntities(3, 3, 3);
    Players are entities If you are in that range, you get added to the list.
    Try with:
    Code (Text):
    int nearbyEntities = (int) dam.getNearbyEntities(3,3,3).stream().filter(ent -> !(ent instanceof Player)).count();
     
    • Like Like x 2
  9. It works thank you so much! What is going on in that if I may ask? So I can learn from this
     
  10. Also it doesn't spawn if items are on the ground nearby, how would I add the filter for items like players?
     
  11. Loop over it and check if the Entity is not a player.
    Code (Text):
    for (Entity near : entities) {
    if (!near instanceof Player) {
    // do your thing
    }
    }
     

  12. Please, if you don't know Java don't write useless messages. Thanks.

    It takes a stream of entities from the list and filter entities that are not players.

    Code (Text):
    int nearbyEntities = (int) dam.getNearbyEntities(3,3,3).stream().filter(ent -> !(ent instanceof Player) && !(ent instanceof Item).count();
     
    • Like Like x 4
  13. Thank you man! And now I think I understand it better with the second part added