Solved getNearbyEntities returning 0

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

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

    vemacs

    Here is the code for a thread I have:

    Code:
    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;
            this.safe = safe;
            this.radius = radius;
        }
     
        public void run() {
            for (;;) {
                try {
                    Thread.sleep(delay * 1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                for (Player player : Bukkit.getServer().getOnlinePlayers()) {
                    LOGGER.info("Checking " + player.getName());
                    List<Entity> entities = player.getNearbyEntities(radius,
                            radius, radius);
                    LOGGER.info(player.getName() + " has " + entities.size() + " entities");
                    Iterator cleanup = entities.iterator();
                    while (cleanup.hasNext()) {
                        Entity checked = (Entity) cleanup.next();
                        if (!(checked instanceof Monster)) {
                            entities.remove(checked);
                        }
                    }
                    LOGGER.info(player.getName() + " has " + entities.size() + " monsters");
                    LOGGER.info("Limit is " + limit + " monsters");
                    if (entities.size() > limit) {
                        LOGGER.info(player.getName() + " hit the limit of " + limit
                                + " monsters!");
                        new Thread(
                                new DepopThread(safe, entities, player.getName()))
                                .start();
                    }
                }
            }
        }
    }
    
    The values are loaded from config.yml:

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

    [​IMG]

    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. Use the iterator to remove the entries... cleanup.remove();
     
  3. Offline

    vemacs

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

Share This Page