Repeating Tasks

Discussion in 'Spigot Plugin Development' started by jamesd5, Jan 13, 2019.

  1. Hello, I am trying to have a Sync Repeating Task in a sub class how would I do this, without implementing "Plugin", or casting "this" to "Plugin"?
    Code (Text):

    Bukkit.getServer().getScheduler().scheduleSyncRepeatingTask(this, new Runnable() {
                public void run() {
                }
    }, 0, 100);
    return;
     
    Ty :>
     
  2. I'm not sure, what you mean under 'subclass'(the child of a base class or a class that is not the main class of your plugin). I suppose you meant the second one.

    You have two option. Either you pass an instance of your plugin to your 'subclass' or you use the following code I attach later.
    Option 1:

    Code (Text):
    // Java
    class Main extends JavaPlugin {
        public void onEnable() {
            new Other(this)
        }
    }

    class Other {
        private Main plugin;

        public Other(Main main) {
            this.plugin = main;
        }

        // You can access main with this.plugin in this class
    }

    // Kotlin
    class Main : JavaPlugin {
        fun onEnable() {
            Other(this)
        }
    }

    class Other(private val plugin: Main) {
        // You can access main with this.plugin in this class
    }
    Option 2:
    Code (Text):
    // Java
    Plugin plugin = Bukkit.getServer().getPluginManager().getPlugin("Your plugin name")

    // Kotlin
    val plugin = Bukkit.getServer().pluginManager.getPlugin("Your plugin name")
    After that you can schedule sync repeating task with the following code(I'm using Java 8 feauters):
    Code (Text):
    // Java
    Bukkit.getServer().getScheduler().scheduleSyncRepeatingTask(plugin, () -> {
        // put some code here
    }, 0, 100);

    // Kotlin
    Bukkit.getServer().scheduler.scheduleSyncRepeatingTask(plugin, {
        // put some code here
    }, 0, 100)
    If you don't know what kotlin is, just ignore those lines commented with "Kotlin". Only the ones commented with "Java" is relevant for you.
     
    • Like Like x 1
  3. Sorry for the confusion. Yes I am using a class that is not the main class of my plugin.

    I tried the second option but it does not seem to work for me. (I am also using Java 8)
    Code (Text):
    Plugin plugin = Bukkit.getServer().getPluginManager().getPlugin("MyPluginName");
    Code (Text):
    Bukkit.getServer().getScheduler().scheduleSyncRepeatingTask(plugin, () -> {
                public void run() {
                //code
                }
    }, 0, 100);
    return;
    The problem highlights ".scheduleSyncRepeatingTask"
    stating
    Code (Text):
    The method scheduleSyncRepeatingTask(Plugin, Runnable, long, long) in the type BukkitScheduler is not applicable for the arguments (Plugin, () -> {}, int, int)
    Any suggestions?
     
  4. Instead of this
    Code (Text):
    Bukkit.getServer().getScheduler().scheduleSyncRepeatingTask(plugin, () -> {
                public void run() {
                //code
                }
    }, 0, 100);
    Use this
    Code (Text):
    Bukkit.getServer().getScheduler().scheduleSyncRepeatingTask(plugin, () -> {
        System.out.println("code")
    }, 0, 100);
    In this post you see the usage of lamda expression. The two runnables are equivalent.
     
    #4 Bod8, Jan 13, 2019
    Last edited: Jan 13, 2019
    • Useful Useful x 1
  5. Okay it works now :> but not in a timed manor. I am trying to have the code repeat every 5 seconds. But instead it is repeating the code every tick.
    Code (Text):
    public void run() {

    Bukkit.getServer().getScheduler().scheduleSyncRepeatingTask(plugin, () -> {
        System.out.println("code")
    }, 0, 100);

    }
    Any suggestions? Ty
     
  6. I call the "plugin" like this:
    Code (Java):
        public static MClass plugin;
        @Override
        public void onEnable() {
            plugin = this;
        }

    To call it use: (Name of the main class).plugin for example "MClass.plugin".

    Code (Java):
    Bukkit.getServer().getScheduler().scheduleSyncRepeatingTask(MClass.plugin, new Runnable() {
                public void run() {
                }
    }, 0, 100);
    return;

    I make the repeating tasks like this:
    Code (Java):
     new BukkitRunnable() {
          @Override
           public void run() {
           //CODE
          }
    }.runTaskLater(MClass.plugin, 2400);
     
    #6 irvinglink, Jan 13, 2019
    Last edited: Jan 13, 2019
  7. Very Informative :>. However it delays the task the amount of time but, it does not separate the code in periods of the amount of time.
    Code (Text):

    Bukkit.getServer().getScheduler().scheduleSyncRepeatingTask(MainName.plugin, new Runnable() {
                public void run() {
                        new BukkitRunnable() {
                            @Override
                            public void run() {
                                Bukkit.getServer().getScheduler().scheduleSyncDelayedTask(MainName.plugin, new Runnable() {
                                public void run() {
                                    //code
                                    }
                                }, 100);
                                return;

                            }
                        }.runTaskLater(Launchables.plugin, 100);
                       
                }
               
            }, 0, 100);
            return;
    Any suggestions?
     
  8. What do you mean with "it does not separate the code in periods of the amount of time."? What do you want to do exactly?
     
  9. why did you put the Bukkit Runnable inside the Scheduler ?
     
    • Agree Agree x 1
  10. You only need one BukkitRunnable. To schedule a repeating task, you'd use BukkitRunnable#runTaskTimer. No need to use Bukkit's scheduler at all.
     
  11. Personally, for referencing my main plugin, I use something like this:
    Code (Java):
    public static Plugin INSTANCE;

    public MainClass() {
         INSTANCE = this;
    }
    As you'll notice I define it in the class's constructor, deviating from @irvinglink 's solution. I do this in case by some fluke chance, another of my classes is called before my onEnable() (Such as another plugin depending on my own, or if I create an onLoad() method later on). Considering your plugin instance is effectively a constant in how you will be using it, UPPER_CASE is the proper variable notation for java. As for your runnable issues, from how you've responded to others it sounds like you're trying to either make a stopwatch of sorts or a countdown. So to be creative, I'll post something that takes advantage of OOP:

    Code (Java):
    public class Countdown extends BukkitRunnable {

        public static BukkitTask start(int seconds) {
            Countdown countdown = new Countdown();
            countdown.seconds = seconds;
            return countdown.runTaskTimer(MainClass.INSTANCE, 0, 20);
        }

        private int seconds;

        @Override
        public void run() {
            Bukkit.getOnlinePlayers().forEach(p -> {
                if (seconds <= 0) {
                    cancel();
                } else {
                    p.sendTitle(String.valueOf(seconds), "", 3, 3, 3);
                    seconds--;
                }
            });
        }
    }
    with this, a 5 second countdown is as simple as
    Code (Java):
    Countdown.start(5);
    I hope I understood your problem correctly!
     
    • Useful Useful x 1
  12. this is static abuse
     
  13. Thank you for all the replies :> However, what I am trying to do is not just delay the task 5 seconds for example, I am also trying to, once the countdown is over, have the code repeat itself also every 5 seconds not every tick. As you can tell I am not very Good with delays, repeats etc... Any suggestions? Thank you :D
     
  14. Ah, in that case the easiest solution would in fact be with a second task timer. In this second timer, have the repeat time be the amount of time you want between PLUS the length of time the countdown will go for. Inside, call your countdown timer.
     

Share This Page