Getting Entities Within Two Locations (Cuboid)

Discussion in 'Plugin Development' started by Lorinthio, Jan 5, 2014.

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

    Lorinthio

    Hey guys! I'm working on developing an arena type system where you can define a room (cube shape) with World Edit, and then it checks the room for mobs and if it is empty the exit door is opened (via redstone block placement trigger)

    I found some code for searching chunks for entities however this is not as specific as I need it to be For example I have 2 small arena rooms next to each other and it reads them both as full when only one actually is.

    Heres the code I have been using, Thanks in advance! The main problem spot is the Notes in the middle

    Code:java
    1. }
    2. for(String arena : arenas){
    3. if(!(arena == null)){
    4. Entity[] entities = null;
    5. Boolean emptyregion = true;
    6. //Get the region to look at
    7. World world = Bukkit.getServer().getWorld((String) this.arenasConfig.get("arenas." + arena + ".world"));
    8. Integer x1 = (int) this.arenasConfig.get("arenas." + arena + ".region.pos1.X");
    9. Integer x2 = (int) this.arenasConfig.get("arenas." + arena + ".region.pos2.X");
    10. Integer y1 = (int) this.arenasConfig.get("arenas." + arena + ".region.pos1.Y");
    11. Integer y2 = (int) this.arenasConfig.get("arenas." + arena + ".region.pos2.Y");
    12. Integer z1 = (int) this.arenasConfig.get("arenas." + arena + ".region.pos1.Z");
    13. Integer z2 = (int) this.arenasConfig.get("arenas." + arena + ".region.pos2.Z");
    14. Location location1 = new Location(world, x1, y1, z1);
    15. Location location2 = new Location(world, x2, y2, z2);
    16. //Math to check each chunk
    17. Chunk c1 = location1.getChunk();
    18. Chunk c2 = location2.getChunk();
    19. int xMin = Math.min(c1.getX(), c2.getX());
    20. int xMax = Math.max(c1.getX(), c2.getX());
    21. int zMin = Math.min(c1.getZ(), c2.getZ());
    22. int zMax = Math.max(c1.getZ(), c2.getZ());
    23. for(int x = xMin; x <= xMax; x++){
    24. for(int z = zMin; z <= zMax; z++){
    25. entities = world.getChunkAt(x, z).getEntities();
    26. for(Entity e : entities){
    27. //Check if e.getLocation() is between location1 and location2
    28. //This is where I need help =)
    29. }
    30. }
    31. for (Entity e : entities){
    32. if(e instanceof Creature){
    33. emptyregion = false;
    34. }
    35. }
    36. if(emptyregion){
    37. Integer trigX = (Integer) this.arenasConfig.get("arenas." + arena + ".trigger.X");
    38. Integer trigY = (Integer) this.arenasConfig.get("arenas." + arena + ".trigger.Y");
    39. Integer trigZ = (Integer) this.arenasConfig.get("arenas." + arena + ".trigger.Z");
    40. try{
    41. Location trigger = new Location (world, trigX, trigY, trigZ);
    42. trigger.getBlock().setType(Material.REDSTONE_BLOCK);
    43. //
    44. }
    45. }else{
    46. Integer trigX = (Integer) this.arenasConfig.get("arenas." + arena + ".trigger.X");
    47. Integer trigY = (Integer) this.arenasConfig.get("arenas." + arena + ".trigger.Y");
    48. Integer trigZ = (Integer) this.arenasConfig.get("arenas." + arena + ".trigger.Z");
    49. try{
    50. Location trigger = new Location (world, trigX, trigY, trigZ);
    51. trigger.getBlock().setType(Material.AIR);
    52. //
    53. }
    54. }
    55. }
    56. }
     
  2. Offline

    bobacadodl

    Lorinthio
    In this situation its probably just easier to loop through all entities in the world
     
  3. Offline

    Lorinthio

    bobacadodl

    But how would I check specifically if they are in that region? Thats really what I need specifically
     
  4. Offline

    bobacadodl

    Lorinthio
    ????
    Its basic logic! check if the entity's x value is between the 2 x values, if its y value is between the 2 y values, and its z value is between the 2 z values of the cuboid

    Anyways, here's an easy way to do it
    Code:
    public boolean isInCuboid(Location l, Location c1, Location c2){
            double maxx = Math.max(c1.getX(),c2.getX());
            double maxy = Math.max(c1.getY(),c2.getY());
            double maxz = Math.max(c1.getZ(),c2.getZ());
            Vector max = new Vector(maxx,maxy,maxz);
     
            double minx = Math.min(c1.getX(), c2.getX());
            double miny = Math.min(c1.getY(),c2.getY());
            double minz = Math.min(c1.getZ(),c2.getZ());
            Vector min = new Vector(minx,miny,minz);
     
            return l.toVector().isInAABB(min,max);
        }
     
  5. Offline

    Lorinthio

    bobacadodl
    Oh wow... Thanks -.- I wasn't thinking simple at all. Thanks again!
     
Thread Status:
Not open for further replies.

Share This Page