首页 > 解决方案 > 事件后发生的 Bukkit 倒数计时器

问题描述

我正在尝试制作一个 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;
}}

标签: javapluginsrunnablebukkitcountdowntimer

解决方案


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

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);

}

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


推荐阅读