Jump to content

[1.19.2] How to add items to player's inventory


strega

Recommended Posts

Hi! I tried the above method, but it did not work. There's my function. The message appears in the chat, but the item is not added to the inventory. I can’t figure out what the problem is, so please help.

    @SubscribeEvent
    public void onJoin(EntityJoinLevelEvent event)
    {
        if (event.getEntity() instanceof Player && event.getEntity().getCommandSenderWorld().isClientSide) {
            Minecraft.getInstance().player.getInventory().add(new ItemStack(Items.CARROT));
            Entity playerEntity = event.getEntity();
            playerEntity.sendSystemMessage(Component.nullToEmpty("Hello!"));
        }
    }

 

Link to comment
Share on other sites

I wanna say you would use something along the lines of:
 

@SubscribeEvent
public void onJoin(EntityJoinWorldEvent event) {
    if (event.getEntity() instanceof Player && !event.getWorld().isClient()) {
        Player player = (Player) event.getEntity();
        player.getInventory().add(new ItemStack(Items.CARROT));
        player.sendMessage(Component.nullToEmpty("Hello!"));
    }
}

I may be wrong though.

  • Thanks 1

tenor.gif?itemid=27352888

 

Not even gonna lie, I think I have autism

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Announcements



×
×
  • Create New...

Important Information

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