You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Minecraft Forge 1.16.5 实体注册后生成提示Unknown entity错误求助

修复方案

以下是你代码中存在的问题和对应的修改方法:

1. 注册逻辑冲突+DeferredRegister使用错误

你同时使用了DeferredRegister和手动RegistryEvent.Register两种实体注册方式,存在冲突,且DeferredRegister的使用方法不正确,导致实体ID没有正确注册到游戏中。

修改AllEntities.java:

package net.themcking.serverutils.entity;
import static net.themcking.serverutils.ServerUtils.MOD_ID;
import net.minecraft.entity.EntityClassification;
import net.minecraft.entity.EntityType;
import net.minecraftforge.registries.DeferredRegister;
import net.minecraftforge.registries.ForgeRegistries;
import net.minecraftforge.registries.RegistryObject;

public class AllEntities {
    public static final DeferredRegister<EntityType<?>> ENTITY_TYPES = DeferredRegister.create(ForgeRegistries.ENTITIES, MOD_ID);
    
    public static final RegistryObject<EntityType<NpcEntity>> NPC = ENTITY_TYPES.register("npc", 
        () -> EntityType.Builder.of(NpcEntity::new, EntityClassification.CREATURE)
                .sized(0.6F, 1.8F)
                .build(MOD_ID + ":npc")
    );
}

修改ServerUtils.java:

删除类中整个RegistryEvents静态内部类,你已经调用了AllEntities.ENTITY_TYPES.register(bus)完成注册,不需要额外手动写注册事件。

2. 实体属性未注册

LivingEntity的子类必须注册实体属性,否则实体无法正常生成。

修改NpcEntity.java:

在类中添加属性构建方法:

import net.minecraft.entity.ai.attributes.AttributeModifierMap;
import net.minecraft.entity.ai.attributes.Attributes;

// 其他原有代码不变
public static AttributeModifierMap.MutableAttribute createAttributes() {
    return LivingEntity.createLivingAttributes()
            .add(Attributes.MAX_HEALTH, 20.0D)
            .add(Attributes.FOLLOW_RANGE, 32.0D)
            .add(Attributes.MOVEMENT_SPEED, 0.3D);
}

修改ServerUtils.java:

添加属性注册事件监听:

public ServerUtils() {
    IEventBus bus = FMLJavaModLoadingContext.get().getModEventBus();
    bus.addListener(this::setup);
    bus.addListener(this::clientSetup);
    // 新增属性注册事件监听
    bus.addListener(this::onEntityAttributeCreation);

    AllEntities.ENTITY_TYPES.register(bus);
    MinecraftForge.EVENT_BUS.register(this);
}

// 新增属性注册处理方法
private void onEntityAttributeCreation(final EntityAttributeCreationEvent event) {
    event.put(AllEntities.NPC.get(), NpcEntity.createAttributes().build());
}

3. NpcEntity构造方法写法错误

现有构造方法硬编码了实体类型,不符合规范,修改为:

public NpcEntity(EntityType<? extends NpcEntity> type, World worldin) {
    super(type, worldin);
}

修改后可删除原有的@SuppressWarnings("unchecked")注解。

4. 相关调用修改

因为现在实体类型用RegistryObject包装,所有调用AllEntities.NPC的地方都要改为AllEntities.NPC.get(),以clientSetup方法为例:

private void clientSetup(final FMLClientSetupEvent event) {
    RenderingRegistry.registerEntityRenderingHandler(AllEntities.NPC.get(), NpcRenderer2::new);
}

修改后可删除原有的@SuppressWarnings("unchecked")注解。

5. 语法错误修复

你贴出的ServerUtils.java代码末尾多了一个多余的闭合大括号},请删除,否则会编译失败。

所有修改完成后重新构建模组,serverutils:npc实体就可以正常生成了。

内容的提问来源于stack exchange,提问作者TheMcKing_

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.05 22:30:05