Remove keys from config file

Discussion in 'Plugin Development' started by beatcomet, Jul 13, 2011.

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

    beatcomet

    Hello guys, I need some help here.
    Is there any way to remove a key from the config file?
     
  2. Offline

    Lolmewn

    Using Properties or Configuration class?
     
  3. Offline

    beatcomet

    configuration

    here take a look at my main class :

    Code:java
    1.  
    2. package me.beatcomet.RC;
    3.  
    4. import java.io.File;
    5. import java.util.logging.Logger;
    6.  
    7. import org.bukkit.ChatColor;
    8. import org.bukkit.command.Command;
    9. import org.bukkit.command.CommandSender;
    10. import org.bukkit.entity.Player;
    11. import org.bukkit.inventory.ItemStack;
    12. import org.bukkit.plugin.PluginDescriptionFile;
    13. import org.bukkit.plugin.java.JavaPlugin;
    14. import org.bukkit.util.config.Configuration;
    15.  
    16. import com.nijiko.permissions.PermissionHandler;
    17. import com.nijikokun.bukkit.Permissions.Permissions;
    18. import org.bukkit.plugin.Plugin;
    19.  
    20. public class RC extends JavaPlugin{
    21. Logger log = Logger.getLogger("Minecraft");
    22. File configFile = new File("plugins/" + "RC" + "/codes.xD1");
    23. Configuration config = new Configuration(configFile);
    24. public static PermissionHandler permissionHandler;
    25. private void setupPermissions() {
    26. if (permissionHandler != null) {
    27. return;
    28. }
    29. Plugin permissionsPlugin = this.getServer().getPluginManager().getPlugin("Permissions");
    30. if (permissionsPlugin == null) {
    31. log.info("Permission system not detected, defaulting to OP");
    32. return;
    33. }
    34. permissionHandler = ((Permissions) permissionsPlugin).getHandler();
    35. log.info("Found and will use plugin "+((Permissions)permissionsPlugin).getDescription().getFullName());
    36. }
    37.  
    38. public void onDisable(){
    39. PluginDescriptionFile pdf = this.getDescription();
    40. log.info(pdf.getName() + " Version " + pdf.getVersion() + " is DISABLED!");
    41. }
    42. public void onEnable(){
    43. PluginDescriptionFile pdf = this.getDescription();
    44. log.info(pdf.getName() + " Version " + pdf.getVersion() + " is ENABLED!");
    45. setupPermissions();
    46. new File("plugins/" + "RC").mkdir();
    47. if (!configFile.exists()) {
    48. try {
    49. configFile.createNewFile();
    50.  
    51. } catch (Exception e) {
    52. // sending console message in case the data file could not be
    53. // created
    54. log.info("[Freeze] Error when creating config file.");
    55. }
    56. }
    57. // Loading data file
    58. config.load();
    59. }
    60. public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args){
    61. if(sender instanceof Player){
    62. Player p = (Player) sender;
    63. String type;
    64. String amount;
    65. String name;
    66. if(commandLabel.equalsIgnoreCase("code") && args.length == 4 && RC.permissionHandler.has(p, "code.set")){
    67. if(args[0].equalsIgnoreCase("set")){
    68. name = args[1];
    69. type = args[2];
    70. amount = args[3];
    71. config.setProperty(name + "." + "id", Integer.parseInt(type));
    72. config.setProperty(name + "." + "amount", Integer.parseInt(amount));
    73. config.save();
    74. p.sendMessage(ChatColor.DARK_PURPLE + "[RC]" + ChatColor.BLACK + name + ChatColor.AQUA + " Has Beed Added To The Code List!");
    75. }return true;
    76. }
    77. if(commandLabel.equalsIgnoreCase("redeem")){
    78. if(args.length == 0){
    79. p.sendMessage(ChatColor.DARK_PURPLE + "[RC]" + ChatColor.RED + "No code inserted!");
    80. }
    81. if(args.length > 1){
    82. p.sendMessage(ChatColor.DARK_PURPLE + "[RC]" + ChatColor.RED + "Invalid code inserted!");
    83. }
    84. if(args.length == 1){
    85. String code = args[0];
    86. if(config.getKeys(code) == null){
    87. p.sendMessage(ChatColor.DARK_PURPLE + "[RC]" + ChatColor.RED + "No code found");
    88. }else{
    89. int amnt = (Integer) config.getProperty(args[0] + "." + "amount");
    90. int typeid = (Integer) config.getProperty(args[0] + "." + "id");
    91. p.getInventory().addItem(new ItemStack(amnt, typeid));
    92. p.sendMessage(ChatColor.DARK_PURPLE + "[RC]" + ChatColor.BLACK + args[0] + ChatColor.AQUA + " Redeemed!");
    93. }
    94. }return true;
    95. }if(commandLabel.equalsIgnoreCase("code") && args.length == 2 && RC.permissionHandler.has(p, "code.remove")){
    96. if(args[0].equalsIgnoreCase("Remove")){
    97. String key = args[1];
    98. if(config.getKeys(key) != null){
    99. config.removeProperty(args[1]+"."+"amount");
    100. config.removeProperty(args[1]+"."+"id");
    101. config.save();
    102. p.sendMessage(ChatColor.DARK_PURPLE + "[RC]" + ChatColor.BLACK + key + ChatColor.AQUA + " Removed!");
    103. }
    104. if(config.getKeys(key) == null){
    105. p.sendMessage(ChatColor.DARK_PURPLE + "[RC]" + ChatColor.BLACK + key + ChatColor.AQUA + " Does Not Exist!");
    106. }
    107. }return true;
    108. }
    109. }if(!(sender instanceof Player)){
    110. log.info("You cannot reddem or set codes using console commands!");
    111. }
    112. return false;
    113. }
    114. }


    It's not finished yet so...
     
  4. Offline

    Lolmewn

    Hmm, I see. Well, You can either use Configuration c = new Configuration(configFile); or Properties prop = new Properties();. I like both :p
     
  5. Offline

    beatcomet

    So is there a way to remove keys?
     
  6. Offline

    Lolmewn

    I know there is in the Properties thingy, that's prop.removeKey or something similar, and in Configuraion there also is one. Need small code snippet?
     
  7. Offline

    beatcomet

    yea
     
  8. Offline

    Lolmewn

    static File Settings = new File("plugins/Skillz/settings.yml");
    Configuration c = new Configuration(Settings);
    c.removeProperty("Node");
     
  9. Offline

    beatcomet

    It just removed the property, not the ket itself...
     
  10. Offline

    Lolmewn

    Oh, I thought it would remove everything. You can also do
    Code:
    Properties prop = new Properties();
    try{
    FileInputStream in = new FileInputStream(settings);
    prop.load(in);
    prop.remove("Key");
    }catch(IOException e){
    e.printStackTrace();
    }
     
  11. Offline

    beatcomet

    I don't really like it...
    I am too lazy and rewriting all the code is not an easy task.
    I will leave it as it is right now.
    Thanks for trying :)

    BTW the plugin will probably be released today under the name of RC (Redeem codes).
     
Thread Status:
Not open for further replies.

Share This Page