Breaking a 3x3 or 4x4 area with a pickaxe.

Discussion in 'Plugin Development' started by Paul122, Aug 22, 2020.

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

    Paul122

    Hi,

    I'm trying to make it so when a player breaks a block, it breaks a 3x3 area (and eventually a 4x4, 5x5 etc). So far, I have this:
    Code:
    @EventHandler
        public void onBlockBreak(BlockBreakEvent e) {
        
        
                
            
                    Block origin= e.getBlock();
            
                    List<Block> blocks = new ArrayList<>();
                
                    blocks.add(origin);
                    blocks.add(origin.getRelative(BlockFace.NORTH_WEST));
                    blocks.add(origin.getRelative(BlockFace.NORTH));
                    blocks.add(origin.getRelative(BlockFace.NORTH_EAST));
                    blocks.add(origin.getRelative(BlockFace.WEST));
                    blocks.add(origin.getRelative(BlockFace.EAST));
                    blocks.add(origin.getRelative(BlockFace.SOUTH_WEST));
                    blocks.add(origin.getRelative(BlockFace.SOUTH));
                    blocks.add(origin.getRelative(BlockFace.SOUTH_EAST));
              
              
                    for (Block b : blocks) {
                        b.breakNaturally();
                    }
              
        
        
        }
    This part of the code works flawlessly if the player is facing up or down when breaking a block, however when facing a wall and breaking a block, it breaks the area as if the player is facing up/down (which is expected due to how I used the directions). How would I go about making it so if a player is facing a wall and breaks a block, it breaks the surrounding 8 blocks as well?

    Additionally, how would I make it so players cannot break bedrock with this, or any WG protected regions that they don't have permissions to edit.

    Thanks.
     
  2. Offline

    Awesom_AA

    Not too sure whats wrong with your block breaking code, but you can check to make sure each block isn’t bedrock or protected inside the for loop, and only break the block if it passes the if statements
     
  3. You could do 5x5 like this:
    Code:
    int bx = (int) e.getBlock().getLocation().getX();
    int by = (int) e.getBlock().getLocation().getY();
    int bz = (int) e.getBlock().getLocation().getZ();
    World world = e.getBlock().getWorld();
    
    for (int x =  bx - 2; x <= bx + 2; x++) {
      for (int y = by - 2; y <= by + 2; y++) {
        for (int z = bz - 2; z <= bz + 2; z++) {
          blocks.add(new Location(world, (double) x, (double) y, (double) z).getBlock());
        }
      }
    }
    
     
Thread Status:
Not open for further replies.

Share This Page