OnlinePlayer 位置循环
OnlinePlayer Location Loop
我有一个创建墙并使用参数 Location l
的方法。当雪球击中方块或人并沿 X
或 Z
轴创建墙时会触发此事件。我的问题是,如果我使用 Location l
,我无法使用 Player p
来获取玩家面向的方向并相应地沿 X
和 Z
轴旋转墙.如果我使用Player p
,我找不到被雪球击中的方块的Location
。因此,为了解决我的困境,我使用了 Location l
并且当需要 Player p
时,我遍历了所有在线玩家位置并找到了一个位于 Location l
的玩家,然后将其转换为 Player p
.
我的代码:
public static void wall(Location l){
Player p = null;
for(Player players: Bukkit.getOnlinePlayers()){
if (players.getLocation().equals(l)){
p = players;
}
else{
return;
}
}
我的问题:
这是解决问题的有效方法吗?它有效但我做错了吗?有没有更好的方法解决问题?
提前致谢!
一种解决方案是扩展 Location 以跟踪在该位置的玩家。
public static void wall(Location l) {
List<Player> pList = l.getPlayers();
for(Player p : pList) {
//do Something
}
}
由于您已经将位置存储在播放器中,因此您可以在播放器移动到新位置时更新值。
另一种解决方案是维护位置和玩家地图
Map <Location, Set<Player>> locationPlayerMap //or
Map <Player, Location> playerLocationMap
确定哪种解决方案最好实际上取决于以下因素:
"How Many Players are going to be on a Server"、"How many Locations are there"、"Are Multiple Players Allowed at a single Location"、等等...
编辑:
您还可以像下面这样构造您的方法:
public static void wall(Object o) {
if(o instanceof Player) {
//Snowball hit a player, use the direction they are facing and location
} else if(o instanceof Location) {
//Showball hit a Location, use some other factor.
}
}
我有一个创建墙并使用参数 Location l
的方法。当雪球击中方块或人并沿 X
或 Z
轴创建墙时会触发此事件。我的问题是,如果我使用 Location l
,我无法使用 Player p
来获取玩家面向的方向并相应地沿 X
和 Z
轴旋转墙.如果我使用Player p
,我找不到被雪球击中的方块的Location
。因此,为了解决我的困境,我使用了 Location l
并且当需要 Player p
时,我遍历了所有在线玩家位置并找到了一个位于 Location l
的玩家,然后将其转换为 Player p
.
我的代码:
public static void wall(Location l){
Player p = null;
for(Player players: Bukkit.getOnlinePlayers()){
if (players.getLocation().equals(l)){
p = players;
}
else{
return;
}
}
我的问题:
这是解决问题的有效方法吗?它有效但我做错了吗?有没有更好的方法解决问题?
提前致谢!
一种解决方案是扩展 Location 以跟踪在该位置的玩家。
public static void wall(Location l) {
List<Player> pList = l.getPlayers();
for(Player p : pList) {
//do Something
}
}
由于您已经将位置存储在播放器中,因此您可以在播放器移动到新位置时更新值。
另一种解决方案是维护位置和玩家地图
Map <Location, Set<Player>> locationPlayerMap //or
Map <Player, Location> playerLocationMap
确定哪种解决方案最好实际上取决于以下因素: "How Many Players are going to be on a Server"、"How many Locations are there"、"Are Multiple Players Allowed at a single Location"、等等...
编辑: 您还可以像下面这样构造您的方法:
public static void wall(Object o) {
if(o instanceof Player) {
//Snowball hit a player, use the direction they are facing and location
} else if(o instanceof Location) {
//Showball hit a Location, use some other factor.
}
}