Solved ConcurrentModificationException?

Discussion in 'Spigot Plugin Development' started by TheGamerPlayz, May 29, 2016.

Thread Status:
Not open for further replies.
  1. I am getting this error on my plugin. I am getting it when I try to get all the contents from a list in a for loop, any help? Or a reason why this error usually appears?

    Here is the code i removed the actually Listener as I dont' believe it has anything to do with the error
    Code (Java):

    package GameStart;

    public class GameStart implements Listener {
        Main pl;
        public GameStart(Main pl){
            this.pl = pl;
        }
        List<String> red;
        List<String> blue;
        public void start(){
            playerManager pm = new playerManager(pl);
            FileConfiguration data = pm.getData();
            TeamData td = new TeamData(pl);
            FileConfiguration teams = td.getData();
            red = teams.getStringList("Teams.Red");
            blue = teams.getStringList("Teams.Blue");
            Location locr = new Location(
                    Bukkit.getWorld(data.getString("RedSpawn.world")),
                    data.getInt("RedSpawn.X"), data.getInt("RedSpawn.Y"), data.getInt("RedSpawn.Z"));
            Location locb = new Location(
                    Bukkit.getWorld(data.getString("BlueSpawn.world")),
                    data.getInt("BlueSpawn.X"), data.getInt("BlueSpawn.Y"), data.getInt("BlueSpawn.Z"));
            for(String player : red){            //Line 100
                Bukkit.getPlayer(player).teleport(locr);
                red.remove(player);
            }
            for(String player : blue){
                Bukkit.getPlayer(player).teleport(locb);
                blue.remove(player);
            }
            teams.set("Teams.Red", null);
            teams.set("Teams.Blue", null);
            td.saveData();
        }
    }
     
    Here is the error:
    Code (error (Unknown Language)):

    [04:38:29 ERROR]: Could not pass event PlayerMoveEvent to FlagPvPStart v1.0
    org.bukkit.event.EventException
            at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:310) ~[spigot-1.8.8.jar:git-Spigot-db6de12-18fbb24]
            at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:62) ~[spigot-1.8.8.jar:git-Spigot-db6de12-18fbb24]
            at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.java:502) [spigot-1.8.8.jar:git-Spigot-db6de12-18fbb24]
            at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:487) [spigot-1.8.8.jar:git-Spigot-db6de12-18fbb24]
            at net.minecraft.server.v1_8_R3.PlayerConnection.a(PlayerConnection.java:270) [spigot-1.8.8.jar:git-Spigot-db6de12-18fbb24]
            at net.minecraft.server.v1_8_R3.PacketPlayInFlying.a(SourceFile:126) [spigot-1.8.8.jar:git-Spigot-db6de12-18fbb24]
            at net.minecraft.server.v1_8_R3.PacketPlayInFlying$PacketPlayInPositionLook.a(SourceFile:18) [spigot-1.8.8.jar:git-Spigot-db6de12-18fbb24]
            at net.minecraft.server.v1_8_R3.PlayerConnectionUtils$1.run(SourceFile:13) [spigot-1.8.8.jar:git-Spigot-db6de12-18fbb24]
            at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source) [?:1.8.0_73]
            at java.util.concurrent.FutureTask.run(Unknown Source) [?:1.8.0_73]
            at net.minecraft.server.v1_8_R3.SystemUtils.a(SourceFile:44) [spigot-1.8.8.jar:git-Spigot-db6de12-18fbb24]
            at net.minecraft.server.v1_8_R3.MinecraftServer.B(MinecraftServer.java:715) [spigot-1.8.8.jar:git-Spigot-db6de12-18fbb24]
            at net.minecraft.server.v1_8_R3.DedicatedServer.B(DedicatedServer.java:374) [spigot-1.8.8.jar:git-Spigot-db6de12-18fbb24]
            at net.minecraft.server.v1_8_R3.MinecraftServer.A(MinecraftServer.java:654) [spigot-1.8.8.jar:git-Spigot-db6de12-18fbb24]
            at net.minecraft.server.v1_8_R3.MinecraftServer.run(MinecraftServer.java:557) [spigot-1.8.8.jar:git-Spigot-db6de12-18fbb24]
            at java.lang.Thread.run(Unknown Source) [?:1.8.0_73]
    Caused by: java.util.ConcurrentModificationException
            at java.util.ArrayList$Itr.checkForComodification(Unknown Source) ~[?:1.8.0_73]
            at java.util.ArrayList$Itr.next(Unknown Source) ~[?:1.8.0_73]
            at GameStart.GameStart.start(GameStart.java:100) ~[?:?]
            at GameStart.GameStart.onPlayerMove(GameStart.java:64) ~[?:?]
            at sun.reflect.GeneratedMethodAccessor65.invoke(Unknown Source) ~[?:?]
            at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0_73]
            at java.lang.reflect.Method.invoke(Unknown Source) ~[?:1.8.0_73]
            at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:306) ~[spigot-1.8.8.jar:git-Spigot-db6de12-18fbb24]
            ... 15 more
     
     
  2. ArrayList documentation:
     
  3. I see, but I also don't see a way to avoid this error
     
  4. I even highlighted it for you.

     
  5. Is there even any other way of doing it? I am only changing using its own methods

    Here is the full class:
    Code (Java):

    public class GameStart implements Listener {
        Main pl;
        public GameStart(Main pl){
            this.pl = pl;
        }
        List<String> red;
        List<String> blue;
        @EventHandler
        public void onPlayerMove(PlayerMoveEvent event){
            TeamData td = new TeamData(pl);
            FileConfiguration teams = td.getData();
            red = teams.getStringList("Teams.Red");
            blue = teams.getStringList("Teams.Blue");
            teams.set("Teams.Blue", null);
            teams.set("Teams.Red", null);
            Player p = event.getPlayer();
            if(p.getWorld().getName().equals("world_nether")){
                Block b = p.getLocation().getBlock().getRelative(BlockFace.DOWN);
                if(b.getState().getData() instanceof Wool){
                    Wool wool = (Wool) b.getState().getData();
                    DyeColor dc = wool.getColor();
                    if(dc.toString() == "BLUE"){
                        if(!blue.contains(p.getName())){
                            blue.add(p.getName());
                            teams.set("Teams.Blue", blue);
                            red.remove(p.getName());
                            teams.set("Teams.Red", red);
                            td.saveData();
                        }
                    }else if(dc.toString() == "RED"){
                        if(!red.contains(p.getName()))
                            red.add(p.getName());
                            teams.set("Teams.Red", red);
                            blue.remove(p.getName());
                            teams.set("Teams.Blue", blue);
                            td.saveData();
                        }
                    } else{
                    red.remove(p.getName());
                    teams.set("Teams.Blue", red);
                    blue.remove(p.getName());
                    teams.set("Teams.Blue", blue);
                    td.saveData();
                }
                }
                    if(teams.getStringList("Teams.Red").size() == teams.getStringList("Teams.Blue").size() &&
                            teams.getStringList("Teams.Red").size() > 0){
                        start();
                    } else if((teams.getStringList("Teams.Red").size() + teams.getStringList("Teams.Blue").size())%2 == 1
                            && teams.getStringList("Teams.Red").size() + teams.getStringList("Teams.Blue").size() > 1){
                        if(teams.getStringList("Teams.Red").size() > teams.getStringList("Teams.Blue").size() + 1){
                            blue.add(red.get(1));
                            red.remove(red.get(1));
                            teams.set("Teams.Red", null);
                            teams.set("Teams.Blue", null);
                            teams.set("Teams.Red", red);
                            teams.set("Teams.Blue", blue);
                            td.saveData();
                            start();
                        } else if(teams.getStringList("Teams.Red").size() < teams.getStringList("Teams.Blue").size() + 1){
                            red.add(blue.get(0));
                            blue.remove(blue.get(0));
                            teams.set("Teams.Red", null);
                            teams.set("Teams.Blue", null);
                            teams.set("Teams.Red", red);
                            teams.set("Teams.Blue", blue);
                            td.saveData();
                        }
                    }
        }
        public void start(){
            playerManager pm = new playerManager(pl);
            FileConfiguration data = pm.getData();
            TeamData td = new TeamData(pl);
            FileConfiguration teams = td.getData();
            red = teams.getStringList("Teams.Red");
            blue = teams.getStringList("Teams.Blue");
            Location locr = new Location(
                    Bukkit.getWorld(data.getString("RedSpawn.world")),
                    data.getInt("RedSpawn.X"), data.getInt("RedSpawn.Y"), data.getInt("RedSpawn.Z"));
            Location locb = new Location(
                    Bukkit.getWorld(data.getString("BlueSpawn.world")),
                    data.getInt("BlueSpawn.X"), data.getInt("BlueSpawn.Y"), data.getInt("BlueSpawn.Z"));
            for(String player : red){
                Bukkit.getPlayer(player).teleport(locr);
                red.remove(player);
            }
            for(String player : blue){
                Bukkit.getPlayer(player).teleport(locb);
                blue.remove(player);
            }
            teams.set("Teams.Red", null);
            teams.set("Teams.Blue", null);
            td.saveData();
        }
    }
     
  6. I don't understand why you won't use an Iterator as @Spottedleaf suggested.
     
  7. Well it seems gets to see another noob thread as I do not know how-to
     
  8. You cannot remove an item from list while performing a foreach.

    Change this

    Code (Text):
         for(String player : red){           //Line 100
                Bukkit.getPlayer(player).teleport(locr);
                red.remove(player);
           }
    to
    Code (Text):
    // red ...
    Iterator<String> redIt = red.iterator();

    while (redIt.hasNext()) {
      Bukkit.getPlayer(redIt.next()).teleport(locr);
      redIt.remove();
    }

    // blue ....
     
Thread Status:
Not open for further replies.