Checking if a player is in a cuboid

Discussion in 'Resources' started by Jake0oo0, Aug 14, 2013.

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

    Jake0oo0

    Note: This is probably around somewhere..but this is useful either way :)

    So I needed a method to check if a player is within a region for my plugin, so I created this:

    Code:java
    1. public static Boolean isInside(Location loc, Location corner1, Location corner2) {
    2. double xMin = 0;
    3. double xMax = 0;
    4. double yMin = 0;
    5. double yMax = 0;
    6. double zMin = 0;
    7. double zMax = 0;
    8. double x = loc.getX();
    9. double y = loc.getY();
    10. double z = loc.getZ();
    11.  
    12. xMin = Math.min(corner1.getX(), corner2.getX());
    13. xMax = Math.max(corner1.getX(), corner2.getX());
    14.  
    15. yMin = Math.min(corner1.getY(), corner2.getY());
    16. yMax = Math.max(corner1.getY(), corner2.getY());
    17.  
    18. zMin = Math.min(corner1.getZ(), corner2.getZ());
    19. zMax = Math.max(corner1.getZ(), corner2.getZ());
    20.  
    21. return (x >= xMin && x <= xMax && y >= yMin && y <= yMax && z >= zMin && z <= zMax);
    22. }



    It can be used like so:
    Code:java
    1. @EventHandler
    2. public void onBlockBreak(BlockBreakEvent event) {
    3. Location location = event.getBlock().getLocation();
    4. if (CuboidAPI.isInside(location, new Location(world, x, y, z), new Location(world, x, y, z)) {
    5. event.setCancelled(true);
    6. }
    7.  
    8. }


    In that event you the CuboidAPI checks if the block placed is inside the two locations, which could be called min and max, but can be reversed. If the block is within the locations, the event will be cancelled.
     
    Vanidor1424 likes this.
  2. Offline

    ZeusAllMighty11

    If you convert the location to vectors, you can just use isInAABB()

    Faster calculations, less lines of code
     
    _DSH105_ and Kazzababe like this.
  3. Teach me master!
     
Thread Status:
Not open for further replies.

Share This Page