How can I check when a player does enter an area?

Discussion in 'Plugin Development' started by Pitazzo, Nov 20, 2013.

Thread Status:
Not open for further replies.
  1. Offline

    Pitazzo

    Hello guys,
    I would like to know how can I listen to an event such as onPlayerJoinsArea (I know that this one doesn't exist, yes...); I'm NOT looking for something like this:
    Code:java
    1. public void onPlayerPlaceBlock (PlaceBlockEvent event){
    2. Player player = event.get player();
    3. if(playerIsInTheArea(player)){
    4. //Stuff
    5. }
    6. }


    What I ned is something like...
    Code:java
    1. public void onPlayerJoinsTheArea(PlayerJoinsTheAreaEvent event){
    2. //Stuff
    3. }
     
  2. Offline

    agokhale1

    Pitazzo
    Just add a player to the HashMap and teleport them into the arena.
     
  3. Offline

    Pitazzo


    I'm talking about areas, not about arenas! :D
    I just want to do stuff when a player is in a exact location :)

    Thanks in advance
     
  4. Offline

    CookieGamer100

    Pitazzo likes this.
  5. Offline

    agokhale1

    Pitazzo
    Oh xD I read that completely wrong. Anyways, what you want to do is get the player's location:
    Code:java
    1. player.getLocation().getX();
    2. player.getLocation().getY();
    3. player.getLocation().getZ();

    Then, you want to check if their coordinates match the area (setup the area's location in a method and reference it).
     
    Pitazzo likes this.
  6. Offline

    Pitazzo

    CookieGamer100 Thanks for answering!
    Another question:
    Checking if the player is in the location with the agokhale1 code with an onPlayerMoveEvent would cosume too much?

    Thanks
     
  7. Offline

    JRL1004

    Pitazzo Best way to use the move event would be to check if the players location is not the same as the on prior (I think it's getLocation() and getTo() or something like that) and if the distance is 0 then return out. This will prevent you from using the method when a player looks around
     
  8. Offline

    xTrollxDudex

    Pitazzo
    Check the player's position every 2 blocks x and z per PlayerMoveEvent, the difference will be so minute no one can notice. It may be a bit off, but hey, your server won't lag :)
     
  9. Offline

    Plo124

    Do you mean blocks as ticks? Because checking every 2 blocks would still need the PlayerMoveEvent, but if it was in ticks, a runnable would be better suited.
     
  10. Offline

    xTrollxDudex

    Plo124
    Ehh no. I kinda worded that wrong let me fix it...
     
  11. Offline

    saturnine_nl

    It depends a bit on the situation, but you could first check if the targetchunk (the chunk where the coordinates lies in) is loaded into memory. if it is a player must be in the area and then you can check with an onplayerMove event if the player is at specified coordinates. It saves a lot of unused checks every tick.
     
Thread Status:
Not open for further replies.

Share This Page