An unusual request

Discussion in 'Archived: Plugin Requests' started by boardinggamer, Nov 21, 2012.

  1. Offline

    boardinggamer

    So I want to learn A LOT about configuration files/yml files

    I am really interested in them when it comes to storing data in a file and having more then one file for it. I only know how to make the default config.yml file using the getConfig() method so I would like to learn about how to make different types and different files.

    I want a plugin that simply makes the different types of files and lets you edit them from in game if possible so I can use it to study from.

    I find this way better then javadocs or just reading about it because this way has a more direct way of seeing how it works. and how to use everything
     
  2. Offline

    RingOfStorms

    Well im not writing a plugin for writing configs, seems like a bit of a waste. But ill put together a few tips here:

    Loading up custom files (Used for configs.yml as well)

    Here we are checking if your custom file.yml exists, and if it doesn't then it will be created base on the one you made in your plugin. Saving from the resource means it must be in the plugin, like where you make the plugin.yml, and config.yml files.
    Code:
    mainClassThatExtendsJavaPlugin plugin;
    File file = new File(plugin.getDataFolder(), "file.yml");
    if(file.exists() == false) {
      plugin.saveResource("file.yml", false);
    }
    
    Here is how you would do the same as getConfig() but with a custom file:
    Code:
    File file = new File(plugin.getDataFolder(), "file.yml");
    if(file.exists() == false) return; //Make sure the file exists before doing things with it :P
    FileConfiguration fileConf = YamlConfiguration.loadConfiguration(file);
     
    //use fileConf just like getConfig()
    fileConf.set("path", "value");
    fileConf.contains("path");
     
    //Make sure to save after you do things like .set() or they won't apply to the file!
    fileConf.save(file);
    
    Hope this helps. @boardinggamer
     
  3. Offline

    boardinggamer

    RingOfStorms
    Thank you so much. This is so helpful!!!!

    EDIT: I have one question though.
    How would I check if the config contains nothing at all? I checked if the config contained the players name and it sends out a null pointer exception
     
  4. Offline

    RingOfStorms

    @boardinggamer

    Code:
    if(fileConf.contains("playerName") == false) {
      //Don't do anything, it doesn't exist
    }else{
      //Player is there, do stuff
    }
    
    Or simply cut that in half
    Code:
    if(fileConf.contains("playerName") {
      //do stuff
    }
    
     
  5. Offline

    boardinggamer

    RingOfStorms
    That's what I have it gets a null pointer exception

    Code:java
    1. @EventHandler
    2. public void PlayerJoinEvent(PlayerJoinEvent event) {
    3. String name = event.getPlayer().getName();
    4. if (money.playerInMoneyConfig(name) == false){ //this is line 23
    5. money.addPlayerToConfig(name);
    6. this.plugin.reloadConfigs();
    7. }
    8. }


    Code:java
    1. public boolean playerInMoneyConfig(String name){
    2. boolean in = false;
    3. File players = new File(this.plugin.getDataFolder(), "players.yml");
    4. if (players.exists()){
    5. FileConfiguration pconf = YamlConfiguration.loadConfiguration(players);
    6. if (pconf.contains(name)){
    7. in = true;
    8. }
    9. }
    10. return in;
    11. }


    Code:bash
    1. 01:43:47 [SEVERE] Could not pass event PlayerJoinEvent to Ecahmoney v1.0
    2. org.bukkit.event.EventException
    3. at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:341)
    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.ServerConfigurationManagerAbstract.c(ServerConfigurationManagerAbstract.java:153)
    8. at net.minecraft.server.ServerConfigurationManagerAbstract.a(ServerConfigurationManagerAbstract.java:93)
    9. at net.minecraft.server.NetLoginHandler.d(NetLoginHandler.java:132)
    10. at net.minecraft.server.NetLoginHandler.c(NetLoginHandler.java:45)
    11. at net.minecraft.server.DedicatedServerConnectionThread.a(DedicatedServerConnectionThread.java:44)
    12. at net.minecraft.server.DedicatedServerConnection.b(SourceFile:29)
    13. at net.minecraft.server.MinecraftServer.r(MinecraftServer.java:595)
    14. at net.minecraft.server.DedicatedServer.r(DedicatedServer.java:222)
    15. at net.minecraft.server.MinecraftServer.q(MinecraftServer.java:493)
    16. at net.minecraft.server.MinecraftServer.run(MinecraftServer.java:426)
    17. at net.minecraft.server.ThreadServerApplication.run(SourceFile:856)
    18. Caused by: java.lang.NullPointerException
    19. at boardinggamer.Ecahmoney.Listeners.PlayerListener.PlayerJoinEvent(PlayerListener.java:23)
    20. at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    21. at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    22. at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    23. at java.lang.reflect.Method.invoke(Method.java:616)
    24. at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:339)
    25. ... 14 more
    26.  
     
  6. Offline

    RingOfStorms

  7. Offline

    boardinggamer

Share This Page