您需要在該方法之外存儲您已經提醒玩家的方法。 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.
}
}
}
}
你會希望它只是你,一旦他們再次進入探測半徑,然後只能ping如果他們離開平重新進入? – StephenTG
1.有沒有聽說過布爾變量? 2.如果玩家離開30分鐘然後回來,該怎麼辦?那麼玩家不應該被警告嗎? –
如果您可以使用某種MovementListener,我建議將這一小段代碼移到那裏。 – Vulcan