Is it possible to schedule a delayed task within a repeated task?

Discussion in 'Plugin Development' started by Caedus, Jan 19, 2016.

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

    Caedus

    Hi,

    as the title suggests, i'm wondering whether it's possible to schedule a delayed task within a task that repeats itself.

    I want to have a task repeat itself every minute, part of that task is performing IF statements. If those IF statements are met, I would like it to execute a certain block of code after X amount of time.

    I tried it in Eclipse but it didn't work and google revealed no answers.
     
  2. Offline

    mythbusterma

    @Caedus

    There is no reason you can't do this syntactically, but you can easily lose track of your code like this, so be careful.
     
  3. Offline

    Zombie_Striker

    @Caedus
    You should have no problem doing that, but that's not why I'm posting.
    Can you show us what you tried? Can you explain why it did not work?
     
  4. Offline

    Lightspeed

    @Caedus Can you share some example code please after testing it? Or the code you already tried?
     
  5. Offline

    Zombie_Striker

    @Lightspeed @Caedus
    It should look like the following:
    Code:
    final Mainclass instance = this;
    Bukkit.getServer().getScheduler().scheduleSynchRepeatingTask(instance, new runnable(){
    public void run(){
    
    Bukkit.getServer().getScheduler().scheduleSynchDelayedTask(instance, new runnable(){
    public void run
    }
    
    
    })
     
  6. Offline

    Lightspeed

    Erm thats a little bit wierd/very hard to read.

    Core would be your main class
    somthingHappen woulden't be there it's just an example.
    Change the delay/repeat delay to what you want?
    Code:
    final Core c = this;
    final boolean somthingHappen = true;
    Bukkit.getScheduler().scheduleSyncRepeatingTask(c, new Runnable()
    {
        @Override
        public void run()
        {
            //Do stuff!
            if (somthingHappen) //WHOA SOMTHING HAPPENED!
            {
                Bukkit.getScheduler().scheduleSyncDelayedTask(c, new Runnable()
                {
                    @Override
                    public void run()
                    {
                      //Delayed in a repeating task WHOOAAAAAA BRoooo
                    }
                }, 20);
            } //END OF IF
        }
    }, 0, 20);
    This is more clear?
    I wana learn too! ;)
     
  7. Offline

    Zombie_Striker

    @Lightspeed
    Why do you even have that boolean? There's no need for that both for what he want and in this example. It's because of that, your comments (and your JavaScript format that should not be used here) that makes it even harder to read IMHO.
     
  8. Offline

    Caedus

    @Zombie_Striker @Lightspeed

    Thanks for the help guys, I ended up using your "cleaned up" example, Lightspeed. It's the one I used before but I hadn't defined "final Core c = this;" and simply putting "this" where "c" is didn't work.

    Weirdly now I get a NPE, only at the end of my last delayed task, as seen here: http://i.imgur.com/LVe0CMW.png

    Not sure if that helps understand the problem though.

    Also, any idea how I can get it to go back to a repeat task that I have in my onEnable? For example, right now it keeps doing the IF check every 20L, but once the IF statement is true it continues doing what it should but after that is done it doesn't go back to checking if the IF statement is true again.
     
  9. Offline

    Zombie_Striker

    @Caedus
    First, we need your WHOLE error log, not just a small picture. We need to see what you actually put down. Finally, make sure your formatting is correct. You should be able to see your problem that way.
     
  10. Offline

    Lightspeed

    I read it above and I think I know your error!
    For both runnables you have this instead of c well thats not gonna give you your main class for the delayed task in the repeating thats gonna give you your repeating task!
    So do
    final MainClass mc = this;
    and use mc instead of this to give it the right instance.


    Also send code for it not going back to the repeating task and tell us what it should do.



    I have it in that format to help visibility and it's a bit better than uncompleted/misspelled code
    Synch?
    No if example?
    Not even the args for the schedulars?
     
    Last edited: Jan 20, 2016
  11. Offline

    mcdorli

    Javascript format? Can you explain this to me?


    @Lightspeed you probably wouldn't
     
  12. Offline

    Zombie_Striker

    Forgot the name of the type of formatting it is, but it's where you put the encapsulation under the if statement. For Java, you should have the brackets on the same lines as the if statements.
     
  13. Offline

    mcdorli

    Javascript is same as java, at least according to conventions. You meant Allman, I only know know C++, C and C#, wivh uses that szylr.
     
    Zombie_Striker likes this.
  14. Offline

    teej107

    It's Java conventional but it's not crucial. I prefer to use Allman style.

    Lol
     
Thread Status:
Not open for further replies.

Share This Page