Cancelling this running task any help ?

Discussion in 'Plugin Development' started by jonny1011, Mar 2, 2014.

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

    jonny1011

    Hello i have a task the to make a count down for a player how would i cancel the task once the countdown is complicate here is the code.
    I would like to cancel it where i put
    >>>>>>>>>>>> CANCEL THE TASK HERE ONCE ITS ENDED <<<<<<<<<<<<<<>
    Code:java
    1. public void count(final Player player1, final Player target1,final Plugin plugin, final int amount) {
    2. Player player = (Player) player1;
    3. Player target = (Player) target1;
    4. String playername = player.getName().toString();
    5. String targetname = target.getName().toString();
    6.  
    7. MM.Messagemanager(plugin.getConfig().getString("MessageAskForitemTarget"), target, targetname, targetname, 0);
    8. MM.Messagemanager(plugin.getConfig().getString("MessageAskForitemPlayer"), player, playername, targetname, 0);
    9.  
    10. long CountDownStart = plugin.getConfig().getLong("CountDownStartDelay");
    11. long CountDownInterval = plugin.getConfig().getLong("CountDownInterval");
    12. final int CountDownDeduction = plugin.getConfig().getInt("CountDownDeduction");
    13.  
    14. final int taskID = plugin.getServer().getScheduler().scheduleSyncRepeatingTask(plugin, new Runnable() {
    15.  
    16. int countdown = amount;
    17. Player player = (Player) player1;
    18. Player target = (Player) target1;
    19. String playername = player.getName().toString();
    20. String targetname = target.getName().toString();
    21.  
    22.  
    23.  
    24. public void run() {
    25. Boolean forcestop = player.isSneaking();
    26.  
    27. if (forcestop == true) {
    28. player.sendMessage("Stopped by " + player.getDisplayName() + " sneeking");
    29. target.sendMessage("Stopped by " + player.getDisplayName() + " sneeking");
    30. }
    31.  
    32.  
    33. if (countdown == 0 || countdown < 0 && forcestop == false) {
    34. >>>>>>>>>>>> CANCEL THE TASK HERE ONCE ITS ENDED <<<<<<<<<<<<<<>
    35. ISAR.removeItems(ChatColor.RED + plugin.getConfig().getString("TaggedItemName"), target, player);
    36.  
    37.  
    38. }
    39.  
    40. if (countdown > 0) {
    41.  
    42. MM.Messagemanager(plugin.getConfig().getString("MessageCountdownTarget"), target, targetname, targetname, countdown);
    43. MM.Messagemanager(plugin.getConfig().getString("MessageCountdownPlayer"), player, playername, targetname, countdown);
    44. countdown = countdown - CountDownDeduction;
    45. }
    46.  
    47. }
    48. }, CountDownStart, CountDownInterval );
    49.  
    50. }
     
  2. Offline

    qhenckel

    Something I have done is create another scheduled task but make it delayed so it cancels the repeating task and since a delayed task only runs once then you are task free at the end. there are a few problems and some errors that might come from this though. I'm sure anyone with more Java experience will be having a heart attack now. =D but thats what I do. hope it helps.
     
  3. Offline

    jonny1011

    This wont work tho as its getting the task id thats generated i dont know how to do
     
  4. Offline

    qhenckel

    Just start them both at the same time:
    Code:java
    1. final int taskID = plugin.getServer().getScheduler().scheduleSyncRepeatingTask(plugin, new Runnable() {
    2. public void run() {
    3. //countdown
    4. }
    5.  
    6. }
    7. final int stopTask = plugin.getServer().getScheduler().scheduleDelayedTask(plugin, new Runnable(taskID) {
    8.  
    9. int id;
    10.  
    11. Runnable(int id){
    12. taskId = id;
    13. }
    14.  
    15. public void run(id) {
    16. //stop task with id
    17. }
    18.  
    19. }
     
Thread Status:
Not open for further replies.

Share This Page