0

is it posible to read the "worldInfo" from another class ?

the following is part of the class that holds it:

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

i want to use it in my class to get the name. "worldInfo.getWorldName"

EDIT 1: Ok i created a class in the same package with the World.. "World_Spy.class"

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);
    }

}

But when i call it from the main class it crashes the game..

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

Is it about the parameters?

5 Answers5

0

In order to access worldInfo you'll have to extend World but as worldName is set to the second parameter of World constructor, it means you have to know it in your child class, so ..

0

For the functionality you want, either change the keyword protected to public, or create a public function in the class. It would look something like this:

public String getWorldName(){ this.worldInfo.getWorldName(); }

bdean20
  • 822
  • 8
  • 14
0

The class is abstract, so it cannot be initiated. You can read static variables from this class, but you cannot create object of this class. You can make this variabale as static and then you read it or inherit this class and make object of it or make it non-abstract and make object of it.

This variable holds constant? Make it static.

Pawel
  • 1,457
  • 1
  • 11
  • 22
0

Actually, protected means it can be used by childclasses but also by any other class in the same package. So yes, you can use it by all classes in the same package even if they're not subclassing the abstract class

0

The field can be accessed directly if one of the following is true:

  • That another class extends World (inherited protected fields are visible in derived classes, also World is not final and has non private constructor)
  • That another class belongs to the same package (protected fields are visible in classes from the same package, same as package private).

The field can also be accessed through reflection from any other class after setting accessible property to true on that field (as long as security manager permits).

Community
  • 1
  • 1
Audrius Meškauskas
  • 20,936
  • 12
  • 75
  • 93