Getting if player has moved outside of a certain area.

Discussion in 'Plugin Development' started by TopTobster5, Mar 13, 2014.

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

    TopTobster5

    I am making an arena plugin, but I need to detect if a player is in the arena or not. I'm guessing I would have to use PlayerMoveEvent but how do I work out if they are inside an area? I have the 2 corners of the 'box' which is the arena.
     
  2. Im using HashMaps instead of checking area.
     
  3. Offline

    TopTobster5

  4. Offline

    Europia79

    Alternatively, you could just use Multiverse-Portals to teleport the player back inside the area.

    If the only thing you want to do is keep them inside the defined area of the arena... then this will work.

    If you're looking to do other stuff... like slowly deplete their health, then yeah, you'll have to create your own detection system.

    If all of your areas are box shaped, then N3rdFall's algorithm is good. If you think any of your areas will be circular or spherical, then I have an idea:

    define a center point for an Arena.
    check the distance to the center point.

    Code:java
    1. if (player.getLocation().distanceSquared(CENTER_POINT) > ARENA_RADIUS_SQUARED)
     
  5. Offline

    LexLaiden

  6. Offline

    deivisxm

    I have some code that checks if player is in cuboid and if hes not, it pushes him back into cuboid. If you want i can post it here. I just need to get on my pc because im on my mobile now. Also i might help you with checking if he has to be in arena if you want.
     
  7. TopTobster5
    Simple, a HashMap isPlayerInGame <String playerName, Boolean boolean>. When someone joins, put it to true. When he leaves, put it to false. When you need to check, use Bukkit.getPlayer(isPlayerInGame.get(Player.getName()));
     
  8. Offline

    RawCode

    every X ticks (NOT DAMN MOVE EVENT)(but ok, use damn move event, anyway i wont install your plugin) check if player X and Z fewer or larger then Y

    if check passed store player name in lower case in LIST, not hashmap, there is no reason to use hashmap here.
     
  9. Offline

    deivisxm

    Code:java
    1. public boolean isInside(Player p)
    2. {
    3. Location loc = p.getLocation();
    4. if(loc.getX() < firstLocX || loc.getX() > secondLocX)
    5. return false;
    6. if(loc.getZ() < firstLocZ || loc.getZ() > secondLocZ)
    7. return false;
    8. if(loc.getY() < firstLocY || loc.getY() >secondLocY)
    9. return false;
    10. return true;
    11. }

    This should work. just ofcourse replace first and second locations with normal locations.
    This boolean returns true if player is inside location and false if he is somewhere else
    Also if you want player to be in that cuboid i suggest you use this:
    Code:java
    1. if(!isInside(p)){
    2. Vector direction = cr.getPlayerRealmSpawn(p).toVector().subtract(p.getLocation().toVector()).normalize();
    3. direction.setX( direction.getX()*1.0);
    4. direction.setY( direction.getY()*1.0);
    5. direction.setZ( direction.getZ()*1.0);
    6. p.setVelocity(direction);
    7.  
    8. }
     
  10. Offline

    TopTobster5

    Kk thanks everyone, I think I have enough info now to do what I need to. Out of curiosity, what's so bad about using PlayerMoveEvent? I have viewed a couple of threads similar to this ones, and they too have been discouraged from using it, so what's so bad about it?
     
Thread Status:
Not open for further replies.

Share This Page