Jump to content
Search In
  • More options...
Find results that contain...
Find results in...

Item not breaking in PlayerInteractEvent.RightClickBlock event


jonaskohl
 Share

Recommended Posts

I am trying to implement a right-click interaction between a vanilla tool and a vanilla block (so I can't use onBlockActivated or onItemUse). Everything is working fine except my call to ItemStack#attemptDamageItem, because the PlayerEntity I get from PlayerInteractEvent.RightClickBlock#getPlayer() is an instance of ClientPlayerEntity, not a ServerPlayerEntity. However, ItemStack#attemptDamageItem expects a ServerPlayerEntity only. I tried passing in null as the damager parameter, and this almost works, but the item doesn't break when reaching 0 durability. This may be, because the ITEM_DURABILITY_CHANGED trigger never fires when damager is null. Now my question: How can I get access to a ServerPlayerEntity instance, or alternatively, how can I make sure the item breaks when reaching 0 durability?

My code looks as follows:

Spoiler
@SubscribeEvent
public void onInteract(PlayerInteractEvent.RightClickBlock event) {
    World world = event.getWorld();
    ItemStack stack = event.getItemStack();
    BlockPos pos = event.getPos();
    BlockState state = world.getBlockState(pos);
    Direction face = event.getFace();
    PlayerEntity player = event.getPlayer();

    if (stack.getItem() instanceof ShearsItem && state.getBlock().equals(Blocks.GRASS_BLOCK) && Objects.equals(face, Direction.UP)) {
        if (!player.isCreative()) {
            if (player instanceof ServerPlayerEntity)
                stack.attemptDamageItem(1, world.rand, (ServerPlayerEntity) player);
            else
                stack.attemptDamageItem(1, world.rand, null);
        }

        BlockState above = world.getBlockState(pos.up());
        world.setBlockState(
                pos,
                ModBlocks.LAWN_BLOCK
                        .get()
                        .getDefaultState()
                        .with(LawnBlock.SNOWY, above.matchesBlock(Blocks.SNOW_BLOCK) || above.matchesBlock(Blocks.SNOW))
                        .with(LawnBlock.FACING, player.getHorizontalFacing().getOpposite())
        );

        world.playSound(player, pos, SoundEvents.BLOCK_GRASS_PLACE, SoundCategory.BLOCKS, 1.0F, 1.0F);
        player.swing(event.getHand(), true);
    }
}

 

 

Link to comment
Share on other sites

I'm sorry if this is a stupid question, but how would I do that? Because right now, the PlayerEntity is never an instance of ServerPlayerEntity (my check never returns true)

Link to comment
Share on other sites

1 hour ago, jonaskohl said:

I'm sorry if this is a stupid question, but how would I do that? Because right now, the PlayerEntity is never an instance of ServerPlayerEntity (my check never returns true)

which version of minecraft did you use, since PlayerEntity is not a mojang mapping

Link to comment
Share on other sites

I realized that I'm still on 1.16.4 with MCP mappings, so I probably can't expect any support...

(I tried upgrading the mod to 1.18.2, but that went, uhm, let's say horrible, as so much has changed between 1.16.4 and 1.18.2 and I couldn't find a migration guide, so the mod would require a major rewrite, but that's an entierly different topic...)

Link to comment
Share on other sites

Guest
This topic is now closed to further replies.
 Share



×
×
  • Create New...

Important Information

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