Jump to content

storing values in an external file


hkiller1

Recommended Posts

Or if you don't need the player to be able to modify these values, just compress and export the map with the mappings directly. You can do this like this:

Saving:

File savedirectory = new File(world.getSaveHandler().getSaveDirectoryName().concat("/MySaveInfo.dat"));
savedirectory.createNewFile();
ObjectOutputStream os = new ObjectOutputStream(new GZIPOutputStream(new FileOutputStream(savedirectory)));
os.writeObject(MapContainingPlayerMappedVariables);

Loading:

File savedirectory = new File(world.getSaveHandler().getSaveDirectoryName().concat("/MySaveInfo.dat"));
if(savedirectory.exists()){
ObjectInputStream os = new ObjectInputStream(new GZIPInputStream(new FileInputStream(savedirectory)));
MapContainingPlayerMappedVariables.putAll((Map)os.readObject());
}

 

Remember to encompass file saving/loading in a try catch block in case there are write errors.

Link to comment
Share on other sites

I got the save function to work but im having some trouble with the load function, curently I have

package economy.common;

import java.io.*;
import java.util.*;

import org.yaml.snakeyaml.Yaml;

public class Data {

public static String LoadData(String player) throws IOException {
File file = new File("config/EconomyAcounts.yml");

if(!file.exists()) { 
	try {
		file.createNewFile();
	} catch (IOException e) {
		e.printStackTrace();
	}
}

//TODO: Load file

 Yaml yaml = new Yaml();
 List<String> list = (List<String>) yaml.load(document);

 //TODO: find the amount of money the player has

//return amount
}

public static void SaveData(String player, String amount) throws FileNotFoundException {
File file = new File("config/EconomyAcounts.yml");

if(!file.exists()) { 
	try {
		file.createNewFile();
	} catch (IOException e) {
		e.printStackTrace();
	}
}

    Map<String, Object> data = new HashMap<String, Object>();
    data.put(player, amount);
    Yaml yaml = new Yaml();
    String output = yaml.dump(data);
    PrintWriter out = new PrintWriter(file);
    out.println(output);
    out.close();
    
}

}

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.