Jump to content

How do i detect a block being broken?


Recommended Posts

Hello, i started modding a few weeks ago(1.12.2 modding btw) and i am making a mod that has some enchantments like an enchantment called Miners Touch(It is supposed to give a player a iron ingot and leave stone where the ore was broken), but i can't seem to get a way to detect the player breaking a block... my current code is this:

if (player != null) { BlockPos block = findBlockAtCursor(10, player, world); if (block != null && world.getBlockState(block) == Blocks.IRON_ORE.getDefaultState()) { player.addItemStackToInventory(new ItemStack(Items.IRON_INGOT)); displayMessage("Iron Mined",world,player); } } } //Methods protected static BlockPos findBlockAtCursor(float range, EntityPlayer player, World world) { Vec3d posVec = new Vec3d(player.posX, player.posY + player.getEyeHeight(), player.posZ); Vec3d lookVec = player.getLookVec(); // Draw a line from the player to where the player is aiming, save it if // we hit a block. // TODO: 1.12 Change - lookVec uses x, y, z RayTraceResult blockHit = world.rayTraceBlocks(posVec, posVec.addVector(lookVec.x * range, lookVec.y * range, lookVec.z * range)); if (blockHit == null) return null; BlockPos block = blockHit.getBlockPos().offset(blockHit.sideHit); return block; } protected static void displayMessage(String message, World world, EntityPlayer player) { if (!world.isRemote) { player.sendMessage(new TextComponentString(message)); } } }


Any help is appreciated!

Thanks in advance - Chocolate_Cherry

Link to comment
Share on other sites

This topic is now closed to further replies.

  • Create New...

Important Information

By using this site, you agree to our Terms of Use.