Delay of ScheduleAsyncTask in other class than main not working...

Discussion in 'Plugin Development' started by Zandor300, May 22, 2014.

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

    Zandor300

    Hi,

    I have been trying to fix this error for a week now and i can't figure it out how to fix it!

    I have this on my onEnable():
    Code:java
    1. Bukkit.getScheduler().scheduleAsyncRepeatingTask(this, new StartCountdown(), 0, 20);


    And i have this on my StartCountdown class:
    Code:java
    1. @Override
    2. public void run() {
    3. // Code
    4. }
    5.  
    6. try {
    7. Thread.sleep(1000);
    8. } catch (InterruptedException e) {
    9. e.printStackTrace();
    10. }


    But the delay of the onEnable() line isn't working and the Thread.sleep isn't working because the function that needs to be called is being called every tick without errors!?

    Can someone help me???

    Greetings, Zandor300

    The StartCountdown class does extend BukkitRunnable btw...
    Code:java
    1. public class StartCountdown extends BukkitRunnable {}


    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 8, 2016
  2. Offline

    minoneer


    I'm not 100% sure but I think the bukkit scheduler runs the "run" method every time the repeating task is called - regardless of the threads state. Also, if I'm not mistaken (haven't actually checked the code yet) the Scheduler doesn't start a new Thread for every Asynch task but uses some kind of Threat pool - therefore your sleep doesn't have the expected outcome.

    I basically see two ways you could do it. First would be to remove the sleep and instead increase the interval between the runs - which would have about the same effect, though the actual time would be dependant on ticks and not on the actual server time (and therefore could increase when you have some server lag). The other option would be to not use a repeating but a one-time-task. Then you could create your own timer with the thread.sleep method. But you would have to rerun the method manually after your wait is over.

    I'm not sure how familiar you are with multi threats and the Bukkit API - please make sure you aren't calling any API methods from your asynch task.
     
  3. Offline

    Zandor300


    Can you give me an example?


    I'm new to this stuff because I'm making a minigame plugin for a friend of mine and it irritates me and him that the countdown doesn't work...
     
  4. Offline

    minoneer

    Code:java
    1. @Override
    2. public void run()
    3. {
    4. //do your stuff
    5. try
    6. {
    7. Thread.sleep(1000)
    8. {
    9. //Handle exception
    10. }
    11. run();
    12. }


    There may be a few typos etc. - not testet. But you should get the idea.

    If you are making a mini game, and the countdown does more than to actually count (which it pbl does), I think your best bet would be to use the scheduler to schedule a synch task and handle the countdown based on ticks. But you would have to do it slightly differently, since you can't use a sleep in the main thread.
     
  5. Offline

    chingo247

    Why not use a simple method that does the countdown recursively?

    Code:
        public static void countDown(int start) {
            System.out.println("Countdown: " + start);
    //        Bukkit.broadcastMessage("Countdown: " + start); or use a broadcast instead
            final int nextNumber = start - 1;
            if (nextNumber >= 0) {
                Bukkit.getScheduler().runTaskLater(Bukkit.getPluginManager().getPlugin("yourplugin"), new Runnable() {
     
                    @Override
                    public void run() {
                        countDown(nextNumber);
                    }
                }, 20); // 20 ticks = one second
            }
        }
     
Thread Status:
Not open for further replies.

Share This Page