Async or Sync tasks?

Discussion in 'Plugin Development' started by bowlerguy66, Nov 2, 2015.

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

    bowlerguy66

    So, what I'm trying to do is loop through a list then change a block. The problem is I get an Error when I use an Async task and I get an error when I use a Sync task. Heres my code:
    Code:
        //@SuppressWarnings("deprecation")
        public void regenerateGravelTimer() {
            this.getServer().getScheduler().scheduleSyncRepeatingTask(this, new Runnable() {
                @Override
                public void run() {
                   
                    regenerateGravel();
                   
                }       
            }, 0L, 300L);
        }
       
        public void regenerateGravel() {
           
            if(gravel.size() == 0) {
                return;
            }
    
            int a = gravel.size();
            int r = new Random().nextInt(a) + 1;
            forcount = 0;
           
            for(String uuid : gravel) {
                                   
                forcount++;
               
                if(forcount == r) {
                   
                    forcount = 0;
                   
                    String world = getGravel().getString("Gravel." + uuid + ".World");
                    int x = getGravel().getInt("Gravel." + uuid + ".X");
                    int y = getGravel().getInt("Gravel." + uuid + ".Y");
                    int z = getGravel().getInt("Gravel." + uuid + ".Z");
                   
                    if(world == null) {
                        getGravel().set("Gravel." + uuid, null);
                        saveGravel();
                        return;
                    }
                   
                    Location loc = new Location(Bukkit.getWorld(world), x, y, z);
                    Block b = loc.getBlock();
                   
                    b.setType(Material.GRAVEL);
                   
                    getGravel().set("Gravel." + uuid, null);
                    saveGravel();
                   
                    gravel.remove(uuid);
                   
                }                   
            }
    
        }
    When I use the Sync task I get a ConcurrentModificationException and when I use and Async task I get a Asynchronous block creation error. What do I do?
     
  2. Offline

    567legodude

    @bowlerguy66 The reason you are getting a ConcurrentModificationException is because you are removing items from the list while looping through it.
    When you do:
    gravel.remove(uuid);

    If you want to remove the items as you loop, try using an iterator, simply change the beginning of your loop to:
    for(String uuid : gravel.iterator()) {
    and see if that works. I'm not gonna do it for you, but with that, you should be able to figure it out.
     
  3. Offline

    bowlerguy66

    @567legodude When I do that I get
    "Can only iterate over an array or an instance of java.lang.Iterable"
     
  4. Offline

    567legodude

    @bowlerguy66 You didn't show us the gravel list in your code (You need to include everything) so I don't know what kind of collection it is.
    Is it a List, ArrayList, HashMap?
     
  5. Offline

    teej107

    @bowlerguy66
    1. Don't use bukkit methods inside async tasks or you'll give @mythbusterma cancer or something.
    2. @567legodude's suggestion was a poor one. It would be best to use an iterator but you should use it with a while or a regular for-loop.
     
    Zombie_Striker likes this.
  6. Offline

    bowlerguy66

    @567legodude This is my code for it: List<String> gravel = new ArrayList<String>();
     
  7. Offline

    mythbusterma

    Everything checks out.

    The concurrent modification exception comes from the fact you modify when you're iterating. Use an Iterator and your code will work fine.

    Also, don't use asynchronous if you don't know what it means.
     
    teej107 likes this.
  8. Offline

    bowlerguy66

  9. Offline

    mythbusterma

    @bowlerguy66

    I was responding to Teej.

    If you use Bukkit methods inside asynchronous methods, you risk breaking the server in ways you can't even imagine (or even reproduce).
     
  10. Offline

    bowlerguy66

    @mythbusterma

    Hmm. Okay, so if I were to use .setType() in an Async task, it would screw everything up?
     
  11. Offline

    mythbusterma

  12. Offline

    567legodude

    @teej107 The code example was my bad, I was just trying to get a quick reply. But I still had the right idea.
     
Thread Status:
Not open for further replies.

Share This Page