使用NeoForge开发Minecraft模组时物品注册致游戏加载崩溃
NeoForge模组开发游戏崩溃问题排查
我正在使用NeoForge开发Minecraft模组,跟随一套视频教程学习。添加ModItems.register(modEventBus);代码后,游戏加载时出现崩溃错误。主类代码如下:
package net.ohusq.emeraldutils; import net.minecraft.world.item.CreativeModeTabs; import net.ohusq.emeraldutils.item.ModItems; import org.slf4j.Logger; import com.mojang.logging.LogUtils; import net.neoforged.api.distmarker.Dist; import net.neoforged.bus.api.IEventBus; import net.neoforged.bus.api.SubscribeEvent; import net.neoforged.fml.ModContainer; import net.neoforged.fml.common.EventBusSubscriber; import net.neoforged.fml.common.Mod; import net.neoforged.fml.config.ModConfig; import net.neoforged.fml.event.lifecycle.FMLClientSetupEvent; import net.neoforged.fml.event.lifecycle.FMLCommonSetupEvent; import net.neoforged.neoforge.common.NeoForge; import net.neoforged.neoforge.event.BuildCreativeModeTabContentsEvent; import net.neoforged.neoforge.event.server.ServerStartingEvent; // The value here should match an entry in the META-INF/neoforge.mods.toml file @Mod(EmeraldUtils.MOD_ID) public class EmeraldUtils { public static final String MOD_ID = "emeraldutils"; private static final Logger LOGGER = LogUtils.getLogger(); // The constructor for the mod class is the first code that is run when your mod is loaded. // FML will recognize some parameter types like IEventBus or ModContainer and pass them in automatically. public EmeraldUtils(IEventBus modEventBus, ModContainer modContainer) { // Register the commonSetup method for modloading modEventBus.addListener(this::commonSetup); // Register ourselves for server and other game events we are interested in. // Note that this is necessary if and only if we want *this* class (ExampleMod) to respond directly to events. // Do not add this line if there are no @SubscribeEvent-annotated functions in this class, like onServerStarting() below. NeoForge.EVENT_BUS.register(this); ModItems.register(modEventBus); // 引发崩溃的代码行 // Register the item to a creative tab modEventBus.addListener(this::addCreative); // Register our mod's ModConfigSpec so that FML can create and load the config file for us modContainer.registerConfig(ModConfig.Type.COMMON, Config.SPEC); } private void commonSetup(final FMLCommonSetupEvent event) { } // Add the example block item to the building blocks tab private void addCreative(BuildCreativeModeTabContentsEvent event) { if(event.getTabKey() == CreativeModeTabs.INGREDIENTS) { event.accept(ModItems.BISMUTH); } } // You can use SubscribeEvent and let the Event Bus discover methods to call @SubscribeEvent public void onServerStarting(ServerStartingEvent event) { } // You can use EventBusSubscriber to automatically register all static methods in the class annotated with @SubscribeEvent @EventBusSubscriber(modid = MOD_ID, bus = EventBusSubscriber.Bus.MOD, value = Dist.CLIENT) public static class ClientModEvents { @SubscribeEvent public static void onClientSetup(FMLClientSetupEvent event) { } } }
我的代码仓库为GitHub上的NeoForge-Tutorial项目,已尝试移除部分代码定位问题,但仍未解决。
可能的崩溃原因
- ModItems注册逻辑错误:检查
ModItems.register方法实现,常见问题包括:DeferredRegister未正确关联模组ID,或注册时未调用register(modEventBus)- 物品实例创建时参数错误(如未正确初始化
Item.Properties)
- 注册表名称不规范:Minecraft要求注册表名称只能包含小写字母、数字、下划线或连字符,检查
BISMUTH等物品的注册名是否符合要求 - 静态初始化异常:
ModItems类的静态代码块或静态字段初始化时出错,导致类加载失败 - 版本兼容问题:使用的NeoForge版本与教程版本不一致,API调用方式存在差异
排查建议
- 检查
ModItems类的正确实现,标准的物品注册模板如下:
package net.ohusq.emeraldutils.item; import net.minecraft.world.item.Item; import net.neoforged.bus.api.IEventBus; import net.neoforged.neoforge.registries.DeferredRegister; import net.neoforged.neoforge.registries.ForgeRegistries; import net.ohusq.emeraldutils.EmeraldUtils; public class ModItems { // 初始化物品注册表,关联模组ID public static final DeferredRegister<Item> ITEMS = DeferredRegister.create(ForgeRegistries.ITEMS, EmeraldUtils.MOD_ID); // 注册物品,注册表名称为"bismuth" public static final DeferredRegister.RegisteredObject<Item> BISMUTH = ITEMS.register("bismuth", () -> new Item(new Item.Properties())); // 将注册表注册到模组事件总线 public static void register(IEventBus modEventBus) { ITEMS.register(modEventBus); } }
- 查看完整的崩溃堆栈日志(运行日志中会有详细信息),定位具体的异常类型和代码行
- 确认NeoForge版本与教程一致,避免API变更导致的调用错误
- 检查
META-INF/neoforge.mods.toml文件,确保模组ID与代码中的MOD_ID完全一致
内容的提问来源于stack exchange,提问作者Mjennow
相关产品推荐
相关产品推荐

