Problems with default config file

Discussion in 'Plugin Development' started by ocomobock, Jun 10, 2013.

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

    ocomobock

    I can't seem to save values to the default config file and I'm not really sure what I'm doing wrong. My main file contains a bunch of stuff that doesn't have to do with configuration, so here's various parts of my code:

    Show Spoiler
    Code:java
    1. public class OcoKits extends JavaPlugin implements Listener{
    2.  
    3. public static HashMap<String, String> kits = new HashMap<String, String>();
    4.  
    5. public void onEnable()
    6. {
    7. getConfig().createSection("kits");
    8. this.getConfig().options().copyDefaults(true);
    9. saveDefaultConfig();
    10. getServer().getPluginManager().registerEvents(this, this);
    11. for (String key : getConfig().getConfigurationSection("kits").getKeys(false))
    12. {
    13. kits.put(key, getConfig().getString("kits." + key));
    14. }
    15. saveConfig();
    16. }
    17.  
    18. public void onDisable()
    19. {
    20. for (Entry<String, String> entry : kits.entrySet())
    21. {
    22. getConfig().set(entry.getKey(), entry.getValue());
    23. }
    24. }
    25.  
    26. public void setKit(String name, Kit kit)
    27. {
    28. kits.put(name, Kit.toString(kit));
    29. }
    30.  
    31. public boolean hasKit(String name, Kit kit)
    32. {
    33. if (kits.get(name) != null)
    34. {
    35. if (Kit.getKitFromString(kits.get(name)).equals(kit))
    36. {
    37. return true;
    38. }else
    39. {
    40. return false;
    41. }
    42. }else
    43. {
    44. return false;
    45. }
    46. }
    47.  
    48.  
    49. @EventHandler
    50. public void onPlayerInWater(PlayerMoveEvent e)
    51. {
    52. Player p = e.getPlayer();
    53. if (p.getGameMode() != GameMode.CREATIVE)
    54. {
    55. if (hasKit(p.getName(), Kit.POSEIDON))
    56. {
    57. Material m = p.getLocation().getBlock().getType();
    58. if (m == Material.STATIONARY_WATER || m == Material.WATER && m != null)
    59. {
    60. isInWater = true;
    61. p.setAllowFlight(true);
    62. p.setFlying(true);
    63. p.addPotionEffect(new PotionEffect(PotionEffectType.NIGHT_VISION, 86400 * 20, 1));
    64. p.addPotionEffect(new PotionEffect(PotionEffectType.INCREASE_DAMAGE, 86400 * 20, 1));
    65. }else
    66. {
    67. isInWater = false;
    68. p.setAllowFlight(false);
    69. p.setFlying(false);
    70. p.removePotionEffect(PotionEffectType.FAST_DIGGING);
    71. p.removePotionEffect(PotionEffectType.NIGHT_VISION);
    72. p.removePotionEffect(PotionEffectType.INCREASE_DAMAGE);
    73. }
    74. }
    75. }


    Part of onCommand:

    Show Spoiler
    Code:java
    1. if (commandLabel.equalsIgnoreCase("givekit") && args.length == 2)
    2. {
    3. setKit(args[0], Kit.getKitFromString(args[1]));
    4. p.sendMessage(green+"Given "+args[0]+" "+args[1]);
    5. StringBuilder poop = new StringBuilder();
    6. poop.append(Integer.toString(kits.size()) + " : ");
    7. for (String stringy: kits.values())
    8. {
    9. poop.append(stringy + ", ");
    10. }
    11. p.sendMessage(poop.toString());
    12. reloadConfig();
    13. }


    And yeah, I have "givekit" in my plugin.yml. It sends the player "Given ocomobock POSEIDON" when I run /givekit ocomobock POSEIDON. This is my config.yml in my project folder, which I wasn't actually sure if you were supposed to put anything in it or not:

    Code:yaml
    1. kits:
    2.  

    The hasKit method works fine. If I run '/givekit ocomobock POSEIDON' it adds my name to the HashMap and everything and then I have the poseidon's abilities, but I'm just having trouble saving the HashMap to the yml. It creates the default config folder (\plugins\OcoKits\config.yml), and it says
    kits: {}
    but like I said, it never adds the values to the config file. Any help would be appreciated.
    I'm probably just being stupid, like with most of my threads here.
     
  2. Offline

    Compressions

    ocomobock You aren't going to want to save a HashMap to a config file. Use an ArrayList.
    Code:
    ArrayList<String> kitName = getConfig().getStringList("kits");
    list.add("blah");
    getConfig().set("kits", list);
    saveConfig();
     
  3. Offline

    ocomobock

    Code:
    ArrayList<String> kitName = getConfig().getStringList("kits");
    That basically creates an ArrayList from the config file, right? Before I load from the config file I should probably be able to write to it.
     
  4. Offline

    Compressions

    ocomobock Why should you be able to write to a nonexistent config file? If you want to write to it, do:
    Code:
    getConfig().set("kits", list);
     
  5. Offline

    ocomobock

    I'm saying 'ArrayList<String> kitName = getConfig().getStringList("kits");' wouldn't do anything because it's getting values from the config file, which I haven't actually written to yet. So I'm pretty sure 'getConfig().set("kits", list);' would return nothing.

    I'm probably confused, I just started dealing with yml files <_<
     
  6. Offline

    Compressions

    ocomobock Well then you need to write the config file.
     
  7. Offline

    ocomobock

    Which is essentially what this thread is about.

    Bump. Anyone know why it isn't writing to the config file when onDisable is called?

    I guess I needed to wait 1 more hour to bump, but I need to go now.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 1, 2016
  8. Offline

    Sagacious_Zed Bukkit Docs

    A few things.
    getConfig will always return a valid FileConfigurtion object. getConfig will only read from disk the first time it is called. getConfig only returns a different FileConfiguration after reloadConfig has been called otherwise it will return the same object. FileConfiguration's set method does not modify the contents on disk. Set can take any ConfigurationSerializable type, java primitives, and certain collections including hashmap<String, String>
     
  9. Offline

    ocomobock

    I'm sorry, but do you know what I should actually do to fix it? I kind of just started with config stuff so I'm still confused <_<

    EDIT: I changed these two methods:

    Code:java
    1. public void onEnable()
    2. {
    3. getConfig().createSection("kits");
    4. this.getConfig().options().copyDefaults(true);
    5. saveDefaultConfig();
    6. getServer().getPluginManager().registerEvents(this, this);
    7. for (String key : getConfig().getConfigurationSection("kits").getKeys(false))
    8. {
    9. kits.put(key, getConfig().getString("kits." + key));
    10. }
    11. }
    12.  
    13. public void onDisable()
    14. {
    15. for (Entry<String, String> entry : kits.entrySet())
    16. {
    17. getConfig().set(entry.getKey(), entry.getValue());
    18. }
    19. saveConfig();
    20. }


    And that works, but it's writing it outside of the kits section.

    Like I said, my config.yml in my project folder looks like this:

    Code:
    kits:
     
    
    Should I even have a config.yml there? And this is my new config.yml in my actual server folder:

    ocomobuck: THOR
    ocomobock: POSEIDON
    ocomoback: POSEIDON

    After running /givekit ocomoback POSEIDON, /givekit ocomobock POSEIDON, /givekit ocomobuck THOR
     
  10. Offline

    Sagacious_Zed Bukkit Docs

    ocomobock
    As I said, the set method does not modify the file on disk, it is purely a memory operation. You can set anything you want in memory and it will stay in memory until saveConfig is called to write it to disk.

    Additionally, saveDefaultConfig works independently. The method copies the config.yml in your jar into the datafolder only if the datafolder does not have a config.yml already. The method is by design idempotent.
     
  11. Offline

    ocomobock

    Sagacious_Zed
    I got it to work, thanks.

    Most of the commands from my plugin that were working before aren't working now, but I'll try to fix that.
     
Thread Status:
Not open for further replies.

Share This Page