Solved Stopping BukkitTask

Discussion in 'Plugin Development' started by ProMCKingz, Jan 18, 2015.

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

    ProMCKingz

    Hey there, i am using the following method for one of my schedulers:
    Code:
       
      public void startPD() {
         PigDelay.pd = getConfig().getInt("Pig-Delay");
         startPDD = getServer().getScheduler().runTaskTimer(this, new PigDelay(this), 20l, 20l);
       }
    
       public void stopPD() {
         BukkitTask task = getServer().getScheduler().runTaskTimer(this, new PigDelay(this), 20l, 20l);
         task.cancel();
       }
        }
    The problem is that the scheduler will not stop with the stop method here is the scheduler

    Code:
    package me.promckingz.pigquest;
    
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.Location;
    import org.bukkit.World;
    import org.bukkit.entity.Pig;
    import org.bukkit.potion.PotionEffect;
    import org.bukkit.potion.PotionEffectType;
    import org.bukkit.scheduler.BukkitRunnable;
    
    import com.promckingz.pigquest.ChatUtils;
    
    public class PigDelay extends BukkitRunnable {
    
        public static int repeat = 0;
    
        Main plugin;
    
        public PigDelay(Main pl) {
            plugin = pl;
        }
       
        private PigDelay() { }
           static PigDelay instance = new PigDelay();
          
            public static PigDelay getInstance() {
                    return instance;
            }
    
        SettingsManager settings = SettingsManager.getInstance();
    
        public static int pd;
    
        public void run() {
            if (pd == 0) {
                if (repeat <= plugin.getConfig().getInt("Pig-Spawn-Limit")) {
                    repeat++;
                } else
                    this.cancel();
            }
    
            if (pd % 10 == 0 || pd < 3) {
                ChatUtils.broadcast(String.valueOf(pd)
                        + " seconds until the next pigs spawn..");
            }
            if (pd >= 1) {
                pd -= 1;
            } else if (pd <= 1) {
                plugin.stopPD();
                ChatUtils.broadcast(ChatColor.BOLD + "Pigs have spawned!"
                        + ChatColor.GOLD + " [" + repeat + " / "
                        + plugin.getConfig().getInt("Pig-Spawn-Limit") + "]");
                double x = settings.getData().getDouble("centreX");
                double y = settings.getData().getDouble("centreY");
                double z = settings.getData().getDouble("centreZ");
                World centreWorld = Bukkit.getServer().getWorld(
                        settings.getData().getString("centreWorld").toString());
                Location finallocation = new Location(centreWorld, x, y, z);
                Pig pg = centreWorld.spawn(finallocation, Pig.class);
                Pig pg2 = centreWorld.spawn(finallocation, Pig.class);
                pg.setAdult();
                pg.setSaddle(false);
                pg.addPotionEffect(new PotionEffect(PotionEffectType.SLOW, 18000,
                        900000000));
                pg2.addPotionEffect(new PotionEffect(PotionEffectType.SLOW, 18000,
                        900000000));
                pg2.setSaddle(false);
                pg2.setAdult();
                pg.setCustomName(ChatColor.GOLD + "PigQuest");
                pg2.setCustomName(ChatColor.GOLD + "PigQuest");
                if (repeat == plugin.getConfig().getInt("Pig-Spawn-Limit")) {
                    ChatUtils.broadcast(ChatColor.RED + "No more pigs will spawn!");
                    plugin.stopPD();
                    this.cancel();
                    return;
                }
                    if (repeat < plugin.getConfig().getInt("Pig-Spawn-Limit")) {
                        plugin.stopPD();
                        plugin.startPD();
                        return;
                }
                    else
                this.cancel();
            }
        }
    }
     
  2. @ProMCKingz That's because in the first part of the code, you're cancelling the brand new task you created.
    Assuming that startPDD is the taskID:
    Code:
    Bukkit.getServer().getScheduler().cancelTask(startPDD);
     
  3. Offline

    ProMCKingz

    @DJSkepter
    The method cancelTask(int) in the type BukkitScheduler is not applicable for the arguments (BukkitTask)
     
  4. Offline

    Sagacious_Zed Bukkit Docs

    When you schedule a task with the scheduler you get a task id of type int wrapped in the return value. You have to use that task id to cancel the task.
     
  5. Offline

    ProMCKingz

    Last edited by a moderator: Jun 13, 2016
  6. Offline

    1Rogue

    You would call startPDD.cancel(), what you're doing incorrectly here is creating a second task and immediately cancelling it.
     
  7. Offline

    ProMCKingz

    @1Rogue
    So like this?
    Code:
    public void stopPD() {
            startPDD.cancel();
    if so, that gives me a NPE, when I try to join

    EDIT: merged to previous post, forgot to edit it.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 13, 2016
  8. Offline

    1Rogue

    So make sure the task isn't null before you try to cancel it :D

    You can also use this null check to avoid scheduling a second timer by accident.
     
  9. Offline

    ProMCKingz

    @1Rogue
    Code:
        public void stopPD() {
            if(!(startPDD == null)){
                startPDD.cancel();
            }
            else return;
        }
    Like this?

    @1Rogue @Sagacious_Zed
    The problem is that, on my ending scheduler it randomly starts this scheduler, so pigs spawn. Even with the above method, the pigs still spawn, here is the code for that class:
    Code:
    package me.promckingz.pigquest;
    
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.Location;
    import org.bukkit.World;
    import org.bukkit.entity.Entity;
    import org.bukkit.entity.Pig;
    import org.bukkit.entity.Player;
    import org.bukkit.plugin.java.JavaPlugin;
    import org.bukkit.scheduler.BukkitRunnable;
    
    import com.promckingz.pigquest.ChatUtils;
    
    public class GameTimer extends BukkitRunnable {
       
        SettingsManager settings = SettingsManager.getInstance();
       
        Main plugin;
       
        public GameTimer(Main pl) {
            plugin = pl;
            // TODO Auto-generated constructor stub
        }
    
        public static int time = 0;
       
    public static int pd = JavaPlugin.getPlugin(Main.class).getConfig().getInt("Game-Time");
    
        public void run(){
            for (Player player : Main.InGame.keySet()){
            for (Entity e : player.getPlayer().getWorld().getEntities()) {
                if (e instanceof Pig){
                Pig pig = (Pig) e;
                    if(pig.getCustomName().contains("PigQuest")){
            if (pig.getLocation().getBlockX() == (plugin.getConfig().getInt("Spawn_BlueBX")) && (e.getLocation().getBlockY() == (plugin.getConfig().getInt("Spawn_BlueBY")))){
                Bukkit.getServer().broadcastMessage("debug1");
            }
            }
                }
            }
            }
                    if(pd == 0){
                        time++;
                    }
                   
                    if (pd % 30 ==0 || pd<5){
                        ChatUtils.broadcast(String.valueOf(pd) + ChatColor.RED + " seconds until the game ends!");
            }
                    if (pd >= 1){
                        pd-= 1;
        }else if(pd <= 1){
            ChatUtils.broadcast("Game has ended!");
            int r = Scores.RedScore;
            int b = Scores.BlueScore;
            int g = Scores.GreenScore;
            int ye = Scores.YellowScore;
            if(Scores.RedScore > Scores.BlueScore){
                if(Scores.RedScore > Scores.GreenScore)
                    if(Scores.RedScore > Scores.YellowScore)
                        ChatUtils.broadcast(ChatColor.RED + ">>>  " + ChatColor.BOLD + "Red Team won!" + ChatColor.RED + "  <<<");
        }
                    else if(b > r){
                        if (b > g)
                            if(b > ye)
                                ChatUtils.broadcast(ChatColor.RED + ">>>  " + ChatColor.BOLD + "Blue Team won!" + ChatColor.RED + "  <<<");
        }
                            else if(g > r){
                                if (g > b)
                                    if(g > ye)
                                        ChatUtils.broadcast(ChatColor.RED + ">>>  " + ChatColor.BOLD + "Green Team won!" + ChatColor.RED + "  <<<");
        }
                            else if(ye > r){
                                if (ye > b)
                                    if(ye > g)
                                        ChatUtils.broadcast(ChatColor.RED + ">>>  " + ChatColor.BOLD + "Yellow Team won!" + ChatColor.RED + "  <<<");
        }
                            else ChatUtils.broadcast(ChatColor.RED + ">>>  " + ChatColor.GOLD + ChatColor.BOLD + "It was a draw!" + ChatColor.RED + "  <<<");
            for (Player player : Main.InGame.keySet()){
                World w = Bukkit.getServer().getWorld(
                        settings.getData().getString("leave.world"));
                double x = settings.getData().getDouble("leaveX");
                double y = settings.getData().getDouble("leaveY");
                double z = settings.getData().getDouble("leaveZ");
                player.teleport(new Location(w, x, y, z));
                player.setScoreboard(Scoreboards.manager.getNewScoreboard());
                LobbyCount.Gamer.clear();
                new Teams("Red").getName();
                new Teams("Blue");
                new Teams("Yellow");
                new Teams("Green");
                Scores.RedScore = 0;
                Scores.BlueScore = 0;
                Scores.GreenScore = 0;
                Scores.YellowScore = 0;
                PigDelay.repeat = 0;
                plugin.stopPD();
                Scores.TakenR.clear();
                Game.setCanStart(false);
                Game.stop();
                player.performCommand("pigquest kill");
                plugin.number = plugin.getConfig().getInt("Countdown");
                Scoreboards.makeScoreboard();
                for (Player pl : Bukkit.getServer().getOnlinePlayers()) {
                    pl.setScoreboard(Scoreboards.manager.getNewScoreboard());
                }
                plugin.arena = 0;
                plugin.stopCountdown();
                plugin.stopGT();
                for (Player in : plugin.InGame.keySet()) {
                    in.getInventory().clear();
                }
                Main.InGame.clear();
            }
        }
        }
    }
    
    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 13, 2016
  10. Offline

    1Rogue

    Basically, although that can be simplified to:

    Code:java
    1. public void stopPD() {
    2. if (startPDD != null) {
    3. startPDD.cancel();
    4. }
    5. }


    As for your other question, please format your code first. Follow Google's java style guide for (imo) the best methods to use for code.
     
  11. Offline

    ProMCKingz

    @1Rogue @Sagacious_Zed
    Thanks for the help, it was confusing because it was irrelavant to the code I provided. The problem was in a for loop where I was casting this method. Thanks for the help anyways!
     
Thread Status:
Not open for further replies.

Share This Page