Solved Help with cancelling task

Discussion in 'Spigot Plugin Development' started by Epixel, Mar 26, 2018.

  1. Hello, I'm trying to make it so that if somebody does /home they need to wait 10 seconds before it actually warps. If they move, the warp will be cancelled. I've made it so that if they move while they're in the hashmap Warping, they will be removed from it, so that the warp doesn't occur. But when I try to cancel the task it says "The local variable timer may not have been initialized" with a red line under "(timer)". How can I cancel the task from the inside? I know you guys don't like spoonfeeding, but I learn the most by doing it that way, any help is appreciated tho.
    Code (Text):

                    public static HashMap<Player, Integer> Warping = new HashMap<Player, Integer>();
                    final int timer;
                 
                    timer = Bukkit.getScheduler().scheduleSyncRepeatingTask(
                            plugin,
                            new Runnable() {
                                @Override
                                public void run() {
                                    if(!(Warping.containsKey(player))){
                                        Bukkit.getScheduler().cancelTask(timer);
                                        return;
                                    }
                                    if(Warping.get(player) < 10){
                                        int s = Warping.get(player);
                                        Bukkit.getServer().broadcastMessage("Warping " + s);
                                        Warping.put(player, s+1);
                                    }
                                    if(Warping.get(player) == 10){
                                    String worldname = (String) homesConfig.get(puuid + ".World");
                                    World world = Bukkit.getWorld(worldname);
                                    Double x = (Double) homesConfig.get(puuid + ".X");
                                    Double y = (Double) homesConfig.get(puuid + ".Y");
                                    Double z = (Double) homesConfig.get(puuid + ".Z");
                                    //Float yaw = (Float) homesConfig.get(puuid + ".Yaw");
                                    float yaw = (float) homesConfig.getDouble(".Yaw");
                                    float pitch = (float) homesConfig.getDouble(".Pitch");
                                    //Float pitch = (Float) homesConfig.get(puuid + ".Pitch");
                                    Location loc = new Location(world, x, y, z, yaw, pitch);
                                    player.teleport(loc);
                                    player.sendMessage(ChatColor.GRAY + "Warp successful.");
                                    Warping.remove(player);
                                 
                         
                                    }
                             
                                     
                                     
                                    }
                               }
                           ,
                           0L,
                           20L);
    [CODE]
     
  2. Put it inside thr runnable but above the run method. This should be self self explanatory, you haven't assiged anything to that variable. You allocated the space for it, but nothing specific.
    Bukkit tasks or Threads I think all have a cancel() method so try that.
     
  3. Use a BukkitRunnable. Then you can just call .cancel() on it.
     
  4. If I put it above the run method, it won't fire every second, it will just happen once and then quit. cancel() doesn't work either
    I tried doing that, but it keeps going anyways
     
  5. You should create a hashmap that would store bukkittasks on players as a key.

    Example:
    Code (Java):

    HashMap<Player, BukkitTask> hmap = new HashMap<>();
    hmap.put(yourPlayer, new BukkitRunnable() {
    @Override
    public void run() {
    // your code (count down etc)
    }
    }.runTaskTimer(yourPlugin, 0,20));
     
    Now you should be checking on the PlayerMoveEvent if the player moved his X,Y or Z and if he did then you should get the task, cancel it and remove him from the hashmap.
    Code (Java):

    hmap.get(yourPlayer).cancel();
    hmap.remove(yourPlayer);
    // Send a message maby that his TP got cancelled.
     
    Hope that helps out a bit.

    You should keep your current hashmap where you save integers on players for your countdowns.
     
    • Like Like x 1
    • Agree Agree x 1