Bukkit.getOnlinePlayers Repeats

Discussion in 'Plugin Development' started by x128, Sep 7, 2013.

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

    x128

    Hi,
    I am trying to have a message displayed to everyone who has player visibility disabled every 30 seconds. in my onEnable, I have invisMsg() to start the loop. If there is only one player with player invisibility enabled, then the plugin displays the message once. If there are more than one, the message gets displayed several times. Here is my invisMsg code:
    Code:java
    1. public void invisMsg() {
    2. new BukkitRunnable(){
    3. @Override
    4. public void run(){
    5. for (Player p : Bukkit.getOnlinePlayers()) {
    6. if (vanished.contains(p.getName())) {
    7. p.sendMessage("§ePlayer visibility is disabled! Click your Nether Star to see players again!");
    8. }
    9. invisMsg();
    10. }
    11. }
    12. }.runTaskLater(this, 30 * 20L);
    13. }


    If anyone knows how to get around this, I would greatly appreciate it. Thanks!
     
  2. Offline

    CubieX

    Don't call invisMsg() recursively.
    Make a repeating task instead. (.runTaskTimer()) and delete the "invisMsg()" call from the loop.
    Because what you currently do is creating a new timer task for each player currently online,
    causing multiple messaging tasks to run simultaneously.
     
Thread Status:
Not open for further replies.

Share This Page