0

Basicly i want to get the name of the map that is played from the "World.class", in a string on my main mod class...

    public abstract class World implements IBlockAccess{
    protected WorldInfo worldInfo;
    //=====OtherStuff=====
    public World(ISaveHandler par1ISaveHandler, String par2Str, WorldSettings par3WorldSettings, WorldProvider par4WorldProvider, Profiler par5Profiler, ILogAgent par6ILogAgent)
        {
        this.worldInfo.setWorldName(par2Str);
        }
    //=====OtherStuff=====
}

i created a class in the same package with this one

public class World_Spy extends World{

    public World_Spy(ISaveHandler par1iSaveHandler, String par2Str,
            WorldProvider par3WorldProvider, WorldSettings par4WorldSettings,
            Profiler par5Profiler, ILogAgent par6iLogAgent) {
        super(par1iSaveHandler, par2Str, par3WorldProvider, par4WorldSettings,
                par5Profiler, par6iLogAgent);
    }

    @Override
    protected IChunkProvider createChunkProvider() {
        return null;
    }

    @Override
    public Entity getEntityByID(int i) {
        return null;
    }


    String TheName = "";
    public void gotIt(){
        TheName = this.worldInfo.getWorldName();
        System.out.println(TheName);
    }

}

and i call it from my main class with:

World_Spy WName = new World_Spy(null, null, null, null, null, null);

but it chrashes on startup... any ideas?

4

3 回答 3

1

World 不是静态类......您需要一个 World 兼容对象的实例来获取名称。获取 World 实例和名称的一种方法:

World world = Minecraft.getMinecraft().isIntegratedServerRunning() ? mc.getIntegratedServer().worldServerForDimension(Minecraft.getMinecraft().thePlayer.dimension) : Minecraft.getMinecraft().theWorld;
String worldName = world.getWorldInfo().getWorldName();

此代码应该在客户端工作。

于 2013-09-30T17:05:29.413 回答
0

你还没有初始化worldInfo

protected WorldInfo worldInfo; // initialization MISSING!

因此,当您尝试实例化World_Spywhich 进而调用其父类构造函数World()时,您会得到一个NullPointerExceptionat

this.worldInfo.setWorldName(par2Str); // NullPointerException here

要解决此问题,只需提供一个实例

protected WorldInfo worldInfo = new WorldInfo();
于 2013-08-25T13:29:20.107 回答
0

我相信它NullPointerException在这里抛出“崩溃”:

this.worldInfo.setWorldName(par2Str);

事实上,变量worldInfo从未被您尝试调用它的方法初始化setWorldName()。由于变量null在这一点上抛出NullPointerException听起来很合理。

在java中(就像我知道的所有其他编程语言一样)你必须在使用它之前初始化变量。实际上,原始类型默认使用某种“正常”值进行初始化。然而,自定义类型的变量被初始化为null可能会使初学者感到困惑。

要初始化,您必须在new构造函数调用之后使用关键字:

worldInfo = new WorldInfo();

现在你可以调用 setter 和其他方法了worldInfo

于 2013-08-25T13:29:33.603 回答