Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Only trigger proximity detector when object enters the range, not when he moves within range

Tags:

java

minecraft

I'm making a text-based radar for Minecraft. If a player comes within 20 blocks of you, it will say in chat. As of now, it spams the chat. How can I get it to only write to chat about that player ONCE? Even if you don't play the 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
    }
  }
}
like image 543
Brad Avatar asked Aug 01 '13 18:08

Brad


People also ask

How do proximity sensors work?

There are various types of proximity sensors out there in the market, but one can say this device often performs its object presence detection task by emitting an electromagnetic field or a beam of electromagnetic radiation ( infrared , for instance), and looking for changes in the field or return signal.

What is an inductive proximity sensor?

What Is a Sensor? Learn all about the principles, structures, and features of eight sensor types according to their detection principles. A inductive proximity sensor can detect metal targets approaching the sensor, without physical contact with the target.

What is an active low output proximity sensor?

NPN proximity sensors provide an active LOW output. This means that when an object enters the detecting range of the sensor, the output of the sensor is connected with the ground. This type of sensor is also known as ‘ sinking ’ sensor.

What are the different types of proximity sensors?

It has the following three different models; Reflective, Through-beam, and Retro-reflective. Each model offers varying light emission methods, though they are all highly efficient when it comes to distance detection. Magnetic proximity sensors are proximity devices used to detect magnetic objects through their large sensing ranges.


3 Answers

You'll need to keep track of when the player leaves the range and set a flag, so you'll know when they're transitioning from "out of range" to "in range". Might also want to add a timer so that you can only alert once every N seconds.

like image 81
Troy Avatar answered Oct 26 '22 23:10

Troy


You could try creating a List or Array of nearby players, and add them to that list when they are within 20 blocks. When you find an entity within range, check to see if its in your list. If not, notify and add it to the list, if so, game on :)

For removing items from your list, check the entities in your list and compare them to the players position. If they are out of range, remove them. This may need to happen in a separate loop.

like image 24
framauro13 Avatar answered Oct 26 '22 23:10

framauro13


If you make a class PlayerData, it can contain a hashmap of playernames mapped to booleans. You give each player a PlayerData object and then when somebody enters the radius of that player, you toggle his/her boolean.

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*/
      }
    }

}
like image 43
Kammeot Avatar answered Oct 26 '22 23:10

Kammeot