Started by
Virinas-code
on
Topic category: Help with MCreator software
How do I trigger a procedure when a player starts to break a block?
Goal
I am trying to make a custom tools that insta-break any block when it has power.
Actual solution
I made a procedure to break the block and add its item to the player's inventory if the tool has power.
Problem
I need an event Starting to break block
to trigger my procedure, but I can't find it.
My question
Is there any way to make a Starting to break block
trigger?
If not, is there any other way to reach my goal?
Code
Objects
Redstone Pickaxe
It's the tool that can insta-break stuff. I configured it to be as good as bare hand by default.
Block breaking procedure
Triggered when starting to break a block. If using a redstone pickaxe with power, insta-breaks the block.
Code
Block breaking procedure
package org.sustakenusername.redpower.procedures;
/* imports omitted */
@Mod.EventBusSubscriber
public class RedstoneToolBreakProcedure {
@SubscribeEvent
public static void onBlockBreak(BlockEvent.BreakEvent event) {
execute(event, event.getLevel(), event.getPos().getX(), event.getPos().getY(), event.getPos().getZ(), event.getState(), event.getPlayer());
}
public static void execute(LevelAccessor world, double x, double y, double z, BlockState blockstate, Entity entity) {
execute(null, world, x, y, z, blockstate, entity);
}
private static void execute(@Nullable Event event, LevelAccessor world, double x, double y, double z, BlockState blockstate, Entity entity) {
if (entity == null)
return;
if ((entity instanceof LivingEntity _livEnt ? _livEnt.getMainHandItem() : ItemStack.EMPTY).getItem().isCorrectToolForDrops(blockstate)) {
if ((entity instanceof LivingEntity _livEnt ? _livEnt.getMainHandItem() : ItemStack.EMPTY).getOrCreateTag()
.getDouble("redpower.Power") > 0) {
if (entity instanceof Player _player) {
ItemStack _setstack = (new ItemStack(blockstate.getBlock()));
_setstack.setCount(1);
ItemHandlerHelper.giveItemToPlayer(_player, _setstack);
}
world.setBlock(new BlockPos(x, y, z), Blocks.AIR.defaultBlockState(), 3);
(entity instanceof LivingEntity _livEnt ? _livEnt.getMainHandItem() : ItemStack.EMPTY).getOrCreateTag().putDouble("redpower.Power",
((entity instanceof LivingEntity _livEnt ? _livEnt.getMainHandItem() : ItemStack.EMPTY).getOrCreateTag()
.getDouble("redpower.Power") - 1));
}
}
}
}
Edited by Virinas-code on Tue, 01/24/2023 - 23:32
If your tools are custom, you might be able to do it with the “when item is swung” trigger. (That trigger is in the item itself, not in the procedure trigger)
Thannk you!