Get specific block in radius of Location

Discussion in 'Plugin Development' started by rcth, Mar 7, 2014.

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

    rcth

    Hi guys,

    I have a Location loc and int radius, for example 7. Is it possible to get all specific blocks (like: all stone blocks) in a (x-y-z) radius from that location and remove these? I'm pretty sure there's a way to achieve it, but I don't know how.

    Thanks.
     
  2. Offline

    Scizzr

    You just need to loop the x, y, and z coords and check the block at their locations.

    Just one way of doing what you're wanting:
    Code:
    int radius = 7;
    Location loc = ???;
    World world = loc.getWorld();
    for (int x = -radius; x < radius; x++) {
        for (int y = -radius; y < radius; y++) {
            for (int z = -radius; z < radius; z++) {
                Block block = world.getBlockAt(loc.getBlockX()+x, loc.getBlockY()+y, loc.getBlockZ()+z);
                if (block.getType() == Material.STONE) {
                    block.setType(Material.AIR);
                }
            }
        }
    }
    
     
  3. Offline

    rcth

    Thanks, it works! :D

    Scizzr Do you maybe know how to get Minecarts in a radius? (I want to set their velocity) I know it are entities, but there isn't really a world.getEntitiesAt();

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 7, 2016
  4. Offline

    Bart

  5. Offline

    rcth

    I know that one, but that's not in a radius to a location, but a radius to an entity, right?
     
  6. Offline

    Scizzr

    Code:
    List<Minecart> carts = new ArrayList<Minecart>();
    int radius = 7;
    Location locSrc = ???;
    for(Entity e : locSrc.getWorld().getEntities()) {
        if (e instanceof Minecart) {
            Minecart cart = (Minecart)e;
            Location locCart = cart.getLocation();
            int xDiff = Math.abs(locCart.getBlockX()-locSrc.getBlockX());
            int yDiff = Math.abs(locCart.getBlockY()-locSrc.getBlockY());
            int zDiff = Math.abs(locCart.getBlockZ()-locSrc.getBlockZ());
            if (xDiff <= radius && yDiff <= radius && zDiff <= radius) {
                carts.add(cart);
            }
        }
    }
    //now do something with the list of minecarts
    
    Untested, but should work

    Edit: Fixed typo
     
    rcth likes this.
  7. Offline

    rcth

    Thanks, it worked!
     
  8. Offline

    Barinade

    OP, why not a recursive break rather than radial?
     
  9. Offline

    rcth

    What do you mean?
     
  10. Offline

    GameplayJDK

  11. Offline

    Barinade

    Get connected blocks of same type, add to list, get blocks with same type connected to those blocks, add to list
     
  12. Offline

    Scizzr

    You're joking right? The code you linked to does the exact same thing, it's just much harder to read.

    Code:
    public static List<Entity> getNearbyEntities(Location where, int range) {
        List<Entity> found = new ArrayList<Entity>();
     
        for (Entity entity : where.getWorld().getEntities()) {
            if (isInBorder(where, entity.getLocation(), range)) {
                found.add(entity);
            }
        }
        return found;
    }
     
    public static boolean isInBorder(Location center, Location notCenter, int range) {
        int x = center.getBlockX(), z = center.getBlockZ();
        int x1 = notCenter.getBlockX(), z1 = notCenter.getBlockZ();
     
        if (x1 >= (x + range) || z1 >= (z + range) || x1 <= (x - range) || z1 <= (z - range)) {
            return false;
        }
        return true;
    }
    
    That can be squished together (and adjusted to only get Minecarts) to become:

    Code:
    public static List<Minecart> getNearbyEntities(Location locSrc, int rad) {
        List<Minecart> found = new ArrayList<Minecart>();
     
        for (Entity entity : locSrc.getWorld().getEntities()) {
            if (entity instanceof Minecart) {
                Location locEnt = entity.getLocation();
                int srcX = locSrc.getBlockX();
                int srcZ = locSrc.getBlockZ();
                int entX = locEnt.getBlockX();
                int entZ = locEnt.getBlockZ();
     
                if (Math.abs(srcX-entX) <= rad && Math.abs(srcZ-entZ) <= rad) {
                    found.add((Minecart)entity);
                }
            }
        }
        return found;
    }
    
    It's not much different from what I have.

    Code:
    List<Minecart> carts = new ArrayList<Minecart>();
    int radius = 7;
    Location locSrc = ???;
    for(Entity e : locSrc.getWorld().getEntities()) {
        if (e instanceof Minecart) {
            Minecart cart = (Minecart)e;
            Location locCart = cart.getLocation();
            int xDiff = Math.abs(locCart.getBlockX()-locSrc.getBlockX());
            int yDiff = Math.abs(locCart.getBlockY()-locSrc.getBlockY());
            int zDiff = Math.abs(locCart.getBlockZ()-locSrc.getBlockZ());
            if (xDiff <= radius && yDiff <= radius && zDiff <= radius) {
                carts.add(cart);
            }
        }
    }
    
    The real only difference is that mine also has Y checked, and I didn't actually make it a method.

    Anyways, that aside...

    What if he's using entities? You can't really check that "entities are connect to each other." :) And what if his need wasn't to get "connected blocks", but all blocks in an area?
     
  13. Offline

    GameplayJDK

    Scizzr
    I thought less calculating would be better.. nvm in fact they do the same :)
     
  14. Offline

    BloodBacker

    Scizzr Any idea why this is not working?
    Code:java
    1. int radius = 9;
    2. int xx = this.plugin.getConfig().getInt("glass.x");
    3. int yy = this.plugin.getConfig().getInt("glass.y");
    4. int zz = this.plugin.getConfig().getInt("glass.z");
    5. Location loc = new Location(p.getWorld(), xx, yy, zz);
    6. World world = loc.getWorld();
    7. for (int x = -radius; x < radius; x++){
    8. for (int y = -radius; y < radius; y++){
    9. for (int z = -radius; z < radius; z++){
    10. Block block = world.getBlockAt(loc.getBlockX() + x, loc.getBlockY() + y, loc.getBlockZ() + z);
    11. if (block.getType() == Material.GLASS){
    12. block.setType(Material.AIR);
    13. }
    14. }
    15. }
    16. }
    17. }
    18. }
    19. }
    20. }
     
Thread Status:
Not open for further replies.

Share This Page