Solved Reapeting Task won't cancel if two are running!

Discussion in 'Plugin Development' started by Archgames, Jan 26, 2014.

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

    Archgames

    Hi,
    When I run this task I want to cancel it, which I can do but I don't know how to cancel it when two exist at once seems bukkit gets confused and leaves one running!
    Code:java
    1. this.fireTrail = Bukkit.getScheduler().scheduleSyncRepeatingTask(this.plugin ,
    2. new Runnable() {
    3. public void run() {
    4. fireworkDarkMagic(egg);
    5. }
    6. }, 0, 1);

    How would I cancel this task if it is running multiple times?
     
  2. Offline

    RawCode

    store all instances in list.
     
  3. Offline

    Archgames

  4. Offline

    xTrollxDudex

  5. Offline

    Archgames

  6. Offline

    xTrollxDudex

    Archgames
    PHP:
    List<Integer> list = new ArrayList<Integer>();

    // ...
    list.add(this.fireTrail);

    // ...
    Bukkit.getScheduler().cancelTask(list.get(/* ... */));
     
  7. Offline

    Archgames

    xTrollxDudex
    How do I know which task to cancel in the
    Code:java
    1. .cancelTask(list.get(/*what do I put here?*/));
     
  8. Offline

    xTrollxDudex

    Archgames
    Actually, let's store a ref with it.
    PHP:
    Map<IntegerIntegermap = new HashMap<IntegerInteger>();

    // ...
    map.put(1fireTrail);
    map.put(2/* another */);

    //...
    Bukkit.getScheduler().cancelTask(map.get(1)):
    Bukkit.getScheduler().cancelTask(map.get(2)):
     
  9. Offline

    Archgames

    xTrollxDudex
    The problem is I only want to cancel one task not multiple
    Code:java
    1. @EventHandler
    2. public void shootStaff(PlayerInteractEvent e){
    3. final Player player = e.getPlayer();
    4. int speed = 3;
    5. final Vector direction = player.getEyeLocation().getDirection()
    6. .multiply(speed);
    7. if(e.getAction() == Action.RIGHT_CLICK_AIR || e.getAction() == Action.RIGHT_CLICK_BLOCK){
    8. if(player.getItemInHand().getType() == Material.DIAMOND_HOE){
    9. final Egg egg = player.getEyeLocation().getWorld().spawn(player.getEyeLocation(), Egg.class);
    10. egg.setVelocity(direction);
    11. egg.setShooter(player);
    12. this.taskID = Bukkit.getScheduler().scheduleSyncRepeatingTask(this ,
    13. new Runnable() {
    14. public void run() {
    15. fireworkDarkMagic(egg);
    16. }
    17. }, 0, 1);
    18. fireTrail.add(this.taskID);
    19. }
    20. }
    21. }
    22. @EventHandler
    23. public void eggCollide(ProjectileHitEvent e){
    24. final Projectile proj = e.getEntity();
    25. if (proj instanceof Egg) {
    26. Egg egg = (Egg) proj;
    27. final Location eggLoc = egg.getLocation();
    28. Player player = (Player) egg.getShooter();
    29. player.getWorld().playSound(eggLoc, Sound.FIREWORK_LARGE_BLAST, 1, 1);
    30. Bukkit.getScheduler().cancelTask(fireTrail.get(1));
    31. egg.getWorld().createExplosion(egg.getLocation(), 4);
    32. }
    33. }

    Multiples of this one task will be running at the same time and I need to cancel it in the eggcollide event but if two are running at the same time the fireworks particle system will not be stopped on impact!
     
  10. Offline

    xTrollxDudex

    Archgames
    You don't need to cancel both. I was implying how you can store the ID of the runnable with a counter so you can refer back to it and cancel the task.
     
  11. Offline

    Archgames

    xTrollxDudex
    Been working at this for quite a while and still am stumped how can cancel a task if two of the task are running at the same time?
     
  12. Offline

    xTrollxDudex

    Archgames
    Again, get it from the HashMap using your counter.
     
  13. Offline

    Archgames

    If anyone can help I still need help!
    Thanks!
     
  14. Offline

    afistofirony

    Archgames As xTrollxDudex suggested, try storing them in a HashMap. However, a better idea may be to store the name of the task. Then, you can retrieve it using that name:

    Code:
    HashMap<String, Integer> taskIds = new HashMap<String, Integer>();
    When you want to add a task (lovingly named myTask):
    Code:
    taskIds.put("myTask", Bukkit.getScheduler().scheduleSyncRepeatingTask(this.plugin, new Runnable () { ... }));
    And then to cancel the task:
    Code:
    Bukkit.getScheduler().cancelTask(taskIds.get("myTask"));
     
  15. Offline

    Archgames

Thread Status:
Not open for further replies.

Share This Page