How to create a tick for my plugin using Scheduler

Discussion in 'Plugin Development' started by caldabeast, Jun 14, 2012.

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

    caldabeast

    I'm working on a rather large plugin, but during the first stages, I have encountered a problem. I need to give each user on the server half-a-heart of hunger/food every 30 ticks (1.5seconds), but I am extremely inept when it comes to using the scheduler. I have the following code in the onEnable(), but it doesn't do anything. Could anyone give me some help or possible direct me to a nice tutorial (I have read the one on BukkitWiki many times and it doesn't go very in-depth), that would be very nice.

    Code:
           
    Bukkit.getServer().getScheduler().scheduleSyncDelayedTask(this, new Runnable() {
         public void run() {
              Player[] players = Bukkit.getOnlinePlayers();
              if(players.length > 0){
                   for(Player p : players){
                        int currentMana = p.getFoodLevel();
                        int newMana = currentMana++;
                        p.setFoodLevel(newMana);
                   }
              }
         }
    }, 30L);
    Thanks for any help you give!
     
  2. Offline

    chaseoes

    It shouldn't be in onEnable as far as I know.
     
  3. Offline

    imjake9

    You need to use this instead:
    Code:java
    1. Bukkit.getScheduler().scheduleSyncRepeatingTask(this, new Runnable() {
    2. @Override
    3. public void run() {
    4. // Code here
    5. }
    6. }, 30, 30);
    You can't use scheduleSyncDelayedTask(), since it only runs once. You have to use scheduleSyncRepeatingTask() instead to actually make it run multiple times. It will work fine in onEnable().
     
  4. Offline

    caldabeast

    thanks! I'll try that in a bit!

    I did that, and it still doesn't do anything. I now have this code:
    Code:
        @Override
        public void onEnable(){
            Bukkit.getServer().getScheduler().scheduleSyncRepeatingTask(this, new Runnable() {
                public void run() {
                    Player[] players = Bukkit.getOnlinePlayers();
                    if(players != null){
                        if(players.length > 0){
                            for(Player p : players){
                                int currentMana = p.getFoodLevel();
                                int newMana = currentMana++;
                                p.setFoodLevel(newMana);
                            }
                        }
                    }else{
                        System.out.println("Null list");
                    }
                }
            }, 30L, 30L);
        }
    If anyone could give me a direction to go, I would appreciate it

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 26, 2016
  5. you get anny errors?
     
  6. Offline

    caldabeast

    I do not....
     
  7. you sure the code inside the task is calles?
     
  8. Offline

    caldabeast

    I don't have much experience with scheduler, but I'd assume so, considering the wiki tells me the format...

    any ideas?
    (sorry for the bump, this is a quite irritating issue...)

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 26, 2016
  9. place some debug code inside the task, to see if its realy calles, if its not called, something went wrong earlier, place some debugging statements inside onenable then
     
  10. Offline

    caldabeast

    ok thanks, ill try that
     
  11. Offline

    Eistee²

    Hello , looked for something which shows me how to work with Scheduler and your code works fine for me
    I only changed it to

    Code:
        public void PlayerCheck(){
            Bukkit.getServer().getScheduler().scheduleSyncRepeatingTask(this, new Runnable() {
     
                @Override
                public void run() {
               
                    System.out.println("Works");
                   
                   
                }
     
            }, 20L, 60L);
       
       
       
    }
    For testing :) works fine for me :p
     
  12. Offline

    caldabeast

    Strange... I rewrote the code the same way and it seems to work now. Thanks for pushing me in the right direction!

    (It was doing everything but the setFoodLevel)
     
Thread Status:
Not open for further replies.

Share This Page