Jump to content

Recommended Posts

Posted

Here is some base code to work with on "picking up" an item.  You can look for attributes on the item picked up, and apply effects as you like.

 

Spoiler
@SubscribeEvent
    public void onItemPickup(PlayerEvent.ItemPickupEvent event) {
        ItemStack itemStack = event.getStack();
        String itemNamespace = "";
        String itemPath = "";
        Item item = itemStack.getItem(); //Somehow translates to the item name (minecraft:sand is "sand" here)
        try {
            itemNamespace = Objects.requireNonNull(item.getRegistryName()).getNamespace();
            itemPath = Objects.requireNonNull(item.getRegistryName()).getPath();
        } catch (Exception e) {
            LOGGER.info("Unable to get item namespace or path.");
        }
        String itemId = itemNamespace + ":" + itemPath;
        ServerPlayerEntity player = (ServerPlayerEntity) event.getPlayer();
        String playerName = player.getName().getUnformattedComponentText();  //Just player name.

        Minecraft mc = Minecraft.getInstance();
        if(mc.player != null) {
            //mc.player.sendChatMessage("[" + playerName + "][" + player.getPlayerIP() + "] picked up [" + itemId + "][" + itemStack.getCount() + "]");
            LOGGER.info("[" + playerName + "] picked up [" + itemId + "]");
        }
    }

 

 

Posted

first you mix Server and Client Side code,
since you cast the Player of the Event to a ServerPlayerEntity which is Server-Side
and then you use the Minecraft class which is completely Client Side
you should use a instanceof check bevor like:

PlayerEntity player = event.getPlayer();
if (player instanceof ServerPlayerEntity) {
  // do server stuff here
} else if (player instanceof ClientPlayerEntity) {
  // do client stuff here
}

if you cleaned up your code (which you should definitely do),
you could use PlayerEntity#addEffect

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.