Solved [STILL NOT SOLVED] Need help reading from the config! (READ COMMENTS)

Discussion in 'Plugin Development' started by SleepyDog, Nov 2, 2014.

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

    SleepyDog

    I am trying to get this to work... The default config is not saving, therefore i cannot set or read from it. This is messing my plugin up. Can you please help me?

    This is the code:
    Code:java
    1. package me.sleepydog935;
    2.  
    3. import org.bukkit.Bukkit;
    4. import org.bukkit.command.Command;
    5. import org.bukkit.command.CommandSender;
    6. import org.bukkit.plugin.java.JavaPlugin;
    7.  
    8. public class Main extends JavaPlugin{
    9.  
    10. public void OnEnable(){
    11. loadConfiguration();
    12. }
    13.  
    14. public void OnDisable(){
    15.  
    16. saveConfig();
    17. }
    18.  
    19.  
    20. public void loadConfiguration()
    21. {
    22. getConfig().options().copyDefaults(true);
    23. saveConfig();
    24. }
    25.  
    26. public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args){
    27. this.reloadConfig();
    28.  
    29. if (cmd.getName().equalsIgnoreCase("apply")){
    30. sender.sendMessage("§0§l§m====================");
    31. sender.sendMessage("§a§l" + getConfig().getString("apply1"));
    32. sender.sendMessage("§a§l" + getConfig().getString("apply2"));
    33. sender.sendMessage("§a§l" + getConfig().getString("apply3"));
    34. sender.sendMessage("§a§l" + getConfig().getString("apply4"));
    35. sender.sendMessage("§a§l" + getConfig().getString("apply5"));
    36. sender.sendMessage("§a§l" + getConfig().getString("apply6"));
    37. sender.sendMessage("§a§l" + getConfig().getString("apply7"));
    38. sender.sendMessage("§0§l§m====================");
    39. }
    40. else if (cmd.getName().equalsIgnoreCase("acceptapp")){
    41.  
    42. if (sender.isOp()) {
    43. String name = "";
    44. //Set name to args if they are not blank
    45. if (args.length > 0) name = args[0];
    46. //run code if name is not blank
    47. if (!name.equalsIgnoreCase("")) {
    48. //set <name>.isAccepted: True
    49. getConfig().set(name + ".isAccepted", "True");
    50. sender.sendMessage("§a" + name +" was accepted!");
    51. saveConfig();
    52. }
    53. else {
    54. sender.sendMessage("Usage: /accept <player>");
    55. //send the user the usage message
    56. }
    57.  
    58. }
    59. }
    60. else if (cmd.getName().equalsIgnoreCase("denyapp")){
    61.  
    62. if (sender.isOp()) {
    63. String name = "";
    64. //Set name to args if they are not blank
    65. if (args.length > 0) name = args[0];
    66. //run code if name is not blank
    67. if (!name.equalsIgnoreCase("")) {
    68. //set <name>.isAccepted: False
    69. getConfig().set(name + ".isAccepted", "False");
    70. saveConfig();
    71. sender.sendMessage("§c" + name +" was denied!");
    72. }
    73. else {
    74. sender.sendMessage("Usage: /deny <player>");
    75. //send the user the usage message
    76. }
    77.  
    78. }
    79. }
    80. else if (cmd.getName().equalsIgnoreCase("checkapp")){
    81. this.reloadConfig();
    82. String isAccepted = getConfig().getString(sender + ".isAccepted");
    83. if (isAccepted == ""){
    84. sender.sendMessage("§0§l§m====================");
    85. sender.sendMessage("§5§lYour application is pending...");
    86. sender.sendMessage("§5§lAn admin will read your application soon!");
    87. sender.sendMessage("§5§lDo /apply if you have not applied.");
    88. sender.sendMessage("§0§l§m====================");
    89. sender.sendMessage("§a§lDo not ask an admin to read your application till it has been 3 days since you have applied!");
    90.  
    91. }
    92. if (isAccepted == "True"){
    93. sender.sendMessage("§0§l§m====================");
    94. sender.sendMessage("§5§lYour application was §5Accepted!");
    95. sender.sendMessage("§5§lWelcome to the server!");
    96. sender.sendMessage("§0§l§m====================");
    97. Bukkit.getServer().dispatchCommand(getServer().getConsoleSender(), "pex user " + sender + " group set member");
    98. sender.sendMessage("§a§lYou are now in group member!");
    99. getConfig().set(sender + ".isAccepted", "Accepted and Promoted");
    100. saveConfig();
    101.  
    102.  
    103. }
    104. else if (isAccepted == "False"){
    105.  
    106. }
    107. else if (isAccepted == "Accepted and Promoted"){
    108.  
    109. sender.sendMessage("§0§l§m====================");
    110. sender.sendMessage("§5§lYour application was §5Accepted!");
    111. sender.sendMessage("§5§lWelcome to the server!");
    112. sender.sendMessage("§0§l§m====================");
    113. sender.sendMessage("§c§lYou have already been promoted!");
    114. sender.sendMessage("§c§lIf you are still not promoted ask a member of staff!");
    115.  
    116. }
    117. else {
    118. sender.sendMessage("§c§lThere was a problem when checking your application. Try again later.");
    119. }
    120. }
    121.  
    122.  
    123.  
    124.  
    125.  
    126. return false;
    127. }
    128.  
    129. }
    130.  
     
  2. Offline

    Gerov

    SleepyDog Before you do saveConfig(); you have to copy the defaults, so after you are finished altering the config add this.getConfig().options.copyDefaults(true);
     
  3. Offline

    SleepyDog

    This is not doing anything:
    Code:java
    1. public void loadConfiguration()
    2. {
    3. getConfig().options().copyDefaults(true);
    4. saveConfig();
    5. }


    I have that already, but it is not saving when i start the plugin. The /plugin/apply folder is not being made ether.

    Gerov
    This is the plugin file
    [​IMG]
    There should be an apply folder aswell

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 14, 2016
  4. Offline

    Gerov

    Do you have anything creating the file?
     
  5. Offline

    fireblast709

    SleepyDog the main reason of using the @Override annotation: it is a compile time error if the annotated method isn't overriding a method from any of the super classes.

    Your onEnable and onDisable method names are spelled incorrectly.
     
    AdamQpzm likes this.
  6. Offline

    SleepyDog

    This is what happens when i try to make a plugin without using copy and paste. Thankyou for the help!

    I was saving into the wrong server too...

    fireblast709
    Could you tell me why this reading is not working?
    I am trying to show the right message depending on what the isAccepted: is set to.
    At this time it is always running the else statement
    Code:java
    1. else if (cmd.getName().equalsIgnoreCase("checkapp")){
    2. this.reloadConfig();
    3. if (getConfig().getString(sender + ".isAccepted") == ""){
    4. sender.sendMessage("§0§l§m====================");
    5. sender.sendMessage("§5§lYour application is pending...");
    6. sender.sendMessage("§5§lAn admin will read your application soon!");
    7. sender.sendMessage("§5§lDo /apply if you have not applied.");
    8. sender.sendMessage("§0§l§m====================");
    9. sender.sendMessage("§a§lDo not ask an admin to read your application till it has been 3 days since you have applied!");
    10.  
    11. }
    12. else if (getConfig().getString(sender + ".isAccepted") == "True"){
    13. sender.sendMessage("§0§l§m====================");
    14. sender.sendMessage("§5§lYour application was §5Accepted!");
    15. sender.sendMessage("§5§lWelcome to the server!");
    16. sender.sendMessage("§0§l§m====================");
    17. Bukkit.getServer().dispatchCommand(getServer().getConsoleSender(), "pex user " + sender + " group set member");
    18. sender.sendMessage("§a§lYou are now in group member!");
    19. getConfig().set(sender + ".isAccepted", "Accepted and Promoted");
    20. saveConfig();
    21.  
    22.  
    23. }
    24. else if (getConfig().getString(sender + ".isAccepted") == "False"){
    25.  
    26. }
    27. else if (getConfig().getString(sender + ".isAccepted") == "Accepted and Promoted"){
    28.  
    29. sender.sendMessage("§0§l§m====================");
    30. sender.sendMessage("§5§lYour application was §5Accepted!");
    31. sender.sendMessage("§5§lWelcome to the server!");
    32. sender.sendMessage("§0§l§m====================");
    33. sender.sendMessage("§c§lYou have already been promoted!");
    34. sender.sendMessage("§c§lIf you are still not promoted ask a member of staff!");
    35.  
    36. }
    37. else {
    38. sender.sendMessage("§c§lThere was a problem when checking your application. Try again later.");
    39. }
    40. }
    41.  


    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 14, 2016
  7. Offline

    fireblast709

    SleepyDog first of all, don't compare Strings with ==. == checks if the references are the same (in other words, if they point to the same address in memory). equals, on the other hand, checks if the values of the objects match (or equalsIgnoreCase for Strings).
     
  8. Offline

    SleepyDog

    Thankyou, this would be correct?

    Code:java
    1. else if (cmd.getName().equalsIgnoreCase("checkapp")){
    2. this.reloadConfig();
    3. if (getConfig().getString(sender + ".isAccepted").equalsIgnoreCase("")){
    4. sender.sendMessage("§0§l§m====================");
    5. sender.sendMessage("§5§lYour application is pending...");
    6. sender.sendMessage("§5§lAn admin will read your application soon!");
    7. sender.sendMessage("§5§lDo /apply if you have not applied.");
    8. sender.sendMessage("§0§l§m====================");
    9. sender.sendMessage("§a§lDo not ask an admin to read your application till it has been 3 days since you have applied!");
    10.  
    11. }
    12. else if (getConfig().getString(sender + ".isAccepted").equalsIgnoreCase("true")){
    13. sender.sendMessage("§0§l§m====================");
    14. sender.sendMessage("§5§lYour application was §5Accepted!");
    15. sender.sendMessage("§5§lWelcome to the server!");
    16. sender.sendMessage("§0§l§m====================");
    17. Bukkit.getServer().dispatchCommand(getServer().getConsoleSender(), "pex user " + sender + " group set member");
    18. sender.sendMessage("§a§lYou are now in group member!");
    19. getConfig().set(sender + ".isAccepted", "Accepted and Promoted");
    20. saveConfig();
    21.  
    22.  
    23. }
    24. else if (getConfig().getString(sender + ".isAccepted").equalsIgnoreCase("false")){
    25.  
    26. }
    27. else if (getConfig().getString(sender + ".isAccepted").equalsIgnoreCase("accepted and promoted")){
    28.  
    29. sender.sendMessage("§0§l§m====================");
    30. sender.sendMessage("§5§lYour application was §5Accepted!");
    31. sender.sendMessage("§5§lWelcome to the server!");
    32. sender.sendMessage("§0§l§m====================");
    33. sender.sendMessage("§c§lYou have already been promoted!");
    34. sender.sendMessage("§c§lIf you are still not promoted ask a member of staff!");
    35.  
    36. }
    37. else {
    38. sender.sendMessage("§c§lThere was a problem when checking your application. Try again later.");
    39. }
    40. }
    41.  
    42.  


    I tried this, still wrong.
    Code:java
    1. else if (cmd.getName().equalsIgnoreCase("checkapp")){
    2. this.reloadConfig();
    3. if ((getConfig().getString(sender + ".isAccepted")).equalsIgnoreCase("")){
    4. sender.sendMessage("§0§l§m====================");
    5. sender.sendMessage("§5§lYour application is pending...");
    6. sender.sendMessage("§5§lAn admin will read your application soon!");
    7. sender.sendMessage("§5§lDo /apply if you have not applied.");
    8. sender.sendMessage("§0§l§m====================");
    9. sender.sendMessage("§a§lDo not ask an admin to read your application till it has been 3 days since you have applied!");
    10.  
    11. }
    12. else if ((getConfig().getString(sender + ".isAccepted")).equalsIgnoreCase("true")){
    13. sender.sendMessage("§0§l§m====================");
    14. sender.sendMessage("§5§lYour application was §5Accepted!");
    15. sender.sendMessage("§5§lWelcome to the server!");
    16. sender.sendMessage("§0§l§m====================");
    17. Bukkit.getServer().dispatchCommand(getServer().getConsoleSender(), "pex user " + sender + " group set member");
    18. sender.sendMessage("§a§lYou are now in group member!");
    19. getConfig().set(sender + ".isAccepted", "Accepted and Promoted");
    20. saveConfig();
    21.  
    22.  
    23. }
    24. else if ((getConfig().getString(sender + ".isAccepted")).equalsIgnoreCase("false")){
    25.  
    26. }
    27. else if ((getConfig().getString(sender + ".isAccepted")).equalsIgnoreCase("accepted and promoted")){
    28.  
    29. sender.sendMessage("§0§l§m====================");
    30. sender.sendMessage("§5§lYour application was §5Accepted!");
    31. sender.sendMessage("§5§lWelcome to the server!");
    32. sender.sendMessage("§0§l§m====================");
    33. sender.sendMessage("§c§lYou have already been promoted!");
    34. sender.sendMessage("§c§lIf you are still not promoted ask a member of staff!");
    35.  
    36. }
    37. else {
    38. sender.sendMessage("§c§lThere was a problem when checking your application. Try again later.");
    39. }
    40. }
    41.  
    42.  
    43.  


    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 14, 2016
  9. Offline

    fireblast709

    SleepyDog sender in a String concatenation is a bit strange, I doubt that will work :p. Either you want a name or UUID (most likely the latter).
     
  10. Offline

    SleepyDog

    Could you translate that to BTEC please?

    How would you code that bit of code. I know people dont like to code for people. This is just a personal plugin for my server, i need it done soon.

    @fireblast709

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 14, 2016
  11. Offline

    fireblast709

    SleepyDog BTEC? Anyway you should just filter out the Player CommandSenders (assuming the console can't apply :p), and use the UUID.
     
  12. fireblast709 It's a type of qualification we have here in the UK (and maybe others) which is basically more practical learning than academic learning.
     
  13. Offline

    SleepyDog

    I know you devs dont like to 'spoon feed' but i need this for my server. Urgently. I cant work it out. Please help.

    bump

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 14, 2016
  14. Offline

    JordyPwner

    put on your onEnable: saveDefaultConfig();
     
  15. Offline

    SleepyDog

    I have fixed that, i need this code to work now..
    Please help me fix this.
    Code:java
    1. else if (cmd.getName().equalsIgnoreCase("checkapp")){
    2. this.reloadConfig();
    3. String config = (getConfig().getString(sender + ".isAccepted"));
    4. if (config.equalsIgnoreCase("pending")){
    5. sender.sendMessage("§0§l§m====================");
    6. sender.sendMessage("§5§lYour application is pending...");
    7. sender.sendMessage("§5§lAn admin will read your application soon!");
    8. sender.sendMessage("§5§lDo /apply if you have not applied.");
    9. sender.sendMessage("§0§l§m====================");
    10. sender.sendMessage("§a§lDo not ask an admin to read your application till it has been 3 days since you have applied!");
    11.  
    12. }
    13. else if (config.equalsIgnoreCase("true")){
    14. sender.sendMessage("§0§l§m====================");
    15. sender.sendMessage("§5§lYour application was §5Accepted!");
    16. sender.sendMessage("§5§lWelcome to the server!");
    17. sender.sendMessage("§0§l§m====================");
    18. Bukkit.getServer().dispatchCommand(getServer().getConsoleSender(), "pex user " + sender + " group set member");
    19. sender.sendMessage("§a§lYou are now in group member!");
    20. getConfig().set(sender + ".isAccepted", "Accepted and Promoted");
    21. saveConfig();
    22.  
    23.  
    24. }
    25. else if (config.equalsIgnoreCase("false")){
    26. sender.sendMessage("§0§l§m====================");
    27. sender.sendMessage("§5§lYour application was §cDenied!");
    28. sender.sendMessage("§5§lSorry, You cant join the server.");
    29. sender.sendMessage("§0§l§m====================");
    30. sender.sendMessage("§c§lYou can apply again in §6§l2 §c§ldays.");
    31. sender.sendMessage("§c§lGoodLuck!");
    32. }
    33. else if (config.equalsIgnoreCase("accepted and promoted")){
    34.  
    35. sender.sendMessage("§0§l§m====================");
    36. sender.sendMessage("§5§lYour application was §5Accepted!");
    37. sender.sendMessage("§5§lWelcome to the server!");
    38. sender.sendMessage("§0§l§m====================");
    39. sender.sendMessage("§c§lYou have already been promoted!");
    40. sender.sendMessage("§c§lIf you are still not promoted ask a member of staff!");
    41.  
    42. }
    43. else {
    44. sender.sendMessage("§c§lThere was a problem when checking your application. Try again later.");
    45. }
    46. }
    47.  
    48.  
     
  16. Offline

    SuperOriginal

    Although everyone is suggesting using UUID's (Which is probably better in the long haul), you are probably looking for this: use sender.getName() instead of just "sender" in line 3, and everywhere else you have it.
     
  17. Offline

    SleepyDog

    Is that what is creating the problem?

    Still not working, Full code:

    Code:java
    1. package me.sleepydog935;
    2.  
    3. import org.bukkit.Bukkit;
    4. import org.bukkit.command.Command;
    5. import org.bukkit.command.CommandSender;
    6. import org.bukkit.plugin.java.JavaPlugin;
    7.  
    8. public class Main extends JavaPlugin{
    9. @Override
    10. public void onEnable(){
    11. loadConfiguration();
    12. }
    13. @Override
    14. public void onDisable(){
    15.  
    16. saveConfig();
    17. }
    18.  
    19.  
    20. public void loadConfiguration()
    21. {
    22. getConfig().options().copyDefaults(true);
    23. saveConfig();
    24. }
    25.  
    26. public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args){
    27. this.reloadConfig();
    28.  
    29. if (cmd.getName().equalsIgnoreCase("apply")){
    30. sender.sendMessage("§0§l§m====================");
    31. sender.sendMessage("§a§l" + getConfig().getString("apply1"));
    32. sender.sendMessage("§a§l" + getConfig().getString("apply2"));
    33. sender.sendMessage("§a§l" + getConfig().getString("apply3"));
    34. sender.sendMessage("§a§l" + getConfig().getString("apply4"));
    35. sender.sendMessage("§a§l" + getConfig().getString("apply5"));
    36. sender.sendMessage("§a§l" + getConfig().getString("apply6"));
    37. sender.sendMessage("§a§l" + getConfig().getString("apply7"));
    38. sender.sendMessage("§0§l§m====================");
    39. }
    40. else if (cmd.getName().equalsIgnoreCase("acceptapp")){
    41.  
    42. if (sender.isOp()) {
    43. String name = "";
    44. //Set name to args if they are not blank
    45. if (args.length > 0) name = args[0];
    46. //run code if name is not blank
    47. if (!name.equalsIgnoreCase("")) {
    48. //set <name>.isAccepted: True
    49. getConfig().set(name + ".isAccepted", "True");
    50. sender.sendMessage("§a" + name +" was accepted!");
    51. saveConfig();
    52. }
    53. else {
    54. sender.sendMessage("Usage: /accept <player>");
    55. //send the user the usage message
    56. }
    57.  
    58. }
    59. }
    60. else if (cmd.getName().equalsIgnoreCase("denyapp")){
    61.  
    62. if (sender.isOp()) {
    63. String name = "";
    64. //Set name to args if they are not blank
    65. if (args.length > 0) name = args[0];
    66. //run code if name is not blank
    67. if (!name.equalsIgnoreCase("")) {
    68. //set <name>.isAccepted: False
    69. getConfig().set(name + ".isAccepted", "False");
    70. saveConfig();
    71. sender.sendMessage("§c" + name +" was denied!");
    72. }
    73. else {
    74. sender.sendMessage("Usage: /deny <player>");
    75. //send the user the usage message
    76. }
    77.  
    78. }
    79. }
    80. else if (cmd.getName().equalsIgnoreCase("checkapp")){
    81. this.reloadConfig();
    82. String config = (getConfig().getString(sender.getName() + ".isAccepted"));
    83. if (config.equalsIgnoreCase("pending")){
    84. sender.sendMessage("§0§l§m====================");
    85. sender.sendMessage("§5§lYour application is pending...");
    86. sender.sendMessage("§5§lAn admin will read your application soon!");
    87. sender.sendMessage("§5§lDo /apply if you have not applied.");
    88. sender.sendMessage("§0§l§m====================");
    89. sender.sendMessage("§a§lDo not ask an admin to read your application till it has been 3 days since you have applied!");
    90.  
    91. }
    92. else if (config.equalsIgnoreCase("true")){
    93. sender.sendMessage("§0§l§m====================");
    94. sender.sendMessage("§5§lYour application was §5Accepted!");
    95. sender.sendMessage("§5§lWelcome to the server!");
    96. sender.sendMessage("§0§l§m====================");
    97. Bukkit.getServer().dispatchCommand(getServer().getConsoleSender(), "pex user " + sender + " group set member");
    98. sender.sendMessage("§a§lYou are now in group member!");
    99. getConfig().set(sender.getName() + ".isAccepted", "Accepted and Promoted");
    100. saveConfig();
    101.  
    102.  
    103. }
    104. else if (config.equalsIgnoreCase("false")){
    105. sender.sendMessage("§0§l§m====================");
    106. sender.sendMessage("§5§lYour application was §cDenied!");
    107. sender.sendMessage("§5§lSorry, You cant join the server.");
    108. sender.sendMessage("§0§l§m====================");
    109. sender.sendMessage("§c§lYou can apply again in §6§l2 §c§ldays.");
    110. sender.sendMessage("§c§lGoodLuck!");
    111. }
    112. else if (config.equalsIgnoreCase("accepted and promoted")){
    113.  
    114. sender.sendMessage("§0§l§m====================");
    115. sender.sendMessage("§5§lYour application was §5Accepted!");
    116. sender.sendMessage("§5§lWelcome to the server!");
    117. sender.sendMessage("§0§l§m====================");
    118. sender.sendMessage("§c§lYou have already been promoted!");
    119. sender.sendMessage("§c§lIf you are still not promoted ask a member of staff!");
    120.  
    121. }
    122. else {
    123. sender.sendMessage("§c§lThere was a problem when checking your application. Try again later.");
    124. }
    125. }
    126.  
    127.  
    128.  
    129.  
    130.  
    131. return false;
    132. }
    133.  
    134. }
    135.  


    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 14, 2016
  18. Offline

    fireblast709

    SleepyDog What does it do at the moment? Have you debugged the current code? Have you taken in account that YAML is case sensitive (so the playername you give in /acceptapp and /denyapp will need to have the same case as the name of the player that executes /checkapp)?

    On a side note, you should use ChatColor instead of section signs :p (not that it should matter for the code, this is just for the readability and robustness later on ;3)
     
  19. Offline

    SleepyDog

    The config looks like this when i acccept the app ==>



    # default config.yml
    apply1: Welcome to the server!
    apply2: To build, mine and play
    apply3: you need to apply for Member!
    apply4: The application is a simple
    apply5: questionare to see why you
    apply6: want to join the community.
    apply7: Apply at <Website>/apply/member/
    sleepydog935:
    isAccepted: 'True'

    I want the code above to check what the config says and then act,
    if the config says true next to their name it will promote them to member when they do /checkapp and then change their status to 'accepted and promoted'

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 14, 2016
  20. Offline

    fireblast709

    SleepyDog print 'config' to the console (the one in checkapp)?
     
  21. Offline

    SleepyDog

    Dont worry, i was using equalsIgnoreCase insted of .equals.
     
  22. Offline

    fireblast709

Thread Status:
Not open for further replies.

Share This Page