Turn on the proximity sensor only when the subject is in range, not when it moves within range

I am making a text radar for Minecraft. If a player enters 20 blocks of you, he will say in the chat. At the moment, he is sending out a chat. How can I get him to write only in chat about this ONCE player? Even if you are not playing a game, it should be easy to understand.

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 } } } 
+6
source share
6 answers

You will need to keep track of when the player leaves the range and sets the flag, so you will know when they move from “out of range” to “in range”. You may also need to add a timer so that you can only alert every N seconds.

+15
source

If you create the PlayerData class, it may contain a hash map of game names mapped to booleans. You give each player a PlayerData object, and then when someone enters the radius of that player, you switch his / her logical value.

 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
source

You can try to create a list or an array of neighboring players and add them to this list if they are within 20 blocks. When you find an object within a range, check to see if it is listed. If not, let me know and add it to the list, if so, the game is on :)

To remove items from the list, check the entities in the list and compare them with the position of the players. If they are out of reach, remove them. This may be required in a separate cycle.

+2
source

You need to save outside of this method that has already been warned about the player. A Map perfect for this. Even better is WeakHashMap in case you don't want to skip those 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!"); } } } } } 

You can also save time with them to re-notify of every X time.

 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
source

Add a boolean flag to EntityPlayer detected using getter / setter methods.

Inside your loop:

 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
source

I suggest doing the following:

 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!"); } } 
0
source

Source: https://habr.com/ru/post/950844/


All Articles