Posted March 13, 20169 yr So the goal I have is to have a file that is instantiated in memory while a particular world is running. It will hold data that other pieces of the mod need to reference and change, no matter what chunks are loaded. I want it to write to NBT when the world is closed and read from NBT when the world is opened. It seems like this is thoroughly doable with capabilities, but I'm getting lost in the exact implementation. Do I need to register it as a new capability with CapabilityManager, or can I use ICapabilitySerializable<NBTTagCompound>? If so, how do I handle the hasCapability and getCapability functions? Here the very small amount of code I have so far: import net.minecraft.nbt.NBTTagCompound; @SuppressWarnings("rawtypes") public class VillageTracker implements net.minecraftforge.common.capabilities.ICapabilitySerializable<NBTTagCompound> { public int test; public VillageTracker() { } private void readFromNBT(NBTTagCompound nbt) { if(nbt.hasKey("testInt")) test = nbt.getInteger("testInt"); } private void writeToNBT(NBTTagCompound nbt) { nbt.setInteger("testInt", test); } @Override public boolean hasCapability(net.minecraftforge.common.capabilities.Capability<?> capability, net.minecraft.util.EnumFacing facing) { } @Override public <T> T getCapability(net.minecraftforge.common.capabilities.Capability<T> capability, net.minecraft.util.EnumFacing facing) { } @Override public void deserializeNBT(NBTTagCompound nbt) { this.readFromNBT(nbt); } @Override public NBTTagCompound serializeNBT() { NBTTagCompound nbt = new NBTTagCompound(); this.writeToNBT(nbt); return nbt; } } Check out my Mod: The RPCraft Toolkit!
March 13, 20169 yr Author Well, it doesn't necessarily need to be attached to the world, just saved and read in as NBT. Its going to be values like a player's reputation with different factions, and those faction's relationship with each other, which should be visible (and editable) no matter which Faction Base Block is actually loaded. Is WorldSavedData the best way to implement that? This is deeper than I have yet dived into Forge, so I'm not even sure which classes to check out. Check out my Mod: The RPCraft Toolkit!
March 13, 20169 yr You can read and write NBT files outside of the Capabilities and WorldSavedData system. It's a file like any other, run through CompressedStreamTools, IIRC. 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.
March 13, 20169 yr All you need is a file to manage the alterations to the data and save/load that information using regular java io. I use this during preInit to get the file location for my extra information event.getModConfigurationDirectory().toString() +"\\LightDrafter\\Draftables.json" which is just a custom folder inside of the config colder. The biggest thing to remember is that the instance of this class will be different on server side and client side, so you will have to manage updates between them using IMessage and IMessageHandler, or make sure that you only create an instance when you are server side. Current Project: Armerger Planned mods: Light Drafter | Ore Swords Looking for help getting a mod off the ground? Coding | Textures
March 13, 20169 yr Author Well I went from not knowing how to do it to having three different options. Thank you guys Check out my Mod: The RPCraft Toolkit!
March 13, 20169 yr If your classes are only using simple types: public class example{ String string; int val; Helper help; } public class Helper{ String info } you can use Example ex = new Example(); String json = GsonBuilder.prettyPrinting().create().toJson(ex. Example.class); and it will give you JSON as a string that you can then write to a file. Reading from the file is just as easy. String json = //from file Example ex = GsonBuilder.prettyPrinting().create().fromJson(json, Example.class); Current Project: Armerger Planned mods: Light Drafter | Ore Swords Looking for help getting a mod off the ground? Coding | Textures
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.