Jump to content

Recommended Posts

Posted

The easiest would be to create an xml file or yaml which alot of other mods use. If ur not sure how to write to an xml or yaml file you can just search for java tutorials tyere are quite a few out there. Hooe that helps u out, good luck

Posted

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.

Posted

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();
    
}

}

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.