Bukkit倒计时消息发送消息4次

时间:2016-06-09 13:43:00

标签: bukkit

我刚刚在Java中创建了一个倒计时方法,但是当countdowner广播消息时我遇到了一个问题:60(及以下)秒直到游戏开始! 广播被发送* 4。有谁知道解决这个问题?

这是我的代码:

Main plugin;

public StartCountdown(Main pl) {
    plugin = pl;
}

public static int timeUntilStart;

@Override
public void run() {
    for(Player p1 : Bukkit.getOnlinePlayers()){
            if(timeUntilStart == 0) {

                if(!Game.canStart()) {

                    plugin.restartCountdown();
                    ChatUtilities.broadcast(ChatColor.RED + "Not enough players to start. Countdown will");
                    ChatUtilities.broadcast(ChatColor.RED + "restart.");
                    p1.playSound(p1.getLocation(), Sound.ENDERDRAGON_WINGS, 5, 1);
                    return;
                }
                Game.start();
            }

             for(Player p : Bukkit.getOnlinePlayers()){
                    p.setLevel(timeUntilStart);

            if(timeUntilStart < 11  || timeUntilStart == 60 || timeUntilStart == 30) {
                p.playSound(p.getLocation(), Sound.ORB_PICKUP, 5, 0);
                if(timeUntilStart == 1) {
                    p.playSound(p.getLocation(), Sound.ORB_PICKUP, 5, 1);
                }
                ChatUtilities.broadcast(String.valueOf(timeUntilStart)
                        + " §6Seconds until the game starts!");



             }







             }
}
    timeUntilStart -= 1;   

}

}

2 个答案:

答案 0 :(得分:0)

您正在为每个在线玩家广播。您需要移动任何您不想为for循环之外的每个玩家运行的代码。

@Override
public void run() {
    if (timeUntilStart == 0) {
        if (!Game.canStart()) {
            plugin.restartCountdown();
            ChatUtilities.broadcast(ChatColor.RED + "Not enough players to start. Countdown will");
            ChatUtilities.broadcast(ChatColor.RED + "restart.");
            for (Player p : Bukkit.getOnlinePlayers()) p.playSound(p.getLocation(), Sound.ENDERDRAGON_WINGS, 5, 1);
            return;
        }

        Game.start();
    }

    boolean broadcast;

    for (Player p : Bukkit.getOnlinePlayers()) {
        p.setLevel(timeUntilStart);

        if (timeUntilStart < 11  || timeUntilStart == 60 || timeUntilStart == 30) {
            p.playSound(p.getLocation(), Sound.ORB_PICKUP, 5, 0);
            if (timeUntilStart == 1) p.playSound(p.getLocation(), Sound.ORB_PICKUP, 5, 1);
            broadcast = true;
        }
    }

    if (broadcast) ChatUtilities.broadcast(String.valueOf(timeUntilStart) + " §6Seconds until the game starts!");

    timeUntilStart -= 1;   
}

答案 1 :(得分:0)

正如Tanner Little上面所说的

  

您正在为每个在线玩家广播。您需要移动任何您不想为for循环之外的每个玩家运行的代码。

您还需要确保取消任务。我建议使用内置的调度程序。您可以通过这种方式访问​​调度程序

private int countDownTimer
private int countDownTime
public void runCountDown() {
 countDownTimer = Bukkit.getScheduler.scheduleSyncDelayedTask(plugin, new runnable() {
  public void run {
   if (countDownTime <= 0) {
    //do your bradcasting here
    for (Player ingame : Bukkit.getOnlinePlayers()) {
     //Do your player specific stuff here
    }
    Bukkit.getScheduler.cancelTask(countDownTimer);
   }
   if (countDownTime % 10 == 0) { //You can pick whaterver times u want this is just for an example
    //Do periodic broadcasting
   }
   countDownTime -= 1;
  }
 }, 0L, 20L); //This means that it would wait 0 ticks to start the countdown and do the task every 20 ticks ie) 1 second.
}

希望这可以帮到你。