I'm new to Java and Spigot-API. I want to do a BlockBreakEvent
which detects if a GOLD_ORE
block got destroyed by a player. If so, it should replace that GOLD_ORE
block with a STONE
block. However it doesn't work and instead of replacing the block it drops the item which is a cobblestone of course.
My code:
public class GoldOreListener implements Listener {
@EventHandler
public void onGoldOreDestroyed(BlockBreakEvent event)
{
Block block = event.getBlock();
Player player = event.getPlayer();
Material material = block.getType();
if (material == Material.GOLD_ORE)
{
Location locationOfBlock = block.getLocation();
Material newMaterial = Material.STONE;
System.out.println(locationOfBlock.getBlock());
locationOfBlock.getBlock().setType(newMaterial);
}
}
}
Output of locationOfBlock.getBlock()
:
CraftBlock{pos=BlockPosition{x=-158, y=83, z=303},type=GOLD_ORE,data=Block{minecraft:gold_ore},fluid=net.minecraft.world.level.material.FluidTypeEmpty@1507c3c3}