Making cuboid regions

Discussion in 'Resources' started by CaptainUniverse, Aug 23, 2014.

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

    CaptainUniverse

    Hello guys I have recently made a simple Boolean to tell weather a player is in a location.

    This is a very simple method with no loops what so ever and it is not very long too. This doesn't only work for players it can be any Location of any block. It also has IlligalArgumentExceptions on if the location being sent is in another world, weather the two corners are identical, and if the two corners are in seperate worlds. Well here you guys go.

    Cube Class Boolean
    Code:java
    1. public static boolean isInside(Location loc, Location corner, Location corner2) {
    2. if (corner == corner2) throw new IllegalArgumentException("Can not be the Same location for corner and corner2");
    3. if (corner.getWorld() != corner2.getWorld()) throw new IllegalArgumentException("Can not be in another world");
    4. if (loc.getWorld() != corner.getWorld())
    5. return false;
    6. boolean jon = new Boolean(false);
    7. int locx = loc.getBlockX();
    8. int locz = loc.getBlockZ();
    9. int BigX = 0;
    10. int BigZ = 0;
    11. int SmallX = 0;
    12. int SmallZ = 0;
    13. if (corner.getBlockX() > corner2.getBlockX()) {
    14. BigX = corner.getBlockX();
    15. SmallX = corner2.getBlockX();
    16. }
    17. else if (corner2.getBlockX() > corner.getBlockX()) {
    18. BigX = corner2.getBlockX();
    19. SmallX = corner.getBlockX();
    20. }
    21. if (corner.getBlockZ() > corner2.getBlockZ()) {
    22. BigZ = corner.getBlockZ();
    23. SmallZ = corner2.getBlockZ();
    24. }
    25. else if (corner2.getBlockZ() > corner.getBlockZ()) {
    26. BigZ = corner2.getBlockZ();
    27. SmallZ = corner.getBlockZ();
    28. }
    29. if (locx > BigX || locx < SmallX || locz > BigZ || locz < SmallZ) {
    30. jon = new Boolean(false);
    31. }
    32. if (locx <= BigX && locx >= SmallX && locz <= BigZ && locz >= SmallZ) {
    33. jon = new Boolean(true);
    34. }
    35. return jon;
    36. }


    ExampleClass
    Code:java
    1. @EventHandler
    2. public void onMove(PlayerMoveEvent e) {
    3. Location loc = e.getFrom();
    4. if (Cube.isInside(loc, new Location(Bukkit.getWorld("world"), 144,3,98), new Location(Bukkit.getWorld("world"),143,3,99))) {
    5. e.getPlayer().sendMessage("You are in the cube");
    6. }
    7. }
     
    Meatiex likes this.
  2. Offline

    nlthijs48

    CaptainUniverse Why are you using this?
    Code:java
    1. jon = new Boolean(true);

    You can just use the following:
    Code:java
    1. jon = true;

    Edit: It is not smart to compare World objects by using ==, you have to use .equals() for that.
     
  3. Offline

    CaptainUniverse

    I'm not actually sure It's cause I'm new with booleans and I thought you had to do it that way but anyway thanks for the advice nlthijs48
     
  4. Offline

    viper_monster

    CaptainUniverse

    Isn't this simpler?
    Code:java
    1. public static boolean isInside(Location location, Location min, Location max) {
    2. return location.toVector().isInAABB(min.toVector(), max.toVector());
    3. }
     
    Phasesaber and Bart like this.
  5. Offline

    CaptainUniverse

    Ya I guess but I felt like making my own sort of API :p
     
    Panjab likes this.
  6. Offline

    xTrollxDudex

    Getting the world name is safer
     
    Panjab and Phasesaber like this.
Thread Status:
Not open for further replies.

Share This Page