Delayed task help

Discussion in 'Plugin Development' started by raGan., Aug 8, 2012.

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

    raGan.

    I use abstract class to schedule sync task, but I need to pass variable to run method of subclass.
    Code:
    Code:
    public abstract class Qvt implements ConfigurationSerializable, Runnable {
     
    protected final long delay;
     
    public Qvt(int del) {
    delay = del*20;
    }
      
    public int execute(Player player) {
    if(delay > 0)
    return Bukkit.getScheduler().scheduleSyncDelayedTask(Quester.plugin, this, delay);
    else
    run();
    return 0;
    }
      
      //some abstract methods and other stuff
     
    }
    run() is defined in subclass. How can I make it get player variable from execute ? Thanks.
     
  2. Offline

    Njol

    Define your own abstract run(Player) method in Qvt and instead of implementing Runnable and using 'this' in the scheduled task use this:
    Code:
    return Bukkit.getScheduler().scheduleSyncDelayedTask(Quester.plugin, new Runnable() {
      public void run() {
        this.run(player);
      }
    }, delay);
    
     
    raGan. likes this.
  3. Offline

    raGan.

    Thanks ! I will try that.

    But wait. That 'this' is inside anonymous class

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 27, 2016
  4. Offline

    Njol

    Remove the 'this' or use 'Qvt.this' - both should work.
     
  5. Offline

    raGan.

    I solved it by creating protected variable player and:
    Code:
    public int execute(final Player player) {
        this.player = player;
        if(delay > 0)
            return Bukkit.getScheduler().scheduleSyncDelayedTask(Quester.plugin, this, delay);
        else
            run();
        return 0;
    }
    using player variable works as expected. I wonder what will it do with long delay and player logging off.
     
  6. Offline

    Njol

    This will not work if the execute method is called twice within 'delay' ticks on two different players.
     
  7. Offline

    raGan.

    Ah, I see. Thanks again for pointing that out. Qvt.this. does the job.
     
Thread Status:
Not open for further replies.

Share This Page