Why does this crash my test server?

Discussion in 'Spigot Plugin Development' started by yhl, Jul 29, 2018.

  1. yhl

    yhl

    I want to make it so when I run this it loops thru and waits 1 second (it's not a thread.sleep as u can see), but it just freezes

    Code (Java):
                    int i = 0;
                   
                    while(i == 0) {
                        Location cur = rel.clone();
                        if(cur.getBlock().getType() != Material.AIR) {
                            i = 1;
                        } else{
                           
                           
                            Bukkit.getServer().getScheduler().runTaskLater(this, new Runnable() {
                                public void run() {
                                    setType(cur, Material.OBSIDIAN);
                                    rel.add(0,-1,0);
                                }
                            }, 20);
                           
                        }
                        }
     
  2. Cause of the while loop.
    if the first block it finds is not air, it'll always go to the else, ask for something to be done later, and keep doing the while.

    Go ahead, add a println in the else, you'll get it spammed a gazillion times.
    runTaskLater is asynch, it runs the run function later and continues with whatever you were doing, youre acting like it's sync (which wouldve frozen the server for 1s anyways, whichd be bad anyways)
     
  3. yhl

    yhl

    well then what can I do instead
     
  4. Move the coord change outside the runnable & increment the wait time
     
  5. yhl

    yhl

    wdym increment the wait time
     
  6. First time in loop 20t, second time 40t, 60t, 80t, etc