Timer function..

Discussion in 'Plugin Development' started by Infernus, Mar 1, 2011.

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

    Infernus

    I tried alot, almost ate my keyboard for rage but couldnt get the timer to work..

    code (open)
    import org.bukkit.scheduler.BukkitScheduler;

    callSyncMethod(this, doTasks());

    public static void doTasks() {
    //TODO.
    }

    error (open)
    The method callSyncMethod(plgInf, void) is undefined for the type plgInf


    /me wonders why Java doesn't just have a SetTimer(amount, loop) and Timer() function.
     
  2. Offline

    Edward Hand

  3. Offline

    Infernus

    Tried all of them, all giving the same error..

    Any examples please, examples are the best way for me to learn using something.

    Thanks!
     
  4. Offline

    Edward Hand

    Oh, sorry. I should have payed more attention to your code/error.
    It should be this:
    Code:
    getServer().getScheduler().scheduleSyncTask(.....
     
  5. Offline

    Infernus

    Thanks, at least that got fixed, but what's the 'Runnable task' meaning? I do have a void here..
     
  6. Offline

    Crash

    An instance of a class that implements Runnable

    eg :
    Code:
    public class AClass implements Runnable {
    
        @Override
        public void run(){
    
            //Do whatever every tick
    
        }
    
    }
     
  7. Offline

    Infernus

    Got it, but getting error at 'Tasks tasks = new Tasks();', I have no idea how to otherwisely use it..

    code (open)
    Tasks tasks = new Tasks();

    public void onEnable() {
    PluginManager pm = getServer().getPluginManager();
    pm.registerEvent(Event.Type.PLAYER_COMMAND, playerListener, Event.Priority.Normal, this);
    pm.registerEvent(Event.Type.PLAYER_RESPAWN, playerListener, Event.Priority.Normal, this);
    pm.registerEvent(Event.Type.BLOCK_DAMAGED, blockListener, Event.Priority.Normal, this);
    pm.registerEvent(Event.Type.CREATURE_SPAWN, entityListener, Event.Priority.Normal, this);
    getServer().getScheduler().scheduleSyncDelayedTask(this, tasks);
    PluginDescriptionFile pdfFile = this.getDescription();
    System.out.println( pdfFile.getName() + " version " + pdfFile.getVersion() + " is enabled" );
    }


    Below I have the same error at the plgInf line.

    code (open)
    public class Tasks implements Runnable {
    plgInf plugin = new plgInf();
    @Override
    public void run(){
    //Doing something with plugin
    plugin.getServer().getScheduler().scheduleSyncDelayedTask(plugin, this);
    }
    }
     
  8. Offline

    Crash

    If you're going to pass this in for the first one then it needs to implement Runnable and it needs to specialize the run method and I also think you want it to repeat so use scheduleSyncRepeatingTask. For your second one, I don't know why you're calling the schedule method...

    Code:
    public class PluginWhatever extends JavaPlugin implements Runnable {
    
        @Override
        public void onEnable(){
    
            ...
    
            getServer().getScheduler().scheduleSyncRepeatingTask(this, this, 0, ticks);//ticks = how many ticks before it gets executed again
    
        }
    
        @Override
        public void onDisable(){ ... }
    
    
        @Override
        public void run(){
    
            //Your code to get repeatedly executed after the period
    
        }
    
    }
     
  9. Offline

    Cheesier

    One thing that I've never had the chance to wrap my finger around is the ticks, what unit is it given in ?
    seconds or microseconds i guess but I'm not sure.
     
  10. Offline

    Infernus

    Thank you very much, going to try now, i'll post back if it didn't work!

    And also, I assume it was milliseconds. (1s = 1000ms) <-- forget about that, it's not milliseconds, 20 ticks = 1 sec
     
Thread Status:
Not open for further replies.

Share This Page