Jump to content

[SOLVED] Safest way to check if an entity is the game's player


Reflxction

Recommended Posts

Hello. In the process of making a mod I have come across some weird behavior of the game. Anytime I attempt to call Minecraft.getMinecraft().player#getUUID() in singleplayer the game crashes with a NPE. The same thing happened with #getPersistentID(). Is there any reliable way to check if an entity is the player without causing crashes or something. I'm afraid comparing names may not 100% work, and since retrieving the UUID crashed the game, is there any better way to do so?

Edited by Reflxction
Link to comment
Share on other sites

1 hour ago, Reflxction said:

Hello. In the process of making a mod I have come across some weird behavior of the game. Anytime I attempt to call Minecraft.getMinecraft().player#getUUID() in singleplayer the game crashes with a NPE. The same thing happened with #getPersistentID(). Is there any reliable way to check if an entity is the player without causing crashes or something. I'm afraid comparing names may not 100% work, and since retrieving the UUID crashed the game, is there any better way to do so?

Two things Minecraft is a client only class. And either Minecraft.getMinecraft is returning null and which means you are calling this too soon or Minecraft#player is null and which would also mean you are calling this too soon.

VANILLA MINECRAFT CLASSES ARE THE BEST RESOURCES WHEN MODDING

I will be posting 1.15.2 modding tutorials on this channel. If you want to be notified of it do the normal YouTube stuff like subscribing, ect.

Forge and vanilla BlockState generator.

Link to comment
Share on other sites

1 hour ago, Animefan8888 said:

Two things Minecraft is a client only class. And either Minecraft.getMinecraft is returning null and which means you are calling this too soon or Minecraft#player is null and which would also mean you are calling this too soon.

I was calling it in EntityJoinWorldEvent, where I assume the player wouldn't be null. Anyway, I did a few more googling and found out that what I was looking for is Minecraft.getSession().getProfile()#getId(), as this is what worked for me

Link to comment
Share on other sites

6 minutes ago, Reflxction said:

I was calling it in EntityJoinWorldEvent, where I assume the player wouldn't be null. Anyway, I did a few more googling and found out that what I was looking for is Minecraft.getSession().getProfile()#getId(), as this is what worked for me

Did you test it on the dedicated server?

VANILLA MINECRAFT CLASSES ARE THE BEST RESOURCES WHEN MODDING

I will be posting 1.15.2 modding tutorials on this channel. If you want to be notified of it do the normal YouTube stuff like subscribing, ect.

Forge and vanilla BlockState generator.

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.