help please! player enter in area

Discussion in 'Plugin Development' started by y4sk, May 1, 2021.

  1. Offline

    y4sk

    how do i identify when a player enters an area set by 2 high and low positions?

    I used it but it didn't work:

    double minX = loc1.getX();
    double minY = loc1.getY();
    double minZ = loc1.getZ();

    double maxX = loc2.getX();
    double maxY = loc2.getY();
    double maxZ = loc2.getZ();

    double x = p.getLocation().getX();
    double y = p.getLocation().getY();
    double z = p.getLocation().getZ();


    if (x >= minX && x <= maxX && y >= minY && y <= maxY && z >= maxZ && z <= maxZ) {
    p.sendMessage("area");
    }
     
  2. Offline

    pixelrider2000

    I always use this method that returns either false or true depending on if the player is inside the region or not.

    public boolean isInRegion(Location playerLocation, Location lowestPos, Location highestPos) {

    double X = playerLocation.getX();
    double Y = playerLocation.getY();
    double Z = playerLocation.getZ();

    double highX;
    double highY;
    double highZ;

    double lowX;
    double lowY;
    double lowZ;

    if(lowestPos.getX() < highestPos.getX()) {
    lowX = lowestPos.getX();
    highX = highestPos.getX();
    } else {
    lowX = highestPos.getX();
    highX = lowestPos.getX();
    }

    if(lowestPos.getY() < highestPos.getY()) {
    lowY = lowestPos.getY();
    highY = highestPos.getY();
    } else {
    lowY = highestPos.getY();
    highY = lowestPos.getY();
    }

    if(lowestPos.getZ() < highestPos.getZ()) {
    lowZ = lowestPos.getZ();
    highZ = highestPos.getZ();
    } else {
    lowZ = highestPos.getZ();
    highZ = lowestPos.getZ();
    }

    if((X <= highX && X >= lowX) && (Y <= highY && Y >= lowY) && (Z <= highZ && Z >= lowZ)) {

    return true;
    } else {

    return false;
    }

    }
     
  3. Offline

    davidclue

    Your code should work but your problem is that you need a repeating task to always check if a player is in that set of coordinates. You could use PlayerMoveEvent but that would cause a lot of lag if you have a lot of people, I recommend using an async repeating task for this so it will not affect the server's performance. Be sure not to do anything else in the async repeating task like DO NOT alter the world or move things, you should be okay if you are just cycling the players and checking their coordinates to send them a message if they are in the area.
     
    Xp10d3 likes this.
  4. Offline

    Xp10d3

    Side note: You could use WorldGuard API instead depending on what exactly you want to do.
     

Share This Page