[TUT] Bukkit's new FileConfiguration API! Create a YAML configuration!

Discussion in 'Resources' started by Windwaker, Oct 23, 2011.

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

    Windwaker

    Introduction
    Well there seems to be a lack of help on this topic and a lot of need for it so in this tutorial I will teach you how to create a configuration for your bukkit plugin! This tutorial assumes that you already have a basic understanding of Java and Bukkit API and know how to set up a standard plugin.

    Index:
    This tutorial covers:
    • Loading a configuration
    • Creating "default" fields and values
    • Setting a fields value
    • Getting a fields value
    • Adding to a list
    • The config.yml and how you can use it (located in the same directory as your plugin.yml)
    Loading your configuration for the first time!
    Loading your configuration (open)
    Before you start writing your configuration you are going to have to create it first! You're going to want to create a method that load's the config and it's defaults (don't worry we will create the defaults soon). We will call this, "loadConfiguration();"
    Code:java
    1. public void loadConfiguration(){
    2. //See "Creating you're defaults"
    3. plugin.getConfig().options().copyDefaults(true); // NOTE: You do not have to use "plugin." if the class extends the java plugin
    4. //Save the config whenever you manipulate it
    5. plugin.saveConfig();

    After creating this method in your main class, go ahead and add it to your onEnable();
    Code:java
    1. public void onEnable(){
    2. loadConfiguration();
    3. System.out.print("[TestPlugin] TestPlugin Enabled!");

    Reloading your config:
    At times it is going to seem necessary to reload your plugin, perhaps to get a fields value. You can simply create this method to reload your configuration.
    Code:java
    1. plugin.reloadConfig()

    Great! Now we have the config loaded! +[diamond] (It should still be blank)

    Creating your defaults
    Creating your defaults (open)
    What are defaults? Defaults are what the default values of a given field is when someone starts the plugin without configuring it in anyway. This is how the config will be loaded for the first time or if a given field doesn't exist. Bukkit's Yaml configuration is a certain format, so we have to make sure we satisfy their needs. Let's edit our "loadConfiguration();" method again. Example default:
    Code:java
    1. public void loadConfiguration(){
    2. String path = "Who.Stole.The.Cookie"
    3. plugin.getConfig().addDefault(path, "I did!");
    4. plugin.getConfig().options().copyDefaults(true);
    5. plugin.saveConfig();

    This would produce when loaded:
    Code:
    Who:
      Stole:
        The:
          Cookie: I did!
    You see! The first argument when setting defaults is the "path" while the second is the "value" Wasn't that easy?

    Setting a field's value
    Setting config values (open)
    In many cases you are going to need to set the values of defaults in your config. You can write many things to your config: Strings, integers, boolean, etc. and you can use these to set the values of some of your defaults.

    Set a boolean value:
    Code:java
    1. plugin.getConfig().set("path.to.boolean", true);

    Would produce:
    Code:
    path:
      to:
        boolean: true
    Set a String value:
    Code:java
    1. plugin.getConfig().set("path.to.string", "Hello world :D");

    Would produce:
    Code:
    path:
      to:
        string: Hello world :D
    Setting a list (note this is NOT adding to a list)
    Code:java
    1. String[] list = {"This", "Is", "A", "List"}
    2. plugin.getConfig().set("path.to.list", Arrays.asList(list));

    Would produce:
    Code:
    path:
      to:
        list:
        - This
        - Is
        - A
        - List

    Getting a fields value
    Getting a fields value (open)
    Getting a fields value is imperative for having your config have any use by getting a value you can tell how your plugin is supposed to respond according to the config's settings. Bukkit's FileConfiguration currently allows you to get and set
    • Boolean
    • Int
    • String
    • List
    If you need to get one of these follow these examples, respectively:

    Boolean:
    Code:java
    1. plugin.getConfig().getBoolean("path.to.boolean");

    So if I had:
    Code:
    path:
      to:
        boolean: true
    I would get "true"

    Int:
    Code:java
    1. plugin.getConfig().getInt("path.to.int");

    So if I had:
    Code:
    path:
      to:
        int: 1
    I would get "1"

    String:
    Code:java
    1. plugin.getConfig().getString("path.to.string");

    So if I had:
    Code:
    path:
      to:
        string: Hello world :D
    I would get "Hello world :D"

    List:
    Code:java
    1. plugin.getConfig().getList("path.to.list");

    So if I had:
    Code:
    path:
      to:
        list:
        - This
        - Is
        - A
        - List
    I would get the list.

    Adding to a list
    Adding to a list (open)
    Adding to a list is necessary if you want to create a working list in your config. Above I showed you how to set a list but this is different. Setting would be setting the contents of the list while adding would add a new variable to the contents of the list.

    How to add to a list:
    Code:java
    1. plugin.getConfig().getList("path.to.list").add("A new string");

    Would produce something like this:
    Code:
    path:
      to:
        list:
        - An old string
        - A new string

    Making use of the config.yml (optional but better)
    Making use of the config.yml (optional) (open)
    If you so desire, you can write out your default values in a config.yml located in the same library as your plugin.yml. This makes commenting really easy as well as not having a bunch of defaults crowd your code. You can get the defaults from the config.yml

    What I reccommend:
    1. Create your defaults in your loadConfiguration(); method as described above
    2. Run the plugin to create a config
    3. Copy config
    4. Paste into the same directory as your plugin.yml
    5. Delete defaults in code
    6. Keep config.options().copyDefaults(true);
    Now your config will write from the config.yml and you are formatted correctly :)

    Making comments:
    Comments are lines in YAML format that are ignored. They can be denoted with the '#' key. For example:
    PHP:
    #This line would be ignored
    while this onewould not
    This can be useful for providing information in your config as well as licenses, copyrights, etc. Whateva you want! :D


    Thank you for baring with me and I hope you found this tutorial informative :) If you found any flaws in the tutorial or additions please point them out and I would be happy to edit the OP. Please respond telling me if this was helpful or not and if you have any questions.
     
    f1oating, Lance798, ZygoZ and 39 others like this.
  2. Offline

    iffa

    I'll just leave this here:
    My way of creating config files (open)

    Code:java
    1. // Package Declaration
    2. package some.thing.goes.here;
    3.  
    4. // Java Imports
    5. import java.io.File;
    6. import java.io.FileNotFoundException;
    7. import java.io.FileOutputStream;
    8. import java.io.IOException;
    9. import java.io.InputStream;
    10.  
    11. // Bukkit Imports
    12. import org.bukkit.Bukkit;
    13. import org.bukkit.configuration.InvalidConfigurationException;
    14. import org.bukkit.configuration.file.YamlConfiguration;
    15.  
    16. /**
    17.  * A class that handles the configuration file.
    18.  *
    19.  * @author iffa
    20.  * @author Pandarr
    21.  * @author Sammy
    22.  * @author kitskub
    23.  */
    24. public class Config {
    25. // Variables
    26. private static YamlConfiguration myConfig;
    27. private static File configFile;
    28. private static boolean loaded = false;
    29.  
    30. /**
    31.   * Gets the configuration file.
    32.   *
    33.   * @return the myConfig
    34.   */
    35. public static YamlConfiguration getConfig() {
    36. if (!loaded) {
    37. loadConfig();
    38. }
    39. return myConfig;
    40. }
    41.  
    42. /**
    43.   * Gets the configuration file.
    44.   *
    45.   * @return Configuration file
    46.   */
    47. public static File getConfigFile() {
    48. return configFile;
    49. }
    50.  
    51. /**
    52.   * Loads the configuration file from the .jar.
    53.   */
    54. public static void loadConfig() {
    55. configFile = new File(Bukkit.getServer().getPluginManager().getPlugin("YourPlugin").getDataFolder(), "config.yml");
    56. if (configFile.exists()) {
    57. myConfig = new YamlConfiguration();
    58. try {
    59. myConfig.load(configFile);
    60. } catch (FileNotFoundException ex) {
    61. // TODO: Log exception
    62. } catch (IOException ex) {
    63. // TODO: Log exception
    64. } catch (InvalidConfigurationException ex) {
    65. // TODO: Log exception
    66. }
    67. loaded = true;
    68. } else {
    69. try {
    70. Bukkit.getServer().getPluginManager().getPlugin("BananaSpace").getDataFolder().mkdir();
    71. InputStream jarURL = Config.class.getResourceAsStream("/config.yml");
    72. copyFile(jarURL, configFile);
    73. myConfig = new YamlConfiguration();
    74. myConfig.load(configFile);
    75. loaded = true;
    76. // TODO: Log that config has been loaded
    77. } catch (Exception e) {
    78. // TODO: Log exception
    79. }
    80. }
    81. }
    82.  
    83. /**
    84.   * Copies a file to a new location.
    85.   *
    86.   * @param in InputStream
    87.   * @param out File
    88.   *
    89.   * @throws Exception
    90.   */
    91. static private void copyFile(InputStream in, File out) throws Exception {
    92. InputStream fis = in;
    93. try {
    94. byte[] buf = new byte[1024];
    95. int i = 0;
    96. while ((i = fis.read(buf)) != -1) {
    97. fos.write(buf, 0, i);
    98. }
    99. } catch (Exception e) {
    100. throw e;
    101. } finally {
    102. if (fis != null) {
    103. fis.close();
    104. }
    105. if (fos != null) {
    106. fos.close();
    107. }
    108. }
    109. }
    110.  
    111. /**
    112.   * Constructor of SpaceConfig.
    113.   */
    114. private Config() {
    115. }
    116. }


    Hey, it's not the perfect solution but this way you can have as many comments as you'd like (anywhere), but Bukkit will destroy them when saving :(.

    Also, thanks for a good tutorial.
     
    Walker Crouse likes this.
  3. Offline

    DomovoiButler

    @iffa same as my tuts xD...but better(no offence meant)... u dont like copyDefaults and addDefaults too?
     
  4. Offline

    iffa

    Most users(/customers) are dumber than a rock, so you need to have a comment for pretty much everything. This way they have comments. If copyDefaults kept the comments I would use it.
     
  5. Offline

    DomovoiButler

  6. Offline

    iffa

  7. Offline

    Sagacious_Zed Bukkit Docs

    Ultimate_n00b, Unscrewed and zhuowei like this.
  8. Offline

    DomovoiButler

    eh...well the advanced topic was not there when i posted my tut, and also as i said in my tuts...i dont like copyDefaults so i made a tut in here
     
  9. Offline

    thehutch

    Hmm I did my little tutorial the same as iffa and yet you @Sagacious_Zed complained about it I dont see anything wrong with his so whats wrong with mine?
     
  10. Offline

    Sagacious_Zed Bukkit Docs

    @thehutch
    By the third time, I really did not want to point out the use of addDefaults, but there you made me do it.

    On other factual stuff, there are actually more getters and even though there is only one set methods, you can actually set several more types than what you have listed. As well as any class that implements ConfigurationSerializable but that is worthy of it's own article.
     
  11. Offline

    iffa

    Oh, and I also made this (proven to be extremely useful, you just add an enum and the file itself in the jar):
    Code:java
    1. // Package Declaration
    2. package me.iffa.bananaspace.config;
    3.  
    4. // Java Imports
    5. import java.io.File;
    6. import java.io.FileNotFoundException;
    7. import java.io.FileOutputStream;
    8. import java.io.IOException;
    9. import java.io.InputStream;
    10. import java.util.EnumMap;
    11. import java.util.Map;
    12. import java.util.logging.Level;
    13.  
    14. //BananaSpace Imports
    15. import me.iffa.bananaspace.BananaSpace;
    16. import me.iffa.bananaspace.api.SpaceMessageHandler;
    17.  
    18. // Bukkit Imports
    19. import org.bukkit.Bukkit;
    20. import org.bukkit.configuration.InvalidConfigurationException;
    21. import org.bukkit.configuration.file.YamlConfiguration;
    22.  
    23. /**
    24.  * A class that handles the configuration file.
    25.  *
    26.  * @author iffa
    27.  * @author Pandarr
    28.  * @author Sammy
    29.  * @author kitskub
    30.  */
    31. public class SpaceConfig {
    32. // Variables
    33. private static Map<ConfigFile, YamlConfiguration> config = new EnumMap<ConfigFile, YamlConfiguration>(ConfigFile.class);
    34. private static Map<ConfigFile, File> configFile = new EnumMap<ConfigFile, File>(ConfigFile.class);
    35. private static Map<ConfigFile, Boolean> loaded = new EnumMap<ConfigFile, Boolean>(ConfigFile.class);
    36.  
    37. /**
    38.   * Gets the configuration file.
    39.   *
    40.   * @param configfile ConfigFile to get
    41.   *
    42.   * @return YamlConfiguration object
    43.   */
    44. public static YamlConfiguration getConfig(ConfigFile configfile) {
    45. if (loaded.containsKey(configfile) && !loaded.get(configfile)) {
    46. loadConfig(configfile);
    47. }
    48. return config.get(configfile);
    49. }
    50.  
    51. /**
    52.   * Gets the configuration file.
    53.   *
    54.   * @param configfile ConfigFile to get
    55.   *
    56.   * @return Configuration file
    57.   */
    58. public static File getConfigFile(ConfigFile configfile) {
    59. return configFile.get(configfile);
    60. }
    61.  
    62. /**
    63.   * Checks if the configuration file is loaded.
    64.   *
    65.   * @param configfile ConfigFile to get
    66.   *
    67.   * @return True if configuraton file is loaded
    68.   */
    69. public static boolean getLoaded(ConfigFile configfile) {
    70. return loaded.get(configfile);
    71. }
    72.  
    73. /**
    74.   * Loads all configuration files. (can be used to save a total of 2 lines!)
    75.   */
    76. public static void loadConfigs() {
    77. for (ConfigFile configfile : ConfigFile.values()) {
    78. loadConfig(configfile);
    79. }
    80. }
    81.  
    82. /**
    83.   * Loads the configuration file from the .jar.
    84.   *
    85.   * @param configfile ConfigFile to load
    86.   */
    87. public static void loadConfig(ConfigFile configfile) {
    88. configFile.put(configfile, new File(Bukkit.getServer().getPluginManager().getPlugin("BananaSpace").getDataFolder(), configfile.getFile()));
    89. if (configFile.get(configfile).exists()) {
    90. config.put(configfile, new YamlConfiguration());
    91. try {
    92. config.get(configfile).load(configFile.get(configfile));
    93. } catch (FileNotFoundException ex) {
    94. SpaceMessageHandler.print(Level.WARNING, ex.getMessage());
    95. loaded.put(configfile, false);
    96. return;
    97. } catch (IOException ex) {
    98. SpaceMessageHandler.print(Level.WARNING, ex.getMessage());
    99. loaded.put(configfile, false);
    100. return;
    101. } catch (InvalidConfigurationException ex) {
    102. SpaceMessageHandler.print(Level.WARNING, ex.getMessage());
    103. loaded.put(configfile, false);
    104. return;
    105. }
    106. loaded.put(configfile, true);
    107. } else {
    108. try {
    109. Bukkit.getServer().getPluginManager().getPlugin("BananaSpace").getDataFolder().mkdir();
    110. InputStream jarURL = SpaceConfig.class.getResourceAsStream("/" + configfile.getFile());
    111. copyFile(jarURL, configFile.get(configfile));
    112. config.put(configfile, new YamlConfiguration());
    113. config.get(configfile).load(configFile.get(configfile));
    114. loaded.put(configfile, true);
    115. SpaceMessageHandler.print(Level.INFO, "Generated " + configfile.getFile() + " file for version " + BananaSpace.version);
    116. } catch (Exception e) {
    117. SpaceMessageHandler.print(Level.SEVERE, e.toString());
    118. }
    119. }
    120. }
    121.  
    122. /**
    123.   * Copies a file to a new location.
    124.   *
    125.   * @param in InputStream
    126.   * @param out File
    127.   *
    128.   * @throws Exception
    129.   */
    130. static private void copyFile(InputStream in, File out) throws Exception {
    131. InputStream fis = in;
    132. try {
    133. byte[] buf = new byte[1024];
    134. int i = 0;
    135. while ((i = fis.read(buf)) != -1) {
    136. fos.write(buf, 0, i);
    137. }
    138. } catch (Exception e) {
    139. throw e;
    140. } finally {
    141. if (fis != null) {
    142. fis.close();
    143. }
    144. if (fos != null) {
    145. fos.close();
    146. }
    147. }
    148. }
    149.  
    150. /**
    151.   * Constructor of SpaceConfig.
    152.   */
    153. private SpaceConfig() {
    154. }
    155.  
    156. /**
    157.   * All config files.
    158.   */
    159. public enum ConfigFile {
    160. // Enums
    161. PLANETS("planets.yml"),
    162. CONFIG("config.yml"),
    163. IDS("ids.yml");
    164.  
    165. // Variables
    166. private String file;
    167.  
    168. /**
    169.   * Constructor of ConfigFile.
    170.   * @param file
    171.   */
    172. ConfigFile(String file) {
    173. this.file = file;
    174. }
    175.  
    176. /**
    177.   * Gets the file associated with the enum.
    178.   *
    179.   * @return File associated wiht the enum
    180.   */
    181. public String getFile() {
    182. return this.file;
    183. }
    184. }
    185. }

    You simply call configclass.loadConfigs() to load all of them, but you can use .loadConfig(ConfigFile configfile) as well if you only want one loaded.
     
    dadaemon likes this.
  12. I love it! Simple and easy to use.
     
  13. Offline

    enjikaka

    I am having problems...

    I have all my different messages for my plugins in a single class (Msg.class)
    Whener I want to, for example, display that they are welcome home I just type
    Main.pm(player, Msg.hwh); Which is the same as player.sendMessage("§8Welcome home!");

    Now the problem is that I can't use the Config thingy inside Msg.class

    Example of one of my Strings in Msg.class:
    PHP:
    public static String np cc "You are not allowed to use this command.";
    I have set this in the main class (above all public static strings):
    PHP:
    FileConfiguration config getConfig();
    But when I do:
    PHP:
    public static String np config.getString("messages.regular.np");
    It says that config "Cannot make a static reference to the non-static field config".
    On eifx is to change modifyer of config to static but when I do that, getConfig() gets redunderlined saying "Cannot make a static reference to the non-static method getConfig(); from the type JavaPlugin".

    How do I fix this?
    I load the Configuration like described in my Main.class

    EDIT:
    If I change it to a non-staic:
    PHP:
    public static String np = new Msg().config.getString("messages.regular.np");
    it gives a huge error in console :/

     
  14. Offline

    SpikeMeister

    As a rule, try to avoid using the static keyword. It goes against OO principles and leads to errors like the one you are having. Try changing the string np to not be static. This will make it so that you can't just go Msg.np, you will have to first create a new Msg object (Msg m = new Msg()) and then ask for the string from the object (m.np). This is still not optimal as it breaks encapsulation (you should avoid using the public keyword for fields whenever possible) but it will work.

    To avoid this in the future, I would suggest reading up on the difference between static and non-static and public/private.
     
    Walker Crouse likes this.
  15. Offline

    enjikaka

    As fast as I do things as Msg m = new Msg(); my code breaks all plugins that needs this plugin breaks too.

    EDIT:
    I spend 10 hours yesterday chaning my Msg m = new Msg(); to static ones because bukkit didn't seem to like it! I'd love to NOT change it back...
     
  16. Offline

    SpikeMeister

    I'm not sure but you could maybe assign config.getString to a local variable, then assign the local variable to np.
     
  17. Offline

    enjikaka

    PHP:
    private String FFnp config.getString("messages.regular.np");
    public static 
    String np cc FFnp;
    Now it says that non-staic thing on FFnp on line 2 instead :/

    Ok a new problem then!

    How do I read the same config.yml from 2 different plugins? :)

    I got everything working like a charm now :p
    I made this for you people out there:
    PHP:
    package se.enji;

    import java.io.File;
    import java.util.logging.Logger;

    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.configuration.file.FileConfiguration;
    import org.bukkit.entity.Player;
    import org.bukkit.plugin.PluginDescriptionFile;
    import org.bukkit.plugin.java.JavaPlugin;

    public class 
    Plugin extends JavaPlugin {
        
    Logger log Logger.getLogger("Minecraft"); //Logger to print stuff in console
        
    FileConfiguration config;//Tell plugin we are going to usa a config file

        
    @Override
        
    public void onEnable() {
            
    loadConfiguration();//Load configfile
            
    PluginDescriptionFile pff this.getDescription();//Get info from plugin.yml
            
    String PluginName pff.getName();//Get the name of the plugin from plugin.yml
            
    String PluginVersion pff.getVersion();//Get the version number of the plugin from plugin.yml
            
    log.info(PluginName PluginVersion " is enabled.");//Print to console that plugin is enabled
        
    }

        @
    Override
        
    public void onDisable() {
            
    PluginDescriptionFile pff this.getDescription();//Get info from plugin.yml
            
    String PluginName pff.getName();//Get the name of the plugin from plugin.yml
            
    String PluginVersion pff.getVersion();//Get the version number of the plugin from plugin.yml
            
    log.info(PluginName PluginVersion " is disabled.");//Print to console that plugin is disabled
        
    }

        public 
    void loadConfiguration() {//to load config file
            
    config getConfig();//get the configuration

            
    addDefault("msg.hello.command""You did the hello command");//add and set default

            
    config.options().copyDefaults(true);//copy the default from above
            
    saveConfig();//write them to file
        
    }

        private 
    String getNode(String m) {//to get a string from the config file
            
    return config.getString(m);
        }

        private 
    void addDefault(String oString p) {//add and set default
            
    File configFile = new File("plugins" File.separator this.getDescription().getName() + File.separator "config.yml");//config file
            
    config.addDefault(op);//add default to config
            
    if (configFile.exists() == false) {//if there is no config file, set defaults as well!
                
    config.set(op);
            }
        }

        public 
    boolean onCommand(CommandSender senderCommand cmdString commandString[] args) {//when a player sends a command
            
    if (sender instanceof Player) {//if the sender of the command is a player on the server->
                
    Player player = (Player)sender;//then the player is a player
                
    if(command.equalsIgnoreCase("hello")) {//if the command the player on the server sent was "/hello"
                    
    if (player.hasPermission("plugin.use.hello") || player.isOp()) {//Check if the player has permission to use the command or if the player is OP
                        
    String sendThis getNode("msg.hello.command"); //Get the value of msg.hello.command and put it in the String named sendThis
                        
    player.sendMessage(sendThis);//Send the string sendThis to the player in chat
                    
    }
                    else {
    //If player does not have permission for the command
                        
    player.sendMessage("§cYou are not allowed to use this command.");//Give him info that he can't use it
                    
    }
                }
            }
            else {
                
    log.info("This command is made to be executed in game only.");//Display message if command is ran in console or similar
            
    }
            return 
    false;
        }
    }
    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 20, 2016
  18. Offline

    Windwaker

    @enjikaka
    Glad you got it all sorted out :)
     
  19. Offline

    md_5

    Oh well at least I know that it actually works now.
     
  20. Offline

    iffa

    You do know you could replace most of your //-comments with some proper JavaDoc-stuff. Would make it a lot more readable!
     
  21. Offline

    enjikaka

    Instead of JavaDoc-thing why not just do:
    Code:java
    1.  
    2. /*
    3. comment
    4. on
    5. multi
    6. line
    7. (readable)
    8. */
    9.  


    Or is that the JavaDoc thingy?
     
  22. Offline

    iffa

    Yes, with an extra * in the starting line.
     
  23. Offline

    Sagacious_Zed Bukkit Docs

    Actually, since Fieldmaster brought up reloading configs on the wiki page. Here are a few more things that could be improved upon.

    The loadConfiguration method has a misleading name. The first time the method is called, rather first time getConfig is called it will load from the disc, but all subsequent calls to getConfig will return the same object. Which means loadConfig becomes just a saveConfig method after the first call.

    The name also implies that anytime the method is called, it will load from disc, which is generally not the case, so it cannot be used to update the configuration object based on updates to the underlying file.

    Finally, the bit about reloading. if the user calls reloadConfig, you must manually reassign the config variable with the new configuration object, due to the way reloadConfig works apparently.

    These changes help give new comers the best foundation to build their plugin's upon.
     
  24. Offline

    enjikaka

    Can I create a let's say notch.yml? So that i have 1 config yaml file for each player (enjikaka.yml, notch.yml etc)?
     
  25. Offline

    Sagacious_Zed Bukkit Docs

    You can, but i would not recommend it, as it will load more files from the disk.
     
  26. Yes you can. You simply would need to create a player Listener to get the player name when he joins and create a file with this name then ;)
     
  27. Offline

    Icelaunche

    what does this mean?

    PHP:
    09:39:32 [SEVEREError occurred while enabling ServerTime v1.0 (Is it up to date?): me.Icelaunche.ServerTime.ServerTime.getConfig()Lorg/bukkit/configuration/file/FileConfiguration;
    java.lang.NoSuchMethodErrorme.Icelaunche.ServerTime.ServerTime.getConfig()Lorg/bukkit/configuration/file/FileConfiguration;
        
    at me.Icelaunche.ServerTime.ServerTime.loadConfiguration(ServerTime.java:56)
        
    at me.Icelaunche.ServerTime.ServerTime.onEnable(ServerTime.java:47)
        
    at org.bukkit.plugin.java.JavaPlugin.setEnabled(JavaPlugin.java:126)
        
    at org.bukkit.plugin.java.JavaPluginLoader.enablePlugin(JavaPluginLoader.java:920)
        
    at org.bukkit.plugin.SimplePluginManager.enablePlugin(SimplePluginManager.java:278)
        
    at org.bukkit.craftbukkit.CraftServer.loadPlugin(CraftServer.java:173)
        
    at org.bukkit.craftbukkit.CraftServer.enablePlugins(CraftServer.java:156)
        
    at net.minecraft.server.MinecraftServer.e(MinecraftServer.java:297)
        
    at net.minecraft.server.MinecraftServer.a(MinecraftServer.java:284)
        
    at net.minecraft.server.MinecraftServer.init(MinecraftServer.java:152)
        
    at net.minecraft.server.MinecraftServer.run(MinecraftServer.java:348)
        
    at net.minecraft.server.ThreadServerApplication.run(SourceFile:417)
    I get it when i try to run the plugin after i added the config
     
  28. Offline

    Windwaker

    Post you're main class code
     
  29. Offline

    Icelaunche

    Dont mind the server time... i turned this into a test plugin for testing stuff

    Code:text
    1. /*
    2. * To change this template, choose Tools | Templates
    3. * and open the template in the editor.
    4. */
    5. package me.Icelaunche.ServerTime;
    6.  
    7. import java.util.ArrayList;
    8. import java.util.HashMap;
    9. import java.util.logging.Logger;
    10. import org.bukkit.ChatColor;
    11. import org.bukkit.block.Block;
    12. import org.bukkit.command.Command;
    13. import org.bukkit.command.CommandSender;
    14. import org.bukkit.entity.Player;
    15. import org.bukkit.plugin.PluginManager;
    16. import org.bukkit.plugin.java.JavaPlugin;
    17. import org.bukkit.Bukkit;
    18. import org.bukkit.configuration.InvalidConfigurationException;
    19. import org.bukkit.configuration.file.YamlConfiguration;
    20.  
    21. import java.io.File;
    22. import java.io.FileNotFoundException;
    23. import java.io.FileOutputStream;
    24. import java.io.IOException;
    25. import java.io.InputStream;
    26. import java.util.Arrays;
    27. import org.bukkit.configuration.file.FileConfiguration;
    28.  
    29. /**
    30. *
    31. * @author Icelaunche
    32. */
    33. public class ServerTime extends JavaPlugin {
    34.  
    35. private static final Logger log = Logger.getLogger("Minecraft");
    36. FileConfiguration config;
    37.  
    38. public class Config {
    39. }
    40.  
    41. public void onDisable() {
    42. log.info("[ServerTime] ServerTime Version 1.0 By Icelaunche DISABLED! ");
    43.  
    44. }
    45.  
    46. public void onEnable() {
    47. loadConfiguration();
    48. PluginManager pm = getServer().getPluginManager();
    49. log.info("[ServerTime]ServerTime ENABLED!");
    50. log.info("[ServerTime]By Icelaunche ");
    51. log.info("[ServerTime]ServerTime Version 1.0 ");
    52.  
    53. }
    54.  
    55. public void loadConfiguration() {
    56. config = getConfig();
    57. String path = "Configs.Smell.Bad";
    58. config.addDefault(path, "true");
    59. config.set("The.End.is.comming", true);
    60. config.set("derp.Smells", "true");
    61. config.getInt("path.to.int");
    62. config.getString("path.to.string");
    63. String[] list = {"This", "Is", "A", "List"};
    64. config.set("path.to.list", Arrays.asList(list));
    65. config.getList("path.to.list");
    66. getConfig().getList("path.to.list").add("Cookies");
    67. config.options().copyDefaults(true);
    68. saveConfig();
    69.  
    70. }
    71.  
    72. public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) {
    73. if (commandLabel.equalsIgnoreCase("explevel")) {
    74. Player player = (Player) sender;
    75. player.getTotalExperience();
    76. return true;
    77. }
    78. return false;
    79. }
    80. private static YamlConfiguration myConfig;
    81. private static File configFile;
    82. private static boolean loaded = false;
    83.  
    84. public static File getConfigFile() {
    85. return configFile;
    86. }
    87. }
    88.  
     
  30. Offline

    arnie231

    @Walker_Crouse

    Thank you for this
     
Thread Status:
Not open for further replies.

Share This Page