Custom configuration default values not getting written

Discussion in 'Plugin Development' started by thescreem, Aug 3, 2012.

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

    thescreem

    I am writing this plugin which requires custom configuration files for each player (named "player-name.yml") and I can't get the default values to save.

    Here is the stack trace:
    Stack trace (open)
    Code:JAVA
    1. 2012-08-03 12:53:53 [SEVERE] Could not pass event PlayerJoinEvent to PLUGIN_NAME
    2. org.bukkit.event.EventException
    3. at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:304)
    4. at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:62)
    5. at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.java:477)
    6. at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:462)
    7. at net.minecraft.server.ServerConfigurationManager.c(ServerConfigurationManager.java:134)
    8. at net.minecraft.server.NetLoginHandler.b(NetLoginHandler.java:129)
    9. at net.minecraft.server.NetLoginHandler.a(NetLoginHandler.java:35)
    10. at net.minecraft.server.NetworkListenThread.a(NetworkListenThread.java:65)
    11. at net.minecraft.server.MinecraftServer.w(MinecraftServer.java:559)
    12. at net.minecraft.server.MinecraftServer.run(MinecraftServer.java:451)
    13. at net.minecraft.server.ThreadServerApplication.run(SourceFile:492)
    14. Caused by: java.lang.NullPointerException
    15. at com.screem.util.User.<init>(User.java:58)
    16. at com.screem.PLUGIN_NAME.addUser(MAIN_CLASS.java:101)
    17. at com.screem.listeners.PlayerListener.onPlayerJoin(PlayerListener.java:20)
    18. at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    19. at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    20. at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    21. at java.lang.reflect.Method.invoke(Unknown Source)
    22. at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:302)
    23. ... 10 more

    And this is the code:

    I have two objects I use for the configuration file:
    Objects (open)
    Code:JAVA
    1. private FileConfiguration data;
    2. private File dataFile;


    The constructor in this User class I made calls my custom reloadConfig() method:
    reloadConfig() Method (open)
    Code:JAVA
    1. public void reloadConfig() {
    2. if(dataFile == null) {
    3. dataFile = new File("plugins/Lyrethia/users/" + player.getName() + ".yml");
    4. if(!dataFile.exists()) {
    5. try {
    6. dataFile.createNewFile();
    7. } catch (IOException e) {
    8. e.printStackTrace();
    9. }
    10. }
    11. }
    12.  
    13. data = YamlConfiguration.loadConfiguration(dataFile);
    14.  
    15. data.addDefault("group", "ws");
    16. data.addDefault("nick-name", player.getName());
    17. data.addDefault("char-name", "");
    18. data.addDefault("home", "N/A");
    19. data.addDefault("back", "N/A");
    20. data.addDefault("warnings", "N/A");
    21. data.addDefault("mode", 0);
    22. data.addDefault("minion", false);
    23. data.addDefault("god-mode", false);
    24. data.addDefault("muted", false);
    25. data.addDefault("msg-spy", false);
    26. data.addDefault("characters", "");
    27. saveConfig();
    28. }


    Which then calls the saveConfig() method:
    saveConfig() Method (open)
    Code:Java
    1. public void saveConfig() {
    2. if(data == null || dataFile == null) return;
    3.  
    4. try {
    5. getConfig().save(dataFile); //getConfig() just returns the data object if it isn't null
    6. } catch(IOException e) {
    7. plugin.logError("Unable to save user file for " + player.getName());
    8. e.printStackTrace();
    9. }
    10. }


    The error's saying it's on line 58 of my User class, which is this:
    Code:Java
    1. if(data.getConfigurationSection("characters").getKeys(false) != null)


    When I open the configuration file, it's empty and none of the default values got written, so it throws a NullPointerException. I looked at the Docs in the Wiki, but I couldn't find anything to help me, so I thought I'd ask here.

    Thanks in advance for any help! ;)
     
  2. Offline

    Bavestry

    Maybe just try this?:
    Code:
    if(data.get("characters").getKeys(false) != null)
    
    I don't see anything else wrong. Also, just checking, you did notice the other two errors, right? One in your MAIN_CLASS, and one in your PlayerListener class.
     
  3. Offline

    thescreem

    Well, the PlayerListener class calls the "createUser()" method in the main class, which creates a new User object thus calling it's constructor and eventually throwing the NullPointerException.

    Thanks for the help, but doing that doesn't seem possible, as the get() method in the FileConfiguration class returns an Object, and there's no getKeys() method in the Object class.

    The NullPointerException is thrown because none of the default values get written to the configuration file. As in, when I open the file, nothing is there. So when it tries to look for something in the configuration file, it returns null because nothing is there.
     
  4. Offline

    Sagacious_Zed Bukkit Docs

    As far as i can tell, data is null.
     
  5. Offline

    thescreem

    I added a null check in the reloadConfig() which prints out "Data is null!" into the console, and it didn't get printed, so data isn't null.

    Any other ideas? :confused:
     
  6. Offline

    krconv

    I am having this same problem. It would be great if someone could figure it out :)
     
  7. Offline

    Sagacious_Zed Bukkit Docs

    Im not going to take your word for it :p Not until i see all of User.java
     
  8. Offline

    travja

    I would try:
    Code:java
    1. config.options().copyDefaults();
    2. this.saveDefaultConfig();

    Instead of you saveConfig as saveConfig doesn't copy all the config defaults in... But I may be completely wrong...
     
  9. Offline

    krconv

    I think this will do it because copyDefaults(); is false by default. Works for me anyways
    Code:
    config.options().copyDefaults(true);
    this.saveDefaultConfig();
     
  10. Offline

    thescreem

    travja
    Sadly that won't work as the method saveDefaultConfig() is part of the JavaPlugin class, and my User class doesn't extend it. I tried to add in the first line of your suggestion, but it hasn't worked. :/

    @SagaciousZed
    Well, data can't be null as it's set with this line inside reloadConfig():
    Code:Java
    1. data = YamlConfiguration.loadConfiguration(dataFile);

    And dataFile is set right before it with a null check, and it creates the file is if it doesn't exist:
    Code:Java
    1. if(dataFile == null) {
    2. dataFile = new File("plugins/PLUGIN-NAME/users/" + player.getName() + ".yml");
    3. if(!dataFile.exists()) {
    4. try {
    5. dataFile.createNewFile();
    6. } catch (IOException e) {
    7. e.printStackTrace();
    8. }
    9. }
    10. }


    But here's the User class anyways, I snipped out about 20 get and set methods though, as the whole class is over 400 lines.
    EDIT: No idea what happened with the formatting and why there's all these "[/I]".
    Code:Java
    1. package com.screem.util;
    2.  
    3. import java.io.File;
    4. import java.io.IOException;
    5. import java.util.HashMap;
    6. import java.util.Map;
    7. import java.util.Set;
    8.  
    9. import org.bukkit.GameMode;
    10. import org.bukkit.Location;
    11. import org.bukkit.configuration.file.FileConfiguration;
    12. import org.bukkit.configuration.file.YamlConfiguration;
    13. import org.bukkit.entity.Player;
    14.  
    15. import com.screem.MAIN_CLASS;
    16.  
    17. public class User {
    18. private Player player;
    19.  
    20. private MAIN_CLASS plugin;
    21.  
    22. private FileConfiguration data;
    23. private File dataFile;
    24.  
    25. private int selectedChar = -1;
    26. private String currentCharName;
    27.  
    28. private Map<String, Object> charDetails = new HashMap<String, Object>();
    29.  
    30. private String group;
    31. private String nickName;
    32. private String warnings;
    33. private Location home;
    34. private Location back;
    35. private int mode;
    36. private int currentChar;
    37. private boolean muted;
    38. private boolean minion;
    39. private boolean msgSpy;
    40. private boolean godMode;
    41.  
    42. public User(Player player, MAIN_CLASS plugin) {
    43. this.player = player;
    44. this.plugin = plugin;
    45. reloadConfig();
    46.  
    47. group = data.getString("group", "new");
    48. nickName = data.getString("nick-name", player.getName());
    49. home = formatStringToLoc(data.getString("home", "N/A"));
    50. back = formatStringToLoc(data.getString("back", "N/A"));
    51. warnings = data.getString("warnings", "N/A");
    52. mode = data.getInt("mode", 0);
    53. minion = data.getBoolean("minion", false);
    54. godMode = data.getBoolean("god-mode", false);
    55. muted = data.getBoolean("muted", false);
    56. msgSpy = data.getBoolean("msg-spy", false);
    57.  
    58. if(data.getConfigurationSection("characters").getKeys(false) != null) {
    59. Set<String> test = data.getConfigurationSection("characters").getKeys(false);
    60.  
    61. for(int i = 0; i < test.size(); i++) {
    62. String name = (String) test.toArray()[I];[/I]
    63. [I] charDetails.put("name" + i, name);[/I]
    64. [I] charDetails.put("race" + i, data.getString("characters." + name + ".race", "No race"));[/I]
    65. [I] charDetails.put("age" + i, data.getInt("characters." + name + ".age", 0));[/I]
    66. [I] charDetails.put("description" + i, data.getString("characters." + name + ".description", ""));[/I]
    67. [I] if(data.getBoolean("characters." + name + ".current", true)) {[/I]
    68. [I] currentChar = i;[/I]
    69. [I] currentCharName = name;[/I]
    70. [I] }[/I]
    71. [I] }[/I]
    72. [I] }[/I]
    73.  
    74. [I] player.setDisplayName((currentCharName + " " + nickName).replaceAll("(&([a-fk-okr0-9]))", "\u00A7$2"));[/I]
    75. [I] if(mode == 0) player.setGameMode(GameMode.SURVIVAL);[/I]
    76. [I] else if(mode == 1) player.setGameMode(GameMode.CREATIVE);[/I]
    77. [I] }[/I]
    78.  
    79. [I] public void reloadConfig() {[/I]
    80. [I] if(dataFile == null) {[/I]
    81. [I] dataFile = new File("plugins/PLUGIN_NAME/users/" + player.getName() + ".yml");[/I]
    82. [I] if(!dataFile.exists()) {[/I]
    83. [I] try {[/I]
    84. [I] dataFile.createNewFile();[/I]
    85. [I] } catch (IOException e) {[/I]
    86. [I] e.printStackTrace();[/I]
    87. [I] }[/I]
    88. [I] }[/I]
    89. [I] }[/I]
    90.  
    91. [I] data = YamlConfiguration.loadConfiguration(dataFile);[/I]
    92. [I] if(data == null) {[/I]
    93. [I] plugin.logInfo("Data is null!");[/I]
    94. [I] }[/I]
    95.  
    96. [I] //YamlConfiguration dataConfig = new YamlConfiguration();[/I]
    97. [I] data.addDefault("group", "ws");[/I]
    98. [I] data.addDefault("nick-name", player.getName());[/I]
    99. [I] data.addDefault("char-name", "");[/I]
    100. [I] data.addDefault("home", "N/A");[/I]
    101. [I] data.addDefault("back", "N/A");[/I]
    102. [I] data.addDefault("warnings", "N/A");[/I]
    103. [I] data.addDefault("mode", 0);[/I]
    104. [I] data.addDefault("minion", false);[/I]
    105. [I] data.addDefault("god-mode", false);[/I]
    106. [I] data.addDefault("muted", false);[/I]
    107. [I] data.addDefault("msg-spy", false);[/I]
    108. [I] data.addDefault("characters", "");[/I]
    109. [I] data.options().copyDefaults();[/I]
    110. [I] saveConfig();[/I]
    111. [I] }[/I]
    112.  
    113. [I] public FileConfiguration getConfig() {[/I]
    114. [I] if(data == null) reloadConfig();[/I]
    115. [I] return data;[/I]
    116. [I] }[/I]
    117.  
    118. [I] public void saveConfig() {[/I]
    119. [I] if(data == null || dataFile == null) return;[/I]
    120.  
    121. [I] try {[/I]
    122. [I] getConfig().save(dataFile);[/I]
    123. [I] } catch(IOException e) {[/I]
    124. [I] plugin.logError("Unable to save user file for " + player.getName());[/I]
    125. [I] e.printStackTrace();[/I]
    126. [I] }[/I]
    127. [I] }[/I]
     
  11. Offline

    travja

    So then how can you use saveConfig but not saveDefaultConfig?
     
  12. Offline

    thescreem

    *facepalm* I'm using the saveConfig() method I made in my User class...
     
  13. Offline

    travja

    Oh, ok. Umm.... All I can think is to manually add the different options.
     
Thread Status:
Not open for further replies.

Share This Page