Jump to content

[1.12.2] How to create a custom RendererLivingEntity client side


Cospaia

Recommended Posts

I want to render some data below the name tag on player entities. Searching this forum I get that I need to create a custom renderer for doing this. But, I'm new to modding and don't know how to go about it. Is there a particular tutorial I could look at or could someone give some pointers? Thanks in advance!

Edited by Cospaia
Link to comment
Share on other sites

To modify vanilla behavior you often should consider using events. There is a RenderLivingEvent which allows you to replace the renderer. So what you can do is create your own player renderer class that copies the vanilla one but adds the text you want to add and then handle the event to use your renderer instead.

 

So to start you need to learn how to handle events. I have a tutorial on this here: http://jabelarminecraft.blogspot.com/p/minecraft-forge-172-event-handling.html I need to update the list of available events but most of the information should be good to get you started.

 

Get an event handler working for RenderLivingEvent and have it check for player. Actually I think that event is generic so maybe you can just handle RenderLivingEvent<EntityPlayerSP> or similar.

 

Then copy the RenderPlayer class into your own class and add stuff you want.

 

Then make sure your event handler replaces the renderer with your own.

 

Try what you can and post the code if you get stuck. We can help further then.

  • Like 1

Check out my tutorials here: http://jabelarminecraft.blogspot.com/

Link to comment
Share on other sites

On 2/18/2018 at 8:24 PM, jabelar said:

Get an event handler working for RenderLivingEvent and have it check for player. Actually I think that event is generic so maybe you can just handle RenderLivingEvent<EntityPlayerSP> or similar.

 

Thanks. I am not sure I follow along, but I have this handler right now (before I posted this question, that is):

 

    @SubscribeEvent
    public void render(RenderLivingEvent.Pre event) {
        float health = event.getEntity().getHealth();
        String name = event.getEntity().getDisplayName().getFormattedText();
        //System.out.println(name + ", " + health);
        event.getEntity().setCustomNameTag("" + health + "§c❤");
        event.getEntity().setAlwaysRenderNameTag(true);
        return;
    }

 

Which adds a health display for living entities, except for players. The event has a RenderLivingEntity instance, according to : http://takahikokawasaki.github.io/minecraft-resources/javadoc/forge/1.7.10-10.13.2.1291/index.html?net/minecraft/util/package-summary.html

 

I want the health display to render like a below name objective in vanilla, which seems to require that I somehow can modify the behaviour of this renderer. Am I taking the wrong approach thinking like this?

Link to comment
Share on other sites

§c can be replaced with EnumChatFormatting.RED

  • Like 1

Apparently I'm a complete and utter jerk and come to this forum just like to make fun of people, be confrontational, and make your personal life miserable.  If you think this is the case, JUST REPORT ME.  Otherwise you're just going to get reported when you reply to my posts and point it out, because odds are, I was trying to be nice.

 

Exception: If you do not understand Java, I WILL NOT HELP YOU and your thread will get locked.

 

DO NOT PM ME WITH PROBLEMS. No help will be given.

Link to comment
Share on other sites

Now I have gotten the mod to do what I want. But I'm not sure I am doing it right. So posting some code here for feedback.

 

This is the event handler:

 

    @SubscribeEvent
    public void render(RenderLivingEvent.Pre event) {
        EntityLivingBase entity = event.getEntity();
        RenderLivingBase baseRenderer = event.getRenderer();
        if (entity instanceof EntityPlayer) {
            if (this.renderer == null) {
                this.renderer = new RenderPlayerHealthDisplay(baseRenderer.getRenderManager(), baseRenderer.getMainModel(), 1.0F);
            }
            String health = String.format("%.1f", event.getEntity().getHealth()) + " §c❤";
            this.renderer.renderHealthDisplay((AbstractClientPlayer) entity, event.getX(), event.getY(), event.getZ(), health);
        }
    }

 

Which uses this subclass of RenderLivingBase:

 

@SideOnly(Side.CLIENT)
public class RenderPlayerHealthDisplay extends RenderLivingBase<AbstractClientPlayer> {

    public RenderPlayerHealthDisplay(RenderManager renderManagerIn, ModelBase modelBaseIn, float shadowSizeIn) {
        super(renderManagerIn, modelBaseIn, shadowSizeIn);
    }

    public void renderHealthDisplay(AbstractClientPlayer entity, double x, double y, double z, String s) {
        double distanceSq = entity.getDistanceSq(this.renderManager.renderViewEntity);
        y += (double)((float)this.getFontRendererFromRenderManager().FONT_HEIGHT * 1.15F * 0.025F);

        if (distanceSq < 100.0D)
        {
            Scoreboard scoreboard = entity.getWorldScoreboard();
            ScoreObjective scoreobjective = scoreboard.getObjectiveInDisplaySlot(2);

            if (scoreobjective != null)
            {
                y += (double)((float)this.getFontRendererFromRenderManager().FONT_HEIGHT * 1.15F * 0.025F);
            }
        }
        this.renderLivingLabel(entity, s, x, y, z, 64);
    }

    @Nullable
    @Override
    protected ResourceLocation getEntityTexture(AbstractClientPlayer entity) {
        return entity.getLocationSkin();
    }
}

 

(The scoreboard handling is how it is done in RenderPlayer.)

 

This displays the health above player's nametags. But should I be instantiating a full new renderer like this? It seems it would be more efficient to replace  the render class being sent to the event. Don't know if that makes sense, but anyway.

Link to comment
Share on other sites

Did I say the mod works? Well, it has a terrible flaw right now. When it is built as a jar package and used in the regular launcher, the heart is not rendered as it should, but instead as four strange looking characters. (Looks like a Unicode issue.) So, it works in the dev server, but not in the real one. Anyone have any clue what that could be about?

Link to comment
Share on other sites

10 minutes ago, Cospaia said:

Did I say the mod works? Well, it has a terrible flaw right now. When it is built as a jar package and used in the regular launcher, the heart is not rendered as it should, but instead as four strange looking characters. (Looks like a Unicode issue.) So, it works in the dev server, but not in the real one. Anyone have any clue what that could be about?

 

We got it working by using \u2764 instead of a literal heart in the code. Strange, but whatever works, works. :)

Link to comment
Share on other sites

1 hour ago, Cospaia said:

Found it. I needed to use TextFormatting.RED

Yeah, the name changed. At least twice. EnumChatFormatting is the only one I remember.

Apparently I'm a complete and utter jerk and come to this forum just like to make fun of people, be confrontational, and make your personal life miserable.  If you think this is the case, JUST REPORT ME.  Otherwise you're just going to get reported when you reply to my posts and point it out, because odds are, I was trying to be nice.

 

Exception: If you do not understand Java, I WILL NOT HELP YOU and your thread will get locked.

 

DO NOT PM ME WITH PROBLEMS. No help will be given.

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.