Concurrent Modification Exception

Discussion in 'Plugin Development' started by maxben34, Nov 12, 2013.

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

    maxben34

    I'm getting a concurrent modification exception on line 29. I'm basically trying to make the blocks get placed 1 block above and then get removed 5 seconds later. Sorry, for some reason the code got formatted really weirdly.

    Code:java
    1. package me.maxben34.blocks;
    2.  
    3. import java.util.ArrayList;
    4.  
    5. import org.bukkit.Bukkit;
    6. import org.bukkit.ChatColor;
    7. import org.bukkit.Location;
    8. import org.bukkit.Material;
    9. import org.bukkit.event.EventHandler;
    10. import org.bukkit.event.EventPriority;
    11. import org.bukkit.event.Listener;
    12. import org.bukkit.event.block.BlockPlaceEvent;
    13. import org.bukkit.plugin.java.JavaPlugin;
    14.  
    15. public class blocks extends JavaPlugin implements Listener{
    16. ArrayList<Location> blocks = new ArrayList<Location>();
    17.  
    18. public void onEnable(){
    19. Bukkit.getPluginManager().registerEvents(this, this);
    20. }
    21. @EventHandler(priority = EventPriority.HIGH)
    22. public void onblockplace(final BlockPlaceEvent e){
    23. if(e.getBlock().getType().equals(Material.DIAMOND_BLOCK)){
    24. e.getBlock().setType(Material.AIR);
    25. blocks.add(e.getBlock().getLocation());
    26. Bukkit.broadcastMessage(ChatColor.AQUA + "" + ChatColor.BOLD + "Placing Blocks!");
    27. Bukkit.getScheduler().scheduleSyncRepeatingTask(this, new Runnable(){
    28. public void run(){
    29. for(Location loc : blocks){
    30. loc.setY(loc.getY() + 1);
    31. loc.getBlock().setType(Material.DIAMOND_BLOCK);
    32. blocks.add(loc);
    33. }
    34. }}, 1, 100);
    35. Bukkit.getScheduler().scheduleSyncRepeatingTask(this, new Runnable(){
    36. public void run(){
    37. for(Location loc : blocks){
    38. loc.setY(loc.getY() - 1);
    39. loc.getBlock().setType(Material.AIR);
    40. }
    41. }}, 5, 100);
    42. Bukkit.getScheduler().scheduleSyncDelayedTask(this, new Runnable(){
    43. public void run(){
    44. blocks.clear();
    45. }
    46. }, 100);
    47. }
    48. }
    49. }
    50.  
     
  2. Offline

    1Rogue

    You don't modify a collection while iterating through it. Keep a collection of things to add/remove after or iterate through a clone.
     
    The_Doctor_123 likes this.
  3. Offline

    maxben34

    Yea, I knew that's what it meant... I just don't know how to make this work without modifying it.
     
  4. Offline

    1Rogue


    You can modify it, just modify it after you are done iterating through it. Keep a local collection and .addAll()/.removeAll() on it, or use a .clone() of the object.
     
  5. Offline

    The_Doctor_123

    maxben34
    He just said. Iterate through a clone.
     
  6. Offline

    maxben34

    1Rogue
    I know this sounds really silly, but I've never used clones before. I know it would be
    Code:
    blocks.clone()
    but I don't know how I would iterate through that clone. I know that sounds really nooby :/
     
  7. Offline

    1Rogue

    A clone is just a copy of the object.
     
  8. Offline

    maxben34

    Yes but how do I iterate through the clone instead of the blocks array?
     
  9. Offline

    1Rogue

    Code:java
    1. YourObject o = (YourObject) originalObject.clone();
    2. for (YourObject temp : o) {
    3. //...
    4. }


    roughly
     
  10. Offline

    maxben34

    oh okay
     
Thread Status:
Not open for further replies.

Share This Page