Solved How to get ID of a specific Sync Task?

Discussion in 'Plugin Development' started by piano9uber, Mar 31, 2013.

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

    piano9uber

    I need to find the ID of the task that has a 600 tick delay, none of the others, so that I can remove that task if there are no players in that arena. Here's the code:
    Code:
    for(int derpy = 5; derpy <= 600; derpy += 5)
                                    {
                                        final int de = derpy;
                                        Bukkit.getServer().getScheduler().scheduleSyncDelayedTask(this, new Runnable()
                                        {
                                            @Override
                                            public void run()
                                            {
                                                if(players.get(arenaname) == 0)
                                                {
                                                    Bukkit.getScheduler().cancelAllTasks();
                                                }else
                                                {
                                                        for(int x = 660; x <= 710; x++)
                                                        {
                                                            for(int y = 220; y <= 270; y++)
                                                            {
                                                                for(int z = 461; z <= 511; z++)
                                                                {
                                                                    Location loc = new Location(Bukkit.getWorld("world"), x, y, z);
                                                                    if(loc.getBlock().getType() == Material.SIGN || loc.getBlock().getType() == Material.SIGN_POST || loc.getBlock().getType() == Material.WALL_SIGN)
                                                                    {
                                                                        Sign s = (Sign) loc.getBlock().getState();
                                                                        if(s.getLine(1).equalsIgnoreCase(arenaname))
                                                                        {
                                                                            s.setLine(2, "Players: " + players.get(arenaname));
                                                                            s.update();
                                                                        }
                                                                    }
                                                                }
                                                            }
                                                        }
                                                    if(de == 600)
                                                    {
                                                        int b = 30 / players.get(arenaname);
                                                        if(b == 0)
                                                        {
                                                            b = 1;
                                                        }
                                                        for(Player p : playersinarena.get(arenaname))
                                                        {
                                                            p.sendMessage(ChatColor.GRAY + "Wave: 2");
                                                            p.sendMessage(ChatColor.GRAY + "30 Zombies will be spawned.");
                                                            for(int c = 1; c <= b; c++)
                                                            {
                                                                Random random = new Random();
                                                                int x = random.nextInt(11) - 5;
                                                                int z = random.nextInt(11) - 5;
                                                                Location loc = p.getLocation().add(x, 0, z);
                                                                p.getWorld().spawnEntity(loc, EntityType.ZOMBIE);
                                                                //685 245 486
                                                            }
                                                            for(int x = 660; x <= 710; x++)
                                                            {
                                                                for(int y = 220; y <= 270; y++)
                                                                {
                                                                    for(int z = 461; z <= 511; z++)
                                                                    {
                                                                        Location loc = new Location(Bukkit.getWorld("world"), x, y, z);
                                                                        if(loc.getBlock().getType() == Material.SIGN || loc.getBlock().getType() == Material.SIGN_POST || loc.getBlock().getType() == Material.WALL_SIGN)
                                                                        {
                                                                            Sign s = (Sign) loc.getBlock().getState();
                                                                            if(s.getLine(1).equalsIgnoreCase(arenaname))
                                                                            {
                                                                            s.setLine(3, "Wave: 2");
                                                                            s.update();
                                                                            }
                                                                        }
                                                                    }
                                                                }
                                                            }
                                                        }
                                                    }
                                                }
                                            }
                                        }, derpy);
                                    }
     
  2. Offline

    Burnett1

    Make an int like "int task;" and then do

    Code:
    public class Test extends JavaPlugin implements Listener {
       
        int task;
     
        @Override
        public void onEnable() {
            PluginManager pm = this.getServer().getPluginManager();
     
            pm.registerEvents(this, this);
           
            task = Bukkit.getServer().getScheduler().scheduleSyncDelayedTask(this, new Runnable()
            {
                @Override
                public void run()
                {
                    if(somthing){
                        Bukkit.getScheduler().cancelTask(task);
                    }
                }
            }, 0L);
        }
     
        @Override
        public void onDisable() {
           
           
        }
     
  3. Offline

    piano9uber

  4. Offline

    hockeygoalie5

    piano9uber
    Bukkit has a new way of doing tasks. Instead of what Burnett1 suggested, create a BukkitTask and use runTaskLater or runTaskTimer.
    Code:
    BukkitTask task = getServer().getScheduler().runTaskLater(this, new Runnable() {
        @Override
        public void run() {
            // task
        }
    }, 1L);
    
    task.cancel() will cancel the task.
     
  5. Offline

    piano9uber

Thread Status:
Not open for further replies.

Share This Page