Posted June 26, 201411 yr Hey, Is it possible to use the NBT for saving cutom values? So for example when I close minecraft it saves a String to the NBT and then I can read it to a variable next time I open Minecraft? (I'm not too sure if that's the actual usage of NBT) If the NBT is completly wrong for that how else can I save a String?
June 26, 201411 yr Author Do you want to store additional data into the world? Exact, how can I do this?
June 26, 201411 yr Author I want same data for all dimensions, so I'm going to use this? world.mapStorage.setData("MyData", ); world.mapStorage.loadData(, "MyData"); So, this requires 2 parameters, The first one is I guess the ID!? and the second one the WorldSaveData, but im not too sure what i do have to put there? (just want to save 1 string); And do I have to make a class like in the example too? (don't really know what I would do with it)
June 26, 201411 yr Author Sooo, I was trying around a bit.. (at least not gettin any errors ) SaveStuffThing saveData = new SaveStuffThing(""); NBTTagCompound nbt = new NBTTagCompound(); nbt.setString("MyString", "Whatever"); saveData.writeToNBT(nbt); world.mapStorage.setData("MyData", saveData); and my class public class SaveStuffThing extends WorldSavedData{ public SaveStuffThing(String mapName) { super(mapName); } @Override public void readFromNBT(NBTTagCompound var1) { } @Override public void writeToNBT(NBTTagCompound var1) { } } What's the mapName doing? It's never getting used in SaveWorldData.. Is this going in the right direction? How can I load the String?
June 26, 201411 yr Author So, I changed it to: public class SaveStuffThing extends WorldSavedData{ String myString; public SaveStuffThing(String mapName) { super(mapName); } @Override public void readFromNBT(NBTTagCompound nbt) { this.myString = nbt.getString("anything"); } @Override public void writeToNBT(NBTTagCompound nbt) { nbt.setString("anything", this.myString); } public String getMyString() { return this.myString; } public void setMyString(String myString) { this.myString = myString; } } The loading: SaveStuffThing saveData = new SaveStuffThing("ModID"); world.mapStorage.setData("MyData", saveData); saveData = (SaveStuffThing) world.mapStorage.loadData(SaveStuffThing.class, "MyData"); System.out.print(saveData.getMyString()); And saving: SaveStuffThing saveData = new SaveStuffThing("ModID"); saveData.setMyString("HeyYoThatsMyString"); world.mapStorage.setData("MyData", saveData); but I only get nullnull in the Console
June 26, 201411 yr Author So, I found the world.mapStorage.setData("MyData", saveData); and removed it. My bad When i now do the saving and then the loading It says HeyYoThatsMyString, but when i do the saving, close and open minecraft again, then do the loading, it just crashes?
June 26, 201411 yr Author java.lang.NullPointerException: Unexpected error and why is my code broken?
June 27, 201411 yr Author I'v put a getter and setter in the WordSavedData public class SaveStuffThing extends WorldSavedData{ private static final String IDENTIFIER = "MyModId"; String myString; public SaveStuffThing() { super(IDENTIFIER); } @Override public void readFromNBT(NBTTagCompound nbt) { // this.myString = nbt.getString("anything"); } @Override public void writeToNBT(NBTTagCompound nbt) { nbt.setString("anything", this.myString); } public String getMyString() { return this.myString; } public void setMyString(String myString) { this.myString = myString; } public static SaveStuffThing get(World world) { SaveStuffThing data = (SaveStuffThing)world.mapStorage.loadData(SaveStuffThing.class, IDENTIFIER); if (data == null) { data = new SaveStuffThing(); world.mapStorage.setData(IDENTIFIER, data); } return data; } public static void set(World world, SaveStuffThing saveData) { world.mapStorage.setData("MyModId", saveData); } } The if (data == null) seems to resolve the NullPointerException, (it now returns null, wich is still not what i want, but at least not a crash ^^) I uncommented the line in readFromNBT and nothing changes (still gettin my String, but null after reconnecting) so I think the error might be there..
June 27, 201411 yr Author Added it but still not working correctly I feel like my code is complete bullshit, but I don't know how to do it otherwise: SaveStuffThing saveData = new SaveStuffThing(); saveData.setMyString("HeyYoThatsMyString"); SaveStuffThing.set(world, saveData); } SaveStuffThing saveData = SaveStuffThing.get(world); System.out.print(saveData.getMyString()); } public class SaveStuffThing extends WorldSavedData{ private static final String IDENTIFIER = "MyModId"; String myString; public SaveStuffThing() { super(IDENTIFIER); } @Override public void readFromNBT(NBTTagCompound nbt) { this.myString = nbt.getString("anything"); } @Override public void writeToNBT(NBTTagCompound nbt) { nbt.setString("anything", this.myString); } public String getMyString() { return this.myString; } public void setMyString(String myString) { this.myString = myString; this.markDirty(); } public static SaveStuffThing get(World world) { SaveStuffThing data = (SaveStuffThing)world.mapStorage.loadData(SaveStuffThing.class, IDENTIFIER); if (data == null) { data = new SaveStuffThing(); world.mapStorage.setData(IDENTIFIER, data); } return data; } public static void set(World world, SaveStuffThing saveData) { world.mapStorage.setData("MyModId", saveData); } } What's wrong?
June 27, 201411 yr Author Ok, looks a lot better now but still not working :'( What I don't understand is, since the if statement is checking if the data equals null, and only then saves it, if (data == null) { data = new SaveStuffThing(); world.mapStorage.setData(IDENTIFIER, data); } How would it be able to save my String?
June 27, 201411 yr Author Just to make sure: you are doing this on the server-side, right? I didn't knew I have to.. But if I do, I get an error instead of null, sayin: Failed to instantiate SaveStuffThing java.lang.RuntimeException
June 27, 201411 yr But if I do, I get an error instead of null, sayin: Failed to instantiate SaveStuffThing java.lang.RuntimeException You need to post the whole crash report. The crash reports usually tell you quite a bit about exactly where the issue is. Learn to read through the crash report and think about what it is saying. What line of code did it say was causing the crash? If it was your code, then look at what might be wrong, and if it was forge code look to see which of your code called that forge code. Anyway post the complete crash report, not just the top line. Check out my tutorials here: http://jabelarminecraft.blogspot.com/
June 28, 201411 yr Author I deleted the old class and made a new one, actually exactly the same, but it seems to be working now.. Thanks a lot
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.