Jump to content

[1.10] How can I save world data?


Darki

Recommended Posts

Hi,

I am just working on my mod. I made an algorithm that generates random recipes for something. I want save this recipes in the world data.

I searched for it and found WorldSavedData. I tried it with it but every time the world gets loaded the recipes aren't the same.

Here my Code:

public static List<Combination> combinations;
	private boolean loaded;

	public CombinationManager() {
		combinations = new ArrayList<CombinationManager.Combination>();
		loaded = false;
	}

	private static void generate() {
		List<CacheCombination> cacheCombinations = new ArrayList<CombinationManager.CacheCombination>();
		for (EnumDisease disease : EnumDisease.values()) {
			CacheCombination cacheCombination = generateCombination();
			while (cacheCombinations.contains(cacheCombination)) {
				cacheCombination = generateCombination();
			}
			combinations.add(new Combination(disease, cacheCombination.atom, cacheCombination.capsid,
					cacheCombination.nucleicAcid));
		}
	}

	public void load(World world) {
		if (!loaded) {
			combinations = CombinationData.get(world).combinations;
			for (Combination combination : combinations) {
				System.out.println("COMBI: " + combination.disease.getName() + " = " + combination.atom.toString()
						+ " + " + combination.capsid.toString() + " + " + combination.nucleicAcid.toString());
			}
			loaded = true;
		}
	}

	private static CacheCombination generateCombination() {
		return new CacheCombination(EnumCapsid.values()[new Random().nextInt(EnumCapsid.values().length)],
				EnumAtom.values()[new Random().nextInt(EnumAtom.values().length)],
				EnumNucleicAcid.values()[new Random().nextInt(EnumNucleicAcid.values().length)]);
	}

	public static class CombinationData extends WorldSavedData {

		private static final String DATA_NAME = Diseases.MOD_ID + "_combinationData";
		private List<Combination> combinations;

		public CombinationData() {
			super(DATA_NAME);
		}

		public CombinationData(String name) {
			super(name);
		}
		
		public static CombinationData get(World world) {
			CombinationData data = (CombinationData)world.loadItemData(CombinationData.class, DATA_NAME);
			if (data == null) {
				generate();
				data = new CombinationData();
				data.combinations = CombinationManager.combinations;
				world.setItemData(DATA_NAME, data);
			}
			return data;
	}

		@Override
		public void readFromNBT(NBTTagCompound nbt) {
			for (EnumDisease disease : EnumDisease.values()) {
				if (nbt.hasKey(disease.getTag()))
					combinations.add(new Combination(disease, EnumAtom.valueOf(nbt.getString(disease.getTag())),
							EnumCapsid.valueOf(nbt.getString(disease.getTag())),
							EnumNucleicAcid.valueOf(nbt.getString(disease.getTag()))));
			}
		}
		
		public void setCombinations(List<Combination> combinations) {
			this.combinations = combinations;
		}

		public List<Combination> getCombinations() {
			return combinations;
		}

		@Override
		public NBTTagCompound writeToNBT(NBTTagCompound compound) {
			for (Combination combination : combinations) {
				compound.setString(combination.disease.getTag(), combination.atom.toString() + ";"
						+ combination.capsid.toString() + ";" + combination.nucleicAcid.toString());
			}
			return compound;
		}

	}

 

Link to comment
Share on other sites

How did you associate your class with the actual world or map save data? For your world save data to work I think you have instantiate it and add it to the MapStorage either for the dimension or for the save file. If you want it per dimension then you call the world#perWorldStorage() methods to set your world data into the storage map, if you want it per save file then you call the world#getMapStorage() method to set your world data to that storage map.

 

At least that how I think it works...

Check out my tutorials here: http://jabelarminecraft.blogspot.com/

Link to comment
Share on other sites

Nope still not working:

public static class CombinationData extends WorldSavedData {

		private static final String DATA_NAME = Diseases.MOD_ID + "_combinationData";
		private List<Combination> combinations;

		public CombinationData() {
			super(DATA_NAME);
		}

		public CombinationData(String name) {
			super(name);
		}
		
		public static CombinationData get(World world) {
			MapStorage storage = world.getMapStorage();
			CombinationData data = (CombinationData)storage.getOrLoadData(CombinationData.class, DATA_NAME);
			if (data == null) {
				generate();
				data = new CombinationData();
				data.combinations = CombinationManager.combinations;
				storage.setData(DATA_NAME, data);
			}
			return data;
	}

		@Override
		public void readFromNBT(NBTTagCompound nbt) {
			for (EnumDisease disease : EnumDisease.values()) {
				if (nbt.hasKey(disease.getTag()))
					combinations.add(new Combination(disease, EnumAtom.valueOf(nbt.getString(disease.getTag())),
							EnumCapsid.valueOf(nbt.getString(disease.getTag())),
							EnumNucleicAcid.valueOf(nbt.getString(disease.getTag()))));
			}
		}
		
		public void setCombinations(List<Combination> combinations) {
			this.combinations = combinations;
		}

		public List<Combination> getCombinations() {
			return combinations;
		}

		@Override
		public NBTTagCompound writeToNBT(NBTTagCompound compound) {
			for (Combination combination : combinations) {
				compound.setString(combination.disease.getTag(), combination.atom.toString() + ";"
						+ combination.capsid.toString() + ";" + combination.nucleicAcid.toString());
			}
			return compound;
		}

	}

 

Link to comment
Share on other sites

How do you know it isn't working besides the final effect? To debug things you either need to trace the code in debug mode or you need to add a bunch of console or logger statements to help you understand which parts of your code is executing. I personally put a console statement at the beginning of EVERY method and watch to see that they are all be called as expected. If they are being called as expected I make the console statements more descriptive to indicate the key values at the time of execution, and usually add a few more console statements anywhere there is a branch (if statement) or loop (to monitor the looping).

 

As long as you can see the operation of your code, you can easily debug it yourself.

 

 

Check out my tutorials here: http://jabelarminecraft.blogspot.com/

Link to comment
Share on other sites

I debugged a little bit. Everytime i load the data, its null so that its creating a new data. And i find out that when i save the world an exception will thrown. But not in my code i think:

[16:46:14] [Server thread/INFO] [STDERR]: [net.minecraft.world.storage.MapStorage:saveData:142]: java.lang.IllegalArgumentException: Name and ID cannot both be blank
[16:46:14] [Server thread/INFO] [STDERR]: [net.minecraft.world.storage.MapStorage:saveData:142]: 	at com.mojang.authlib.GameProfile.<init>(GameProfile.java:25)
[16:46:14] [Server thread/INFO] [STDERR]: [net.minecraft.world.storage.MapStorage:saveData:142]: 	at net.minecraft.server.management.PlayerProfileCache.lookupProfile(PlayerProfileCache.java:100)
[16:46:14] [Server thread/INFO] [STDERR]: [net.minecraft.world.storage.MapStorage:saveData:142]: 	at net.minecraft.server.management.PlayerProfileCache.getGameProfileForUsername(PlayerProfileCache.java:183)
[16:46:14] [Server thread/INFO] [STDERR]: [net.minecraft.world.storage.MapStorage:saveData:142]: 	at net.minecraft.village.Village.writeVillageDataToNBT(Village.java:552)
[16:46:14] [Server thread/INFO] [STDERR]: [net.minecraft.world.storage.MapStorage:saveData:142]: 	at net.minecraft.village.VillageCollection.writeToNBT(VillageCollection.java:300)
[16:46:14] [Server thread/INFO] [STDERR]: [net.minecraft.world.storage.MapStorage:saveData:142]: 	at net.minecraft.world.storage.MapStorage.saveData(MapStorage.java:134)
[16:46:14] [Server thread/INFO] [STDERR]: [net.minecraft.world.storage.MapStorage:saveData:142]: 	at net.minecraft.world.storage.MapStorage.saveAllData(MapStorage.java:114)
[16:46:14] [Server thread/INFO] [STDERR]: [net.minecraft.world.storage.MapStorage:saveData:142]: 	at net.minecraft.world.WorldServer.saveLevel(WorldServer.java:1116)
[16:46:14] [Server thread/INFO] [STDERR]: [net.minecraft.world.storage.MapStorage:saveData:142]: 	at net.minecraft.world.WorldServer.saveAllChunks(WorldServer.java:1057)
[16:46:14] [Server thread/INFO] [STDERR]: [net.minecraft.world.storage.MapStorage:saveData:142]: 	at net.minecraft.server.MinecraftServer.saveAllWorlds(MinecraftServer.java:415)
[16:46:14] [Server thread/INFO] [STDERR]: [net.minecraft.world.storage.MapStorage:saveData:142]: 	at net.minecraft.server.integrated.IntegratedServer.saveAllWorlds(IntegratedServer.java:238)
[16:46:14] [Server thread/INFO] [STDERR]: [net.minecraft.world.storage.MapStorage:saveData:142]: 	at net.minecraft.server.integrated.IntegratedServer.tick(IntegratedServer.java:141)
[16:46:14] [Server thread/INFO] [STDERR]: [net.minecraft.world.storage.MapStorage:saveData:142]: 	at net.minecraft.server.MinecraftServer.run(MinecraftServer.java:537)
[16:46:14] [Server thread/INFO] [STDERR]: [net.minecraft.world.storage.MapStorage:saveData:142]: 	at java.lang.Thread.run(Unknown Source)

Maybe I call the method at the wrong time in the wrong place. The load method will be called when an entity joins the world especially the player. Where else should i call the method?

If you need the code where I call the method:

@SubscribeEvent
public void onJoinWorld(EntityJoinWorldEvent e) {
	Diseases.instance.combinationManager.load(e.getWorld());
}

 

Link to comment
Share on other sites

I don't think you should have to do any loading or saving. Rather you should just set and get it from the world storage and it will load and save when needed. Since it is world data you don't need to do anything when entity joins world, rather it will already be loaded when the world is loaded. You probably need to set it when the world is first created though, there is the WorldEvent.Load event that is appropriate for that. And generally you'd also set it anytime the information changes, although in your case I think you only generate it one time, right?

 

So I think just do it in the WorldEvent.Load check if the data already is there and if not generate and set it.

Check out my tutorials here: http://jabelarminecraft.blogspot.com/

Link to comment
Share on other sites

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.



×
×
  • Create New...

Important Information

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