Solved Best way to save objects in config?

Discussion in 'Plugin Development' started by metincasper, Jan 10, 2016.

Thread Status:
Not open for further replies.
  1. Offline

    metincasper

    Hi
    I created a class called "Quest" with some objects, and it look like this: http://pastebin.com/ZLtngL9j
    I need to tell i don't have any errors and it all works fine.

    I use the "Quest" class to handle my quests. So when i create a new Quest i do this:


    Code:
    List<Quest> myListThing = new ArrayList<>();
    Quest quest = new Quest(What it needs here);
    myListThing.add(quest);
    That works fine. But as it is a list it does not save it self over a reload. I tried to save into the config file like this:


    Code:
        public void saveQuests() {
            HashMap<String,String> path;
            for (Quest quest : getQuestArrayList()) {
                questMain.cf.getConfig().set("quests." + quest.getPlayer().getUniqueId() + ".questGroup",quest.getQuestGroup());
                questMain.cf.getConfig().set("quests." + quest.getPlayer().getUniqueId() + ".questName",quest.getQuestName());
                List<String> itemLore = new ArrayList<>();
                for (String lore : quest.getItemLore()) {
                    lore = lore.replaceAll("æ","ae");
                    lore = lore.replaceAll("å","aa");
                    lore = lore.replaceAll("ø","oe");
                    Bukkit.getConsoleSender().sendMessage(ChatColor.BLUE.toString()+ChatColor.BOLD+"Lore: " + lore);
                    itemLore.add(lore);
                }
                questMain.cf.getConfig().set("quests." + quest.getPlayer().getUniqueId() + ".itemLore",itemLore);
                path = quest.getConfigPath();
                for (Map.Entry<String, String> map : path.entrySet()) {
                    String key = map.getKey().replace(".",",");
                    questMain.cf.getConfig().set("quests." + quest.getPlayer().getUniqueId() + ".configPath."+key,map.getValue());
                }
            }
            questMain.cf.saveConfig();
        }
    But as i use ChatColor's in my ItemLore the config went crazy. And the output is this:
    Code:
    quests:
      9c110eea-b0e9-333c-a51d-97b887738270:
        questGroup: Vaaben Quest
        questName: Vaaben Quest 1
        itemLore:
        - "\xa77Status\xa7f:"
        - "\xa7c0\xa7f/\xa7a1 \xa77draebt spiller med en\xa7e Deagle"
        - ''
        - "\xa77Gevinst:"
        - "\xa7a70$"
        configPath:
          PlayerQuests,9c110eea-b0e9-333c-a51d-97b887738270,quests,Vaaben Quest 1,status,kills: '0'
          PlayerQuests,9c110eea-b0e9-333c-a51d-97b887738270,quests,Vaaben Quest 1,active: 'false'
          PlayerQuests,9c110eea-b0e9-333c-a51d-97b887738270,quests,Vaaben Quest 1,name: xXCalleManXx
          PlayerQuests,9c110eea-b0e9-333c-a51d-97b887738270,quests,Vaaben Quest 1,status,done: 'false'
    The configPath section is fine, but the itemLore section is crazy.
    What is the best way?
     
  2. Offline

    mine-care

  3. Offline

    metincasper

    @mine-care can you maybe show me a example of the ConfigurationSerializable, i can't really understand how to do it, and what it's doing.
     
  4. Offline

    mine-care

  5. Offline

    Chloe-chan

    Or you can use Serialis(z)able interface. Here's an example.
     
  6. Offline

    teej107

  7. Offline

    metincasper

    @teej107 you right, and i could use that, but i think that a json file is better cause it does work with ø-æ-å and it does not go crazy with color codes. So i think i would stick to json files :)
     
Thread Status:
Not open for further replies.

Share This Page