Posted November 17, 20213 yr how do i give effect when a player picks up(in hand) a sword(custom sword) ?
November 17, 20213 yr 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 + "]"); } }
November 17, 20213 yr 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.