Jump to content

Recommended Posts

Posted

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?

Posted

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)

Posted

Sooo, I was trying around a bit.. (at least not gettin any errors :D)

        		
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?

Posted

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

Posted

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?

Posted

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..

Posted

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?

Posted

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?

Posted

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

Posted

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/

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.