Solved ConcurrentModificationException Help

Discussion in 'Plugin Development' started by RichardBob123, Apr 30, 2015.

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

    RichardBob123

    I've been developing a plugin that forces the mob's velocity vector to 0. After a few minutes, the console will throw a concurrentmodificationexception. I've tried synchronizing the the arraylist that the mobs are stored in, but it still throws it. Here is the error: pastebin. The code for the plugin is here:

    Code:
    package io.github.mac_genius.drugseller;
    
    import org.bukkit.ChatColor;
    import org.bukkit.World;
    import org.bukkit.entity.Entity;
    import org.bukkit.entity.IronGolem;
    import org.bukkit.plugin.Plugin;
    import org.bukkit.util.Vector;
    
    import java.util.ArrayList;
    
    /**
    * Created by Mac on 4/29/2015.
    */
    public class NoMoving implements Runnable {
        private Plugin plugin;
    
        public NoMoving(Plugin pluginIn) {
            plugin = pluginIn;
        }
    
    
        @Override
        public synchronized void run() {
            String name = plugin.getConfig().getString("dealer name");
            name = ChatColor.translateAlternateColorCodes('&', name);
            ArrayList<World> worlds = new ArrayList<>(plugin.getServer().getWorlds());
            for (int j = 0; j < worlds.size(); j++) {
                synchronized (plugin.getServer().getWorld(worlds.get(j).getName())) {
                    ArrayList<Entity> ironGolems = new ArrayList<>(worlds.get(j).getEntitiesByClass(IronGolem.class));
                    for (int i = 0; i < worlds.get(j).getEntitiesByClass(IronGolem.class).size(); i++) {
                        if (ironGolems.get(i) instanceof IronGolem) {
                            if (!((IronGolem) ironGolems.get(i)).isPlayerCreated() && ironGolems.get(i).getName().equals(name) && ironGolems.get(i).isCustomNameVisible()) {
                                Vector vector = new Vector(0.0, 0.0, 0.0);
                                ironGolems.get(i).setVelocity(vector);
                            }
                        }
                    }
                }
            }
        }
    }
     
  2. Offline

    mythbusterma

    @RichardBob123

    Are you running this on a separate thread?

    Nevermind, I can see it in the error.

    What makes you think you can use the Bukkit API on a seperate thread, when all the documentation says very explicitly that the Bukkit API IS NOT THREAD SAFE.

    Tl;dr: run it on the main thread.
     
  3. Offline

    RichardBob123

    Thanks for the response @mythbusterma . The only problem with running this on the main thread is that if you have say 10000 entities on the server that I have to iterate through, and I'm changing the 100 of the entities' velocities 20 times a second, this is going to cause some lag. Do you have a better way of doing this?

    EDIT: I figured it out.
     
    Last edited: Apr 30, 2015
Thread Status:
Not open for further replies.

Share This Page