Jump to content

Recommended Posts

Posted

Every time a new world/game is created I am looking to generate a set of values ( based on random numbers) and then apply those to the world as it generates. If I call the method to generate these values in any of the FML events, pre init/post init etc means that every time the game is quit, a new set of random values is generating.. thus changing the state of the world every time you quit and re enter.

 

How would i save this set of values for every time a new world is created?

Posted

worldSaveData

Apparently I'm a complete and utter jerk and come to this forum just like to make fun of people, be confrontational, and make your personal life miserable.  If you think this is the case, JUST REPORT ME.  Otherwise you're just going to get reported when you reply to my posts and point it out, because odds are, I was trying to be nice.

 

Exception: If you do not understand Java, I WILL NOT HELP YOU and your thread will get locked.

 

DO NOT PM ME WITH PROBLEMS. No help will be given.

Posted

Ok, I have given it a go, searching through many different threads on the forums to try to understand the save data and writing to nbt/reading from it. I'm not really too familiar with these.

 

Im using 1.8 so hence why perMapStorage has changed to a getter. I am getting a null pointer error in the WorldIslandData get method. If anyone could help it would be greatly appreciated and offer some sort of explanation. Thanks.

 

 

 

Extended World Save Data Class

public class WorldIslandData extends WorldSavedData {

private static final String IDENTIFIER = "islanddata";
public static Random rand = new Random();
public  int[] RandomNameNumber = new int[]{rand.nextInt(5),rand.nextInt(5),rand.nextInt(5),rand.nextInt(5)};




public WorldIslandData() {
	super(IDENTIFIER);
	}

	public WorldIslandData(String identifier) {
	super(identifier);
	}

@Override
public void readFromNBT(NBTTagCompound nbt) {

	for(int i = 0; i < RandomNameNumber.length; i++){
		RandomNameNumber[i] = nbt.getInteger("namenum"+i);
	}
}

@Override
public void writeToNBT(NBTTagCompound nbt) {
	for(int i = 0; i < RandomNameNumber.length; i++){
		nbt.setInteger("namenum" + i, i);
	}
}


	public static WorldIslandData get(World world) {
		MapStorage storage = world.getMapStorage();
		WorldIslandData data = (WorldIslandData)storage.loadData(WorldIslandData.class, IDENTIFIER);
		if (data == null)
		{
			data = new WorldIslandData();
			storage.setData(IDENTIFIER, data);
		}

		return data;
}

}

 

 

 

Output Class

public class IslandHolder extends BiomeGenBase {

protected static World worldObj;


public IslandHolder(int par1) {
	super(par1);
	// TODO Auto-generated constructor stub
}

public static void declareBiomes()
{


	WorldIslandData data = WorldIslandData.get(worldObj);

for (int i = 1; i<5; ++i)
{

BiomeGenBase island = (new IslandGen(i).setBiomeName(String.valueOf(i) + " Rand " + String.valueOf(data.RandomNameNumber[i-1]))); 

}
}
}

Posted

a) In your Biome class you just created a "worldObj" field. What do you expect the value of this to be? Java will not fill in your fields magically with the right values. You need to actually use a World that's passed to you. Remove that field.

 

b) Your writeToNBT method is broken, it does not actualyl save the random numbers. Look closely.

 

Thanks diesieben07. Some oversights there from me.

 

1. I removed that. Stupid me. I then passed the world into the method and then called it in my custom chunk provider. This all seems to work.

 

public static void declareBiomes(World world)
{


	WorldIslandData data = WorldIslandData.get(world);

	for (int i = 1; i<5; ++i)
	{

	BiomeGenBase island = (new IslandGen(i).setBiomeName(String.valueOf(i) + " Rand " + String.valueOf(data.RandomNameNumber[i-1]))); 

}

 

 

and showing it from the

 

CustomChunkProviderGenerate

 

public Chunk provideChunk(int x, int z)
    {
        this.rand.setSeed((long)x * 341873128712L + (long)z * 132897987541L);
        IslandHolder.declareBiomes(worldObj);
        ChunkPrimer chunkprimer = new ChunkPrimer();

 

 

2. Fixed it. I think?

 

public class WorldIslandData extends WorldSavedData {

private static final String IDENTIFIER = "islanddata";
public static Random rand = new Random();
public  int[] RandomNameNumber = new int[]{rand.nextInt(5),rand.nextInt(5),rand.nextInt(5),rand.nextInt(5)};




public WorldIslandData() {
	super(IDENTIFIER);
	}


@Override
public void readFromNBT(NBTTagCompound nbt) {

	for(int i = 0; i < RandomNameNumber.length; i++){
		RandomNameNumber[i] = nbt.getInteger("namenum"+i);
	}
}

@Override
public void writeToNBT(NBTTagCompound nbt) {
	for(int i = 0; i < RandomNameNumber.length; i++){
		nbt.setInteger("namenum" + i, RandomNameNumber[i]);
	}
}


	public static WorldIslandData get(World world) {
		MapStorage storage = world.getMapStorage();
		WorldIslandData data = (WorldIslandData)storage.loadData(WorldIslandData.class, IDENTIFIER);
		if (data == null)
		{
			data = new WorldIslandData();
			storage.setData(IDENTIFIER, data);
		}

		return data;
}

}

 

 

Ok so its no longer giving a NPE. Right now it seems it can read values from the NBT but it still does not save it when you close the world and re-open? Im assuming there must be something wrong in my logic?

Posted

My guess is that the

BiomeGenBase island = ... 

code is running prior to the readFromNBT method.

Apparently I'm a complete and utter jerk and come to this forum just like to make fun of people, be confrontational, and make your personal life miserable.  If you think this is the case, JUST REPORT ME.  Otherwise you're just going to get reported when you reply to my posts and point it out, because odds are, I was trying to be nice.

 

Exception: If you do not understand Java, I WILL NOT HELP YOU and your thread will get locked.

 

DO NOT PM ME WITH PROBLEMS. No help will be given.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Announcements



×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.