Stop repeating task function

Discussion in 'Plugin Development' started by Kicksy, Nov 30, 2019.

Thread Status:
Not open for further replies.
  1. I want to stop this function:
        public void iSpawn() {
            if (spawn) {
                for (String spawn : plugin.file.itemConfig.getConfigurationSection("spawn").getKeys(false)) {
                    Bukkit.getScheduler().scheduleSyncRepeatingTask(plugin, new Runnable() {
                        public void run() {
                            ItemStack stack = new ItemStack(
                                    Integer.parseInt((String) plugin.file.itemConfig.get("spawn." + spawn + ".item")));
                                    .dropItem((Location) plugin.file.itemConfig.get("spawn." + spawn + ".position"), stack)
                    }, 0L, Integer.parseInt((String) plugin.file.itemConfig.get("spawn." + spawn + ".cooldown")));
    from a other function. I tried it so but this dont help:
    else if (args[0].equalsIgnoreCase("start")) {
                            spawn = true;
                        } else if (args[0].equalsIgnoreCase("stop")) {
                            spawn = false;
  2. Offline


    From within your scheduler you must check if spawn == true before spawning the items.
  3. I am doing this.
    is automatically
    if(boolean == true)
    I think
  4. Correct, but you are not doing that check at the right place. Currently, you check if `spawn` is true, and start a repeating task if so. But when you change `spawn` to false, the repeating task has already begun.
    You should (instead) check if `spawn` is true inside the run() method.

    Alternatively, you could cancel the task properly: scheduleSyncRepeatingTask will return an int. If you use that int in Bukkit.getScheduler().cancelTask(), you can stop the entire task.
  5. @knokko how i can get the int?
  6. Code:
    int taskID = Bukkit.getScheduler().scheduleSyncRepeatingTask...
    But whether you should go for your original approach or the alternative one depends on how you are planning to use this. If you want to start and stop the task once, the alternative would be better.

    But if you plan to start and stop the task multiple times, your original approach would be better (but you need to move the if(spawn) to inside the run() method.)
  7. Thanks!

    Edit: But I have also a problem that if I am removing a value from the file there is always a error when the task should be performed. I would prefer to restart the task than before using the try catch method.
    Last edited: Dec 1, 2019
Thread Status:
Not open for further replies.

Share This Page