How to detect the health of the entity that the player is looking at

Started by mikepaps on

Topic category: Help with Minecraft modding (Java Edition)

Joined Mar 2022
Points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • MCreator plugins:
  • Comments:
How to detect the health of the entity that the player is looking at

This is the code so far that i have got but it just gives me bugs:

RayTraceResult rayTraceResult = Minecraft.getInstance().objectMouseOver;

        if (rayTraceResult.getType() == RayTraceResult.Type.ENTITY) {
            Entity entitytoshowhealth = ((EntityRayTraceResult) rayTraceResult).getEntity();
            MymodModVariables.MapVariables.get(world).NumOfHealth = (entitytoshowhealth.getHealth() : -1);

        }

Any help please?

B.T.W 

I am trying to make an overlay that will tell the player the health of the entity they are looking at E.G. when I look at a pig the overlay will display the current health of that pig.