I'm attempting to make a tool that doesn't use the base player mining. I am doing this because eventually I want to expand the area that the tool can mine. The code right now is using a packet sent to the server and attempting to break a block like zombies do with the doors. This only sporadically works, and stops working when I attempt to use this to mine snow. I would also eventually like to render the mining overlay on top of the block I am mining. I have a good amount of java experience, I only need a push in the right direction. Is there anywhere I can look in the API for help on this? Is there something I am missing? Any help would be much appreciated.
public void handle(Supplier<NetworkEvent.Context> ctx) {
ServerPlayer player = ctx.get().getSender();
if(player == null)
return;
ServerLevel pLevel = player.serverLevel();
HitResult targetBlock = player.pick(18.0D, 0.0F, false);
BlockPos blockpos = ((BlockHitResult) targetBlock).getBlockPos();
BlockState blockState = pLevel.getBlockState(blockpos);
Block block = blockState.getBlock();
++breakTime;
int i = (int)((float)breakTime / (float)this.getBlockBreakTime(block) * 10.0F);
if (i != lastBreakProgress) {
pLevel.destroyBlockProgress(player.getId(), blockpos, i);
lastBreakProgress = i;
}
if (breakTime == getBlockBreakTime(block)) {
pLevel.destroyBlock(blockpos, true);
pLevel.levelEvent(2001, blockpos, Block.getId(pLevel.getBlockState(blockpos)));
breakTime = 0;
lastBreakProgress = 0;
}
ctx.get().setPacketHandled(true);
}
EDIT: The solution to rendering the mining overlay is to use:
player.connection.send(new ClientboundBlockDestructionPacket(player.getId(), blockpos, i));
(where player is a "ServerPlayer") in place of "pLevel.destroyBlockProgress...."