Jump to content

[Solved](1.16.5)How to get the Player who pressed a key?


Bowtruckle_HD

Recommended Posts

Hello, I'm fairly new to modding and I was trying to make a player invisible, when he presses the key "g". I set up the KeyboardKeypressed event, its probably not the right way, because you cant change the keybind, but i wanted to learn , how to make a Player invisible first. Sadly I cant seem to get the Player from the event. Could anyone give me a rough explanation, on what to do and where to start? I dont just want the code, but some explanation too, so I can learn from it. I posted my Code down below, but it is not much. Thx in Advance

 

@SubscribeEvent public void onPlayerRender (GuiScreenEvent.KeyboardKeyPressedEvent event) { if(event.getKeyCode() == 71) { } }

Edited by Bowtruckle_HD
Link to comment
Share on other sites

it's a little more complicated than you think, becuase KeyBindings are client side:

  1. create a custom KeyBinding and register it in FMLClientSetupEvent
  2. use TickEvent.ClientTickEvent to check if the KeyBinding is pressed (Note: TickEvents fired twice, check this with TickEvent.phase)
  3. if the KeyBinding is pressed, set Capability value to true (on client)
  4. get Capability value in RenderPlayerEvent.Pre and cancele it if the value is true

I have no idea whether you have to sync the capability to all clients because the RenderPlayerEvent has no Java doc, so its not clear when and how the event is fired

Link to comment
Share on other sites

4 hours ago, diesieben07 said:

You definitely would have to sync the capability. Both pressing a key as well as rendering is entirely client side. So when the key is pressed you need to send a packet to the server, the server updates the capability and syncs the change to all clients.

It is actually not very straightforward to do this. It would be best to just apply the invisibility effect to the player.

Ok, thank you both very much, I did apply the invisibility effect and it works well now. I'm gonna mark this post as solved now

Link to comment
Share on other sites

  • Bowtruckle_HD changed the title to [Solved](1.16.5)How to get the Player who pressed 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.

Announcements



×
×
  • Create New...

Important Information

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