Solved getNearbyEntities returning 0

Discussion in 'Plugin Development' started by vemacs, Apr 4, 2013.

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


    Here is the code for a thread I have:

    package com.nullblock.vemacs.perplayer.threads;
    import java.util.Iterator;
    import java.util.List;
    import java.util.logging.Logger;
    import org.bukkit.Bukkit;
    import org.bukkit.entity.Entity;
    import org.bukkit.entity.Monster;
    import org.bukkit.entity.Player;
    import com.nullblock.vemacs.perplayer.PerPlayer;
    public class MonitorThread implements Runnable {
        private int limit;
        private int safe;
        private int radius;
        private int delay = 5;
        public static Logger LOGGER = Logger.getLogger(PerPlayer.class.getName());
        public MonitorThread(int limit, int safe, int radius) {
            this.limit = limit;
   = safe;
            this.radius = radius;
        public void run() {
            for (;;) {
                try {
                    Thread.sleep(delay * 1000);
                } catch (InterruptedException e) {
                for (Player player : Bukkit.getServer().getOnlinePlayers()) {
          "Checking " + player.getName());
                    List<Entity> entities = player.getNearbyEntities(radius,
                            radius, radius);
           + " has " + entities.size() + " entities");
                    Iterator cleanup = entities.iterator();
                    while (cleanup.hasNext()) {
                        Entity checked = (Entity);
                        if (!(checked instanceof Monster)) {
           + " has " + entities.size() + " monsters");
          "Limit is " + limit + " monsters");
                    if (entities.size() > limit) {
               + " hit the limit of " + limit
                                + " monsters!");
                        new Thread(
                                new DepopThread(safe, entities, player.getName()))
    The values are loaded from config.yml:

    radius: '42'
    limit: '128'
    safe: '100'
    Now, I see this in the console:


    entities.size() is returning 0 for the parameters I gave it, and I can't seem to pinpoint the issue. And yes, I have spawned 400 zombies below me. What I am expecting is DepopThread to come into place, but it's not happening because entities.size() isn't returning anything other than 0.

    Any advice? The full source is here.

    Note: when I hardcode the values for getNearbyEntities, it returns what it's supposed to return, but then I get a ConcurrentModificationException. Is there something wrong with my iterator?

    Edit: it turned out to be with my YAML, but the ConcurrentModificationException is still there

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
    Last edited by a moderator: May 31, 2016
  2. Offline


    Use the iterator to remove the entries... cleanup.remove();
  3. Offline


    Thanks! That worked.
Thread Status:
Not open for further replies.

Share This Page