Jump to content

getting the entity a player is looking at/ getting around a null pointer?


Recommended Posts

Posted

Hello all!

 

So I'm still working on my magic mod and I'm trying to add a knockback type spell. I'm using this method to get the entity the player is looking at:

 

Minecraft.getMinecraft().objectMouseOver.entityHit;

 

This seems to work ok when I aim at an entity, but if i miss the game crashes with a null pointer exception. I understand why the null pointer is happening, but I'm a bit stuck on how to get around it. I saw someone online use if(Minecraft.getMinecraft().objectMouseOver.entityHit != null) to make sure that entityHit isn't null, but the game seems to just crash at that line. The full code in question is:

 

if(Minecraft.getMinecraft().objectMouseOver.entityHit != null){ //<--- Seems to crash here

    if(Minecraft.getMinecraft().objectMouseOver.entityHit instanceof EntityLiving){

        Entity target= Minecraft.getMinecraft().objectMouseOver.entityHit;

        Vec3 look= player.getLookVec();

 

((EntityLiving)target).motionX=look.xCoord*2;

((EntityLiving)target).motionY=look.yCoord*2;

((EntityLiving)target).motionY=look.yCoord*2;

    }

}

 

Does anyone have any tips on how I can make the function return when entityHit is null? Or does anyone know a better way to do what I'm trying to do? Any help is much appreciated! Thank you in advance!

 

 

Posted

Ahh, thank you! So when I was checking if entityHit was null, I got a null Pointer exception because objectMouseOver itself was null. I added in a quick check for that and now it works :P Thanks a ton, you guys are always great here!

Posted

Can I tag one more quick question on here? It has to do with the same code and I believe it's relevant to other people trying to do something similar to this. So I think what I have above is only updating the mob's motion client side, because the mob keeps teleporting back to it's original spot. I've heard that the method Minecraft.getMinecraft().objectMouseOver.entityHit; is client side only. I know how to do basic packet handling but I'm not sure how to get the entity from my packet handler. I tried using the above method again just in case it would work, but the entity keeps teleporting back. This makes sense because the method is still client side only! It there another method I can use which is not client side only? Or, if not, how can I get to the entity from my packet handler to update it's motion on the server?

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.