Solved Scheduler Won't Stop

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

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

    ProMCKingz

    Hey,
    I have the following scheduler: And when it has repeated the amount of times given, it spams Pigs will no longer spawn, also with the message saying how many pigs have spawned(which is the number given in the config section). After alot of debugging, I realised that the issue was in the plugin.stopPD; This method is in my main class, shown below the following code. I want the scheduler to stop after repeat == Pig-Spawn-Limit. Thanks,
    PigDelay (open)
    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.plugin.java.JavaPlugin;
    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;
        }
    
        SettingsManager settings = SettingsManager.getInstance();
    
        public static int pd;
    
        public void run() {
            if (pd == 0) {
                if (repeat <= plugin.getConfig().getInt("Pig-Spawn-Limit")) {
                    repeat++;
                } else
                    plugin.stopPD();
            }
    
            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) {
                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();
                }
                    else if (repeat < plugin.getConfig().getInt("Pig-Spawn-Limit")) {
                        plugin.stopPD();
                        plugin.startPD();
                        ChatUtils.broadcast("asdasd");
    
                }
                    else
                    Bukkit.getServer()
                            .broadcastMessage(
                                    ChatColor.RED
                                            + "An error occured! print screen this and show it to promckingz");
                plugin.stopPD();
            }
        }
    }

    Main Class (Starting and stopping):

    Code:
        public void startPD() {
            PigDelay.pd = getConfig().getInt("Pig-Delay");
            startPDD = getServer().getScheduler().scheduleSyncRepeatingTask(this,
                    new PigDelay(this), 20l, 20l);
        }
    
        public void stopPD() {
            getServer().getScheduler().cancelTask(startPDD);
        }
    
     
  2. Offline

    Noahz123

    Replace all calls to stopPD in your PigDelay Class into this.cancel(); This is better because each instance will always only cancel itself and you won't have to worry about scope.
     
  3. Offline

    ProMCKingz

  4. Offline

    Noahz123

    Did it work out for you okay?
     
  5. Offline

    1Rogue

    "scheduleSyncRepeatingTask" is deprecated, use "runTaskTimer", which returns a BukkitTask object. From there you can call BukkitTask#cancel on the returned object.
     
  6. Offline

    ProMCKingz

    @1Rogue
    Ah, ok, will try that. So cancelling will be the same? just need to change startPDD = getServer().getScheduler.runTaskTimer(this, new PigDelay(this), 20l, 20l); ?


    @Noahz123
    I think I still get the spam
     
    Last edited: Jan 11, 2015
  7. Offline

    1Rogue

    Nearly the same, you call #cancel() on the returned object rather than passing an int to the scheduler.
     
  8. Offline

    ProMCKingz

    @1Rogue
    Sorry, but what do you mean? can you give an example please. Thanks
     
  9. Offline

    1Rogue

    Code:java
    1. BukkitTask task = getServer().getScheduler().runTaskTimer(/* ... */);
    2. task.cancel();
     
  10. Offline

    ProMCKingz

    @1Rogue
    This is for the cancel task, right?
    EDIT: Thanks man, been trying to solve this for some time.
     
    Last edited: Jan 12, 2015
  11. Offline

    mythbusterma

    @ProMCKingz

    That is what BukkitTask#cancel() does.
     
Thread Status:
Not open for further replies.

Share This Page