Find block in radius

Discussion in 'Plugin Development' started by HamOmlet, Jul 24, 2012.

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

    HamOmlet

    Hello,

    I'm trying to search for blocks similar to a block that was placed. I'm using the below code:
    Code:
       for (int x = -5; x <= 5; x++) {
          for (int y = -5; y <= 5; y++) {
             for (int z = -5; z <= 5; z++) {
        if(b.getRelative(x, y, z).getType() == Material.STONE_PLATE) {
           event.setCancelled(true);
           p.sendMessage(ChatColor.RED + "You cannot place a plate trap within five blocks of other plate traps!");
        }
    }
       }
    }
    
    The issue I'm having is whenever I place down the initial block, it's removed. My goal is to check for the similar blocks around that block, and if that block isn't detected, the block can be placed.
     
  2. Offline

    Jnorr44

    Code:
     
            int radius = 5;//or whatever you want it to be   
            final Block block = new Location(b.getWorld(), b.getLocation().getX(), b.getLocation().getY(), b.getLocation().getZ()).getBlock();
            final int x = block.getX();
            final int y = block.getY();
            final int z = block.getZ();
            final int minX = x - radius;
            final int minY = y - radius;
            final int minZ = z - radius;
            final int maxX = x + radius;
            final int maxY = y + radius;
            final int maxZ = z + radius;
            for (int counterX = minX; counterX <= maxX; counterX++) {
                for (int counterY = minY; counterY <= maxY; counterY++) {
                    for (int counterZ = minZ; counterZ <= maxZ; counterZ++) {
                        final Block blockName = world.getBlockAt(counterX, counterY, counterZ);
                        //do what you want
                        }
                    }
                }
    
     
  3. Offline

    r0306

    Jnorr44
    HamOmlet
    That can be simplified to:
    Code:
    int radius = 5;
    final Block block = event.getBlock(); //placed block
    for (int x = -(radius); x <= radius; x ++)
    {
      for (int y = -(radius); y <= radius; y ++)
      {
        for (int z = -(radius); z <= radius; z ++)
        {
          if (block.getRelative(x,y,z).getType() == Material.STONE_PLATE)
          {
              event.setCancelled(true);
              p.sendMessage(ChatColor.RED + "You cannot place a plate trap within five blocks of other plate traps!");
           }
         }
       }
    }
    
     
  4. Offline

    HamOmlet

    I'm still unable to initially place a block. Below is what I have:

    Code:
    int radius = 5;
    final Block block = new Location(b.getWorld(), b.getLocation().getX(), b.getLocation().getY(), b.getLocation().getZ()).getBlock();
            final int x = block.getX();
            final int y = block.getY();
            final int z = block.getZ();
            final int minX = x - radius;
            final int minY = y - radius;
            final int minZ = z - radius;
            final int maxX = x + radius;
            final int maxY = y + radius;
            final int maxZ = z + radius;
            
            for (int counterX = minX; counterX <= maxX; counterX++) {
           for (int counterY = minY; counterY <= maxY; counterY++) {
           for (int counterZ = minZ; counterZ <= maxZ; counterZ++) {
           final Material plate = p.getWorld().getBlockAt(counterX, counterY, counterZ).getType();
           if(plate == Material.STONE_PLATE) {
           // Should only be fired if placing a plate within a radius of an already placed plate
           p.sendMessage("Stone plate detected!");
           }
           }
           }
            }
    
    This doesn't work neither.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 27, 2016
  5. Offline

    pzxc

    Just use your original code, and add a clause to your inner if:

    if(b.getRelative(x, y, z).getType() == Material.STONE_PLATE && (x != 0 || y != 0 || z != 0)) {
    Then it will skip over the case where x,y,z are all zero (the block you just placed)
     
    r0306 likes this.
  6. Offline

    HamOmlet

    That worked perfectly! Thank you and the others who provided code. ;)
     
Thread Status:
Not open for further replies.

Share This Page