-2

我正在学习如何使用 java 并尝试构建一个 minecraft mod。

问题是我在使用 1.8 项目纹理添加系统时遇到了问题,即使 Google 是我最好的朋友,我仍然没有找到我应该做些什么来修复这个 NPE。

这是基本 Mod 类:

package rocha.sekai;

import net.minecraftforge.fml.common.Mod;
import net.minecraftforge.fml.common.Mod.EventHandler;
import net.minecraftforge.fml.common.Mod.Instance;
import net.minecraft.item.Item;
import net.minecraft.client.Minecraft;
import net.minecraft.client.renderer.entity.RenderItem;
import net.minecraft.client.resources.model.ModelResourceLocation;
import net.minecraft.creativetab.CreativeTabs;
import net.minecraftforge.fml.common.event.FMLInitializationEvent;
import net.minecraftforge.fml.common.event.FMLPostInitializationEvent;
import net.minecraftforge.fml.common.event.FMLPreInitializationEvent;
import net.minecraftforge.fml.relauncher.Side;

@Mod(modid=sekai.MODID, name=sekai.MODNAME, version=sekai.MODVER)
public class sekai{

    //items variable
    public static Item testIngot;


    //Set the ID of the mod (Should be lower case).
    public static final String MODID = "sekairocha";
    //Set the "Name" of the mod.
    public static final String MODNAME = "sekai";
    //Set the version of the mod.
    public static final String MODVER = "0.0.1";

    @Instance(value = sekai.MODID) //Tell Forge what instance to use.
    public static sekai instance;

    @EventHandler
    public void preInit(FMLPreInitializationEvent event){
        testIngot = new testItem();
        if(event.getSide() == Side.CLIENT){
            RenderItem renderItem = Minecraft.getMinecraft().getRenderItem();
  line 41-> renderItem.getItemModelMesher().register(testIngot, 0, new ModelResourceLocation("sekai:" + ((testItem) testIngot).getName(), "inventory"));
        }

}


    @EventHandler
    public void load(FMLInitializationEvent event)
    {

    }

    @EventHandler
    public void postInit(FMLPostInitializationEvent event)
    {
    }
}

这是项目类:

package rocha.sekai;

import net.minecraft.item.Item;
import net.minecraft.creativetab.CreativeTabs;
import net.minecraftforge.fml.common.registry.GameRegistry;

public class testItem extends Item{

    private final String name = "testIngot";

    public testItem(){
        GameRegistry.registerItem(this, name);
        maxStackSize = 64;
        setCreativeTab(CreativeTabs.tabMisc);
        setUnlocalizedName("testIngot");

    }

    public String getName(){
        return name;
    }
}

这是错误报告:

java.lang.NullPointerException: Initializing game
    at rocha.sekai.sekai.preInit(sekai.java:41)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at net.minecraftforge.fml.common.FMLModContainer.handleModStateEvent(FMLModContainer.java:536)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at com.google.common.eventbus.EventSubscriber.handleEvent(EventSubscriber.java:74)
    at com.google.common.eventbus.SynchronizedEventSubscriber.handleEvent(SynchronizedEventSubscriber.java:47)

第 41 行在 Base Mod 类中用“第 41-> 行”标记在代码之前。我也会在这里转录:

renderItem.getItemModelMesher().register(testIngot, 0, new ModelResourceLocation("sekai:" + ((testItem) testIngot).getName(), "inventory"));
4

2 回答 2

2

http://www.minecraftforum.net/forums/mapping-and-modding/minecraft-mods/modification-development/2272349-lessons-from-my-first-mc-1-8-mod

这家伙解释说你必须在 init 方法中注册项目渲染。我做到了,它奏效了。

我所要做的就是将渲染部分移动到 init 方法。结果在基础 Mod 类中是这样的:

public void load(FMLInitializationEvent event){
        if(event.getSide() == Side.CLIENT)
        {
            RenderItem renderItem = Minecraft.getMinecraft().getRenderItem();
            renderItem.getItemModelMesher().register(testIngot, 0, new ModelResourceLocation(sekai.MODID + ":" + ((testItem) testIngot).getName(), "inventory"));
        }
    }

无论如何,谢谢你们。

于 2015-05-06T20:48:51.203 回答
0

我相信您的问题是 MODNAME 和 MODID 在ModelResourceLocation. 第一个参数需要“:”之前的 MODID,您使用的是 MODNAME。尝试在您的基础 mod 类中更改此行:

renderItem.getItemModelMesher().register(testIngot, 0, new ModelResourceLocation("sekai:" + ((testItem) testIngot).getName(), "inventory"));

对此:

renderItem.getItemModelMesher().register(testIngot, 0, new ModelResourceLocation("sekairocha:" + ((testItem) testIngot).getName(), "inventory"));

甚至更好:

renderItem.getItemModelMesher().register(testIngot, 0, new ModelResourceLocation(sekai.MODID + ":" + ((testItem) testIngot).getName(), "inventory"));

除了该修复之外,我认为您的项目类别可能需要一些更改。将您的构造函数更改为:

public testItem(){
    super(); //missing initializations from Item class
    maxStackSize = 64;
    this.setCreativeTab(CreativeTabs.tabMisc);
    this.setUnlocalizedName("testIngot");
    GameRegistry.registerItem(this, name); //do this last
}
于 2015-05-06T19:59:05.093 回答