how to stop Scheduler ? (countdown)

Discussion in 'Plugin Development' started by ilethz, Jul 27, 2013.

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

    ilethz

    Hello!
    my countdown it's not stopping at the last messages so i add:
    Code:
    Bukkit.getServer().getScheduler().cancelAllTasks();
    and stills not working :(
    here is my code maybe it's wrong i don't know
    .
    Code:
    //PACKAGE
    package com.gmail.ilethgames;
     
    import java.util.logging.Logger;
     
    import org.bukkit.Bukkit;
    import org.bukkit.Location;
    import org.bukkit.World;
    import org.bukkit.block.Block;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.plugin.java.JavaPlugin;
    import org.bukkit.potion.PotionEffect;
    import org.bukkit.potion.PotionEffectType;
     
    public class cd_t extends JavaPlugin {
    public final Logger logger = Logger.getLogger("Minecraft");
    public static cd_t plugin;
    public int number = 5;
     
    @Override
    public void onEnable() {
     
    }
     
     
    @Override
    public void onDisable() {
        getServer().getScheduler().cancelTask(number);
    }
     
    public boolean onCommand(CommandSender sender, Command cmd,
            String commandLabel, final String[] args) {
        if (commandLabel.equalsIgnoreCase("begin")) {
            final Player p = (Player) sender;
            final World w = p.getWorld();
            this.getServer().getScheduler()
                    .scheduleSyncRepeatingTask(this, new Runnable() {
                        public void run() {
                            if (number != -1) {
                                if (number != 0) {
                                    Bukkit.broadcastMessage("§f[§cLastManStanding§f]§aStarting in §6" + number + "§a seconds");
                                    number--;
                                } else {
                                    Bukkit.broadcastMessage("§f[§cLastManStanding§f]§aGo!Everybody have 10 seconds of grace period!");
                                    p.addPotionEffect(new PotionEffect(
                                            PotionEffectType.DAMAGE_RESISTANCE,
                                            200, 10));
                                    p.addPotionEffect(new PotionEffect(
                                            PotionEffectType.REGENERATION, 200,
                                            10));
                                    p.addPotionEffect(new PotionEffect(
                                            PotionEffectType.SPEED, 200, 2));
                                    if (args.length == 0) {
                                        Block targetblock = p.getTargetBlock(
                                                null, 50);
                                        Location l = targetblock.getLocation();
                                        w.strikeLightning(l);
                                    } else if (args.length == 1) {
                                        if (p.getServer().getPlayer(args[0]) != null) {
                                            Bukkit.getServer().getScheduler().cancelAllTasks();
                                            number--;
                                            Bukkit.getServer().getScheduler().cancelAllTasks();
                                        }
                                   
     
                                    }
                                }
     
                            }
     
                           
                        }
                    }, 0L, 20L);
                   
     
        }
        return false;
    }
    }
     
  2. Offline

    hockeygoalie5

    Create a BukkitRunnable field called counter, and set it to the task:
    Code:java
    1.  
    2. BukkitRunnable counter;
    3.  
    4. ...
    5.  
    6. counter = this.getServer().getScheduler().runTaskTimer(this, new Runnable() {
    7. ...
    8. }, 0L, 20L);
    9.  

    Then you can just call:
    Code:java
    1.  
    2. counter.cancel();
    3.  
     
  3. Offline

    mbaxter ʇıʞʞnq ɐ sɐɥ ı

    Running cancelAllTasks() cancels every task scheduled by any plugin on the server.
     
    ilethz likes this.
  4. Offline

    ilethz

    Thank you! it's working!
     
  5. Offline

    mbaxter ʇıʞʞnq ɐ sɐɥ ı

    I'd hope you're not using cancelAllTasks(). That's going to break people's plugins.
     
  6. Offline

    ilethz

    yeh, i know!
     
Thread Status:
Not open for further replies.

Share This Page