java - 事件后发生的 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;
}}
解决方案
您的计时器在玩家被传送之前启动,但代码不会等到计时器完成,然后再执行其余代码。尝试这个:
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);
}
这段代码是手写的,我没有想到,所以可能有一些错误。
推荐阅读
- sql - SQL - 查找两周或更长时间未登录的用户
- c++ - libcurl:如何使用 curl_easy_pause() 暂停和恢复下载文件
- ios - 从 tableViewCell 访问存储的值并将它们保存到 firebaseDatabase
- python - Python:如何从大字典中存在的 2 个键生成 DataFrame
- ruby-on-rails - RoR:模型之间的关联
- android-studio - 无法在 Android Studio 上登录 GitHub
- sql - 如何在sql每个select语句中使用union排序
- c++ - C++/Raspberry Pi/Mosquitto API:尝试使用 Mosquitto 向 MQTT 代理发布字符串消息,但不断出现无法访问的内存错误
- django - DRF 序列化器,不同的字段类型
- ionic-framework - 为什么 ion-icon 没有在 ionic vue 项目中显示?