Solved Task Classes and ActiveWorkers

Discussion in 'Spigot Plugin Development' started by WAS, May 18, 2017.

  1. WAS

    WAS

    I'm trying to figure out why a task that I have started doesn't show in the ActiveWorkers (BukkitWorkers)?

    I'm trying to add simple functionality for a fail-safe to kill all plugin-based tasks.


    Code (Java):
           int disabled = 0;
           for (BukkitWorker worker : plugin.getServer().getScheduler().getActiveWorkers()) {
             int id = worker.getTaskId();
             Plugin plugin = worker.getOwner();
             plugin.getServer().getScheduler().cancelTask(id);
             sender.sendMessage(ChatColor.translateAlternateColorCodes('&',
                 config.getString("local.global-task-aborted", "&6Aborted &r{PLUGIN} &6Task ID: &r{ID}")
                     .replace("PLUGIN", plugin.getName()).replace("{ID}", String.valueOf(id))));
             disabled++;
           }
           if (disabled > 0) {
             sender.sendMessage(ChatColor.translateAlternateColorCodes('&',
                 config.getString("local.global-task-aborted", "&2All running global plugin tasks aborted.")));
           } else {
             sender.sendMessage(ChatColor.translateAlternateColorCodes('&',
                 config.getString("local.no-global-tasks-aborted", "&cThere were no global tasks to abort.")));
           }
         }
     
    This will return no global tasks to abort, while my timer task is running, and can be stopped via itself just fine (as it was never aborted by the above code)
     
    #1 WAS, May 18, 2017
    Last edited: May 18, 2017
  2. When do you run this snippet? When onDisable is called?
     
  3. WAS

    WAS

    It can be called via command. I was testing it by running my Timer command, which starts a BukkitTask. Than I try running /sysinfo kill tasks global and it fails to kill it. I can run /sysinfo kill tasks which runs the following and works np

    Code (Java):
          if (timers.size() > 0) {
             for (Map.Entry<String, List<Object>> entry : timers.entrySet()) {
               ((BukkitTask) entry.getValue().get(0)).cancel();
               timers.remove(entry.getKey(), entry.getValue());
             }
             sender.sendMessage(ChatColor.translateAlternateColorCodes('&',
                 config.getString("local.tasks-aborted", "&2All timers aborted.")));
           } else {
             sender.sendMessage(ChatColor.translateAlternateColorCodes('&',
                 config.getString("local.no-tasks", "&6&oNo timers to abort.")));
           }
     
    #3 WAS, May 18, 2017
    Last edited: May 18, 2017
  4. It will only have workers if there are async tasks scheduled, and currently running (so after run returns, it'll remove the worker from the task's workers)
     
  5. WAS

    WAS

    Oh ok, that makes sense than, as this would be a sync task.
     
  6. For debug purposes, if you start an async tasks and sleep for an extended period (like, enough for you to run the command), you should see it pop up.
     
    • Like Like x 1