Jump to content

[Version 1.18.2, SOLVED] Give player status effect as it presses a key


LeeCrafts

Recommended Posts

I want to give a player a status effect whenever it presses a certain key. This was my first attempt:

@SubscribeEvent
public static void pressCKey(InputEvent.KeyInputEvent event) {
    LocalPlayer localPlayer = Minecraft.getInstance().player;
    if (localPlayer != null && event.getKey() == 67 && event.getAction() == GLFW.GLFW_PRESS) {
        if (localPlayer.getActiveEffectsMap() != null) localPlayer.addEffect(new MobEffectInstance(MobEffects.MOVEMENT_SPEED, 1000));
    }
}

I quickly realized this didn't work because it was adding the effect to the client-side player (LocalPlayer), and not the server-side player. I then searched through some older forums and found out that I would need to send a packet to the server whenever a key is pressed. There was a vanilla packet called ServerboundKeyPacket, but it confused me when I saw its constructor. I am unsure if this packet is the right one to use because it apparently does not consider which player pressed the key. Should I just use a custom packet instead?

Edited by LeeCrafts
Link to comment
Share on other sites

  • LeeCrafts changed the title to [Version 1.18.2, SOLVED] Give player status effect as it presses a key

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.



×
×
  • Create New...

Important Information

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