Jump to content

[1.5.1] Saving Integers in a Config File to Be Loaded Later


Mitchellbrine

Recommended Posts

Basically what the title says. I want the player to unlock a certain power, but since everything restarts at startup of the mod, it resets all the integers back to 0. I want to know if there is a way to save something to the configuration file while in game, so it saves it for later. After looking at a bunch of tutorials for configuration, I'm baffled at how I work with configs. Can anyone help me?

 

(Sorry about it sounding so complicated.)

-Mitchellbrine

 

Minecraft can do ANYTHING, it's coded in Java and you got the full power of Java behind you when you code. So nothing is impossible.

It may be freaking fucking hard though, but still possible ;)

 

If you create a topic on Modder Support, live by this motto:

I don't want your charity, I want your information
Link to comment
Share on other sites

well theres a variety of ways, you could store in a NBTTagCompound and then use

CompressedStreamTools.writeCompressed(NBTTagCompound, OutPutStream);

you'd have to set up the output stream

public void write(NBTTagCompound yourData, String loc) throws Exception;
File f = new File(loc + "filename.dat");
	if(!f.exists() &&(!f.mkdirs() | !f.createNewFile()))
		throw new Exception("Failed to write @" + f.getAbsolutePath());
FileOutputStream out = new FileOuputStream(f);
CompressedStreamTools.writeCompressed(yourData, out);
out.close(); //ALWAYS ALWAYS ALWAYS CLOSE YOUR STREAMS WHEN THEY ARE OPEN

or if you want a modifiable value in a text file you can use a BufferedWriter

BufferedWriter b = new BufferedWriter(new FileWriter(File yourFile)));
b.close();//ALWAYS

or you could always use the plain file output stream

FileOutputStream out = new FileOutputStream(File yourFile);
out.write(ByteBuffer.wrap(new byte[0]).putInt(yourVal).array());
out.close();//AGAIN

and theres many more methods of writing you can find. it depends on what you want. you could even go heavy on the sauce and implement Serializable and writing your whole class, of course delegating transient values- though that's a tad overkill here.

I think its my java of the variables.

Link to comment
Share on other sites

I sort of get what you're saying. Yet, I sort of don't. Let me explain my situation because I think I would be getting even greater help by detailing.

 

In my mod, the player gets this thing called Stamina after mining up the ore. I want to be able to save that data in, maybe, the level.dat or another dat file, so when the world's opened, it has the saved int. Is there a clear method for that? I just want a way to save the int to be opened later.

-Mitchellbrine

 

Minecraft can do ANYTHING, it's coded in Java and you got the full power of Java behind you when you code. So nothing is impossible.

It may be freaking fucking hard though, but still possible ;)

 

If you create a topic on Modder Support, live by this motto:

I don't want your charity, I want your information
Link to comment
Share on other sites

How do I implement that?

-Mitchellbrine

 

Minecraft can do ANYTHING, it's coded in Java and you got the full power of Java behind you when you code. So nothing is impossible.

It may be freaking fucking hard though, but still possible ;)

 

If you create a topic on Modder Support, live by this motto:

I don't want your charity, I want your information
Link to comment
Share on other sites

RANKSHANK, I implemented it on my main class. Here are my methods. Can you tell me if they work?

 

Here they are: (I don't really need a spoiler)

@Override

public void saveNBTData(NBTTagCompound compound) {

 

compound.setInteger("Special", Special);

compound.setInteger("Valiantite", ValiantActivate);

 

}

 

@Override

public void loadNBTData(NBTTagCompound compound) {

compound.getInteger("Special");

compound.getInteger("Valiantite");

 

}

 

@Override

public void init(Entity entity, World world) {

 

}

 

Would this work?

 

P.S. Special and ValiantActivate are the integers I was talking about for the mod.

-Mitchellbrine

 

Minecraft can do ANYTHING, it's coded in Java and you got the full power of Java behind you when you code. So nothing is impossible.

It may be freaking fucking hard though, but still possible ;)

 

If you create a topic on Modder Support, live by this motto:

I don't want your charity, I want your information
Link to comment
Share on other sites

you are using tick handlers to do that? (the stamina thing).

BTW, if u have the instance of the player u could just use player.getEntityData().

EX:

public int stamina = 0;

some void here or method

stamina = player.getEntityData().getInteger("lol") > 0 ?  player.getEntityData().getInteger("lol") : 0;

//code

player.getEntityData().setInteger("lol", stamina);

Link to comment
Share on other sites

you are using tick handlers to do that? (the stamina thing).

BTW, if u have the instance of the player u could just use player.getEntityData().

EX:

public int stamina = 0;

some void here or method

stamina = player.getEntityData().getInteger("lol") > 0 ?  player.getEntityData().getInteger("lol") : 0;

//code

player.getEntityData().setInteger("lol", stamina);

 

You don't know what I'm saying. I want to know how to SAVE the integer for relogging. It sets the Stamina back to 0 after logging out.

 

The method for setting the int seems like a good idea for setting the int, but I need to know how I can save it.

-Mitchellbrine

 

Minecraft can do ANYTHING, it's coded in Java and you got the full power of Java behind you when you code. So nothing is impossible.

It may be freaking fucking hard though, but still possible ;)

 

If you create a topic on Modder Support, live by this motto:

I don't want your charity, I want your information
Link to comment
Share on other sites

opssemnik, I meant when a player leaves the game. I see your code and I think that's a great idea! Thank you!

-Mitchellbrine

 

Minecraft can do ANYTHING, it's coded in Java and you got the full power of Java behind you when you code. So nothing is impossible.

It may be freaking fucking hard though, but still possible ;)

 

If you create a topic on Modder Support, live by this motto:

I don't want your charity, I want your information
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



×
×
  • Create New...

Important Information

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