Jump to content
View in the app

A better way to browse. Learn more.

Forge Forums

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Featured Replies

Posted

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);
    }
}

 

 

  • Author

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)

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

  • Author

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...)

Guest
This topic is now closed to further replies.

Important Information

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

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.