Solved Add delay in method

Discussion in 'Spigot Plugin Development' started by Nannerss, May 21, 2019.

  1. This is just a general question, but I have a few methods where I want to run some code, have a delay, and then run some more code after the delay. I tried using a Bukkit Runnable as such:
    Code (Java):
    //code before delay

    //BukkitRunnable with runTaskLater

    //code after delay
    However, it just went through the whole code first (including initializing the Runnable) disregarding the task delay. The task still ran when it was scheduled to, but it didn't pause the code after it.

    Is there some way I can add some sort of delay between pieces of code? Any help is appreciated!
     
  2. Let's say you want to execute a void after 5 ticks

    //code
    //void
    // post void

    What you would do, is change the type to a boolean and return true

    //code
    //boolean
    Code (Text):

    while(boolean){
        boolean = false;
        //code
    }
     
    or execute the code past void a tick after the void
     
  3. What? The second part doesn’t make any sense, you should also be wary of busy waiting on the main thread because that will block your server from doing anything.

    Put your “code with delay” inside the BukkitRunnable. The idea of the scheduler is to have tasks run at a later time, not to allow you to pause your code.
     
  4. By doing this, you will block the thread..

    I think your best solution is, as xTrollxDudex suggested, to manage the "code after delay" into the bukkitrunnable
     
  5. You did put your code you wanted to delay inside the runnables "run" method I hope?

    There's a difference between:

    Code (Java):

    public void test() {
        //Code here
       
        new BukkitRunnable() {
            @Override
            public void run() {
                 //Code that needs to be delayed here
            }
        }.runTaskLater(plugin, delay);
    }
     
    And:

    Code (Java):

    public void test() {
        //Code here

        new BukkitRunnable {
            @Override
            public void run() {
            }
        }.runTaskLater(plugin, delay);

        //Code that  needs to be "delayed" (but which won't be because it isn't inside the runnable)
    }
     
    Hope this helps :)
     
  6. I think the easiest way to achieve a delay between a few lines of code is to use Thread.sleep().

    MfG
    Leonhard|EsGibtKeineMitte
     
    • Winner Winner x 1
  7. Nonononononononono nono no no. This will stop the main thread completely and the server will lag for the time of the thread being slept.
     
  8. There is literally no reason to ever use Thread.sleep in a plugin even in an async task, it is an extremely crude way to implement delays and even those who know exactly what they are doing should avoid using it in favor of the scheduler
     
  9. You can run this method asynchroniously^^

    I've said it is the easiest way of doing it:) You can probably outrun the problems by creating a new Thread.

    Of course, there are better ways of doing it but they were already mentioned

    MfG
    Leonhard|EsGibtKeineMitte
     
    • Funny Funny x 1
  10. Creating new threads from java is not the best thing inside spigot and its forks tho.
     
  11. Out of all the things I tried I for some reason didn’t think of just putting the delayed code in the runnable like you guys said.. >_>

    Thanks for the help!
     
  12. Don't worry these things happen to us all sometimes xD
     

Share This Page