[EXAMPLE] Fully-fleshed-out Config Value Changing Command

Discussion in 'Resources' started by Musaddict, Apr 5, 2012.

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

    Musaddict

    I just spent a couple hours writing this and thought; "I bet other people will want this in their plugins too, but don't want to hassle with it."

    So, here you go! Public-Domain code for editing configs for everyone to share (made by moi). This is how this code works, you type:
    Code:
    /[your command] config [config option]
    and it will tell you the current value of the specified option for your plugin's config. If you type:
    Code:
    /[your command] config [config option] (option value)
    then it will set and save the new value to your config file. Don't worry, there's checks for if it's a boolean, int, double, long, or a string (am I forgetting any others?), so it won't let you change a double "10.0" into a string "mwahaha", but it WILL let you enter an integer and convert it to a double if it needs to!

    Also included permissions in there for ya.

    Anyhoo, here ya go:
    Code:java
    1. else if(Command.equalsIgnoreCase("config")){
    2. Player player = (Player)sender;
    3.  
    4. if(player.isOp() || player.hasPermission("[yourplugin].config")){
    5.  
    6. if (arg.length == 3){
    7. String configOption = arg[1].toLowerCase();
    8. String setValue = arg[2].toLowerCase();
    9.  
    10. if(plugin.getConfig().isSet(configOption)){
    11.  
    12. if(plugin.getConfig().isBoolean(configOption)){
    13. player.sendMessage(ChatColor.AQUA + configOption + ChatColor.GRAY + " is a boolean (true|false)");
    14.  
    15. if(setValue.equalsIgnoreCase("true")){
    16. plugin.getConfig().set(configOption, true);
    17. setValue = "true";
    18. }
    19.  
    20. else if(setValue.equalsIgnoreCase("false")){
    21. plugin.getConfig().set(configOption, false);
    22. setValue = "false";
    23. }
    24.  
    25. else{
    26. player.sendMessage(ChatColor.RED + "\"" + ChatColor.AQUA + setValue + ChatColor.RED + "\" is an invalid boolean.");
    27. return true;
    28. }
    29. }
    30.  
    31. else if(plugin.getConfig().isDouble(configOption)){
    32. player.sendMessage(ChatColor.AQUA + configOption + ChatColor.GRAY + " is a double (X.X)");
    33. Double setFinal = null;
    34. try{
    35. setFinal = Double.parseDouble(setValue);
    36. setValue = "" + setFinal;
    37. player.sendMessage(ChatColor.RED + "\"" + ChatColor.AQUA + setValue + ChatColor.RED + "\" is not a valid double. ex: '10.0'");
    38. return true;
    39. }
    40. plugin.getConfig().set(configOption, setFinal);
    41. }
    42.  
    43. else if(plugin.getConfig().isInt(configOption)){
    44. player.sendMessage(ChatColor.AQUA + configOption + ChatColor.GRAY + " is an int (X)");
    45. Integer setFinal = null;
    46. try{
    47. setFinal = Integer.parseInt(setValue);
    48. setValue = "" + setFinal;
    49. player.sendMessage(ChatColor.RED + "\"" + ChatColor.AQUA + setValue + ChatColor.RED + "\" is not a valid int. ex: '10'");
    50. return true;
    51. }
    52.  
    53. plugin.getConfig().set(configOption, setFinal);
    54.  
    55. }
    56.  
    57. else if(plugin.getConfig().isLong(configOption)){
    58. Long setFinal = null;
    59. player.sendMessage(ChatColor.AQUA + configOption + ChatColor.GRAY + " is a long (X)");
    60. try{
    61. setFinal = Long.parseLong(setValue);
    62. setValue = "" + setFinal;
    63. player.sendMessage(ChatColor.RED + "\"" + ChatColor.AQUA + setValue + ChatColor.RED + "\" is not a valid long. ex: '10'");
    64. return true;
    65. }
    66. plugin.getConfig().set(configOption, setFinal);
    67.  
    68. }
    69.  
    70. else if(plugin.getConfig().isString(configOption)){
    71. player.sendMessage(ChatColor.AQUA + configOption + ChatColor.GRAY + " is a string (blah)");
    72. plugin.getConfig().set(configOption, setValue);
    73. }
    74. plugin.saveConfig();
    75. player.sendMessage(ChatColor.GRAY + "You have set " + ChatColor.AQUA + configOption + ChatColor.GRAY + " to " + ChatColor.AQUA + setValue);
    76. return true;
    77. }
    78.  
    79. else {
    80. player.sendMessage(ChatColor.AQUA + configOption + ChatColor.RED + " does not exist.");
    81. return true;
    82. }
    83. }
    84.  
    85. else if(arg.length == 2){
    86. String configOption = arg[1].toLowerCase();
    87.  
    88. if(plugin.getConfig().isSet(configOption)){
    89. String value = null;
    90.  
    91. if(plugin.getConfig().isString(configOption)){
    92. player.sendMessage(ChatColor.AQUA + configOption + ChatColor.GRAY + " is a string (blah)");
    93. value = "" + plugin.getConfig().getString(configOption);
    94. }
    95.  
    96. else if(plugin.getConfig().isBoolean(configOption)){
    97. player.sendMessage(ChatColor.AQUA + configOption + ChatColor.GRAY + " is a boolean (true|false)");
    98. value = "" + plugin.getConfig().getBoolean(configOption);
    99. }
    100.  
    101. else if(plugin.getConfig().isDouble(configOption)){
    102. player.sendMessage(ChatColor.AQUA + configOption + ChatColor.GRAY + " is a double (X.X)");
    103. value = "" + plugin.getConfig().getDouble(configOption);
    104. }
    105.  
    106. else if(plugin.getConfig().isInt(configOption)){
    107. player.sendMessage(ChatColor.AQUA + configOption + ChatColor.GRAY + " is an int (X)");
    108. value = "" + plugin.getConfig().getInt(configOption);
    109. }
    110.  
    111. else if(plugin.getConfig().isLong(configOption)){
    112. player.sendMessage(ChatColor.AQUA + configOption + ChatColor.GRAY + " is a long (X)");
    113. value = "" + plugin.getConfig().getLong(configOption);
    114. }
    115.  
    116. if(value == null){
    117. player.sendMessage(ChatColor.RED + "No value is set for " + ChatColor.AQUA + configOption);
    118. return true;
    119. }
    120.  
    121. else{
    122. player.sendMessage(ChatColor.AQUA + configOption + ChatColor.GRAY + " is set to: " + ChatColor.AQUA + value);
    123. return true;
    124. }
    125. }
    126.  
    127. else{
    128. player.sendMessage(ChatColor.AQUA + configOption + ChatColor.RED + " is not a valid config option!");
    129. return true;
    130. }
    131.  
    132. }
    133.  
    134. else{
    135. player.sendMessage(ChatColor.GRAY + "Invalid number of arguments.");
    136. player.sendMessage(ChatColor.GRAY + "/[command] config [config option] (option value)");
    137. return true;
    138. }
    139. }
    140.  
    141. else{
    142. player.sendMessage(ChatColor.RED + "You do not have permission to use that command.");
    143. return true;
    144. }
    145. }


    I'm sure there's optimizations to be done, but I'll leave that up to you guys.

    Oh, and this is all assuming you have this at the beginning of your commands class:

    Code:java
    1. public class [your commands class] implements CommandExecutor{
    2. public static [your plugin] plugin;
    3.  
    4. public [your commands class] ( [your plugin] instance) {
    5. plugin = instance;
    6. }
    7.  
    8. @Override
    9. public boolean onCommand(CommandSender sender, Command command, String label, String[] arg) {
    10.  
    11. if (arg.length > 0) {
    12. String Command = arg[0];

    and this in your onEnable:
    Code:java
    1. getConfig().options().copyDefaults(true);
    2. saveConfig();
    3.  
    4. getCommand("[your command]").setExecutor(new [your commands class](this));


    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 25, 2016
Thread Status:
Not open for further replies.

Share This Page