我正在尝试制作一个迷你游戏,所以当我想让玩家在倒计时后自动传送,但没有人传送时,我发出了一个命令来开始游戏并传送(可以工作),所以这是我的代码(这是冰岛语服务器,所以变量名在冰岛语和其他一些东西上):
主类中的onEnable:
public void onEnable() {
stada.setjaStodu(stada.Lobby);
nidurtalning.keyra = true;
new Thread(new nidurtalning()).start();
registerEvt();
getCmd();
for (Player p : Bukkit.getOnlinePlayers()) {
p.teleport(playerJoin.wait);
p.setGameMode(GameMode.ADVENTURE);
}
}当倒计时变量为0(Klukka)时:
if(klukka == 0){
try{
Thread.sleep(500);
}catch(InterruptedException e){
e.printStackTrace();
Bukkit.shutdown();
}
for(Player p : Bukkit.getOnlinePlayers()){
p.playSound(p.getLocation(), "random.levelup", 100.0F, 1F);
byrja.teleportToGame(p);
}
stada.setjaStodu(stada.Leikur);
chat.tilkynna("Og þið megið byrja!", false);
keyra = false;
}和远程传输类:
public class byrja {
public static void teleportToGame(Player p){
World war = Bukkit.getWorld("Empty");
Location leikur = new Location(war, -416, 253, 175);
p.teleport(leikur);
}
}发布于 2018-12-31 10:54:24
Use NEVER
Thread.sleep();因为它会暂停整个线程!即使你使用的是asnc调度器,它仍然不是很好!(这将耗费不必要的资源)
请改用RunTaskLater排定程序
然而..。你确定世界上存在空吗?‘
不过,总而言之,这个函数应该可以工作!
如下所示:
protected void endCountdown(int klukka) {
if (klukka == 0) {
Bukkit.getScheduler().runTaskLater(pl, new Runnable() {
@Override
public void run() {
Bukkit.getOnlinePlayers().stream().forEach(all -> {
all.playSound(all.getLocation(), Sound.LEVEL_UP, 100, 1);
teleportToGame(all);
});
// DO MORE STUFF IF NEEDED
}
}, 10);
}
}
private void teleportToGame(Player p) {
World war = Bukkit.getWorld("Empty");
Location loc = new Location(war, -416, 253, 175);
p.teleport(loc);
}https://stackoverflow.com/questions/37125269
复制相似问题