Why are my configs empty? :(

Discussion in 'Plugin Development' started by dmoney12321, Nov 18, 2013.

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

    dmoney12321

    So i am making a plugin that relies heavily on config.yml files. At the advice of some people on an IRC, i made a config.yml in the .jar, but that is empty and it will not make a config.yml in the plugins/WhereToSell/
    Here is the code:
    Code:java
    1. package me.Dmoney12321.WhereToSell;
    2.  
    3. import java.io.File;
    4. import org.bukkit.ChatColor;
    5. import org.bukkit.command.Command;
    6. import org.bukkit.command.CommandSender;
    7. import org.bukkit.entity.Player;
    8. import org.bukkit.plugin.PluginDescriptionFile;
    9. import org.bukkit.plugin.java.JavaPlugin;
    10.  
    11. public class WhereToSell extends JavaPlugin {
    12.  
    13. @Override
    14. public void onDisable()
    15. {
    16. PluginDescriptionFile pdfFile = this.getDescription();
    17. this.getLogger().info(pdfFile.getName() + " Has Been Disabled!");
    18. }
    19.  
    20. @Override
    21. public void onEnable()
    22. {
    23. PluginDescriptionFile pdfFile = this.getDescription();
    24. this.getLogger().info(pdfFile.getName() + "Version" + pdfFile.getVersion() + " Has Been Enabled!");
    25. File configFile = new File(this.getDataFolder(), "config.yml");
    26. if (!configFile.exists()){
    27. this.getConfig().options().copyDefaults(true);
    28. this.getLogger().info("Generating config.yml...");
    29. this.getConfig().addDefault("buydiamond", "Default");
    30. this.getConfig().addDefault("buyiron" , "Default");
    31. this.getConfig().addDefault("buygold", "Default");
    32. this.getConfig().addDefault("buycoal", "Default");
    33. this.getConfig().addDefault("buywood", "Default");
    34. this.getConfig().addDefault("buysand", "Default");
    35. this.getConfig().addDefault("buydirt", "Default");
    36. this.getConfig().addDefault("buycobblestone", "Default");
    37. this.getConfig().addDefault("buyseeds", "Default");
    38. this.getConfig().addDefault("buywheat", "Default");
    39. this.getConfig().addDefault("buybread", "Default");
    40. this.getConfig().addDefault("buycowspawner", "Default");
    41. this.getConfig().addDefault("buymooshroomspawner", "Default");
    42. this.getConfig().addDefault("buypigspawner", "Default");
    43. this.getConfig().addDefault("buychickenspawner", "Default");
    44. this.getConfig().addDefault("buyhorsespawner", "Default");
    45. this.getConfig().addDefault("buysheepspawner", "Default");
    46. this.getConfig().addDefault("buywolfspawner", "Default");
    47. this.getConfig().addDefault("buyocelotspawner", "Default");
    48. this.getConfig().addDefault("buyspiderspawner", "Default");
    49. this.getConfig().addDefault("buyskeletonspawner", "Default");
    50. this.getConfig().addDefault("buyzombiespawner", "Default");
    51. this.getConfig().addDefault("buyendermanspawner", "Default");
    52. this.getConfig().addDefault("buyblazespawner", "Default");
    53. this.getConfig().addDefault("buycreeperspawner", "Default");
    54. //
    55. //Just a divider :P
    56. //
    57. this.getConfig().addDefault("sellwood", "Default");
    58. this.getConfig().addDefault("sellsand", "Default");
    59. this.getConfig().addDefault("sellcobble", "Default");
    60. this.getConfig().addDefault("selldirt", "Default");
    61. this.getConfig().addDefault("sellstone", "Default");
    62. this.getConfig().addDefault("sellredmushroom", "Default");
    63. this.getConfig().addDefault("sellbrownmushroom", "Default");
    64. this.getConfig().addDefault("sellmycellium", "Default");
    65. this.getConfig().addDefault("selldiamond", "Default");
    66. this.getConfig().addDefault("sellgold", "Default");
    67. this.getConfig().addDefault("selliron", "Default");
    68. this.getConfig().addDefault("sellcoal", "Default");
    69. this.getConfig().addDefault("sellemerald", "Default");
    70. this.getConfig().addDefault("sellredstone", "Default");
    71. this.getConfig().addDefault("sellnetherrack", "Default");
    72. this.getConfig().addDefault("sellglowstone", "Default");
    73. this.getConfig().addDefault("sellnetherwart", "Default");
    74. this.getConfig().addDefault("sellrawpork", "Default");
    75. this.getConfig().addDefault("sellrawbeef", "Default");
    76. this.getConfig().addDefault("sellrawsteak", "Default");
    77. this.getConfig().addDefault("sellrawchicken", "Default");
    78. this.getConfig().addDefault("sellrawfish", "Default");
    79. this.getConfig().addDefault("sellcactus", "Default");
    80. this.getConfig().addDefault("sellpumpkin", "Default");
    81. this.getConfig().addDefault("sellmelon", "Default");
    82. this.getConfig().addDefault("selllillypads", "Default");
    83. }
    84. else if(configFile.exists()){
    85. this.getLogger().info("Reloading config.yml");
    86. this.saveConfig();
    87.  
    88. }
    89. }
    90.  
    91. public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args){
    92. Player p = (Player) sender;
    93. if(cmd.getName().equalsIgnoreCase("wts")){
    94. //Sends error message if no args are entered.
    95. if (args.length == 0){
    96. p.sendMessage(ChatColor.RED + "Correct usage is /wts (item name)");
    97. }
    98. //Help args (Sends info)
    99. else if (args[0].equalsIgnoreCase("help")){
    100. p.sendMessage(ChatColor.LIGHT_PURPLE + "WhereToSell by Dmoney12321. Do /wts (item name) to find out where you can sell said item.");
    101. p.sendMessage(ChatColor.LIGHT_PURPLE + "Example: /wts diamond would send you a message saying where you can sell diamond");
    102. }
    103. //begin item naming
    104. else if (args[0].equalsIgnoreCase("diamond")){
    105. p.sendMessage(ChatColor.LIGHT_PURPLE + "You can sell this item at " + this.getConfig().getString("selldiamond"));
    106. }
    107. }
    108. //
    109. // WHERE TO BUY COMMANDS
    110. //
    111. else if (cmd.getName().equalsIgnoreCase("wtb")){
    112. //Sends error message if no args are entered.
    113. if (args.length == 0){
    114. p.sendMessage(ChatColor.RED + "Correct usage is /wtb (item name)");
    115. }
    116. //Help args (Sends info)
    117. else if (args[0].equalsIgnoreCase("help")){
    118. p.sendMessage(ChatColor.LIGHT_PURPLE + "WhereToBuy by Dmoney12321. Do /wtb (item name) to find out where you can buy said item.");
    119. p.sendMessage(ChatColor.LIGHT_PURPLE + "Example: /wtb diamond would send you a message saying where you can buy diamond");
    120. }
    121. //Begin item naming.
    122. else if (args[0].equalsIgnoreCase("diamond")){
    123. p.sendMessage(ChatColor.LIGHT_PURPLE + "You can buy this item at " + this.getConfig().getString("buydiamond"));
    124. }
    125. }
    126. else if (cmd.getName().equalsIgnoreCase("wtreload")){
    127. if(p.isOp()){
    128. this.reloadConfig();
    129. this.saveConfig();
    130. p.sendMessage(ChatColor.GREEN + "Reloaded.");
    131. }
    132. else if(!p.isOp()){
    133. p.sendMessage(ChatColor.RED + "Sorry, you cannot do that.");
    134. }
    135. }
    136. return false;
    137. }
    138. }
     
  2. Offline

    1Rogue

    Code:java
    1. File file = new File(this.getDataFolder(), "config.yml");
    2. if (!file.exists()) {
    3. saveDefaultConfig();
    4. }


    And then keeping a default config in your plugin resources (Where you plugin.yml is). It will make life much easier for you.
     
  3. Offline

    dmoney12321

    1Rogue
    So in the config in the .jar put all the defaults?
     
  4. Offline

    drtshock

    chaseoes and 1Rogue like this.
  5. Offline

    dmoney12321

    drtshock 1Rogue
    Put all defaults in the one in the .jar
    Still doesnt make a config.yml in the plugin's plugin folder
     
  6. Offline

    drtshock

    Put your config where your plugin.yml is in your jar. Then call saveDefaultConfig() in the onEnable. That will then save the config to the server on startup.
     
    chaseoes likes this.
  7. Offline

    dmoney12321

  8. Offline

    drtshock

    What isn't working? Getting any errors?
     
    chaseoes likes this.
  9. Offline

    dmoney12321

  10. Offline

    drtshock

    The only line you need in your config is "saveDefaultConfig()" get rid of everything else.
     
    chaseoes likes this.
  11. Offline

    amhokies

  12. Offline

    dmoney12321

    So
    Code:java
    1. if (!configFile.exists()){
    2. this.saveDefaultConfig();
    3. }
    drtshock
     
  13. Offline

    drtshock

    No. Read what I said. The ONLY line that you need is
    Code:java
    1. saveDefaultConfig();
     
    chaseoes likes this.
  14. Offline

    dmoney12321

    drtshock
    This?
    Code:java
    1. public void onEnable()
    2. {
    3. PluginDescriptionFile pdfFile = this.getDescription();
    4. this.getLogger().info(pdfFile.getName() + "Version" + pdfFile.getVersion() + " Has Been Enabled!");
    5. this.saveDefaultConfig();
    6. }
     
  15. Offline

    drtshock

    Sure but why do you say that the plugin has been enabled? Bukkit does that for you. You also don't need "this."
     
    chaseoes likes this.
  16. Offline

    dmoney12321

  17. Offline

    mattrick

    drtshock
    It says "Enabling Plugin..." not "Plugin Enabled!"
     
  18. Offline

    drtshock

    Yes but it's bad practice to log things that are unnecessary :)
     
    chaseoes and mattrick like this.
  19. Offline

    mattrick

    True....I used to do that until I finally got tired of writing a logger message instead of coding :p
     
Thread Status:
Not open for further replies.

Share This Page