Minecraft Timer with for?

Discussion in 'Plugin Development' started by TryKimko, Dec 4, 2016.

Thread Status:
Not open for further replies.
  1. Hey... me again...
    I just wanna know how i create a timer so every 5 Seconds a Player gets a Message :)

    I know it works somehow with 20 ticks a second so 20 * 60 = 1200 (1 Minute) but how i use it elsewhere as in my Mainclass for example in a command?

    Kind regards,
    - The Cookieunity
     
  2. Online

    timtower Administrator Administrator Moderator

  3. Offline

    Minibros

    You could just use syncRepeatingTask, set 20L and 20L as its delay thing. There you go. It will repeat every 1 second.
     
  4. Offline

    DoggyCode™

    Approach I would take:

    * Create a new class, called whatever you like, for this example I will call it "MessageSenderTask".
    PHP:
    public class MessageSenderTask {
    * Secondly, make this class extend "BukkitRunnable"
    PHP:
    public class MessageSenderTask extends BukkitRunnable {
    * You will get an error, but that's fine.
    * You need some variables inside this class, Player player; etc...
    PHP:
    public class MessageSenderTask extends BukkitRunnable {

      private 
    Player player;

      public 
    MessageSenderTask(Player player) {
        
    this.player player;
      }

    }
    * Inside the class, make a new void method called exactly "run", this will remove the error. In this method you will do most of your code.
    PHP:
    public class MessageSenderTask extends BukkitRunnable {

      private 
    Player player;

      public 
    MessageSenderTask(Player player) {
        
    this.player player;
      }

      public 
    void run() {
        if(
    player!=null) {
          
    player.sendMessage("5 seconds passed, send message.");
        }
      }

    }
    Now. Create a new MessageSenderTask object with the player passed in the parameter.
    PHP:
    public class AnyClass {

      public 
    void anyMethod() {
        
    Player p a player;
        
    JavaPlugin main mainClass;
        
    MessageSenderTask task = new MessageSenderTask(p);
        
    //make it repeat every 5 second
        
    task.runTaskTimerAsynchronously(main0L20*5L)
      }

    }
     
    Last edited: Dec 4, 2016
Thread Status:
Not open for further replies.

Share This Page