Saving multiple strings in a config

Discussion in 'Plugin Development' started by Ty Cleere, May 21, 2014.

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

    Ty Cleere

    What im trying to do is like where i add multiple links under my int in the config. so its like this.

    Links:
    - www.test.org
    - www.hypixel.net
    - www.blah.com

    this is my code..

    Code:java
    1. public boolean onCommand(CommandSender sender, Command cmd,
    2. String commandLabel, String[] args) {
    3. Player player = (Player) sender;
    4. if (commandLabel.equalsIgnoreCase("vote")) {
    5. player.sendMessage(ChatColor.AQUA + "" + Bukkit.getPluginManager().getPlugin("Vote Links").getConfig().getString("Links"));
    6. }
    7. if (commandLabel.equalsIgnoreCase("addlink")) {
    8. if(args.length == 0){
    9. player.sendMessage(ChatColor.RED + "You need a link!");
    10. }
    11.  
    12. if(args.length == 1){
    13. if( Bukkit.getPluginManager().getPlugin("Vote Links").getConfig().getString("Links").contains(args[0])){
    14. player.sendMessage(ChatColor.DARK_GREEN + "List already contains this web link!");
    15. }else{
    16. Bukkit.getPluginManager().getPlugin("Vote Links")
    17. .getConfig().set("Links", args[0]);
    18. player.sendMessage(ChatColor.GREEN + "You've added a link!");
    19. }
    20.  
    21. }
    22.  
    23. }
     
  2. Offline

    tacticalsk8er

    Use a List with the type of string. http://docs.oracle.com/javase/7/docs/api/java/util/List.html

    Here is your modified code using List<String>:
    Code:java
    1.  
    2. public boolean onCommand(CommandSender sender, Command cmd,
    3. String commandLabel, String[] args) {
    4. Player player = (Player) sender;
    5. if (commandLabel.equalsIgnoreCase("vote")) {
    6. player.sendMessage(ChatColor.AQUA + "" + Bukkit.getPluginManager().getPlugin("Vote Links").getConfig().getString("Links"));
    7. }
    8. if (commandLabel.equalsIgnoreCase("addlink")) {
    9. if(args.length == 0){
    10. player.sendMessage(ChatColor.RED + "You need a link!");
    11. }
    12.  
    13. if(args.length == 1){
    14. if( Bukkit.getPluginManager().getPlugin("Vote Links").getConfig().getStringList("Links").contains(args[0])){
    15. player.sendMessage(ChatColor.DARK_GREEN + "List already contains this web link!");
    16. }else{
    17. List<String> links = Bukkit.getPluginManager().getPlugin("Vote Links").getConfig().getStringList("Links");
    18. links.add(args[0]);
    19. Bukkit.getPluginManager.getPlugin("Vote Links").getConfig().set("Links", links);
    20. player.sendMessage(ChatColor.GREEN + "You've added a link!");
    21. }
    22.  
    23. }
    24.  
    25. }
    26.  
     
    Ty Cleere likes this.
  3. Offline

    Ty Cleere

    tacticalsk8er Thanks man!

    EDIT: It doesnt save them in the config like i tho it would. But it lists them! I want to be able to add them in the config and they will be there in the game
     
  4. Offline

    Ty Cleere

  5. Offline

    tacticalsk8er

    Did you save the config? saveConfig();
     
Thread Status:
Not open for further replies.

Share This Page