I need help... again... I have class for saving objects to file (by Tomsik68). I can save, but I can not load any HashMaps. I did test: 1. Save object (String). 2. Load object. 3. Print result. 4. I gained correct result. But I can't still save HashMap. My HashMap: Code: private Map<Player, Boolean> canRef = new HashMap<Player, Boolean>(); These are save and load methods: Spoiler (Move your mouse to the spoiler area to reveal the content) Show Spoiler Hide Spoiler Code: package com.nomaf.refferal.libs; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; /** SLAPI = Saving/Loading API * API for Saving and Loading Objects. * @author Tomsik68 */ public class SLAPI { public static void save(Object obj, File file) throws Exception { ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(file, true)); oos.writeObject(obj); oos.flush(); oos.close(); } public static Object load(File file) throws Exception { ObjectInputStream ois = new ObjectInputStream(new FileInputStream(file)); Object result = ois.readObject(); ois.close(); return result; } }
You can't save a Player object, as it is not serializable (like most Bukkit stuff). If you want to save specific data, make your own class using only primitive data types. If you only need the player's name, save the player name as String in your hashmap.
Save the objects like this: Player's name, true/false It could for for multiple entries, like this: Code: Technius, true Player, false Not_A_Real_Player, true Use BufferedWriter and BufferedReader to do this. Writing: Code:java HashMap<String,Boolean> hashmap = new HashMap<String, Boolean>();File file = //your filetry{ BufferedWriter bw = new BufferedWriter(new FileWriter(file)); for(String p:hashmap.keySet()) { bw.write(p + "," + hashmap.get(p)); bw.newLine(); } bw.flush(); bw.close();} Reading: Code:java HashMap<String, Boolean> hashmap = new HashMap<String, Boolean>();File file = //your filetry{ BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(file))); String l; while((l = br.readLine()) != null) { String[] args = l.split("[,]", 2); if(args.length != 2)continue; String p = args[0].replaceAll(" ", ""); String b = args[1].replaceAll(" ", ""); if(b.equalsIgnoreCase("true"))hashmap.put(p, true); else hashmap.put(p, false); } br.close();}
What for a Exeption with catch? Edit: Got it Code: catch (IOException e){ log.info("[ServerNews] + '" + getDataFolder().getPath() + "/player_toggle.yml' not found."); }
I don't see any problem with saving or loading for a hashmap in that code. I use almost the same thing on one of my plugins and it works perfectly. Do you get any exception when saving or loading? Or does the file just end up empty?
Pandemoneus Not good. I think about do a new class which implements Player, but Technius idea works Technius Thx for code! edocsyl uruhax I haven't any errors. Pandemoneus tells "You can't save a Player object, as it is not serializable (like most Bukkit stuff)." As I see Player class doesn't implements Serializable.
It works.. To check if player toggle true / false Code:java if (hashmap.containsKey(e.getPlayer().getName())){if(hashmap.containsValue(true)){ //Do domething when it's true}} Toggle: Code:java public void toggleJoinNews(String p, CommandSender sender){if(hashmap.containsKey(p)){if(hashmap.get(p)){hashmap.put(p, false); sender.sendMessage(ChatColor.GREEN + "[SN] " + ChatColor.WHITE + "You never get the news on join.");} else {hashmap.put(p, true);sender.sendMessage(ChatColor.GREEN + "[SN] " + ChatColor.WHITE + "You now get the news on join."); }} else {hashmap.put(sender.getName(), defaultboolean); //If you want plugin enabled by default change this value to false.sender.sendMessage(ChatColor.GREEN + "[SN] " + ChatColor.WHITE + "You now get the news on join.");}} Btw: How can u show the code in java ??
Sorry to revive an old thread, but I am doing the same exact thing. Would someone mind explaining this to a noob? Im trying to save Code:JAVA Public Map<Player,Integer> Map1 = new HashMap <Player, Integer>();Public Map<Player,Integer> Map2 = new HashMap <Player, Integer>();
SgtStud You can't save the Player object, because it is not serializable (cannot be written to a file). Try using a HashMap <String, Integer>() and get the player's name. Use the SLAPI from the BukkitWiki also.
Guys, do not store players as it changes a lot. Always store names and use bukkut methods to get player.
spoljo666 BlueMustache lololololol nomaf should probably put this as solved. EDIT by Moderator: merged posts, please use the edit button instead of double posting.