Canceling a Repeating Task Not Working

Discussion in 'Plugin Development' started by Shad, Jul 20, 2013.

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

    Shad

    Here's my code:
    Code:java
    1. //Running the smelt timer
    2. int taskid = Bukkit.getServer().getScheduler().scheduleSyncRepeatingTask(plugin, new Runnable(){
    3.  
    4. public void run() {
    5.  
    6. if(player.getInventory().contains(15)){
    7. int randomNum = 0 + (int)(Math.random()*((1 - 0) + 1));
    8. if(randomNum == 1){
    9. int taken = 0; //The amount that has been taken from the player's inventory
    10. Material mat = Material.getMaterial(15); //id is the item id gotten from the config
    11. int needed = 1;//needed was also gotten from the config and it is the amount that I need to take from the player
    12. itemRemover(player, taken, mat, needed);
    13.  
    14. playersSmithing.remove(players);
    15.  
    16. int empty = playerinv.firstEmpty();
    17. try {
    18. smithExp.put(265, (int) 12.5); //iron
    19.  
    20. Statement s = c.createStatement();
    21. s.executeUpdate("UPDATE MMOSkills_smithing "
    22. + "SET EXP=EXP+"+smithExp.get(clickeditem)+" WHERE Player='"+players+"';");
    23.  
    24.  
    25. levelChecker(players, player);
    26. } catch (SQLException e) {
    27. // TODO Auto-generated catch block
    28. e.printStackTrace();
    29. }
    30.  
    31. player.sendMessage(ChatColor.GOLD+"You successfully smelt a bar.");
    32. ItemCreator(empty, smelteditem, player, clickeditem, clickeditemdata);
    33. }else if(randomNum == 0){
    34. int taken = 0; //The amount that has been taken from the player's inventory
    35. Material mat = Material.getMaterial(15); //id is the item id gotten from the config
    36. int needed = 1;//needed was also gotten from the config and it is the amount that I need to take from the player
    37. itemRemover(player, taken, mat, needed);
    38.  
    39. playersSmithing.remove(players);
    40. player.sendMessage(ChatColor.GOLD+"You fail to smelt a bar.");
    41. }
    42. }else{
    43. player.sendMessage(ChatColor.RED+"You run out of ore to smelt and stop smelting.");
    44. Bukkit.getScheduler().cancelTask(playersSmithing.get(players));
    45. playersSmithing.remove(players);
    46. }
    47.  
    48. }
    49.  
    50. }, 1L, 40L);
    51. playersSmithing.put(players, taskid);
    52.  


    Line 44 is where I attempt to cancel the task.

    It's funny because I use this same exact method for delayed tasks to cancel and it works (in a different context).

    What exactly am I doing wrong for this not to work for a repeating task?
     
  2. Offline

    Shad

    Still having problems with it and still can't figure it out.
     
  3. Offline

    Henzz

    Shad
    PHP:
    Bukkit.getServer().getScheduler().cancelTask(yourbukkittask.getTaskId());
     
  4. Offline

    Shad

    How am I getting the Bukkit task name?

    Also why isn't just getting the taskid via the int taskid = blahblah not working?
     
  5. Offline

    Henzz

    Shad
    Set your task to a variable of BukkitTask, then cancel it using the code I posted.
     
  6. Offline

    xTrollxDudex

    Henzz
    Even I didn't get that.
     
  7. Offline

    Henzz

    Shad
    You're trying to cancel the playersSmithing.get(players), change it to taskId
     
  8. Offline

    Shad

    BukkitTask task = Bukkit,getServer()...ect?

    I tried that it wasn't working. I guess I'm just really stupid at this.

    The hashmap should be able to cancel the task because the key players should bring the taskid, I do it with a delayed task and it works.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 3, 2016
Thread Status:
Not open for further replies.

Share This Page