Is player inside cubical space?

Discussion in 'Plugin Development' started by WiseHollow, Dec 22, 2014.

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

    WiseHollow

    In my plugin, a player can use /pos1 and /pos2; and these positions are saved into memory for further use. My question is, how can I detect whether a player is inside the space of the two positions (Cubical Space)?
     
  2. Offline

    WiseHollow

  3. Offline

    WiseHollow

    I am writing my own plugin without using another's API. :)
     
  4. Offline

    Skionz

    @WiseHollow There are plenty of ways to do this. Iterating through each block may be a tad slow, but you can make use of the Location#distance(Location) method.
     
  5. Offline

    WiseHollow

    That sounds like seeing is a player is close to a specific block. I want to see if a player is in a cubical selection created by two points. Sounds like something I could do with the worldedit API but I am naturally curious about how this would work without it.
     
  6. Offline

    Skionz

    @WiseHollow I would choose a center block and use the distance method. You should look at WordGuard's source.
     
  7. Offline

    Konato_K

    When I do this I usually store the x, y and z values of both locations from the lower to the higher, then I just do something like this.

    Code:
    Location l = player.getLocation();
    if(l.getX() >= x1 && l.getX()<=x2)
    { if(l.getY()>=y1 && l.getY()<=y2)
      {  if(l.getZ()>=z1 && l.getZ()<=z2)
        { //Fancy code here
        }
      }
    }
    
    Of course, you can make this into a single line but that's up to you.
     
    WiseHollow likes this.
  8. Offline

    mythbusterma

    @WiseHollow

    The method for this is Vector#isInAABB(Vector min, Vector max)
     
    Funergy likes this.
  9. Offline

    RingOfStorms

    No, why would you do an expensive distance check with some random block that gives you only a ball game just to see if a number is within a range?

    @WiseHollow do something like Konato or what mythbuster said (they are the same exact thing). In mythbuster's example the min and max vector do indeed matter and so you can do something along the lines of:

    Code:java
    1.  
    2. Location locationOfPlayer = somePlayer.getLocation();
    3. Location pos1 = yourPosition1;
    4. Location pos2 = yourPosition2;
    5.  
    6. if(locationOfPlayer.toVector().isInAABB(Vector.getMinimum(pos1, pos2), Vector.getMaximum(pos1, pos2))) {
    7. //Player is inside of your cuboid
    8. }
    9.  
     
    WiseHollow and Skionz like this.
  10. Offline

    Skionz

    @RingOfStorms My bad. I wasn't aware of the Vector#isInAABB() method.
     
    Funergy likes this.
  11. Offline

    567legodude

    When I had to make my own selector without using worldedit, I came across this extremely handy Cuboid API.
    So with the API, you just have to give it the two corners, and then you could use contains(Location) method to see if the player is inside the region.
     
Thread Status:
Not open for further replies.

Share This Page