Cancelling RepeatingTask

Discussion in 'Plugin Development' started by KyllianGamer, May 27, 2018.

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

    KyllianGamer

    How do i cancel a repeating task? This is what i have now:

    Code:
    package Events;
    
    import org.bukkit.Bukkit;
    import org.bukkit.Effect;
    import org.bukkit.Location;
    import org.bukkit.Material;
    import org.bukkit.Sound;
    import org.bukkit.entity.Entity;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.block.Action;
    import org.bukkit.event.player.PlayerInteractEvent;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.inventory.meta.ItemMeta;
    import org.bukkit.scheduler.BukkitRunnable;
    
    import me.plugin.Main;
    
    public class Grenade3 implements Listener {
       
        private Main plugin = Main.getPlugin(Main.class);
       
    
        @EventHandler
        public void grenadeLaunch(PlayerInteractEvent event) {
           
            ItemStack item = event.getItem();
           
           
            if (item == null) {
                return;
            }
           
           
           
            Action action = event.getAction();
            Player player = event.getPlayer();
            Location loc = player.getLocation();
            loc.setY(loc.getY() + 1.5f);
            ItemMeta meta = item.getItemMeta();
           
           
           
           
            if (action.equals(Action.RIGHT_CLICK_AIR) || action.equals(Action.RIGHT_CLICK_BLOCK)) {
                if (item.getType().equals(Material.FIREWORK_CHARGE)) {
                    if (meta.getDisplayName().equalsIgnoreCase("§aSmoke Grenade")) {
                        ItemStack bomb = new ItemStack(item.getType(), 1);
                        ItemMeta bmeta = bomb.getItemMeta();
                        bmeta.setDisplayName("THROWED");
                        bomb.setItemMeta(bmeta);
                        Entity drop = loc.getWorld().dropItemNaturally(loc, bomb);
                        drop.setVelocity(loc.getDirection().multiply(1.5));
                        item.setAmount(item.getAmount() - 1);
                        new BukkitRunnable() {
                            @SuppressWarnings({"deprecation"})
                            @Override
                            public void run() {
                                if (drop.isOnGround()) {
                                    player.getWorld().playEffect(drop.getLocation(), Effect.EXPLOSION_HUGE, 0);
                                    plugin.getConfig().set(drop.getLocation() + ".Loop", true);
                                    drop.remove();
                                }
                                else {
                                    return;
                                }
                               
                           
                            }
                        }.runTaskLater(plugin, 50);
                       
                        Bukkit.getScheduler().scheduleSyncRepeatingTask(plugin, new Runnable() {
                           
                           
    
                            @Override
                            public void run() {
                                if (plugin.getConfig().getBoolean(drop.getLocation() + ".Loop") == true) {
                                    new BukkitRunnable() {
                                        @SuppressWarnings("deprecation")
                                        @Override
                                        public void run() {
                                            player.getWorld().playEffect(drop.getLocation(), Effect.EXPLOSION_HUGE, 1);
                                           
                                       
                                        }
                                    }.runTaskLater(plugin, 5);
                                    new BukkitRunnable() {
                                        @Override
                                        public void run() {
                                            player.getWorld().playSound(drop.getLocation(), Sound.BLOCK_LAVA_EXTINGUISH, 1, 1);
                                           
                                       
                                        }
                                    }.runTaskLater(plugin, 10);
                                }
                                else {
                                    return;
                                }
                               
                            }
                           
                           
                           
                        }, 1, 5);
                       
                        new BukkitRunnable() {
                            @Override
                            public void run() {
                                plugin.getConfig().set(drop.getLocation() + ".Loop", false);
                           
                            }
                        }.runTaskLater(plugin, 200);
                           
                    }
                    else {
                        return;
                    }
                }
                else {
                    return;
                   
                }
       
            }
            else {
                return;
            }
        }
       
    }
    
     
  2. Online

    timtower Administrator Administrator Moderator

  3. Offline

    KyllianGamer

    upload_2018-5-27_19-29-13.png It is not really working that good... Do you maybe know wy?

    Well i found out you can cancel them all im gonna look if i can progress with that
     
    Last edited by a moderator: May 27, 2018
  4. Online

    timtower Administrator Administrator Moderator

    @KyllianGamer That also stops the ones that aren't from your plugin, that is not good.
    Change Runnable into BukkitRunnable and change scheduleSyncRepeatingTask by runTaskTimer
     
  5. Offline

    KyllianGamer

    Bukkit.getScheduler().runTaskTimer(arg0, arg1, arg2, arg3)

    Okay and wich values do i have to give in there?
     
  6. Online

    timtower Administrator Administrator Moderator

  7. Offline

    KyllianGamer

    Okay... I do not understand... So i have this now:

    where do you want me to put the timer???

    Code:
    package Events;
    
    import org.bukkit.Bukkit;
    import org.bukkit.Effect;
    import org.bukkit.Location;
    import org.bukkit.Material;
    import org.bukkit.Sound;
    import org.bukkit.entity.Entity;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.block.Action;
    import org.bukkit.event.player.PlayerInteractEvent;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.inventory.meta.ItemMeta;
    import org.bukkit.scheduler.BukkitRunnable;
    
    import me.plugin.Main;
    
    public class Grenade3 implements Listener {
       
        private Main plugin = Main.getPlugin(Main.class);
       
    
        @EventHandler
        public void grenadeLaunch(PlayerInteractEvent event) {
           
            ItemStack item = event.getItem();
           
           
            if (item == null) {
                return;
            }
           
           
           
            Action action = event.getAction();
            Player player = event.getPlayer();
            Location loc = player.getLocation();
            loc.setY(loc.getY() + 1.5f);
            ItemMeta meta = item.getItemMeta();
           
           
           
           
            if (action.equals(Action.RIGHT_CLICK_AIR) || action.equals(Action.RIGHT_CLICK_BLOCK)) {
                if (item.getType().equals(Material.FIREWORK_CHARGE)) {
                    if (meta.getDisplayName().equalsIgnoreCase("§aSmoke Grenade")) {
                        ItemStack bomb = new ItemStack(item.getType(), 1);
                        ItemMeta bmeta = bomb.getItemMeta();
                        bmeta.setDisplayName("THROWED");
                        bomb.setItemMeta(bmeta);
                        Entity drop = loc.getWorld().dropItemNaturally(loc, bomb);
                        drop.setVelocity(loc.getDirection().multiply(1.5));
                        item.setAmount(item.getAmount() - 1);
                        new BukkitRunnable() {
                            @SuppressWarnings({"deprecation"})
                            @Override
                            public void run() {
                                if (drop.isOnGround()) {
                                    player.getWorld().playEffect(drop.getLocation(), Effect.EXPLOSION_HUGE, 0);
                                    plugin.getConfig().set(drop.getLocation() + ".Loop", true);
                                    drop.remove();
                                }
                                else {
                                    return;
                                }
                               
                           
                            }
                        }.runTaskLater(plugin, 50);
                       
                        Bukkit.getScheduler().scheduleSyncRepeatingTask(plugin, new Runnable() {
                           
                           
    
                            @Override
                            public void run() {
                                if (plugin.getConfig().getBoolean(drop.getLocation() + ".Loop") == true) {
                                    new BukkitRunnable() {
                                        @SuppressWarnings("deprecation")
                                        @Override
                                        public void run() {
                                            player.getWorld().playEffect(drop.getLocation(), Effect.EXPLOSION_HUGE, 1);
                                           
                                       
                                        }
                                    }.runTaskLater(plugin, 5);
                                    new BukkitRunnable() {
                                        @Override
                                        public void run() {
                                            player.getWorld().playSound(drop.getLocation(), Sound.BLOCK_LAVA_EXTINGUISH, 1, 1);
                                           
                                       
                                        }
                                    }.runTaskLater(plugin, 10);
                                }
                                else {
                                    Bukkit.getScheduler().cancelAllTasks();
                                }
                               
                            }
                           
                           
                           
                        }, 1, 5);
                       
                        new BukkitRunnable() {
                            @Override
                            public void run() {
                                plugin.getConfig().set(drop.getLocation() + ".Loop", false);
                           
                            }
                        }.runTaskLater(plugin, 200);
                           
                    }
                    else {
                        return;
                    }
                }
                else {
                    return;
                   
                }
       
            }
            else {
                return;
            }
        }
       
    }
    
     
  8. Online

    timtower Administrator Administrator Moderator

    @KyllianGamer Replace this: "scheduleSyncRepeatingTask" with "runTaskTimer"
    Don't look at the code, just search and replace.
     
  9. Offline

    KyllianGamer

    So this is what you mean? Okay now what? :D


    Code:
    package Events;
    
    import org.bukkit.Bukkit;
    import org.bukkit.Effect;
    import org.bukkit.Location;
    import org.bukkit.Material;
    import org.bukkit.Sound;
    import org.bukkit.entity.Entity;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.block.Action;
    import org.bukkit.event.player.PlayerInteractEvent;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.inventory.meta.ItemMeta;
    import org.bukkit.scheduler.BukkitRunnable;
    
    import me.plugin.Main;
    
    public class Grenade3 implements Listener {
       
        private Main plugin = Main.getPlugin(Main.class);
       
    
        @EventHandler
        public void grenadeLaunch(PlayerInteractEvent event) {
           
            ItemStack item = event.getItem();
           
           
            if (item == null) {
                return;
            }
           
           
           
            Action action = event.getAction();
            Player player = event.getPlayer();
            Location loc = player.getLocation();
            loc.setY(loc.getY() + 1.5f);
            ItemMeta meta = item.getItemMeta();
           
           
           
           
            if (action.equals(Action.RIGHT_CLICK_AIR) || action.equals(Action.RIGHT_CLICK_BLOCK)) {
                if (item.getType().equals(Material.FIREWORK_CHARGE)) {
                    if (meta.getDisplayName().equalsIgnoreCase("§aSmoke Grenade")) {
                        ItemStack bomb = new ItemStack(item.getType(), 1);
                        ItemMeta bmeta = bomb.getItemMeta();
                        bmeta.setDisplayName("THROWED");
                        bomb.setItemMeta(bmeta);
                        Entity drop = loc.getWorld().dropItemNaturally(loc, bomb);
                        drop.setVelocity(loc.getDirection().multiply(1.5));
                        item.setAmount(item.getAmount() - 1);
                        new BukkitRunnable() {
                            @SuppressWarnings({"deprecation"})
                            @Override
                            public void run() {
                                if (drop.isOnGround()) {
                                    player.getWorld().playEffect(drop.getLocation(), Effect.EXPLOSION_HUGE, 0);
                                    plugin.getConfig().set(drop.getLocation() + ".Loop", true);
                                    drop.remove();
                                }
                                else {
                                    return;
                                }
                               
                           
                            }
                        }.runTaskLater(plugin, 50);
                       
                        Bukkit.getScheduler().runTaskTimer(plugin, new Runnable() {
                           
                           
    
                            @Override
                            public void run() {
                                if (plugin.getConfig().getBoolean(drop.getLocation() + ".Loop") == true) {
                                    new BukkitRunnable() {
                                        @SuppressWarnings("deprecation")
                                        @Override
                                        public void run() {
                                            player.getWorld().playEffect(drop.getLocation(), Effect.EXPLOSION_HUGE, 1);
                                           
                                       
                                        }
                                    }.runTaskLater(plugin, 5);
                                    new BukkitRunnable() {
                                        @Override
                                        public void run() {
                                            player.getWorld().playSound(drop.getLocation(), Sound.BLOCK_LAVA_EXTINGUISH, 1, 1);
                                           
                                       
                                        }
                                    }.runTaskLater(plugin, 10);
                                }
                                else {
                                    Bukkit.getScheduler().cancelAllTasks();
                                }
                               
                            }
                           
                           
                           
                        }, 1, 5);
                       
                        new BukkitRunnable() {
                            @Override
                            public void run() {
                                plugin.getConfig().set(drop.getLocation() + ".Loop", false);
                           
                            }
                        }.runTaskLater(plugin, 200);
                           
                    }
                    else {
                        return;
                    }
                }
                else {
                    return;
                   
                }
       
            }
            else {
                return;
            }
        }
       
    }
    
     
  10. Online

    timtower Administrator Administrator Moderator

    This @KyllianGamer
     
  11. Offline

    KyllianGamer

    Bukkit.getScheduler().runTaskTimer(plugin, new BukkitRunnable()

    so this???
    Or do i really understand wrong?
     
  12. Online

    timtower Administrator Administrator Moderator

  13. Offline

    KyllianGamer

    Okay now what? Now my hole thing is'nt working anymore xD But no errors...

    Code:
    package Events;
    
    import org.bukkit.Bukkit;
    import org.bukkit.Effect;
    import org.bukkit.Location;
    import org.bukkit.Material;
    import org.bukkit.Sound;
    import org.bukkit.entity.Entity;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.block.Action;
    import org.bukkit.event.player.PlayerInteractEvent;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.inventory.meta.ItemMeta;
    import org.bukkit.scheduler.BukkitRunnable;
    
    import me.plugin.Main;
    
    public class Grenade3 implements Listener {
       
        private Main plugin = Main.getPlugin(Main.class);
       
    
        @SuppressWarnings("deprecation")
        @EventHandler
        public void grenadeLaunch(PlayerInteractEvent event) {
           
            ItemStack item = event.getItem();
           
           
            if (item == null) {
                return;
            }
           
           
           
            Action action = event.getAction();
            Player player = event.getPlayer();
            Location loc = player.getLocation();
            loc.setY(loc.getY() + 1.5f);
            ItemMeta meta = item.getItemMeta();
           
           
           
           
            if (action.equals(Action.RIGHT_CLICK_AIR) || action.equals(Action.RIGHT_CLICK_BLOCK)) {
                if (item.getType().equals(Material.FIREWORK_CHARGE)) {
                    if (meta.getDisplayName().equalsIgnoreCase("§aSmoke Grenade")) {
                        ItemStack bomb = new ItemStack(item.getType(), 1);
                        ItemMeta bmeta = bomb.getItemMeta();
                        bmeta.setDisplayName("THROWED");
                        bomb.setItemMeta(bmeta);
                        Entity drop = loc.getWorld().dropItemNaturally(loc, bomb);
                        drop.setVelocity(loc.getDirection().multiply(1.5));
                        item.setAmount(item.getAmount() - 1);
                        new BukkitRunnable() {
                            @Override
                            public void run() {
                                if (drop.isOnGround()) {
                                    player.getWorld().playEffect(drop.getLocation(), Effect.EXPLOSION_HUGE, 0);
                                    plugin.getConfig().set(drop.getLocation() + ".Loop", true);
                                    drop.remove();
                                }
                                else {
                                    return;
                                }
                               
                           
                            }
                        }.runTaskLater(plugin, 50);
                       
                        Bukkit.getScheduler().runTaskTimer(plugin, new BukkitRunnable() {
                           
                           
    
                            @Override
                            public void run() {
                                if (plugin.getConfig().getBoolean(drop.getLocation() + ".Loop") == true) {
                                    new BukkitRunnable() {
                                        @Override
                                        public void run() {
                                            player.getWorld().playEffect(drop.getLocation(), Effect.EXPLOSION_HUGE, 1);
                                           
                                       
                                        }
                                    }.runTaskLater(plugin, 5);
                                    new BukkitRunnable() {
                                        @Override
                                        public void run() {
                                            player.getWorld().playSound(drop.getLocation(), Sound.BLOCK_LAVA_EXTINGUISH, 1, 1);
                                           
                                       
                                        }
                                    }.runTaskLater(plugin, 10);
                                }
                                else {
                                    Bukkit.getScheduler().cancelAllTasks();
                                }
                               
                            }
                           
                           
                           
                        }, 1, 5);
                       
                        new BukkitRunnable() {
                            @Override
                            public void run() {
                                plugin.getConfig().set(drop.getLocation() + ".Loop", false);
                           
                            }
                        }.runTaskLater(plugin, 200);
                           
                    }
                    else {
                        return;
                    }
                }
                else {
                    return;
                   
                }
       
            }
            else {
                return;
            }
        }
       
    }
    
     
  14. Online

    timtower Administrator Administrator Moderator

    @KyllianGamer That is not a very helpful description.
    Print strings, debug.
     
  15. Offline

    KyllianGamer

    I want to know to start with, what are those two strings?

    Bukkit.getScheduler().runTaskTimer(plugin, new BukkitRunnable() {



    @Override
    public void run() {
    if (plugin.getConfig().getBoolean(drop.getLocation() + ".Loop") == true) {
    new BukkitRunnable() {
    @Override
    public void run() {
    player.getWorld().playEffect(drop.getLocation(), Effect.EXPLOSION_HUGE, 1);


    }
    }.runTaskLater(plugin, 5);
    new BukkitRunnable() {
    @Override
    public void run() {
    player.getWorld().playSound(drop.getLocation(), Sound.BLOCK_LAVA_EXTINGUISH, 1, 1);


    }
    }.runTaskLater(plugin, 10);
    }
    else {
    Bukkit.getScheduler().cancelAllTasks();
    }

    }



    }, 100, 5);
     
  16. Online

    timtower Administrator Administrator Moderator

Thread Status:
Not open for further replies.

Share This Page