Cancel runnable from another method in another class.

Discussion in 'Plugin Development' started by MinecraftMart, Feb 27, 2015.

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

    MinecraftMart

    So i got a class where i have a method which needs to cancel a runnable in another class thats in a method.

    This is the code with the runnable.
    Code:
    public void onDeathCheck(final String ar){
            final ArrayList<UUID> listofplayer = new ArrayList<UUID>();
            final BukkitScheduler scheduler = Bukkit.getServer().getScheduler();
            scheduler.scheduleSyncRepeatingTask(main, new Runnable() {
                @Override
                public void run() {
                    listofplayer.clear();
                    for (Entry<UUID, String> entry : main.game.players.entrySet())
                    {
                        if(entry.getValue().equalsIgnoreCase(ar)){
                            listofplayer.add(entry.getKey());
                        }
    
                    }
                    if(listofplayer.size() == 1){
                        if(listofplayer.size() == 1){
                            UUID uuid = listofplayer.get(0);
                               for(Player player : Bukkit.getServer().getOnlinePlayers()){
                                   if(player.getUniqueId().equals(uuid)){
                                       String arenaname = main.game.players.get(player.getUniqueId());
                                       World world = Bukkit.getWorld(main.arenas.getString("arenas." + arenaname + "." + "egg.world"));
                                       Location loc = new Location(world, main.arenas.getDouble("arenas." + arenaname + "." + "egg.X")
                                            ,main.arenas.getDouble("arenas." + arenaname + "." + "egg.Y"),
                                            main.arenas.getDouble("arenas." + arenaname + "." + "egg.Z"));
                                       player.teleport(loc);
                                       player.sendMessage(ChatColor.GREEN + "You are the last man standing! You have been teleported to claim your prize!");
                                       Bukkit.getScheduler().cancelTasks(main);
                                       scheduler.cancelTasks(main);
                                   }
                               }         
                              
                        }
                    }
                }
            } , 0L, 20L);
       
        }
    I made the class accesable via the main.
    Any idea how i would cancel this ?
     
  2. Offline

    DJSkepter

    @MinecraftMart Store the Runnable number in a list. (Or a Map). Then you can use Bukkit.getScheduler().cancelTask(id);
     
  3. Offline

    mine-care

    Huh? the above makes no sense, its like saying:
    Object a =...;
    if(a is an apple){
    //here you are sure it is an apple
    if(a is an apple){
    //here you check again if it is an apple. this is a pointless if :p

    }
    }

    Also why you create a clone of the arraylist in the method?
    get a set of keys as: Set ... = map.keySet();
     
  4. Offline

    MinecraftMart

    Aah thanks, ill check this out yeah.
    But what if more instances of the method are created?
    Like lets say i call the method twice?

    That if thing is a mistake xD and the player map has more players with a different var. the listofplayers is a list with 1 var. And for now i dont know a good way to check if the number of keys with a val equals 1. So yeah.. xD Im still learning. But so far i made an awesome lot of progress for an awesome plugin.
     
    Last edited: Feb 27, 2015
  5. Offline

    mine-care

    @MinecraftMart Good. To fix the above prob, read about lists & maps. it normally takes about 10-20 minutes :p
     
  6. Offline

    MinecraftMart

    You mean the problem with listofplayers or my main problem?
     
  7. Offline

    1Rogue Retired Staff

    Don't store an int from scheduleTask(etc), use .runTaskTimer or similar methods, and store the BukkitTask object that is returned. Then call cancel() on that object.
     
Thread Status:
Not open for further replies.

Share This Page