Checking if a Player is in a Cuboid Zone

Discussion in 'Resources' started by oyasunadev, Aug 19, 2011.

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

    oyasunadev

    This is my method of getting if a Player is in a Cuboid Zone. It's very simple, please feel free to modify it. Its made to find if a Player is in a area defined with 2 blocks. If you do modify it please give credits to me for the original work. Here's the source:

    NEW:
    Code:
    public static boolean checkCuboid(Location player, Location loc1, Location loc2)
        {
            if(loc1 != null && loc2 != null)
            {
                if(player.getX() > loc1.getX() && player.getX() < loc2.getX())
                {
                    if(player.getY() > loc1.getY() && player.getY() < loc2.getY())
                    {
                        if(player.getZ() > loc1.getZ() && player.getZ() < loc2.getZ())
                        {
                            return true;
                        }
                    }
                }
            }
    
            return false;
        }
    OLD:
    Code:
    public boolean checkCuboid(Player player, Block block1, Block block2)
    {
        Location playerLoc = player.getLocation();
        World world = player.getWorld();
    
        Location loc1 = new Location(world,
                Math.min(block1.getLocation().getX(), block2.getLocation().getX()),
                Math.min(block1.getLocation().getY(), block2.getLocation().getY()),
                Math.min(block1.getLocation().getZ(), block2.getLocation().getZ()));
        Location loc2 = new Location(world,
                Math.max(block1.getLocation().getX(), block2.getLocation().getX()),
                Math.max(block1.getLocation().getY(), block2.getLocation().getY()),
                Math.max(block1.getLocation().getZ(), block2.getLocation().getZ()));
    
        if(playerLoc.getX() > loc1.getX() && playerLoc.getX() < loc2.getX())
        {
            if(playerLoc.getY() > loc1.getY() && playerLoc.getY() < loc2.getY())
            {
                if(playerLoc.getZ() > loc1.getZ() && playerLoc.getZ() < loc2.getZ())
                {
                    return true;
                }
            }
        }
    
        return false;
    }
    I surprised no one has commented...

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 18, 2016
    hammale likes this.
  2. Offline

    TruDan

    It's pretty good, i will use it somewhere, just have no where to use it yet!
     
  3. Offline

    shamonj03

    Thanks :) I've been wondering how to do this, just haven't found a need for it yet.
     
  4. Offline

    Bone008

    Looks pretty inefficient to me. You are creating 2 instances of Location without actually needing them. You call getLocation() for blocks 12 times instead of possible 2. Just store stuff in primitive variables, that's more efficient!
    And btw, you don't even use world.

    My approach here:
    Code:java
    1. public boolean checkCuboid(Player player, Block block1, Block block2)
    2. {
    3. checkCuboid(player.getLocation(), block1.getLocation(), block2.getLocation());
    4. }
    5. public boolean checkCuboid(Location checkLoc, Location loc1, Location loc2)
    6. {
    7.  
    8.  
    9. int x1 = Math.min(loc1.getX(), loc2.getX());
    10. int y1 = Math.min(loc1.getY(), loc2.getY());
    11. int z1 = Math.min(loc1.getZ(), loc2.getZ());
    12.  
    13.  
    14. int x2 = Math.min(loc1.getX(), loc1.getX());
    15. int y2 = Math.min(loc1.getY(), loc1.getY());
    16. int z2 = Math.min(loc1.getZ(), loc1.getZ());
    17.  
    18. double cx = checkLoc.getX();
    19. double cy = checkLoc.getY();
    20. double cz = checkLoc.getZ();
    21. return (cx > x1 && cx < x2 && cy > y1 && cy < y2 && cy > z1 && cz < z2);
    22. }

    I added an overloaded call, you can create more to have it flexible.

    Sorry for being so fishy about that, but imagine someone uses that inside onPlayerMove (which is really likely). It would create 2 instances of Location every time it is called - and as we all know that's incredibly often.

    Greetz
     
  5. Offline

    oyasunadev

    well mine is ment for blocks, but your method is great also!
     
  6. Offline

    Bone008

    You saw the overloaded method at the top? It works the same as yours, you can just call it differently as well.

    Oh, and I missed parentheses at player.getLocation()
     
  7. Offline

    garbagemule

    With MobArena, I maintain the invariant that one Location always holds the lowest coordinate values, and the other holds the highest coordinate values. This is more efficient, but obviously requires that the two Locations aren't dynamic. For MobArena, the two static points p1 and p2 define an arena region which is only altered during the setup phase.

    This method is more generalized, but I'm at a loss as to which situations it would be more efficient/the only way to go, than maintaining the above mentioned invariant. Could any of you enlighten me? :3
     
Thread Status:
Not open for further replies.

Share This Page