Solved Restarting a task

Discussion in 'Plugin Development' started by Coopah, Mar 18, 2019.

Thread Status:
Not open for further replies.
  1. Offline

    Coopah

    Hey, I was just wondering if there was anyway to restart a task.

    So I set an int to be equal to a task like so:
    Code:
    flashRunnable = Bukkit.getScheduler().scheduleSyncRepeatingTask(plugin, new Runnable() {
    
    I later cancel it with:
    Code:
    Bukkit.getSchedular().cancelTask(flashRunnable);
    And now I'm wondering if it's possible to restart this runnable?
     
  2. Offline

    timtower Administrator Administrator Moderator

    @Coopah You need to make a new one for that.
     
  3. Offline

    Coopah

    I figured out the solution for anyone wanting a restartable task. Cancel the original one and then start a new one like @timtower suggested. Simply check if the int exists, then cancel it and then set the int to be equal to the new task. So everytime it gets run it will cancel the original one and start a new one.

    For example:
    Code:
    public static int restartableFlashTask;
    
    public void startRestartableFlashTask(final Player p) {
       
            SkillFactory sf = new SkillFactory(plugin);
           
            int skillLevel = sf.getSkillLevel(p, "Assassin", "Flash");
           
            lastRecharge.put(p.getName(), System.currentTimeMillis());
    
            if (restartableFlashTask != 0) {
               
                Bukkit.getScheduler().cancelTask(restartableFlashTask);
               
                restartableFlashTask = Bukkit.getScheduler().scheduleSyncRepeatingTask(plugin, new Runnable() {
    
                    public void run() {
    
                        if (flashCharges.containsKey(p.getName())) {
    
                            if (flashCharges.get(p.getName()) != 3) {
    
                                flashCharges.put(p.getName(), flashCharges.get(p.getName()) + 1);
    
                                lastRecharge.put(p.getName(), System.currentTimeMillis());
    
                                uPlayer.formatMessage(p, "Assassin", "Flash Charges: " + ChatColor.GREEN + flashCharges.get(p.getName()));
                            }
    
                        } else {
    
                            flashCharges.put(p.getName(), 1);
                            lastRecharge.put(p.getName(), System.currentTimeMillis());
    
                            uPlayer.formatMessage(p, "Assassin", "Flash Charges: " + ChatColor.GREEN + flashCharges.get(p.getName()));
    
                        }
                    }
                }, 20 * (11 - skillLevel), 20 * (11 - skillLevel));
               
            } else {
                           
                restartableFlashTask = Bukkit.getScheduler().scheduleSyncRepeatingTask(plugin, new Runnable() {
    
                    public void run() {
    
                        if (flashCharges.containsKey(p.getName())) {
    
                            if (flashCharges.get(p.getName()) != 3) {
    
                                flashCharges.put(p.getName(), flashCharges.get(p.getName()) + 1);
    
                                lastRecharge.put(p.getName(), System.currentTimeMillis());
    
                                uPlayer.formatMessage(p, "Assassin", "Flash Charges: " + ChatColor.GREEN + flashCharges.get(p.getName()));
                            }
    
                        } else {
    
                            flashCharges.put(p.getName(), 1);
                            lastRecharge.put(p.getName(), System.currentTimeMillis());
    
                            uPlayer.formatMessage(p, "Assassin", "Flash Charges: " + ChatColor.GREEN + flashCharges.get(p.getName()));
    
                        }
                    }
                }, 20 * (11 - skillLevel), 20 * (11 - skillLevel));
               
            }
           
        }
     
Thread Status:
Not open for further replies.

Share This Page