Detect when players are inside inbetween two coordinates?

Discussion in 'Plugin Development' started by TheSmallBones, Aug 27, 2013.

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

    TheSmallBones

    Okay I'm coding a personal region plugin for my server and I already have the code made to store two location, sort of like in worldedit. Except it only stores the x and z because all regions are gonna be to sky limit.
    [​IMG]
    How can I tell when players enter inside that area? I know there must be some kind of equation but I can't figure it out. Any help is great.
     
  2. Offline

    xTrollxDudex

    TheSmallBones
    Check if the player's x/z is within the region x/z
     
  3. Offline

    TheSmallBones

    There is no region though. It's just 2 sets of coordinates. How do I tell if they're within it?
     
  4. Offline

    xTrollxDudex

    TheSmallBones
    Take the coordinate's x/z and see if the player's x/z are within it.
     
  5. Offline

    TheSmallBones

    Uh. That's what I'm asking how to do. I know you're trying to help but seriously. I have two coordinates. How do I tell if a players coordinates are within the invisible rectangle the coordinates make?
     
  6. Offline

    werter318

    if x > xPos && x < xPos + width ...... You should be able to figure it out
     
  7. Offline

    Lactem

    https://forums.bukkit.org/threads/random-teleportation-not-in-block-api.169118/ I modified some code from there and pasted it below.
    Code:java
    1. public boolean locationIsInCuboid(Location playerLocation, Location min, Location max) {
    2. boolean trueOrNot = false;
    3. if (playerLocation.getWorld() == min.getWorld() && playerLocation.getWorld() == max.getWorld()) {
    4. if (playerLocation.getX() >= min.getX() && playerLocation.getX() <= max.getX()) {
    5. if (playerLocation.getY() >= min.getY() && playerLocation.getY() <= max.getY()) {
    6. if (playerLocation.getZ() >= min.getZ()
    7. && playerLocation.getZ() <= max.getZ()) {
    8. trueOrNot = true;
    9. }
    10. }
    11. }
    12. if (playerLocation.getX() <= min.getX() && playerLocation.getX() >= max.getX()) {
    13. if (playerLocation.getY() <= min.getY() && playerLocation.getY() >= max.getY()) {
    14. if (playerLocation.getZ() <= min.getZ()
    15. && playerLocation.getZ() >= max.getZ()) {
    16. trueOrNot = true;
    17. }
    18. }
    19. }
    20. }
    21. return trueOrNot;
    22. }
    playerLocation is the location of your player that you want to check. The min and max locations are two opposite corners.
     
  8. Offline

    Tzeentchful

    here is a little util i wrote a while ago for SBW.
    Code:java
    1. public class Border {
    2.  
    3. private Vector p1;
    4. private Vector p2;
    5.  
    6.  
    7. public Border(Vector p1, Vector p2) {
    8. int x1 = Math.min(p1.getBlockX(), p2.getBlockX());
    9. int y1 = Math.min(p1.getBlockY(), p2.getBlockY());
    10. int z1 = Math.min(p1.getBlockZ(), p2.getBlockZ());
    11. int x2 = Math.max(p1.getBlockX(), p2.getBlockX());
    12. int y2 = Math.max(p1.getBlockY(), p2.getBlockY());
    13. int z2 = Math.max(p1.getBlockZ(), p2.getBlockZ());
    14. this.p1 = new Vector( x1, y1, z1);
    15. this.p2 = new Vector( x2, y2, z2);
    16. }
    17.  
    18. public boolean contains(Location loc) {
    19. if(loc == null) {
    20. return false;
    21. }
    22. return loc.getBlockX() >= p1.getBlockX() && loc.getBlockX() <= p2.getBlockX()
    23. && loc.getBlockY() >= p1.getBlockY() && loc.getBlockY() <= p2.getBlockY()
    24. && loc.getBlockZ() >= p1.getBlockZ() && loc.getBlockZ() <= p2.getBlockZ();
    25. }
    26.  
    27. }


    Then you can simply use it like this.
    Code:java
    1. Border border = new Border(new Vector(-10,0,10), new Vector(10,skylimit,-10));
    2. if(border.contains(player.getLocation())) {
    3. //They are in the region
    4. }
    5.  
     
Thread Status:
Not open for further replies.

Share This Page