2013-08-01 8 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を入力したら、あなたは彼らが去る場合にのみ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

あなたは既にプレーヤーに警告した方法の外に保存する必要があります。 Mapはこれに最適です。さらに良いことに、あなたがそれらの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!"); 
       } 
      } 
     } 
    } 
} 

をリークしたくないWeakHashMap場合にはまた、再アラートのすべての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!"); 

     }  
    } 
関連する問題