Registering entity rendering handler with Minecraft Forge 1.16.5



I’m following this tutorial to create a custom entity for my Minecraft mod. Unfortunately, the tutorial is written for a slightly older version of Minecraft (1.14), and some parts do not work with the latest version. I was up to registering the rendering handler for the entity, when I got this error:

RenderRegistry.java:12: error: method registerEntityRenderingHandler in class RenderingRegistry cannot be applied to given types;
        RenderingRegistry.registerEntityRenderingHandler(GlowCowEntity.class, new GlowCowRender.RenderFactory());
                         ^
  required: EntityType<T>,IRenderFactory<? super T>
  found: Class<GlowCowEntity>,RenderFactory
  reason: cannot infer type-variable(s) T
    (argument mismatch; Class<GlowCowEntity> cannot be converted to EntityType<T>)
  where T is a type-variable:
    T extends Entity declared in method <T>registerEntityRenderingHandler(EntityType<T>,IRenderFactory<? super T>)

I tried looking at the Minecraft Forge documentation, but it didn’t say anything about mobs, and I couldn’t find any up-to-date information. I’ve used Java a bit for Android apps, but I’m not very experienced with it.

RenderRegistry.java:

import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.api.distmarker.OnlyIn;
import net.minecraftforge.fml.client.registry.RenderingRegistry;

@OnlyIn(Dist.CLIENT)
public class RenderRegistry {
    public static void registerEntityRenderers() {
        RenderingRegistry.registerEntityRenderingHandler(GlowCowEntity.class, new GlowCowRender.RenderFactory());
    }
}

GlowCowEntity.java:

import net.minecraft.entity.EntityType;
import net.minecraft.entity.ai.goal.AvoidEntityGoal;
import net.minecraft.entity.ai.goal.RandomWalkingGoal;
import net.minecraft.entity.ai.goal.SwimGoal;
import net.minecraft.entity.monster.CreeperEntity;
import net.minecraft.entity.passive.CowEntity;
import net.minecraft.world.World;

public class GlowCowEntity extends CowEntity {
    public GlowCowEntity(EntityType<? extends CowEntity> p_i48567_1_, World p_i48567_2_) {
        super((EntityType<? extends CowEntity>) MyEntities.GLOW_COW_ENTITY, p_i48567_2_);
    }

    @Override
    protected void registerGoals() {
        this.goalSelector.addGoal(0, new SwimGoal(this));
        this.goalSelector.addGoal(1, new AvoidEntityGoal<>(this, CreeperEntity.class, 8.0F, 0.5F, 0.5F));
        this.goalSelector.addGoal(2, new RandomWalkingGoal(this, 5));
    }

}

GlowCowRender.java:

import net.minecraft.client.renderer.entity.EntityRenderer;
import net.minecraft.client.renderer.entity.EntityRendererManager;
import net.minecraft.client.renderer.entity.LivingRenderer;
import net.minecraft.util.ResourceLocation;
import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.api.distmarker.OnlyIn;
import net.minecraftforge.fml.client.registry.IRenderFactory;

@OnlyIn(Dist.CLIENT)
public class GlowCowRender extends LivingRenderer<GlowCowEntity, GlowCowModel> {
    public GlowCowRender(EntityRendererManager manager) {
        super(manager, new GlowCowModel(), 0.5F);
    }

    @Override
    public ResourceLocation getTextureLocation(GlowCowEntity p_110775_1_) {
        return new ResourceLocation("textures/entity/glow_cow.png");
    }

    public static class RenderFactory implements IRenderFactory<GlowCowEntity> {

        @Override
        public EntityRenderer<? super GlowCowEntity> createRenderFor(EntityRendererManager manager) {
            return new GlowCowRender(manager);
        }
    }
}

Answer

I eventually managed to get it to work by looking at other mods which work with 1.16, and I replaced

RenderingRegistry.registerEntityRenderingHandler(GlowCowEntity.class, new GlowCowRender.RenderFactory());

with

RenderingRegistry.registerEntityRenderingHandler((EntityType<GlowCowEntity>) MyEntities.GLOW_COW_ENTITY, GlowCowRender::new);

The compiler still produced warnings for the cast to EntityType<GlowCowEntity>, so there is probably a better way to do it, but this still worked.



Source: stackoverflow