2016-07-07 29 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