您需要在已经提醒玩家的方法之外存储。AMap
非常适合这个。更好的是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.
}
}
}
}