spawnFallingBlock

Teyz

New member
Jun 3, 2023
1
0
1
Imagine a 3x3 vertical wall. If all the blocks in this wall are made of the same material and I have an oak sapling and I right click on the block in the center of this wall. All blocks in the 3x3 wall should be falling blocks. How can I do this? Current code below. The isSurroundedBySameBlock method checks if the blocks in the 3x3 are the same.
@EventHandler
public void onInteractPlayer(PlayerInteractEvent event) {
Player player = event.getPlayer();

if ((event.getAction() == Action.RIGHT_CLICK_BLOCK) && (player.getInventory().getItemInMainHand().getType() == Material.OAK_SAPLING)) {
Block block = event.getClickedBlock();

if ((block != null) && (isSurroundedBySameBlock(block))) {


}
}

}

public boolean isSurroundedBySameBlock(Block block) {
Location blockLocation = block.getLocation();
World world = block.getWorld();

// Get the coordinates of the block
int blockX = blockLocation.getBlockX();
int blockY = blockLocation.getBlockY();
int blockZ = blockLocation.getBlockZ();

// Check the surrounding blocks
for (int x = blockX - 1; x <= blockX + 1; x++) {
for (int y = blockY - 1; y <= blockY + 1; y++) {
for (int z = blockZ - 1; z <= blockZ + 1; z++) {
if (x == blockX && y == blockY && z == blockZ) {
// Skip the center block
continue;
}

Block surroundingBlock = world.getBlockAt(x, y, z);

// Check if the surrounding block is not the same type
if (surroundingBlock.getType() != block.getType()) {
return false;
}
}
}
}
 

stefvanschie

Moderator
Staff member
Dec 17, 2021
101
3
16
18
You'd first want to remove the blocks in the wall. You can change the block to be airs, to get rid of them with Block#setType. Then you can spawn falling blocks in the same positions with World#spawnFallingBlock(Location, BlockData).