Make a custom config file

Discussion in 'Plugin Development' started by dillyg10, Feb 24, 2012.

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

    dillyg10

    Basically, i want somthing like users.yml to be generated, and within that I want it to generate usernames when a player logs in. :)
     
  2. Offline

    Gravity

    Make a new file that corresponds with the one you want to have, (ex:)
    File usersFile = new File(this.getDataFolder(), "users.yml");
    Then, create a FileConfiguration out of like so:
    FileConfiguration users = YamlConfiguration.loadConfiguration(usersFile);

    You can then do users.set("path.to.string", "hello world"); and whatnot.
    Keep in mind the above code does not generate the file if it is missing, make sure you check if (!usersFile.exists()) on onEnable()
     
    Xp10d3 and htmlman1 like this.
  3. Offline

    dillyg10

    h31ix I have the majority of that working, but I tried doing this:
    Code:
    String[] test = {player.getDisplayName()};
    List<String> pTemp = Arrays.asList(test);
    plugin.users.set(player.getName(), pTemp);
    
    but, it returns errors:
    Code:
    10:12:31 [SEVERE] Could not pass event org.bukkit.event.player.PlayerLoginEvent to Friends
    org.bukkit.event.EventException
    at org.bukkit.plugin.java.JavaPluginLoader$103.execute(JavaPluginLoader.java:1026)
    at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:61)
    at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:460)
    at net.minecraft.server.ServerConfigurationManager.attemptLogin(ServerConfigurationManager.java:224)
    at net.minecraft.server.NetLoginHandler.b(NetLoginHandler.java:95)
    at net.minecraft.server.NetLoginHandler.a(NetLoginHandler.java:40)
    at net.minecraft.server.NetworkListenThread.a(NetworkListenThread.java:61)
    at net.minecraft.server.MinecraftServer.w(MinecraftServer.java:537)
    at net.minecraft.server.MinecraftServer.run(MinecraftServer.java:435)
    at net.minecraft.server.ThreadServerApplication.run(SourceFile:465)
    Caused by: java.lang.reflect.InvocationTargetException
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at org.bukkit.plugin.java.JavaPluginLoader$103.execute(JavaPluginLoader.java:1024)
    ... 9 more
    Caused by: java.lang.NullPointerException
    at com.dillyg10.blockempires.friends.Events.onPlayerLogin(Events.java:24)
    ... 14 more
    
     
  4. Code:
    String[] test = new String[] { player.getDisplayName() };
    tried?
     
  5. Offline

    pivotgamer84

    You forgot to null check somewhere in the Player Login Event.
     
  6. Offline

    Haribo98

    Old thread, I know, but how do I save the config file once I have written to it?
     
  7. Offline

    Cammy_the_block

  8. Offline

    Haribo98

  9. Offline

    fireblast709

    post code please :3
     
    Evonoucono likes this.
  10. Offline

    Haribo98

    If I get it to work, I'll paste.
     
  11. Offline

    fireblast709

    I mean paste so I can actually help you -_-
     
  12. Offline

    CorrieKay

    Ive got a sort of tutorial in my sig, check it out :3

    But to specifically answer your question, you need to grab the file that you want to save it to, then call config.save(file)
     
    Haribo98 likes this.
  13. Offline

    Haribo98

    It's still saving blank files. I did edit your code slightly. Heres a paste link: http://pastebin.com/MFiJXeFv
     
  14. Offline

    CorrieKay

    Code:
    String pluginDir = plugin.getDataFolder().toString();
    File dir = new File(pluginDir);
    
    Not actually necessary to do this. If youre just gonna stick the file in your plugin directory, just do this
    Code:
    File file = new File(plugin.getDataFolder(),name);
    
    Try that. By the way, calling toString may be what your issue is. it will return a string "plugins\<yourpluginname>" So... Theres that.

    Also, another nitpicky thing, theres no actual need to put a file extension (.yml) on the end of name. as long as the actual data in the file is the correct format, you can load an exensionless file as a yml.
     
    Haribo98 likes this.
  15. Offline

    Haribo98

    Ok. Given that a shot. ( I prefer to have extensions on files, just so you know ;) )

    Oh dear. Turns out I don't think it was saving to the file, because there wasn't any methods setting anything to the file... I'm not certain of this. But I just checked back in my code and realised I forgot to change it.

    EDIT: Nope. Didn't make a difference. Still not saving to the file properly :(
    Code can be found here: http://pastebin.com/8bjz5S7A
     
  16. Offline

    CorrieKay

    Can you also post the code thats utilizing this class?

    wait, hold on, what are you doing in the save config method?

    Code:
     
    public void saveConfig(String name) {
    if (!name.endsWith(".yml")) {
    name = name + ".yml";
    }
    FileConfiguration config = getConfig(name);
    File file = new File(plugin.getDataFolder(),name);
    createConfig(name);
    try {
    config.save(file);
    } catch (IOException e) {
    e.printStackTrace();
    }
    }
    I dont see you passing the config file in anywhere to a save method, only a string. Your config files arent being saved because youre creating a new instance of the config file, which is blank, then saving that.
     
    Haribo98 likes this.
  17. Offline

    Haribo98

    Erm, sure

    EDIT: This is the only class which currently implements the methods. All the rest are using 1 config file (config.yml) http://pastebin.com/z3SKUTfz

    It's then implemented (That class) in my Main class: http://pastebin.com/r8RPpZtp

    EDIT 2: I save the config after each change of groups. This is so if the server crashes, it will still have all the data from people kills, deaths and killstreaks, etc
     
  18. Offline

    CorrieKay

    No, i mean the problem is in your save method. Youre not saving anything. Instead of passing the config object into the saveConfig method, youre reloading the config from disk and then saving it. Youre losing your changes because youre not saving the config object you modified, only a new one that you just created.
     
    Haribo98 likes this.
  19. Offline

    Haribo98

    Example code please :D thats the bit that always confused me...
     
  20. Offline

    CorrieKay

    In the tutorial i gave, you honestly dont need to change the code at all. It was all in your changes.

    Pass in the config object that youre trying to save as a parameter, dont load the config from scratch. Get the file, and save it to that file. All the code you need is there, but you have some extra code you dont need.

    public void saveConfig(String name, FileConfiguration config) {
    if (!name.endsWith(".yml")) {
    name = name + ".yml";
    }
    FileConfiguration config = getConfig(name);
    File file = new File(plugin.getDataFolder(),name);
    createConfig(name);
    try {
    config.save(file);
    } catch (IOException e) {
    e.printStackTrace();
    }
    }
     
    Haribo98 likes this.
  21. Offline

    Haribo98

    Done that, now I am guessing to save you use:
    Code:JAVA
    1. customConfig.saveConfig("players", customConfig.getConfig("players"));


    EDIT: Tried that, still blank
     
  22. Offline

    CorrieKay

    No.. what??

    Look, to use your config you need to do this

    Code:
    FileConfiguration config = cusomtConfig.getConfig("player");
    //make a change or something
    config.set("players", new String[]{"player1","player2"});
    customConfig.saveConfig("players",config);
    
    I think theres a clear misunderstanding of what a file configuration is. Whenever you set a variable in the config, it does not save it to the file. It saves it to an object, which has litterally nothing to do with the actual harddrive file. You need to give it the config that you modified, not a new one. With the code you just tested, you did the same exact thing that you did in the code that i fixed.
     
    Haribo98 likes this.
  23. Offline

    Haribo98

    CorrieKay
    I praise you almighty one... Thank you!!!

    1 last thing before my problem is solved completely.
    I get an NPE on lines 23 and 51: http://pastebin.com/cPZR6LGR

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

    fireblast709

    Don't create the ConfigAccessor like
    Code:java
    1. new ConfigAccessor(null);
     
  25. Offline

    Haribo98

    I'm not doing that. All my config accessors are used correctly;
    Code:JAVA
    1. ConfigAccessor customConfig = new ConfigAccessor(plugin);
     
  26. Offline

    fireblast709

    then plugin is empty on that line
     
  27. Offline

    Haribo98

    It's not that
    Code:JAVA
    1. private Main plugin;
    2. ConfigAccessor customConfig = null;
    3.  
    4. public TeamAPI(Main plugin) {
    5. this.plugin = plugin;
    6. mAPI = new MapAPI(plugin);
    7. gAPI = new GameAPI(plugin);
    8. customConfig = new ConfigAccessor(plugin);
    9. }
    10.  
    11. FileConfiguration playerConfig = customConfig.getConfig("players");
    12. FileConfiguration matchConfig = customConfig.getConfig("match");
    13.  


    I get NPE on
    Code:JAVA
    1. FileConfiguration playerConfig = customConfig.getConfig("players");


    Then the plugin shuts off and doesn't read the rest of the code.
     
  28. Offline

    fireblast709

    customConfig is null, and initialized after you call customConfig.getConfig("players") and "match"
    Fix:
    Code:java
    1. private Main plugin;
    2. ConfigAccessor customConfig;
    3. FileConfiguration playerConfig;
    4. FileConfiguration matchConfig;
    5.  
    6. public TeamAPI(Main plugin)
    7. {
    8. this.plugin = plugin;
    9. mAPI = new MapAPI(plugin);
    10. gAPI = new GameAPI(plugin);
    11. customConfig = new ConfigAccessor(plugin);
    12. playerConfig = customConfig.getConfig("players");
    13. matchConfig = customConfig.getConfig("match");
    14. }
     
  29. Offline

    Haribo98

    Last edited by a moderator: May 24, 2016
  30. Offline

    Sagacious_Zed Bukkit Docs

    You have a null pointer. It appears that fileName is the null pointer.
     
Thread Status:
Not open for further replies.

Share This Page