Solved Creating and stopping Scheduled/BukkitRunnable task

Discussion in 'Plugin Development' started by Crack498, Apr 7, 2015.

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

    Crack498

    I need to add a scheduler/bukkitrunnable task to a falling block (so when it lands it explodes). It also needs to be cancellable itself (so when it explodes it cancels). I've been searching almost 2 hours :(
    Thanks for your help!
     
  2. When scheduling a task, it returns the task ID. In the scheduler you can cancel it with the id. If you have problems using the id. You can put it in a HashMap with as key the uuid of the falling block.
     
  3. Offline

    Crack498

    Inside the scheduler, cancelling taskID returns "Variable may not be initialized"
     
  4. You need to use the Runnable and not the BukkitRunnable because the BukkitRunnable isn't used anymore. Also as I said before, you can save the task ID that the method scheduleSyncDelayedTask(), etc returns added in a hashmap with as key the uuid as the falling block.
     
  5. Offline

    Zombie_Striker

    Make a boolean in your runnable to test if it should continue or not.
     
  6. Offline

    Rocoty

    @Bram0101 Wow there, slow down. Whoever said BukkitRunnable is discontinued? In no way is BukkitRunnable not used anymore. It actually is the most flexible choice when it comes to cancelling a task, seeing as all you would have to do is call cancel() on the BukkitRunnable object itself.

    Where did you read this anyway?
     
  7. Offline

    Crack498

    Searched Internet again and found a solution. Thanks!
     
  8. It's deprecated, and I don't find a lot of people still using it. I also have no idea why it's deprecated. Sorry that I used the wrong words in the post before.
     
  9. Offline

    CheesyFreezy

    • Make a scheduler using:
      Code:
      Bukkit.getServer().getScheduler().scheduleSyncRepeatingTask([Plugin variable here], new BukkitRunnable() {
         public void run() {
            //Repeating stuff in here
         }
      }, [Ticks when the scheduler starts], [Ticks when it runs this scheduler]);
    • Now create a HashMap where it saves the name of the scheduler or something and the TaskID, to do this do the following:
      Code:
      public HashMap<String, Integer> taskID = new HashMap<String, Integer>();
      
      taskID.put("A scheduler name", Bukkit.getServer().getScheduler().scheduleSyncRepeatingTask([Plugin variable here], new BukkitRunnable() {
         public void run() {
            //Repeating stuff in here
         }
      }, [Ticks when the scheduler starts], [Ticks when it runs this scheduler]));
    • To cancel the task simply put the following line in the scheduler:
      Code:
      public HashMap<String, Integer> taskID = new HashMap<String, Integer>();
      
      int count = 0;
      
      taskID.put("A scheduler name", Bukkit.getServer().getScheduler().scheduleSyncRepeatingTask([Plugin variable here], new BukkitRunnable() {
         public void run() {
            count++;
      
            if(count == 10) {
               Bukkit.getServer().getScheduler().cancelTask(taskID.get("A scheduler name"));
            }
         }
      }, [Ticks when the scheduler starts], [Ticks when it runs this scheduler]));
     
  10. Offline

    Rocoty

    @Bram0101 Can you please provide sources supporting your claim? I can't find anywhere stating BukkitRunnable has been deprecated. Nor is it marked as such in the jar I'm using.
     
  11. Get the lastest 1.8.3 craftbukkit or spigot jar.
    When using the spigot buildtools to get the jars:
    -Go to CraftBukkit\src\main\java\org\bukkit\craftbukkit\scheduler\CraftScheduler.java
    -Go to line 441 there are all the methods that take bukkitRunnable, they are all deprecated.
    When using something else to get the jars:
    -Use a decompiler to get the code
    -go to org.bukkit.craftbukkit.v1_8_R2.scheduler.CraftScheduler
    -Go to line 441 there are all the methods that take bukkitRunnable, they are all deprecated.
     
  12. @Bram0101 But this isn't in the Bukkit Alternates section so there is no way for the 1.8 deprecated methods
     
  13. It's also deprecated in the 1.7.10 version. Just checked
     
  14. Offline

    Rocoty

    @Bram0101 Okay, I see where we're misunderstanding each other. I thought you meant the class BukkitRunnable is itself deprecated. According to what you've shown here, only the methods in CraftScheduler which take a BukkitRunnable as parameter, are deprecated. I get that too, but this is certainly no problem. You can still use BukkitRunnable in the way it was intended. It is not deprecated.
     
  15. Yes, but the meaning of the deprecated annotation is that it's discouraged to use it. So it would be better to change to a runnable instead of BukkitRunnable. Just in case they remove it.
     
  16. Offline

    Gater12

  17. I'm just going to lock this, the op found a solution a day ago.
     
Thread Status:
Not open for further replies.

Share This Page