Getting one string from a list in config?

Discussion in 'Plugin Development' started by CRAZYxMUNK3Y, Aug 5, 2012.

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

    CRAZYxMUNK3Y

    I am having a slight issue with getting one string from a list of string within a config file.

    What i am attempting to do is on a command, check to see if the string is in the config, and if it is, get the sub-config options and go from there;

    Eg: If the command is /spawn, find it in the config, and then get the sub-config options within it. Hopefully this makes sense.

    Code
    Code:java
    1.  
    2. package me.crazy.payforcommands;
    3.  
    4. import java.io.File;
    5. import java.io.FileWriter;
    6. import java.util.List;
    7. import java.util.logging.Level;
    8. import java.util.logging.Logger;
    9.  
    10. import org.bukkit.event.player.AsyncPlayerChatEvent;
    11. import org.bukkit.plugin.java.JavaPlugin;
    12.  
    13. public class PayForCommands extends JavaPlugin {
    14.  
    15. Logger log = Logger.getLogger("Minecraft");
    16.  
    17. protected List<String> commands;
    18. protected int cost;
    19. protected String item;
    20.  
    21. public void onEnable() {
    22. commands = getConfig().getStringList("commands");
    23. try {
    24. File file = new File(getDataFolder(), "config.yml");
    25. if (!file.exists()) {
    26. file.getParentFile().mkdirs();
    27. FileWriter writer = new FileWriter(file);
    28. String crlf = System.getProperty("line.separator");
    29. writer.write("commands : [/spawn, /home]" + crlf + "\"/spawn\" :" + crlf + " item :" + crlf
    30. + " cost :");
    31. writer.close();
    32. log.info("[PayForCommands] Config created");
    33. }
    34. getConfig().load(file);
    35. items = new Items[commands.size()];
    36. for (int i = 0; i < commands.size(); i++) {
    37. items[i] = new Items(commands.get(i), getConfig().getString(commands.get(i) + ".cost",
    38. getConfig().getString(commands.get(i) + ".item")));
    39. }
    40. } catch (Exception e) {
    41. log.log(Level.SEVERE, "[PayForCommands] Exception while reading from config.yml", e);
    42. getServer().getPluginManager().disablePlugin(this);
    43. }
    44. }
    45.  
    46. protected Items[] items;
    47.  
    48. public class Items {
    49.  
    50. public String cost;
    51. public String commands;
    52.  
    53. public Items(String commands, String cost) {
    54. this.cost = cost;
    55. this.commands = commands;
    56.  
    57. }
    58.  
    59. }
    60.  
    61. public void playerChat(AsyncPlayerChatEvent e) {
    62. if (e.getMessage().contains((CharSequence) getConfig().getStringList("commands.")));
    63. // Not sure what do do here
    64. }
    65.  
    66. }
    67.  
    68. [/i]


    Default Config:
    Code:
    commands : [/spawn, /home]
    "/spawn" :
        item :
        cost :
    
    Thanks


    Just an ordinary bump :)

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

    Kodfod

    Well this came from the Bukkit Wiki

    Code:JAVA
    1. List<String> rules = getConfig().getStringList("rules");
    2. for (String s : rules)
    3. sender.sendMessage(s);
    4. }
    5. return true;
     
Thread Status:
Not open for further replies.

Share This Page