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.
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);
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.
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.
This will not work if the execute method is called twice within 'delay' ticks on two different players.