Solved Monster Iterator

Discussion in 'Plugin Help/Development/Requests' started by 2008Choco, Jul 11, 2015.

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

    2008Choco

    I seem to be having issues iterating through every monster within a 50x50 vicinity of the player. The iterator works great, but when it gets to what I want it to do, I have to cast it to a Monster. Unfortunately, for whatever reason, the iterator is also grabbing Chickens, Sheep, Pigs, etc. and attempting to cast them as Monsters, causing an error in my console.

    Here is the current code that I am using:
    Code:
                                List<Entity> nearbyEntities = damaged.getNearbyEntities(50, 10, 50);
                                for (Iterator<Entity> it = nearbyEntities.iterator(); it.hasNext();){
                                    if (it.next() instanceof Monster){
                                        damaged.sendMessage(ChatColor.RED + "MONSTER");
                                        Monster monster = (Monster) it.next();
                                        monster.setTarget((LivingEntity) damaged);
                                    }//Close if the entity it is looking at, is a monster
                                    else{
                                        damaged.sendMessage(ChatColor.GREEN + "NOT MONSTER");
                                    }
                                }//Close listing through every entity
    Not quite sure what I'm doing wrong, if someone could help that would be great.
     
  2. Offline

    teej107

    It's because you are calling #next() on the iterator twice.
     
  3. Offline

    2008Choco

    @teej107 Thank you, I'm not sure why I didn't even catch that. It was me being stupid. I fixed now, so it's working perfectly :D Thanks man
     
Thread Status:
Not open for further replies.

Share This Page