Solved Cancel Repeating Task

Discussion in 'Plugin Development' started by Telmovieira, Mar 26, 2015.

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

    Telmovieira

    Hello everyone!
    Today, i ask you a very simple question. How do i cancel this task?
    Code:
    else if (me.Telmovieira.Liberatum.Team.getTeamType(player) == TeamType.YELLOW) {
                        if (block.getBlock().getType().equals(Material.IRON_BLOCK)
                                || block.getBlock().getType()
                                        .equals(Material.DIAMOND_BLOCK)
                                || block.getBlock().getType()
                                        .equals(Material.EMERALD_BLOCK)
                                || block.getBlock().getType()
                                        .equals(Material.REDSTONE_BLOCK)) {
                            vod.getWorld().spawnEntity(vod, EntityType.LIGHTNING);
                            player.playSound(loc, Sound.AMBIENCE_THUNDER, 1, 1);
                            vod1.getBlock().setType(Material.FIRE);
                            vod2.getBlock().setType(Material.GOLD_BLOCK);
                            vod3.getBlock().setType(Material.GOLD_BLOCK);
                            vod4.getBlock().setType(Material.GOLD_BLOCK);
                            vod5.getBlock().setType(Material.GOLD_BLOCK);
                            vod.getBlock().setType(Material.GOLD_BLOCK);
                            vodnumberblue = 10;
                            vodnumberred = 10;
                            vodnumbergreen = 10;
                            plugin.getServer()
                                    .getScheduler()
                                    .scheduleSyncRepeatingTask(plugin,
                                            new Runnable() {
                                                public void run() {
                                                    if (vodnumberyellow != -1) {
                                                        if (vodnumberyellow != 0) {
                                                            territoriesboard.getObjective("domination").getScore(ChatColor.GREEN + "Vilage Of D.:").setScore(vodnumberyellow);
                                                            vodnumberyellow--;
                                                        } else {
                                                            Bukkit.broadcastMessage(ChatColor.GOLD + "Village Of Downbreaking was conquered by the" + ChatColor.YELLOW + " YELLOW TEAM" );
                                                            vodnumberyellow = 10;
                                                            DomUtils.conquer(Territories.VILLAGE_OF_DOWNBREAKING, TeamType.YELLOW);
                                                            if (DomUtils.getRedTeamTerritories().contains(Territories.VILLAGE_OF_DOWNBREAKING)){
                                                            DomUtils.remove(Territories.VILLAGE_OF_DOWNBREAKING, TeamType.RED);}
                                                            if (DomUtils.getGreenTeamTerritories().contains(Territories.VILLAGE_OF_DOWNBREAKING)){
                                                            DomUtils.remove(Territories.VILLAGE_OF_DOWNBREAKING, TeamType.GREEN);}
                                                            if (DomUtils.getBlueTeamTerritories().contains(Territories.VILLAGE_OF_DOWNBREAKING)){
                                                            DomUtils.remove(Territories.VILLAGE_OF_DOWNBREAKING, TeamType.BLUE);}
                                                        }
    
                                                    }
                                                }
                                            }
    
                                            , 0L, 20L);
    
                        }
                    }
    
    i need to cancel it in the "else" statement, so it doesn't repeat it self over and over again. I would like to know if i can cancel this task from another methods and how. I've tried to:
    -change to delayed task (did't even started the countdown)
    -Do it with the bukkitRunnable (Same error as above)
    -Do .cancelTask(ID) (give's me a java error even if i store the id in a int variable)

    It's been at least 3 hours, and i found no solution... Plz HELP ME!!

    Thank you and have a nice day!
     
  2. Offline

    ColonelHedgehog

    You can store the entire task in an int, and then use:

    Bukkit.getServer().getScheduler().cancelTask(taskid);
     
  3. Offline

    SuchSwegMuchWow

  4. Offline

    Telmovieira

    @ColonelHedgehog
    "The loca variable Task may not have been initialized" This error apears when i do what you said.


    @SuchSwegMuchWow
    this.cancel(); only works for BukkitRunnable, which is no the case, and the setup i have here does no work with a bukkitRunnable, unless there is a way that i don't recall.... But i will look into the wiki and see if there is something usefull
     
  5. Offline

    ColonelHedgehog

  6. Offline

    SuchSwegMuchWow

  7. Offline

    nverdier

    You can only use that if the class extends BukkitRunnable. Which he isn't using.

    EDIT: Wow, for some reason didn't see the posts above... lol
     
  8. Offline

    Telmovieira

    c'mon gentleman.... there must be a way... i looked everywhere and found nothing related...
     
  9. Offline

    ColonelHedgehog

    Make an integer, initialize it, then set it to the task.

    EDIT:

    I realized that you'll have to declare it as final. So it should be something like:

    Code:
    final int[] task = new int[]{-1};
    task = schedule... blah blah, new runnable()
    {
    @Override
    public void run()
    {
    if(something)
    {
    Bukkit.getServer().getScheduler().cancelTask(task[0]);
    }
    }
    }
     
  10. Offline

    Telmovieira

    @ColonelHedgehog , Does't work.... the variable on top has to be int and not int[]... Probabily i am doing something wrong.... Where you have schedule.... blah blah blah, i have :
    Bukkit.getServer().getScheduler().scheduleSyncRepeatingTask(plugin, runnable, long, long)

    is this suposed to work?
     
  11. Offline

    ColonelHedgehog

    That's right. But remember that you're accessing the first index of the array with task[0]. Can you post your code?
     
  12. Offline

    SuchSwegMuchWow

    @Telmovieira
    Code:
    int task;
    
    task = Bukkit.getScheduler().scheduleSyncRepeatingTask(plugin, new Runnable(){
    
    }, 0L, 20L);
    
    Bukkit.getScheduler().cancelTask(task)
     
  13. Offline

    ColonelHedgehog

    Again, you must declare "task" as final. Thus, you can't change its value unless you make it a one-element array.
     
  14. Offline

    Telmovieira

    Yeah, i got it!

    final int task at top and then task = schedule thing...........

    works perfectly thank you every one for your help
     
    ColonelHedgehog likes this.
Thread Status:
Not open for further replies.

Share This Page