2016-07-07 30 views
0

我有一种方法可以创建墙并使用参数Location l。当一个雪球击中一个街区或一个人并沿着轴线创建墙时,触发这个动作。我的问题是,如果我使用Location l,我不能使用Player p来获取玩家面对的方向,并相应地沿着XZ轴旋转墙。如果我使用Player p,我无法找到被雪球击中的块的Location。因此,为了解决我的困境,我使用了Location l,并且当需要Player p时,我通过所有在线玩家位置循环,找到一个玩家在Location l,然后将其投掷到Player pOnlinePlayer位置循环

我的代码:

public static void wall(Location l){ 
     Player p = null; 
     for(Player players: Bukkit.getOnlinePlayers()){ 
      if (players.getLocation().equals(l)){ 
       p = players; 
      } 
      else{ 
       return; 
      } 
     } 

我的问题:

这是对这个问题的有效解决方案?它是否有效,但是我做错了吗?有没有更好的方法来解决问题?

在此先感谢!

回答

0

一个解决方案可以扩展位置以跟踪位于该位置的玩家。

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 

确定哪个方案是最好的真的可以归结为类似因素: “有多少玩家将要在服务器上”, “有多少位置在那里”,“在一个地点允许多个玩家”等......

编辑: 您也可以在构造方法如下所示:

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. 
    } 
} 
+0

我只是想用位置来获得玩家,我并不需要每个人的位置 – J22929

+0

我会在位置存储一个名单,然后代表位于单个位置的玩家或玩家。无论何时玩家移动到新位置,您都必须执行诸如oldLocation.removePlayer(player)newLocation.addPlayer(player)之类的操作。然后,您可以使用Location.getPlayers()返回雪球击中位置的玩家列表。 – Steve

+0

我无所谓,他们移动时,我只需要确认他们在位置l时,他们被雪球击中 – J22929