Delay between messages?

Discussion in 'Plugin Development' started by Awesomebanana2002, Jul 8, 2015.

Thread Status:
Not open for further replies.
  1. Sorry for my bad english.

    Dear,
    Is there an easy way of adding delay between messages?
    My plugin causes to give 2 people a rank on my server, when there are 20 people online, but all messages are sending instantly.. Is there an easy way of putting delay between them? Or do I have to use the Bukkit.getScheduler().runTaskLater() method.
    Here's my code:
    Code:
    for(Player p : playersOnline) {
      for(int i = 0; i < 20; i++) {
        p.sendMessage("");
      }
      p.sendMessage(""+ChatColor.GOLD+ChatColor.BOLD+"We've reached " +  Main.plugin.getConfig().getInt("min-online") + " players!!!");
      p.sendMessage("");
    }
    
    for(int i = 0; i < 2; i++) {
      Player p = pickRandom(shouldPick);
      if(p==null) continue;
      shouldPick = remove(shouldPick, p);
    
      p.sendMessage(""+ChatColor.AQUA+ChatColor.BOLD+"Congrats! You won rank Super!");
    
      Bukkit.broadcastMessage(""+ChatColor.GOLD + ChatColor.BOLD+p.getName()+" won rank Super!");
                 
      Bukkit.dispatchCommand(Bukkit.getConsoleSender(), "pex user " + p.getName() + " group add Super");
                 
    }
    I would like to put some delay at the end of the last for loop.

    bump

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 12, 2016
  2. Please only bump a thread every 24 (or 12 I don't know it anymore ._.) hours
    Back to your problem: Yes you have to use a scheduler/runnable:
    Code:
    new BukkitRunnable() {
    public void run() {
    //Do stuff
    }
    }.runTaskLater(this, delay in ticks);
    Or (what I think is a better solution) instead of broadcasting the message that the user won a rank, send it to each player except the player who won it and to the player who won it the normal message:
    Code:
    for (Player online : Bukkit.getOnlinePlayers()) {
    if (online.equals(p)) online.sendMessage("§lCongrats! You won Super!");
    else online.sendMessage("§l" + p.getName() + " won rank Super");
    }
    //add user to rank
     
    Last edited: Jul 8, 2015
  3. I tried it using a for loop, but no one gets the message that way.. weird.. but anyways, thanks

    EDIT:
    I just came up with this idea.. Should it work if I use a while loop checking if the System.getCurrTimeMils() has moved 1 second? Or will the server freeze.

    Again sorry for my bad english
     
Thread Status:
Not open for further replies.

Share This Page