Jump to content

Recommended Posts

Posted

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

	}

 

Posted

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/

Posted

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

	}

 

Posted

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/

Posted

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

 

Posted

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/

Posted

Thanks. Cause of your tips I could improve it. But that wasnt the probleme. I figured it out and honestly i am an idiot xD . I forgot to call markDirty() after generating my data^^

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



  • Recently Browsing

    • No registered users viewing this page.
  • Posts

    • logs too big for one pastebin https://pastebin.com/ZjUGHu3u  https://pastebin.com/RqCUZf3X  https://pastebin.com/6ZPS99nD
    • You probably used jd-gui to open it, didn't you? Nothing wrong with that, I also made that mistake, except that Notch was a smart guy and he obfuscated the code. That's why you only see files called "a", "b", "c" and then a file that combines them all. As I said, use RetroMCP to deobfuscate the code so that you will 100% understand it and be able to navigate it.
    • Decompiling minecraft indev, infdev, alpha, beta or whichever legacy version is really easy. I'm not a plug, I just also got interested in modding legacy versions (Infdev to be specific). Use https://github.com/MCPHackers/RetroMCP-Java Once you install their client and the Zulu Architecture that they say they recommend (or use your own Java). I encountered some problems, so I run it with: "java -jar RetroMCP-Java-CLI.jar". You should run it in a seperate folder (not in downloads), otherwise the files and folders will go all over the place. How to use RetroMCP: Type setup (every time you want change version), copy-paste the version number from their list (they support indev), write "decompile" and done! The code will now be deobfuscated and filenames will be normal, instead of "a", "b" and "c"! Hope I helped you, but I don't expect you to reply, as this discussion is 9 years old! What a piece of history!  
    • I know that this may be a basic question, but I am very new to modding. I am trying to have it so that I can create modified Vanilla loot tables that use a custom enchantment as a condition (i.e. enchantment present = item). However, I am having trouble trying to implement this; the LootItemRandomChanceWithEnchantedBonusCondition constructor needs a Holder<Enchantment> and I am unable to use the getOrThrow() method on the custom enchantment declared in my mod's enchantments class. Here is what I have so far in the GLM:   protected void start(HolderLookup.Provider registries) { HolderLookup.RegistryLookup<Enchantment> registrylookup = registries.lookupOrThrow(Registries.ENCHANTMENT); LootItemRandomChanceWithEnchantedBonusCondition lootItemRandomChanceWithEnchantedBonusCondition = new LootItemRandomChanceWithEnchantedBonusCondition(0.0f, LevelBasedValue.perLevel(0.07f), registrylookup.getOrThrow(*enchantment here*)); this.add("nebu_from_deepslate", new AddItemModifier(new LootItemCondition[]{ LootItemBlockStatePropertyCondition.hasBlockStateProperties(Blocks.DEEPSLATE).build(), LootItemRandomChanceCondition.randomChance(0.25f).build(), lootItemRandomChanceWithEnchantedBonusCondition }, OrichalcumItems.NEBU.get())); }   Inserting Enchantments.[vanilla enchantment here] actually works but trying to declare an enchantment from my custom enchantments class as [mod enchantment class].[custom enchantment] does not work even though they are both a ResourceKey and are registered in Registries.ENCHANTMENT. Basically, how would I go about making it so that a custom enchantment declared as a ResourceKey<Enchantment> of value ResourceKey.create(Registries.ENCHANTMENT, ResourceLocation.fromNamespaceAndPath([modid], [name])), declared in a seperate enchantments class, can be used in the LootItemRandomChanceWithEnchantedBonusCondition constructor as a Holder? I can't use getOrThrow() because there is no level or block entity/entity in the start() method and it is running as datagen. It's driving me nuts.
  • Topics

×
×
  • Create New...

Important Information

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