我有以下代码
@EventHandler
public void onPlayerQuit(PlayerQuitEvent event){
Player player = event.getPlayer();
final Player[] playerlist = getServer().getOnlinePlayers();
if (playerlist.length <=1) { // if no players are online
getServer().getScheduler().scheduleSyncDelayedTask(this, new Runnable(){
// this is a scheduler.
public void run(){
if(playerlist.length <=1){
getServer().shutdown();
}
}
}, 6000L); // runs every 6000 ticks, which is every 300 seconds, which is every 5 minutes.
}
}
当玩家离开时,它会检查他是否是最后一个,如果是,那么在 5 分钟后,它会再次检查,如果仍然没有人在它应该停止服务器。
在此行中:
if (playerlist.length <=1) { // if no players are online
我必须将它设置为 <=1 否则它根本不起作用,但如果我离开它也会停止服务器,然后重新加入,我是唯一的一个。当我在 =0 并且只有 <1 时它没有工作。
有任何想法吗?
这是我的更新代码(仍然不起作用):
@EventHandler
public void onPlayerQuit(PlayerQuitEvent event){
Player player = event.getPlayer();
final Player[] playerlist = getServer().getOnlinePlayers();
if (playerlist.length <=1) { // if no players are online
getServer().getScheduler().scheduleSyncDelayedTask(this, new Runnable(){
// this is a scheduler.
public void run(){
final Player[] playerlist = getServer().getOnlinePlayers();
if(playerlist.length <=1){
getServer().shutdown();
}
}
}, 500L); // runs every 6000 ticks, which is every 300 seconds, which is every 5 minutes.
}
}