Solved Configuration API problems

Discussion in 'Plugin Development' started by Weasel_Squeezer, May 19, 2013.

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

    Weasel_Squeezer

    Well I have been struggling with this API for a very long time, and this is the second time I have tried to take a stab at using the yaml configuration API in bukkit. All my previous plugins, I reverted to property files because I could never get the YAML config files working. My problem is understanding the API for use with storing data rather than using it for the default config files. I am doing everything that I have read of different threads and watched in tutorials, but I have had do prevail.
    For this project, I am trying to create yml files for holding data but I cannot get it to actually get anything on the file. I know that once I understand this API, it will be much better that using property files.
    I am starting with a blank yml file and want to be able to create the paths and values like such:
    Code:
    plots:
            test:
                min: {}
                max: {}
                flags: {}
                owners: {}
    So here is what I am working with at the moment.

    Code:java
    1. @Override
    2. public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
    3. if (sender instanceof Player == false) {
    4. sender.sendMessage(ChatColor.RED + "This command can only be used in game.");
    5. return true;
    6. }
    7. Player player = (Player)sender;
    8. if (label.equalsIgnoreCase("rem")) {
    9. if (args.length == 2) {
    10. if (args[0].equalsIgnoreCase("create")) {
    11. if (plugin.wg.getWorldEdit() != null) {
    12. Location minPoint = plugin.wg.getSelectionMinPoint(player);
    13. Location maxPoint = plugin.wg.getSelectionMaxPoint(player);
    14. if (maxPoint != null && minPoint != null) {
    15. if (!plugin.config.getPlotConfig().contains("plots."+args[1])) {
    16. ArrayList<String> minCoords = new ArrayList<String>();
    17. ArrayList<String> maxCoords = new ArrayList<String>();
    18. ArrayList<String> flags = new ArrayList<String>();
    19. ArrayList<String> owners = new ArrayList<String>();
    20. minCoords.add("x: " + minPoint.getX());
    21. minCoords.add("Y: " + minPoint.getY());
    22. minCoords.add("Z: " + minPoint.getZ());
    23. maxCoords.add("x: " + maxPoint.getX());
    24. maxCoords.add("Y: " + maxPoint.getY());
    25. maxCoords.add("Z: " + maxPoint.getZ());
    26. Map<String, Object> entry = new HashMap<String, Object>();
    27. entry.put("plots."+args[1]+".min", minCoords);
    28. entry.put("plots."+args[1]+".max", maxCoords);
    29. entry.put("plots."+args[1]+".flags", flags);
    30. entry.put("plots."+args[1]+".owners", owners);
    31. //here is the important code for setting the new paths and values and then saving it to the file
    32. plugin.config.setPlotValues(entry);
    33. plugin.config.savePlotConfig();
    34. } else {
    35. player.sendMessage(ChatColor.RED + "A plot named" + args[1] + " already exists");
    36. }
    37. } else {
    38. player.sendMessage(ChatColor.RED + "You must make a WorldEdit Selection first");
    39. }
    40. } else {
    41. player.sendMessage(ChatColor.RED + "WorldEdit is not loaded on this server");
    42. }
    43. }
    44. }
    45. }
    46. return false;
    47. }



    this comes from my FileUtils class and is being called by: "plugin.config" in the code above
    This is the important code that I am having problems with.

    Code:java
    1. private final Plugin plugin;
    2.  
    3. private File regionsConfigFile = null;
    4. private FileConfiguration regionsConfig = null;
    5.  
    6. public FileUtils(Plugin instance) {
    7. plugin = instance;
    8. }
    9.  
    10. public void reloadRegionsConfig() {
    11. regionsConfigFile = new File(plugin.mainDirectory + "plots.yml");
    12. regionsConfig = YamlConfiguration.loadConfiguration(regionsConfigFile);
    13.  
    14. // Look for defaults in the jar
    15. InputStream defConfigStream = plugin.getResource("plots.yml");
    16. if (defConfigStream != null) {
    17. YamlConfiguration defConfig = YamlConfiguration.loadConfiguration(defConfigStream);
    18. regionsConfig.setDefaults(defConfig);
    19. }
    20. }
    21.  
    22. public FileConfiguration getPlotConfig() {
    23. reloadRegionsConfig();
    24. return regionsConfig;
    25. }
    26.  
    27. public void savePlotConfig() {
    28. if (regionsConfig == null || regionsConfig == null) {
    29. return;
    30. }
    31. try {
    32. getPlotConfig().save(regionsConfigFile);
    33. } catch (IOException ex) {
    34. plugin.getLogger().log(Level.SEVERE, "Could not save config to " + regionsConfigFile, ex);
    35. }
    36. }
    37.  
    38.  
    39. public void setPlotValues(Map<String, Object> map) {
    40. for (Entry<String, Object> values : map.entrySet()) {
    41. getPlotConfig().set(values.getKey(), values.getValue());
    42. }
    43. }


    Thanks for helping me out! I really appreciate it :)

    can anyone help me with this?

    Never mind. I figured it out :p It just took too long.

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

Share This Page