Solved Saving config

Discussion in 'Plugin Development' started by Markyroson, Jun 25, 2014.

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

    Markyroson

    Currently I have the issue of the config being overwritten back to the default if any changes are made to it. The code so far is:
    Code:java
    1. package me.Markyroson.plugins.misc;
    2.  
    3. // Imports
    4. import java.util.logging.Logger;
    5. import org.bukkit.plugin.java.JavaPlugin;
    6.  
    7. // Begin Plugin
    8. public class Main extends JavaPlugin {
    9.  
    10. private final Logger l = Logger.getLogger("Minecraft");
    11.  
    12. // On Enable
    13. public void onEnable()
    14. {
    15. // Displayed in console #1
    16. this.l.info("[Misc] Enabled. Registering events & commands.");
    17.  
    18. Commands s = new Commands(this);
    19. getCommand("staff").setExecutor(s);
    20.  
    21. // Displayed in console #2
    22. this.l.info("[Misc] Events & commands registered. Loading config.");
    23.  
    24. saveDefaultConfig();
    25. // Displayed in console #3
    26. this.l.info("[Misc] Enabled. Thank you for using Misc!");
    27.  
    28.  
    29. }
    30. // On Disable
    31. public void onDisable()
    32. {
    33. // Displayed in console #1
    34. this.l.info("[Misc] Thank you for using Misc! Disabling...");
    35. // Displayed in console #2
    36. this.l.info("[Misc] ...Disabled.");
    37. }
    38.  
    39.  
    40. }


    Please note that the above code is not the code for everything but just the code relating to the config. Also, "Misc" will not be the plugin name and everything stating it will be changed upon settling on a name (ie package etc), it is merely a placeholder until I think of a name (which I have been unable to do so far).

    Please help
     
  2. Offline

    stirante

    Markyroson I usually use:
    Code:
                config = getConfig();
                config.options().copyDefaults(true);
                saveConfig();
                reloadConfig();
     
  3. Offline

    Markyroson

  4. Offline

    stirante

    Markyroson In onEnable method instead of copyDefaultConfig()
     
  5. Offline

    Markyroson

    stirante stirante This is what it looks like now but all the new code just produces errors

    Code:java
    1. package me.Markyroson.plugins.misc;
    2.  
    3. // Imports
    4. import java.util.logging.Logger;
    5. import org.bukkit.plugin.java.JavaPlugin;
    6.  
    7. // Begin Plugin
    8. public class Main extends JavaPlugin {
    9.  
    10. private final Logger l = Logger.getLogger("Minecraft");
    11.  
    12. // On Enable
    13. public void onEnable()
    14. {
    15. // Displayed in console #1
    16. this.l.info("[Misc] Enabled. Registering events & commands.");
    17.  
    18. Commands s = new Commands(this);
    19. getCommand("staff").setExecutor(s);
    20.  
    21. // Displayed in console #2
    22. this.l.info("[Misc] Events & commands registered. Loading config.");
    23.  
    24. config = getConfig();
    25. config.options().copyDefaults(true);
    26. saveConfig();
    27. reloadConfig();
    28. // Displayed in console #3
    29. this.l.info("[Misc] Enabled. Thank you for using Misc!");
    30.  
    31.  
    32. }
    33. // On Disable
    34. public void onDisable()
    35. {
    36. // Displayed in console #1
    37. this.l.info("[Misc] Thank you for using Misc! Disabling...");
    38. // Displayed in console #2
    39. this.l.info("[Misc] ...Disabled.");
    40. }
    41.  
    42.  
    43. }
     
  6. Offline

    stirante

    Markyroson In my class I had config field. Here change config = getConfig(); to FileConfiguration config = getConfig();
     
  7. Offline

    Markyroson

    stirante i stirantet didn't work. I still have the same problem. here. Try the plugin and see (change config/edit values to see what i mean).

    EDIT: Are you able to download it or do you want just the code?

    stirante stirante I wonder why it doesn't work
     

    Attached Files:

    Last edited by a moderator: Jul 1, 2016
  8. Markyroson Check if the config.yml is null, if so, create one. If not, do nothing.

    NOTE: when using config.options().copyDefaults(true);
    still do saveDefaultConfig();

    stirante I don't think he has a config.yml in his plugin when he exports. I think he is trying to create one with code when the server starts.
     
    Markyroson likes this.
  9. Offline

    Markyroson

    HeyAwesomePeople HeyAwesomePeople what do you mean? I do have a config.yml file in my project. Even if I try to edit the default one (after export and when I have shut down the server) it will save but the moment I start it up and do the command to display what is within the fields it is all back to the default stuff.

    Main class:

    Code:java
    1. package me.Markyroson.plugins.misc;
    2.  
    3. // Imports
    4. import java.util.logging.Logger;
    5.  
    6. import org.bukkit.configuration.file.FileConfiguration;
    7. import org.bukkit.plugin.java.JavaPlugin;
    8.  
    9. // Begin Plugin
    10. public class Main extends JavaPlugin {
    11.  
    12. private final Logger l = Logger.getLogger("Minecraft");
    13.  
    14. // On Enable
    15. public void onEnable()
    16. {
    17. // Displayed in console #1
    18. this.l.info("[Misc] Enabled. Registering events & commands.");
    19.  
    20. Commands s = new Commands(this);
    21. getCommand("staff").setExecutor(s);
    22. getCommand("misc").setExecutor(s);
    23.  
    24. // Displayed in console #2
    25. this.l.info("[Misc] Events & commands registered. Loading config.");
    26.  
    27. FileConfiguration config = getConfig();
    28. config.options().copyDefaults(true);
    29. saveConfig();
    30. reloadConfig();
    31. saveDefaultConfig();
    32. // Displayed in console #3
    33. this.l.info("[Misc] Enabled. Thank you for using Misc!");
    34.  
    35.  
    36. }
    37. // On Disable
    38. public void onDisable()
    39. {
    40. // Displayed in console #1
    41. this.l.info("[Misc] Thank you for using Misc! Disabling...");
    42. // Displayed in console #2
    43. this.l.info("[Misc] ...Disabled.");
    44. }
    45.  
    46.  
    47. }


    Here is the code for the Commands class:

    Code:java
    1. package me.Markyroson.plugins.misc;
    2.  
    3.  
    4. import org.bukkit.ChatColor;
    5. import org.bukkit.Material;
    6. import org.bukkit.block.Sign;
    7. import org.bukkit.command.Command;
    8. import org.bukkit.command.CommandExecutor;
    9. import org.bukkit.command.CommandSender;
    10. import org.bukkit.entity.Player;
    11. import org.bukkit.event.EventHandler;
    12. import org.bukkit.event.block.Action;
    13. import org.bukkit.event.block.SignChangeEvent;
    14. import org.bukkit.event.player.PlayerInteractEvent;
    15. import org.bukkit.plugin.java.JavaPlugin;
    16. import org.bukkit.plugin.PluginDescriptionFile;
    17.  
    18. @SuppressWarnings("unused")
    19. public class Commands implements CommandExecutor{
    20. public static Main plugin;
    21.  
    22. public Commands(Main i)
    23. {
    24. plugin = i;
    25. }
    26. @Override
    27. public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args)
    28. {
    29. if ((cmd.getName().equalsIgnoreCase("staff")) || (cmd.getName().equalsIgnoreCase("misc")))
    30. {
    31. if (args.length == 0)
    32. {
    33. sender.sendMessage("§6§l=====< §2§lMisc by Markyroson §6§l>=====");
    34. sender.sendMessage(ChatColor.GOLD + "/staff applications");
    35. sender.sendMessage(ChatColor.GOLD + "/staff members");
    36. sender.sendMessage(ChatColor.GOLD + "/staff reload " + ChatColor.AQUA + "(for admins/owners only)");
    37. sender.sendMessage(ChatColor.GOLD + "/staff help");
    38.  
    39. }
    40. else if (args[0].equalsIgnoreCase("reload"))
    41. {
    42. if (sender.hasPermission("misc.reload"))
    43. {
    44. plugin.reloadConfig();
    45. sender.sendMessage("§aConfig reloaded!");
    46. }
    47. else
    48. {
    49. sender.sendMessage(ChatColor.translateAlternateColorCodes('&', plugin.getConfig().getString("no_perms_message")));
    50. }
    51. }
    52. else if (args[0].equalsIgnoreCase("applications"))
    53. {
    54. sender.sendMessage(ChatColor.translateAlternateColorCodes('&', plugin.getConfig().getString("staff_applications.title")));
    55. sender.sendMessage(ChatColor.translateAlternateColorCodes('&', plugin.getConfig().getString("staff_applications.content")));
    56. }
    57. else if (args[0].equalsIgnoreCase("members"))
    58. {
    59. sender.sendMessage(ChatColor.translateAlternateColorCodes('&', plugin.getConfig().getString("staff_members_list.title")));
    60. // sender.sendMessage(ChatColor.translateAlternateColorCodes('&', plugin.getConfig().getString("staff_members_list.list")));
    61. if (sender.hasPermission("misc.staff.view")) {
    62. sender.sendMessage(ChatColor.GOLD + "Owner(s): " + ChatColor.translateAlternateColorCodes('&', plugin.getConfig().getString("Owner")));
    63. sender.sendMessage(ChatColor.DARK_BLUE + "Admin(s): " + ChatColor.translateAlternateColorCodes('&', plugin.getConfig().getString("Admin")));
    64. sender.sendMessage(ChatColor.GREEN + "Moderator(s): " + ChatColor.translateAlternateColorCodes('&', plugin.getConfig().getString("Mod")));
    65. sender.sendMessage(ChatColor.RED + "On the server: " + ChatColor.translateAlternateColorCodes('&', plugin.getConfig().getString("Server")));
    66. } else {
    67. sender.sendMessage(ChatColor.RED + "You don't have permissions to do this!");
    68. return true;
    69. }
    70. }
    71. // Help command
    72. else if (args[0].equalsIgnoreCase("help"))
    73. {
    74.  
    75. if (sender.hasPermission("misc.staff.view.help")) {
    76. if (args[0].equalsIgnoreCase("help")) {
    77. // sender.sendMessage(ChatColor.WHITE + "[Staffs version " + ChatColor.AQUA + getDescription().getVersion() + ChatColor.WHITE + "] " + ChatColor.DARK_AQUA + "This is the help menu of Staff-Members, made by DoraKlikOpDora (bram3535)");
    78. sender.sendMessage("§6§l=====< §2§lMisc Help by Markyroson §6§l>=====");
    79. sender.sendMessage(ChatColor.GOLD + "/staff " + ChatColor.AQUA + "shows you all the plugin commands");
    80. sender.sendMessage(ChatColor.GOLD + "/staff members " + ChatColor.AQUA + "shows you the staff of the server");
    81. sender.sendMessage(ChatColor.GOLD + "/staff help " + ChatColor.AQUA + "shows you this menu");
    82. sender.sendMessage(ChatColor.GOLD + "/staff reload " +ChatColor.AQUA + "reloads the staff config");
    83. return true;
    84. }
    85. }
    86. }
    87.  
    88. return false;
    89. }
    90. return false;
    91. }
    92.  
    93. // work on implementing
    94. // (currently doesn't work)
    95. @EventHandler
    96. public void onPlayerPlace(SignChangeEvent e) {
    97. if ((e.getPlayer().hasPermission("misc.admin")) &&
    98. (e.getLine(0).contains("[staff]"))) {
    99. e.setLine(0, "&a[&bStaff&a]");
    100. e.setLine(1, "&eClick here to");
    101. e.setLine(2, "&esee the");
    102. e.setLine(3, "&eserver's staff");
    103. }
    104. }
    105.  
    106. @EventHandler
    107. public void interact(PlayerInteractEvent e) {
    108. Player p = e.getPlayer();
    109. if ((e.getAction() == Action.RIGHT_CLICK_BLOCK) &&
    110. (p.hasPermission("misc.staff")) && (
    111. (e.getClickedBlock().getType().equals(Material.WALL_SIGN)) || (e.getClickedBlock().getType().equals(Material.SIGN_POST)))) {
    112. Sign sign = (Sign)e.getClickedBlock().getState();
    113. if ((sign.getLine(0).contains("&a[&bStaff&a]")) && (sign.getLine(1).contains("&eClick here to"))) {
    114. p.sendMessage(ChatColor.GOLD + "Owner(s): " + ChatColor.translateAlternateColorCodes('&', plugin.getConfig().getString("Owner")));
    115. p.sendMessage(ChatColor.DARK_BLUE + "Admin(s): " + ChatColor.translateAlternateColorCodes('&', plugin.getConfig().getString("Admin")));
    116. p.sendMessage(ChatColor.GREEN + "Moderator(s): " + ChatColor.translateAlternateColorCodes('&', plugin.getConfig().getString("Mod")));
    117. p.sendMessage(ChatColor.RED + "On the server: " + ChatColor.translateAlternateColorCodes('&', plugin.getConfig().getString("Server")));
    118. }
    119. }
    120. }
    121.  
    122. }


    config.yml code:

    Code:
    ######################################################
    #              Misc Config File                      #
    ######################################################
    #
    # Message displayed if player does not have the permission node to do the command in question
    no_perms_message: '&4You do not have permission to do that command!'
     
     
    # For color codes for your messages within this config a useful site may be http://minecraft.gamepedia.com/Formatting_codes and remember, a quick google search can work too! :-)
     
    # In the <insert name> spot feel free to place your server name. Please remember that it is merely a template dea and can be totally
    # changed if you wish. All that has to remain is the '' part.
     
    # Staff applications
    staff_applications:
      title: '&6&l=====< &2&l<insert name> Applications&6&l>====='
      content: '&6Your message about applications information.'
     
    # Staff memebers list
     
    staff_members_list:
      title: '&6&l=====< &2&l<insert name> Staff List&6&l>====='
     
      # Following line not currently in use
    #  list: '&6Your list of staff members here.'
     
    Owner: 'Place the Owners of the server here! (See the config file if you are in the game)'
    Admin: 'Place the Admins of the server here! (See the config file if you are in the game)'
    Mod: 'Place the Mods of the server here! (See the config file if you are in the game)'
    Server: 'Place the name of your server here, it will be show when you type the command /staff!'
     
  10. Offline

    TheRedHun1

    I think I may have found the issue:

    Code:java
    1. saveConfig();
    2. reloadConfig();
    3. saveDefaultConfig();


    You are saving your default config after you reload it, so it might be overwriting your saved config.
     
  11. Offline

    Markyroson

    TheRedHun1 TheRedHun1 so what order should they be in? Like this or?

    Code:java
    1. saveDefaultConfig();
    2. saveConfig();
    3. reloadConfig();
     
  12. Offline

    TheRedHun1

    I believe that should work, if not, you can always check if the default path exists and if it a legitimate value. If it isn't then you can over write the defaults. If it does exist and the default is a good value, then do not write that default at all.
     
  13. Offline

    Markyroson

    TheRedHun1 HeyAwesomePeople stirante Thank you for all your help! It is greatly appreciated! I managed to get it to work in a round about way. mI rebuilt it just using the messages in the old one (displayed to user/player) and combined it all into one class as well as cleaned up my config file and it all now works and the problem (still not sure what exactly was casuing it) that prevented new information from being written to the config.yml file has been resolved.

    Thanks again,

    Markyroson
     
  14. Please mark the thread as solved.
     
  15. Offline

    Markyroson

  16. Offline

    TheRedHun1

    Near post reply do More Options and change the thread title. Markyroson
     
  17. Offline

    Markyroson

Thread Status:
Not open for further replies.

Share This Page