java - Bukkit 倒数计时器在 Activity 结束后发生

标签 java plugins runnable bukkit countdowntimer

我正在尝试制作一个 Bukkit 插件,每五分钟随机扭曲一个玩家。我弄清楚了扭曲,但我的倒计时发生在玩家扭曲之后,我想让它发生在之前。 有谁知道为什么这样做?

这是我的代码:

public class Commands implements Listener, CommandExecutor {


public String cmd0 = "startwarp";

private main plugin  = main.getPlugin(main.class);

@Override
@SuppressWarnings("deprecation")
public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
    Player player = (Player) sender;
    if(sender instanceof Player) {

        if (cmd.getName().equalsIgnoreCase(cmd0)) {

            if(args.length == 0) { 


                player.sendMessage(ChatColor.GOLD + "Warp timer begun!");
                Bukkit.getScheduler().scheduleSyncRepeatingTask(plugin, new Runnable(){
                    @Override
                    public void run() {

                        Bukkit.getScheduler().scheduleSyncRepeatingTask(plugin, new Runnable(){
                            int countdown;
                            @Override
                            public void run() {
                                if (countdown >= 1) { 
                                    Bukkit.getServer().broadcastMessage(ChatColor.RED + " " + countdown + "...");
                                    countdown--;
                                }

                            }
                        }, 0, 1 * 20);

                        Location loc = player.getLocation();
                        int randX = (int) (loc.getBlockX() + ((Math.random() * 1000) - 500));
                        int randZ = (int) (loc.getBlockZ() + ((Math.random() * 1000) - 500));
                        int randY = (int) ((Math.random() * 240) + 15);
                        Location randomtp = new Location(player.getWorld(), randX, randY, randZ);
                        player.teleport(randomtp);
                        player.sendMessage(ChatColor.DARK_AQUA + "Teleported!");
                        //warpTimer timer = new warpTimer();
                        //timer.runTaskTimer(plugin, 0, 1 * 20);

                    }
                }, 5 * 60 * 20, 5 * 60 * 20);
            }
            return true;
        }
    } 
    return false;
}}

最佳答案

您的计时器在玩家被传送之前启动,但代码不会等到计时器完成后才执行其余代码。试试这个:

int countdown = 10
new BukkitRunnable() {
  @Override
  public void run() {
    if (countdown >= 1) {
      Bukkit.getServer().broadcastMesssage(countdown);
      countdown--;
    } else {
      Location loc = player.getLocation();
      int randX = (int) (loc.getBlockX() + ((Math.random() * 1000) - 500));
      int randZ = (int) (loc.getBlockZ() + ((Math.random() * 1000) - 500));
      int randY = (int) ((Math.random() * 240) + 15);
      Location randomtp = new Location(player.getWorld(), randX, randY, randZ);
      player.teleport(randomtp);
      player.sendMessage(ChatColor.DARK_AQUA + "Teleported!");
      cancel();
    }

  }.runRepeatingTask(*instanceOfMainClass*, 0, 20);

}

这段代码是我手写的,所以可能有一些错误。

关于java - Bukkit 倒数计时器在 Activity 结束后发生,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61431194/

相关文章:

java - Spring 3.2 使用 SimpleJdbcInsert 检索自动生成的 key

java - 在java中突出显示文本

java - 使用 C++ 中的库为 PhoneGap 创建插件

java - 如何在android中使用ViewFlipper从ImageURLs的ArrayList制作幻灯片?

java - 替换 Wicket 口中的字符串

Java - 打印随机字

javascript - 使用jquery自动扩展文本区域

jquery - 如何在 Angular 4 中使用这个 jQuery 插件?

java - 线程等待多个线程

java - 可运行 jar 文件中的 RXTX - 为什么它不起作用?