Solved BukkitRunnable() I dont get this working ...

Discussion in 'Plugin Development' started by InflamedSebi, Apr 18, 2013.

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

    InflamedSebi

    Code:java
    1. public void breakBlockList(final List<Block> BlockList, final Player player, final ItemStack tool, long speed) {
    2.  
    3. Bukkit.getScheduler().runTaskTimer(plugin, new BukkitRunnable() {
    4. int counter = 0;
    5. @Override
    6. public void run() {
    7. logger.log("" + counter, 0);
    8.  
    9. events.get(counter).breakNaturally(tool);
    10.  
    11. counter++;
    12.  
    13. if (events.size() <= counter) {
    14. this.cancel;
    15. Bukkit.getScheduler().cancelTask(getTaskId());
    16. return;
    17. }
    18. }
    19. }, speed, speed);
    20. }


    2 Problems ...
    the task needs to cancel itself! due to multiple of these tasks are running, i cannot define a id for everyone and neither "this.getTaskId()" nor "this.cancel()" is working ... always telling me "Task not sheduled yet." dunno how to fix this ...

    2nd Problem .... the counter it is always 0 and wont count up ... and i cannot define a global var due to the same problem as above ...

    someone can tell me how to get the "this.getTaskId()" and "this.cancel()" working?
     
  2. Offline

    Technius

    1. BukkitRunnable can schedule itself independently. For example:
    Code:
    new BukkitRunnable()
    {
          public void run()
          {
              Bukkit.getServer().broadcastMessage("Test");
          }
    }.runTask(yourplugin);
    
    2. Your runnable never gets scheduled, so its run method never gets called.
     
  3. First, is that your actual code ? Because: this.cancel -> this.cancel()

    The cancel() method does work.

    I never used getScheduler() with BukkitRunnable so I don't know if that's valid but this works and it's also cleaner (and shorter):
    Code:
    new BukkitRunnable()
    {
        public void run()
        {
             // ...
        }
    }.runTaskTimer(plugin, 0, ticks)
    Have you actually printed any values or are you guessing that they're what you expect ?

    EDIT:
    Yeah apparently using BukkitRunnable with getScheduler() strips it from its ability of using cancel() inside its method... you forgot to mention that you have console errors :)
     
  4. Offline

    InflamedSebi

    Technius Digi
    oh yeah ... if u schedule the Runnable with the sheduler it will not work like it should ... but if i do it your way it does :D
    ty ...

    I will test the counter next ... maybe it will work this way ...
     
Thread Status:
Not open for further replies.

Share This Page