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

    • How to fix file server-1.20.1-20230612.114412-srg.jar  
    • Just a few months ago I was creating my own plugin for Minecraft 1.20.2 spigot that did the same thing, but the skins were not saved, if you can understand this code that I made a long time ago it may help you.   //This is a class method private static String APIRequest(String value, String url, String toSearch) { try { URL api = new URL(url + value); HttpURLConnection connection = (HttpURLConnection) api.openConnection(); connection.setRequestMethod("GET"); int responseCode = connection.getResponseCode(); if (responseCode == HttpURLConnection.HTTP_OK) { BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream())); StringBuilder response = new StringBuilder(); for (String responseChar; (responseChar = reader.readLine()) != null; ) response.append(responseChar); reader.close(); JSONObject responseObject = new JSONObject(response.toString()); if (!toSearch.equals("id")) return responseObject .getJSONArray("properties") .getJSONObject(0) .getString("value"); else return responseObject.getString("id"); } else { AntiGianka.ConsoleMessage(ChatColor.RED, String.format( "Could not get %s. Response code: %s", ((toSearch.equals("id")) ? "UUID" : "texture"), responseCode )); } } catch (MalformedURLException error) { AntiGianka.ConsoleMessage(ChatColor.RED, "An error occurred while trying to access the URL. Error: " + error); } catch (IOException error) { AntiGianka.ConsoleMessage(ChatColor.RED, "An error occurred while attempting to connect to the URL. Error: " + error); } return ""; } //other class method private void SkinGetter() { String uuid; String textureCoded; if ((uuid = APIRequest(args[0], "https://api.mojang.com/users/profiles/minecraft/", "id")).isEmpty() || (textureCoded = APIRequest(uuid, "https://sessionserver.mojang.com/session/minecraft/profile/", "value")).isEmpty() ) sender.sendMessage(ChatColor.RED + String.format( "An error has occurred while trying to obtain the %s player skin, please check if the name %s is spelled correctly or try again later.", args[0], args[0] ) ); else SkinSetter(textureCoded); } //other more private void SkinSetter(String textureCoded) { JSONObject profile = new JSONObject(new String(Base64.getDecoder().decode(textureCoded))); try { URL textureUrl = new URL(profile.getJSONObject("textures"). getJSONObject("SKIN"). getString("url")); if (sender instanceof Player && args.length == 1) { PlayerTextures playerTextures = ((Player) sender).getPlayerProfile().getTextures(); playerTextures.setSkin(textureUrl); ((Player) sender).getPlayerProfile().setTextures(playerTextures); if (((Player) sender).getPlayerProfile().getTextures().getSkin() != null) sender.sendMessage(((Player) sender).getPlayerProfile().getTextures().getSkin().toString()); else sender.sendMessage("Null"); sender.sendMessage("Skin changed successfully.a"); } else { } AntiGianka.ConsoleMessage(ChatColor.GREEN, "Skin command executed successfully."); } catch (MalformedURLException error) { sender.sendMessage(ChatColor.RED + String.format( "An error has occurred while trying to obtain the %s player skin, please check if the name %s is spelled correctly or try again later.", args[0], args[0] ) ); AntiGianka.ConsoleMessage(ChatColor.RED, "An error occurred while trying to access the URL. Error: " + error); } }  
    • Use /locate structure The chat should show all available structures as suggestion For the Ancient City it is /locate structure ancient_city
    • So does it work without this mod? Did you test it with other builds?
  • Topics

×
×
  • Create New...

Important Information

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