gpt4 book ai didi

java - 从迭代器 Bukkit 1.16.5 中获取播放器

转载 作者:行者123 更新时间:2023-12-04 08:13:24 26 4
gpt4 key购买 nike

我有一个问题,今天让我很沮丧,我创建了一个插件,在点击库存时将玩家添加到数组列表中(不幸的是我这样做了,因为我不是很好),游戏只有一个,包含 2 个玩家,我不能确定如果一个玩家退出,通过一个方法,他可以让另一个玩家获胜,我尝试了很多方法,这是最后一个(在下面的这段代码中,我尝试将一个方法放在如果我远程理解它应该向玩家发送一条胜利消息,显然它不会,然后执行 LobbyJoin,这是一种将您带到大厅的方法):

private void makeWinSupport(Player winner, Player loser) {
if (playerInGame.contains(winner) && playerInGame.contains(loser)) {
winner.sendMessage(plugin.cc("&6(!) Hai vinto!"));
LobbyJoin(loser);
LobbyJoin(winner);
playerInGame.remove(winner);
playerInGame.remove(loser);
}
}

public void makePlayerWin(Player loser) {
Iterator<Player> i = playerInGame.iterator();
System.out.println(1);
if(i.hasNext() && i == loser) {
makeWinSupport(i.next(), loser);

} else {
makeWinSupport(playerInGame.get(0), loser);
}

@EventHandler
public void onQuit(PlayerQuitEvent e) {
e.setQuitMessage("");
Player p = e.getPlayer();
if(playerInGame.contains(p)) {
makePlayerWin(p);
}
}
}

最佳答案

您可以使用 PlayerQuitEvent在玩家离开时从您的插件中删除他们。我做了这个例子,假设你有一个名为 yourPlayerList 的列表它存储了 2 个竞争玩家,但您可以对其进行修改以与更多玩家一起工作。

@EventHandler
public void onPlayerLeave(PlayerQuitEvent event) {
if(yourPlayerList.remove(event.getPlayer())){//only true if the player was removed from the queue, which only happens if it was in the queue originally
Player loser=event.getPlayer();//the loser
Player winner=yourPlayerList.get(0);//the other player is still in the queue
makeWinSupport(winner,loser);
}
}
不要忘记在事件管理器中注册事件。
编辑:如果您想从列表中删除玩家并仅在只剩下一名玩家时结束游戏,您可以执行以下操作:
@EventHandler
public void onPlayerLeave(PlayerQuitEvent event) {
if(yourPlayerList.remove(event.getPlayer())&&yourPlayerList.size()==1){//removes the player and checks the amount of active players
Player winner=yourPlayerList.get(0);
//here you can call a method which accepts only the winning player. If you have 4 players, there is no reason to include a 'loser' player.
}
}

关于java - 从迭代器 Bukkit 1.16.5 中获取播放器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65835529/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com