Jump to content
View in the app

A better way to browse. Learn more.

Forge Forums

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

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

Featured Replies

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!

 

 

  • Author

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!

  • Author

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...

Important Information

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

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.