Check if area contains a player's location

Discussion in 'Spigot Plugin Development' started by sinnlosername, May 30, 2016.

  1. Hey,

    how can I check if an arena (only x & z) contains a player?

    My current class:
    Code (Text):
    public class Area {

         private int x1;
         private int x2;
         private int z1;w
         private int z2;

         public Area(int x1, int x2, int z1, int z2) {
              this.x1=Math.min(x1,x2);
              this.x2=Math.max(x1,x2);
              this.z1=Math.min(z1,z2);
              this.z2=Math.max(z1,z2);
         }

         public boolean isInArea(int x, int z) {

         }

    }
     
  2. In a PlayerMoveEvent can put this if using a cuboid or something similar

    if (!arena.arenacuboid.contains(player.getLocation())) {
    player.teleport(arena.getSpawn());
    }
     
  3. You can use the 'Rectangle' class.
    Code (Java):

    Rectangle area = new Rectangle(x1, y1, x2 - x1, y2 - y1); // Create a rectangle at the area's base location, and with the right widt hand height.
                                                                                                // I recommend making this a field so you can re-use this.
    if (area.contains(player.getLocation().getX(), player.getLocation().getZ())) { // check if the player is in the rectangle.
      // player is in the rectangle.
    } else {
      // player isn't in the rectangle.
    }