Solved Adding defaults to config from another class

Discussion in 'Plugin Development' started by boardinggamer, Nov 19, 2012.

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

    boardinggamer

    I used this way of making a config file
    Code:
    Configuration config = getConfig();
    config.addDefault("Test", "test");
    config.options().copyDefaults(true);
    saveConfig();
    I tried adding this in another class
    Code:
    String[] used = { Player.getName() };
    this.plugin.getConfig().addDefault("Used_Command", used);
    this.plugin.getConfig().options().copyDefaults(true);
    this.plugin.saveConfig();
    this.plugin.reloadConfig();
    It adds it to the config normally and seems to work fine. But when the server is reloaded it goes away. Can anyone tell me how to fix this?

    The used command thing is just an example of a list since that's what I am working with
     
  2. Offline

    Sagacious_Zed Bukkit Docs

    There is not enough information.
     
  3. Offline

    boardinggamer

    Sagacious_Zed
    What else do you need?
    I thought that was enough. It adds it to the config, but when the server restarts it goes away as if it was never added.
     
  4. Offline

    Sagacious_Zed Bukkit Docs

    Actual classes would help.
     
  5. Offline

    boardinggamer

    Sagacious_Zed
    The classes are huge so I figured just the main point would be enough.

    main class
    Code:java
    1.  
    2. package boardinggamer.PvPClans;
    3.  
    4. import java.util.HashMap;
    5.  
    6. import org.bukkit.configuration.Configuration;
    7. import org.bukkit.plugin.java.JavaPlugin;
    8.  
    9. import boardinggamer.PvPClans.Commands.Command_Clans;
    10.  
    11. public class PvPClans extends JavaPlugin{
    12.  
    13. public HashMap<String, String> invited;
    14. public HashMap<String, String> ally;
    15.  
    16. String[] clans = { };
    17.  
    18. public void onEnable(){
    19.  
    20. invited = new HashMap<String, String>();
    21. ally = new HashMap<String, String>();
    22.  
    23. new PvPClansAPI(this);
    24.  
    25. getCommand("pvpclans").setExecutor(new Command_Clans(this));
    26.  
    27. Configuration config = getConfig();
    28. config.addDefault("Clans", clans);
    29. config.addDefault("Spawn_Radius_For_Home", 20);
    30. config.options().copyDefaults(true);
    31. saveConfig();
    32. }
    33.  
    34. public void onDisable(){
    35.  
    36. }
    37.  
    38. }
    39.  

    Here's the other class shortened down to what is important
    Code:java
    1.  
    2. package boardinggamer.PvPClans;
    3.  
    4. import java.util.List;
    5.  
    6. import org.bukkit.Location;
    7. import org.bukkit.World;
    8. import org.bukkit.entity.Player;
    9.  
    10. public class PvPClansAPI {
    11.  
    12. private PvPClans plugin;
    13. private static PvPClansAPI singleton;
    14.  
    15. public PvPClansAPI(PvPClans plugin){
    16. this.plugin = plugin;
    17. singleton = this;
    18. }
    19.  
    20. public static final PvPClansAPI getInstance(){
    21. return singleton;
    22. }
    23.  
    24. public void createClan(String name, String clan){
    25. if (!(inClan(name))){
    26. if (!(clanExists(clan))){
    27. String configleader = clan + ".Leaders";
    28. String configadmins = clan + ".Admins";
    29. String configmembers = clan + ".Members";
    30. String configregulars = clan + ".Regulars";
    31. String configally = clan + ".Allies";
    32. String configclaims = clan + ".Claims";
    33. String configdominances = clan + ".Dominances";
    34. String[] leaders = { name };
    35. String[] admins = { };
    36. String[] members = { };
    37. String[] regulars = { };
    38. String[] allies = { };
    39. String[] claims = { };
    40. String[] dominances = { };
    41. List clans = this.plugin.getConfig().getList("Clans");
    42. clans.add(clan);
    43. this.plugin.getConfig().set("Clans", clans);
    44. this.plugin.getConfig().addDefault(configleader, leaders);
    45. this.plugin.getConfig().addDefault(configadmins, admins);
    46. this.plugin.getConfig().addDefault(configmembers, members);
    47. this.plugin.getConfig().addDefault(configregulars, regulars);
    48. this.plugin.getConfig().addDefault(configally, allies);
    49. this.plugin.getConfig().addDefault(configclaims, claims);
    50. this.plugin.getConfig().addDefault(configdominances, dominances);
    51. this.plugin.getConfig().options().copyDefaults(true);
    52. this.plugin.saveConfig();
    53. this.plugin.reloadConfig();
    54. }
    55. }
    56. }
    57.  
     
  6. Offline

    Sagacious_Zed Bukkit Docs

    boardinggamer
    as far as i am concerned there is not enough information here for someone to actually step through the flow of the code.
     
  7. Offline

    boardinggamer

    When you use a command it calls createClan(String name, String clan);
    its not important to know how its called because that has nothing to do with the problem
     
  8. Offline

    Sagacious_Zed Bukkit Docs

    I prefer to asume nothing.
     
  9. Offline

    boardinggamer

    the command only determines the players name and the clan name. all the actual work it does is in what I gave you. the first 2 things though aren't there so here is everything needed to read the code
    Code:java
    1. package boardinggamer.PvPClans;
    2.  
    3. import java.util.List;
    4.  
    5. import org.bukkit.ChatColor;
    6. import org.bukkit.Location;
    7. import org.bukkit.World;
    8. import org.bukkit.entity.Player;
    9.  
    10. public class PvPClansAPI {
    11.  
    12. private PvPClans plugin;
    13. private static PvPClansAPI singleton;
    14.  
    15. public PvPClansAPI(PvPClans plugin){
    16. this.plugin = plugin;
    17. singleton = this;
    18. }
    19.  
    20. public static final PvPClansAPI getInstance(){
    21. return singleton;
    22. }
    23.  
    24. @SuppressWarnings("rawtypes")
    25. public boolean clanExists(String clan){
    26. boolean exists = false;
    27. List clans = this.plugin.getConfig().getList("Clans");
    28. if (!(clans.isEmpty())){
    29. if (clans.contains(clan)){
    30. exists = true;
    31. }
    32. }
    33. return exists;
    34. }
    35.  
    36. public boolean inClan(String name){
    37. boolean inclan = false;
    38. if (!(this.plugin.getConfig().getList("Clans").isEmpty())){
    39. for (Object clans : this.plugin.getConfig().getList("Clans")){
    40. String clan = clans.toString();
    41. String leaders = clan + ".Leaders";
    42. String admins = clan + ".Admins";
    43. String members = clan + ".Members";
    44. String regulars = clan + ".Regulars";
    45. if (this.plugin.getConfig().getList(regulars).contains(name)){
    46. inclan = true;
    47. }
    48. if (this.plugin.getConfig().getList(members).contains(name)){
    49. inclan = true;
    50. }
    51. if (this.plugin.getConfig().getList(admins).contains(name)){
    52. inclan = true;
    53. }
    54. if (this.plugin.getConfig().getList(leaders).contains(name)){
    55. inclan = true;
    56. }
    57. }
    58. }
    59. return inclan;
    60. }
    61.  
    62. public boolean inClan(Player player){
    63. String name = player.getName();
    64. return inClan(name);
    65. }
    66.  
    67. public String getClan(String name){
    68. String clan = "";
    69. for (Object clans : this.plugin.getConfig().getList("Clans")){
    70. String clanname = clans.toString();
    71. String leaders = clanname + ".Leaders";
    72. String admins = clanname + ".Admins";
    73. String members = clanname + ".Members";
    74. String regulars = clanname + ".Regulars";
    75. if (this.plugin.getConfig().getList(regulars).contains(name)){
    76. clan = clanname;
    77. }
    78. if (this.plugin.getConfig().getList(members).contains(name)){
    79. clan = clanname;
    80. }
    81. if (this.plugin.getConfig().getList(admins).contains(name)){
    82. clan = clanname;
    83. }
    84. if (this.plugin.getConfig().getList(leaders).contains(name)){
    85. clan = clanname;
    86. }
    87. }
    88. return clan;
    89. }
    90.  
    91. public String getClan(Player player){
    92. String name = player.getName();
    93. return getClan(name);
    94. }
    95.  
    96. @SuppressWarnings({ "unchecked", "rawtypes" })
    97. public void createClan(String name, String clan){
    98. if (!(inClan(name))){
    99. if (!(clanExists(clan))){
    100. String confighome = clan + ".Home";
    101. String configdesc = clan + ".Description";
    102. String configleader = clan + ".Leaders";
    103. String configadmins = clan + ".Admins";
    104. String configmembers = clan + ".Members";
    105. String configregulars = clan + ".Regulars";
    106. String configally = clan + ".Allies";
    107. String configclaims = clan + ".Claims";
    108. String configdominances = clan + ".Dominances";
    109. String[] leaders = { name };
    110. String[] admins = { };
    111. String[] members = { };
    112. String[] regulars = { };
    113. String[] allies = { };
    114. String[] claims = { };
    115. String[] dominances = { };
    116. List clans = this.plugin.getConfig().getList("Clans");
    117. clans.add(clan);
    118. this.plugin.getConfig().set("Clans", clans);
    119. this.plugin.getConfig().addDefault(confighome, this.plugin.getServer().getPlayer(name).getWorld().getSpawnLocation());
    120. this.plugin.getConfig().addDefault(configdesc, "Not set yet.");
    121. this.plugin.getConfig().addDefault(configleader, leaders);
    122. this.plugin.getConfig().addDefault(configadmins, admins);
    123. this.plugin.getConfig().addDefault(configmembers, members);
    124. this.plugin.getConfig().addDefault(configregulars, regulars);
    125. this.plugin.getConfig().addDefault(configally, allies);
    126. this.plugin.getConfig().addDefault(configclaims, claims);
    127. this.plugin.getConfig().addDefault(configdominances, dominances);
    128. this.plugin.getConfig().options().copyDefaults(true);
    129. this.plugin.saveConfig();
    130. this.plugin.reloadConfig();
    131. }
    132. }
    133. }
    134.  
     
  10. Offline

    Sagacious_Zed Bukkit Docs

    boardinggamer
    The problem you are describing implies there is a state change when the plugin is being disabled, however with the currently provided code, I see no evidence of unintentional interaction.
     
  11. Offline

    boardinggamer

    Code:
    this.plugin.getConfig().addDefault(confighome, this.plugin.getServer().getPlayer(name).getWorld().getSpawnLocation());
    this was the problem. I fixed it now
     
Thread Status:
Not open for further replies.

Share This Page