Solved Getting BukkitRunnable class from a BukkitTask?

Discussion in 'Spigot Plugin Development' started by Pocketkid2, Jun 19, 2016.

  1. So my BukkitRunnable, all it does is take items from a chest, and it has the info for that inside itself. It repeats as part of a consumption feature for my plugin. It already knows how to stop itself when there is no fuel.

    There could be any number of these tasks at any given time, so I've stored the results of runTaskTimer (a BukkitTask class) inside a list.

    What I want to do is be able to ask any BukkitTask in that list, if it's BukkitRunnable class matches certain values I have, and if it does then cancel it. These certain values are stored inside my BukkitRunnable when it is created, and there is a method that checks if the given values equal the stored values. But I'm not sure how to get the original BukkitRunnable to do that.

    Am I missing something? Should I be storing the BukkitRunnable inside the list instead of the BukkitTask? Is there no way to get the original BukkitRunnable class with the BukkitTask?
  2. I have run into similar issues, and for the sake of ease of use, i now use a custom class for runnables.

    I have a main runnable which holds a list of abstract class Task classes with a method void run(), and boolean running, int interval, String name. Each tick, it loops through each task and compares ( System.currentTimemillis() % task.interval ), if it is 0, it runs the task. The tasks also have a boolean on/off switch so they can be easily disabled without deleting them, and have a name.

    This lowers the overhead involved in lots of synchronous tasks, makes it easy to keep track of them, stop and start them, change their time interval (delay) while they are running, and get them by name.

    Another boolean can be set by the task to tell the task handler to delete it from the list, or the list can be accessed from the class extending Task, if it is within scope. list.remove(this) would work.
  3. Thank you so much for the help.

    However, I decided to try out simply storing the BukkitRunnable class, and so far it seems to be working. I'll post back here if anything changes or I come across something helpful.
  4. This is the correct way to do it. A BukkitRunnable is an Object, just like any other Object in Java. Having the instance to the Object you created will give you full control over the Object and it's scheduler. Thats why BukkitRunnables have replaced the old way of using Runnables with the Scheduler.