[SOLVED] How to Copy Defaults withi Custon File Configuration?

Discussion in 'Plugin Development' started by lucasdidur, Apr 12, 2012.

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

    lucasdidur

    Hello

    I have little problem to save the settings.

    I need to edit the config what is already saved, but without delete what is not used.

    I tried to use
    Code:java
    1.  
    2. config.options().copyDefaults(true);
    3.  

    But it did not work



    How i do to edit and save, but without replacing the already existing file saved ?


    My Code:
    Show Spoiler

    Code:java
    1.  
    2. public static void saveInventory(Player player)
    3. {
    4. File saveFile = new File(plugin.getDataFolder() + File.separator + "inventories", player.getName() + ".yml");
    5.  
    6. YamlConfiguration config = new YamlConfiguration();
    7.  
    8. if (EhInventory.inventories.containsKey(player.getName()))
    9. {
    10. ItemStack[] itemStacks = EhInventory.inventories.get(player.getName());
    11.  
    12. int size = itemStacks.length;
    13.  
    14. Inventory inv = Bukkit.createInventory(player, size);
    15. inv.setContents(itemStacks);
    16.  
    17. for(Integer i = 0; i < size; i++)
    18. {
    19. ItemStack item = inv.getItem(i);
    20.  
    21. if(item == null)
    22. continue;
    23.  
    24. config.set(i.toString() + ".amount", item.getAmount());
    25.  
    26. Short durab = Short.valueOf(item.getDurability());
    27.  
    28. config.set(i.toString() + ".durability", durab.intValue());
    29.  
    30. config.set(i.toString() + ".type", item.getTypeId());
    31.  
    32. int pos = 0;
    33. for (Enchantment enchantment : item.getEnchantments().keySet())
    34. {
    35. config.set(i.toString() + ".enchant" + pos, enchantment.getName());
    36. config.set(i.toString() + ".level" + pos, item.getEnchantmentLevel(enchantment));
    37. pos++;
    38. }
    39.  
    40. }
    41. }
    42.  
    43. try
    44. {
    45. config.options().copyDefaults(true);
    46. config.save(saveFile);
    47. }
    48. catch (IOException e)
    49. {
    50. player.sendMessage("The backpack could not be saved.");
    51. e.printStackTrace();
    52. }
    53. }
    54.  




    Thanks
     
  2. Offline

    sd5

    Code:
    InputStream defaultConfig = getRessource("customConfig.yml");
    config.setDefaults(defaultConfig);
    config.copyDefaults(true); //Don't know whether you have to do this...
     
    theplayerjooo and lucasdidur like this.
  3. Offline

    lucasdidur

    I tried to use the function getRessource() but this giving error to find it.

    I changed the code, but not work yet

    Code:java
    1.  
    2. InputStream defaultConfig = new FileInputStream(new File(plugin.getDataFolder() + File.separator + "inventories" + File.separator + player.getName() + ".yml"));
    3. if (defaultConfig != null) {
    4. YamlConfiguration defConfig = YamlConfiguration.loadConfiguration(defaultConfig);
    5. config.setDefaults(defConfig);
    6. }
    7.  


    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 25, 2016
  4. Offline

    sd5

    oh its getResource() not getRessource()
    sorry...
     
  5. Offline

    lucasdidur

    This function is not only if the file is inside the jar?
     
  6. Offline

    sd5

    Yes...? And the default config should be in the jar... Where else?
     
  7. Offline

    lucasdidur

    No, this configuration in a configuration folder created in the plugins folder.

    But I already fix I changed


    Code:java
    1.  
    2. File saveFile = new File(plugin.getDataFolder() + File.separator + "inventories", player.getName() + ".yml");
    3. YamlConfiguration config = new YamlConfiguration();
    4.  

    To
    Code:java
    1.  
    2. File saveFile = new File(plugin.getDataFolder() + File.separator + "inventories", player.getName() + ".yml");
    3. YamlConfiguration config = YamlConfiguration.loadConfiguration(saveFile);
    4.  


    And now works.


    Thanks for help
     
Thread Status:
Not open for further replies.

Share This Page