2013-08-01 49 views
6

我正在爲Minecraft製作基於文本的雷達。如果一名玩家距離您不到20個街區,它會在聊天中說出來。到目前爲止,它會傳播聊天內容。我怎樣才能讓它只寫一次聊天關於該玩家的消息?即使你不玩遊戲,也應該很容易理解。只有物體進入範圍時纔會觸發接近探測器,而不是在範圍內移動時

if (Camb.radar) 
{ 
    for (Entity e: (List <Entity>) mc.theWorld.loadedEntityList) 
    { 
    if (e instanceof EntityPlayer) 
    { 
     EntityPlayer player = (EntityPlayer) e; 
     if (player == mc.thePlayer || mc.thePlayer.getDistanceToEntity(e) > 20.0) 
     continue; 
     mc.thePlayer.addChatMessage("\2479[CAMB] \247e" + player.getEntityName() + " has entered your 20 block radius!"); //Write to chat, only want this line done once for every player 
    } 
    } 
} 
+0

你會希望它只是你,一旦他們再次進入探測半徑,然後只能ping如果他們離開平重新進入? – StephenTG

+0

1.有沒有聽說過布爾變量? 2.如果玩家離開30分鐘然後回來,該怎麼辦?那麼玩家不應該被警告嗎? –

+1

如果您可以使用某種MovementListener,我建議將這一小段代碼移到那裏。 – Vulcan

回答

15

你需要保持當玩家離開該範圍的跟蹤和設置一個標誌,所以當他們從「超出範圍」,「在範圍內」來過渡,你就會知道。可能還需要添加計時器,以便每N秒只能提醒一次。

2

如果您創建一個類PlayerData,它可以包含映射到布爾值的玩家名稱的哈希表。你給每個玩家一個PlayerData對象,然後當有人進入該玩家的半徑時,你切換他/她的布爾值。

public class PlayerData { 
    public Player thePlayer; 
    public HashMap<String,boolean> inRadius = new HashMap<String,boolean>(); 

    public PlayerData(Player thePlayer) { 
     this.thePlayer = thePlayer; 
    } 

    public void checkRadius(P Entity player) { 
    /**function that checks radius and if a new player is there, notify thePlayer*/ 
     if(inRadius.get(player.getEntityName()) == true || thePlayer == player || thePlayer.getDistanceToEntity(player) > 20.0) return; 
     else { 
     thePlayer.addChatMessage("whatever you want to say"); 
     inRadius.put(player.getEntityName(), true); 
     } 
     for(Iterator<String> key=inRadius.keySet().Iterator();key.hasNext()) { 
     String name = key.next(); 
     /**Check to see if that player is still within 20 meters. If not, set value to false*/ 
     /** also be careful to check if player is online*/ 
     } 
    } 

} 
2

您可以嘗試創建列表或附近玩家陣列,並將它們添加到該列表中,當它們位於20個區塊內時。當你在範圍內找到一個實體時,檢查它是否在你的列表中。如果沒有,通知並將其添加到列表中,如果是這樣,遊戲就開始:)

要從列表中刪除項目,請檢查列表中的實體並將它們與玩家位置進行比較。如果他們超出範圍,請將其移除。這可能需要在單獨的循環中發生。

0

您需要在該方法之外存儲您已經提醒玩家的方法。 A Map是完美的。更妙的是一個WeakHashMap如果你不想泄露那些Entities

private final Set<EntityPlayer> playersInRange = Collections 
     .newSetFromMap(new WeakHashMap<EntityPlayer, Boolean>()); 

void onMove() { 
    if (Camb.radar) { 
     for (Entity e : (List<Entity>) mc.theWorld.loadedEntityList) { 
      if (e instanceof EntityPlayer) { 
       EntityPlayer player = (EntityPlayer) e; 

       if (player == mc.thePlayer || mc.thePlayer.getDistanceToEntity(e) > 20.0) { 
        // make sure player is (no longer) in set 
        playersInRange.remove(player); 
        continue; 
       } 

       if (!playersInRange.contains(player)) { 
        playersInRange.add(player); 
        mc.thePlayer.addChatMessage("\2479[CAMB] \247e" + player.getEntityName() 
          + " has entered your 20 block radius!"); 
       } 
      } 
     } 
    } 
} 

你也可以沿着存儲時間與他們重新警報每隔X時間。

private static final long WAIT_BETWEEN_ALERTS = 30000; 
private final WeakHashMap<EntityPlayer, Long> map = new WeakHashMap<EntityPlayer, Long>(); 
void onMove() { 
    if (Camb.radar) { 
     for (Entity e : (List<Entity>) mc.theWorld.loadedEntityList) { 
      if (e instanceof EntityPlayer) { 
       EntityPlayer player = (EntityPlayer) e; 

       if (player == mc.thePlayer || mc.thePlayer.getDistanceToEntity(e) > 20.0) { 
        // clear alerts 
        map.remove(player); 
        continue; 
       } 

       Long lastTimeAlerted = map.get(player); 
       long minimumLastAlert = System.currentTimeMillis() - WAIT_BETWEEN_ALERTS; 

       if (lastTimeAlerted == null || lastTimeAlerted < minimumLastAlert) { 
        map.put(player, System.currentTimeMillis()); 
        mc.thePlayer.addChatMessage("\2479[CAMB] \247e" + player.getEntityName() 
          + " has entered your 20 block radius!"); 
       } // else, already alerted recently. 
      } 
     } 
    } 
} 
0

使用getter/setter方法向EntityPlayer detected添加布爾標誌。

內,您的循環:

if (Camb.radar) { 
    for (....) { 
     if (e instanceof EntityPlayer) { 
      EntityPlayer player = (EntityPlayer) e; 
      if (player.isDetected() || player == mc.thePlayer || mc.thePlayer.getDistanceToEntity(e) > 20.0) { 
       continue; 
      } 

      if (!player.isDetected()) { 
       mc.thePlayer.addChatMessage(....); 
       player.setDetected(true); // reset this flag when player goes out of radar 
      } 
     } 
    } 
}  
0

我建議做如下:

int radius = 0; 
if (Camb.radar) for (Entity e : (List <Entity>) mc.theWorld.loadedEntityList) 
    if (e instanceof EntityPlayer) { 
     EntityPlayer player = (EntityPlayer) e; 
     if (player == mc.thePlayer || mc.thePlayer.getDistanceToEntity(e) > 20.0) 
      continue; 
     while (radius < 1) { 
      mc.thePlayer.addChatMessage("\2479[CAMB] \247e" + player.getEntityName() + " has 
      entered your 20 block radius!"); 

     }  
    }