Jump to content

NBT?


daafganggdg

Recommended Posts

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?

Link to comment
Share on other sites

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)

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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/

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.

Announcements



  • Recently Browsing

    • No registered users viewing this page.
  • Posts

    • Create a new instance and start with these mods - try different builds until you find a working combination Then add the rest of your mods one by one or in groups
    • It started crashed after I added Framework, curios API, and scorched guns mods. I tried to change framework version, but its still not working. But if i launch minecraft with this mod list, its work pretty good
    • I recently created a Minecraft server through Forge Server, in version 1.20.4, I installed the simple voice chat mode to talk to my friends, but it doesn't work, it says it's offline, apparently it's time to put a host port on it, We tried the local one, the one with the tunnel that is from playit.gg and nothing, how do I solve this?
    • I have a forge server with around 200 mods and I would like to have it available 24/7. But instead of constantly having to run the server on a dedicated machine, I want to create a second server that a player must join to start the main server. The second server would permanently run on a lightweight computer which would transfer a player to the main server (or start the main server and then transfer the player when it is offline). But the problem is that I cannot join the second server because "the server is missing the following mods [...]". Is it possible to ignore this error and just join the server? The second server does not need to have any mods (except for the handling of the transfer) because it should just act as a waiting room.
    • RLFactions season 1.... Embrace the chaos! Have you ever wanted to play a heavily modded factions server?   RLFactions is a server where you are offered an unparalleled blend of the brutal survival challenges of RLCraft with the strategic depth of factions-based gameplay. You can immerse yourself in a world where one minute you are fighting for your life through a dungeon, and the next you could be defending your base from players riding dragons. DISCORD: https://discord.gg/z3ZEaCcFFG The combination of these modded elements, as well as the factions mechanics, not only enhances the survival experience but also creates a vibrant, competitive environment. Building and strategizing are a must to ensure your survival, it's the perfect setting for those who crave both a tough survival challenge and the excitement of faction warfare. This isn't just a normal factions server, this is a server for people who want a challenge. Do you think you can survive?
  • Topics

×
×
  • Create New...

Important Information

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