我正在学习如何使用java并尝试构建一个minecraft mod。
问题是我在使用1.8项纹理添加系统时遇到了麻烦,即使谷歌是我最好的朋友,我仍然没有找到我应该做些什么来修复这个NPE。
这是Base 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"));
答案 0 :(得分:2)
这家伙解释说你必须在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"));
}
}
无论如何,谢谢你们。
答案 1 :(得分:0)
我相信你的问题是ModelResourceLocation
实例化中MODNAME和MODID之间的区别。第一个参数需要在"之前使用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
}