Faked: A Location/Block .getNearbyEntities()

Discussion in 'Resources' started by nathanaelps, Dec 21, 2012.

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

    nathanaelps

    Dear interwebs, have you ever wanted to getNearbyEntities for a block? Perhaps for a Location? You were sadly out of luck, weren't you? No more! I present to you:

    Code:
    public List<Entity> getNearbyEntities(Location loc, double x, double y, double z) {
        FallingBlock ent = loc.getWorld().spawnFallingBlock(loc, 132, (byte) 0);
        List<Entity> out = ent.getNearbyEntities(x, y, z);
        ent.remove();
       
        return out;
    }
    As this stands, it gives a moment of stringy flicker... But (for me) that's far more usable than writing the ridiculously long methods I've previously used!

    Can this be simplified/sped up/not so stringy?

    Also, Merry Saturday!
     
    Anrza likes this.
  2. Offline

    Icyene

    What about spawning it one block below the original location, and adding 1 to the distance? In most cases, one block below would be underground, where no one can see the flicker :)

    Also, this is what I use:

    Code:Java
    1.  
    2. /**
    3.   * Checks if the location is near a block in the list on the same layer.
    4.   *
    5.   * @param location the location
    6.   * @param blocks the collection of blocks to search
    7.   * @param radius the radius to search
    8.   * @return boolean
    9.   */
    10. public static boolean isLocationNearBlock(Location location, Collection<Integer> blocks, int radius) {
    11. World world = location.getWorld();
    12. int x = (int) location.getX(), y = (int) location.getY(), z = (int) location.getZ();
    13.  
    14. for (int ox = radius; ox > -radius; ox--)
    15. for (int oz = radius; oz > -radius; oz--)
    16. if (blocks.contains(world.getBlockAt(x + ox, y, z + oz).getTypeId()))
    17. return false;
    18. return true;
    19. }
    20.  


    It's not that long, definitely doesn't make a flicker, and does basically what getNearbyEntities does.
     
  3. Offline

    Hoolean

    nathanaelps

    What if you make an air falling block :)?

    I wonder what would happend :D
     
Thread Status:
Not open for further replies.

Share This Page