scheduleSyncRepeatingTask canceling

Discussion in 'Plugin Development' started by srspore, Sep 22, 2015.

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

    srspore

    How would I cancel the repeating task after 'X' amount of loops or once a condition is met?
    Thank you!
    also sorry if this information is easily available, I feel like it is but I have looked and couldn't find it. I might just be dumb.
     
  2. Offline

    teej107

  3. Offline

    Petersoj

    You can assign an integer variable to the scheduler like
    Code:
    int a = Bukkit.getServer().getScheduler().scheduleSyncRepeatingTask(Blah blah blah);
    
    Then simply cancel it like
    Code:
    Bukkit.getScheduler().cancelTask(a);
    
     
  4. Offline

    srspore

    @Petersoj
    @teej107
    Thank you!

    Actually how would I make it cancel after 'X' number of times, I'm having trouble with this because it requires the variables inside the runnable to be final.

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

    srspore

    @KingFaris11 So I might be doing something wrong... here is my code, I know it's a lot, just focus on the stuff with comments. What I'm trying to do here is make a "dragonborn" class. Which, if you're familiar with Skyrim, will give the player the ability to "Fus-Ro-Dah" other players. If that didn't help explain it then I want it so when a player clicks a potato, they throw invisible snowball. And then every 2 ticks for like 2 seconds the server checks to see if a player is within two blocks of that snowball and if there is a player close to the snowball I push the player back. Problem is I can't make it stop checking after two or so seconds....
    Code:
    package me.srspore.kit;
    
    import java.util.ArrayList;
    
    import net.minecraft.server.v1_8_R3.PacketPlayOutEntityDestroy;
    
    import org.bukkit.Bukkit;
    import org.bukkit.Material;
    import org.bukkit.craftbukkit.v1_8_R3.entity.CraftPlayer;
    import org.bukkit.entity.Player;
    import org.bukkit.entity.Snowball;
    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.scheduler.BukkitRunnable;
    import org.bukkit.util.Vector;
    
    public class DragonBorn implements Listener {
        int counter = 10; // defined counter
        public static void dragonBornMaterials(Player p) {
            Util.give(p, Material.STONE_SWORD, 1);
            p.getInventory().setHelmet(new ItemStack(Material.IRON_HELMET));
            p.getInventory().setChestplate(new ItemStack(Material.IRON_CHESTPLATE));
            Util.give(p, Material.BAKED_POTATO, 1);
        }
        public static void dragonBorn(Player p) {
            Commands.kit.put(p, "dragonBorn");
            dragonBornMaterials(p);
            for (int i=0;i<34;i++) {
                Util.give(p, Material.MUSHROOM_SOUP, 1);
            }
        }
        ArrayList <Player> dragonBornCoolDown = new ArrayList <Player>();
        @SuppressWarnings("deprecation")
        @EventHandler
        public void onPlayerInteract(final PlayerInteractEvent e) {
            final Player p = (Player) e.getPlayer();
            if (Commands.kit.get(p) != "dragonBorn") return;
            if (!(e.getAction() == Action.RIGHT_CLICK_AIR) && !(e.getAction() == Action.RIGHT_CLICK_BLOCK)) return;
            if (!(p.getItemInHand().getType() == Material.BAKED_POTATO)) return;
            e.setCancelled(true);
            final Snowball sb = p.launchProjectile(Snowball.class);
            final Vector vel = sb.getVelocity();
            PacketPlayOutEntityDestroy packet = new PacketPlayOutEntityDestroy(new int[]{sb.getEntityId()});
            for(Player t : Bukkit.getOnlinePlayers())
                ((CraftPlayer)t).getHandle().playerConnection.sendPacket(packet);
            for (Player target : Bukkit.getServer().getOnlinePlayers()) {
                final Player t = target;
                Bukkit.getScheduler().runTaskTimer(Commands.that, new BukkitRunnable() {
                    public void run() { // runnable I want to cancel
                        if (counter < 0) this.cancel(); // tried to cancel it here
                        counter--;
                        p.sendMessage("C: ");    // debug message
                        p.sendMessage("B: " + counter); // debug message
                        p.sendMessage("test");  // debug message
                        if (t.equals(p)) return;
                        if (!(t.getLocation().getBlockX() > sb.getLocation().getBlockX()-2)) return;
                        if (!(t.getLocation().getBlockX() < sb.getLocation().getBlockX()+2)) return;
                        if (!(t.getLocation().getBlockY() > sb.getLocation().getBlockY()-2)) return;
                        if (!(t.getLocation().getBlockY() < sb.getLocation().getBlockY()+2)) return;
                        if (!(t.getLocation().getBlockZ() > sb.getLocation().getBlockZ()-2)) return;
                        if (!(t.getLocation().getBlockZ() < sb.getLocation().getBlockZ()+2)) return;
                        // Vector with length 1 pointing from p to t:
                        Vector dir = t.getLocation().subtract(p.getLocation()).toVector().normalize();
                        double strength = 2.0D; // some fixed strength
                        // pushing t in the given direction with the given strength:
                        t.setVelocity(dir.multiply(strength));
                        t.setVelocity(dir);
                        sb.setVelocity(vel);
                    }
                }, 0, 2);
               
            }
           
        }
    }
    
     
  6. You should have the counter in the runnable instance, so that it's one counter per instance. Also, you should exit out of the method once the counter is less than or equal to 0.
    New code:

    Code:
    new BukkitRunnable() {
        private int counter = 20; // 2 seconds in ticks divided by 2. (20 ticks = 1 second normally, but every 2 ticks this counter is being reduced by 1, so 20 ticks = 2 seconds).
    
        @Override
        public void run() {
            if (!p.isOnline() || !t.isOnline() || p.getUniqueId().equals(t.getUniqueId())) {
                this.cancel();
                return;
            }
            if (this.counter > 0) {
                if (!(t.getLocation().getBlockX() > sb.getLocation().getBlockX() - 2)) return;
                if (!(t.getLocation().getBlockX() < sb.getLocation().getBlockX() + 2)) return;
                if (!(t.getLocation().getBlockY() > sb.getLocation().getBlockY() - 2)) return;
                if (!(t.getLocation().getBlockY() < sb.getLocation().getBlockY() + 2)) return;
                if (!(t.getLocation().getBlockZ() > sb.getLocation().getBlockZ() - 2)) return;
                if (!(t.getLocation().getBlockZ() < sb.getLocation().getBlockZ() + 2)) return;
                // Vector with length 1 pointing from p to t:
                Vector dir = t.getLocation().subtract(p.getLocation()).toVector().normalize();
                double strength = 2D; // Some fixed strength
                //Pushing t in the given direction with the given strength:
                t.setVelocity(dir.multiply(strength));
                sb.setVelocity(vel);
    
                this.counter--;
            } else {
                this.cancel();
            }
        }
    }.runTaskTimer(Commands.that, 0L, 2L);
    
     
Thread Status:
Not open for further replies.

Share This Page