Solved Getting a List from Config

Discussion in 'Plugin Development' started by Blah1, Oct 5, 2013.

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

    Blah1

    I'm getting an error on "config" (line 23) I don't know what's wrong..

    /help class:
    Code:java
    1. package me.MirrorRealm.kadmin;
    2.  
    3. import org.bukkit.Bukkit;
    4. import org.bukkit.ChatColor;
    5. import org.bukkit.command.Command;
    6. import org.bukkit.command.CommandExecutor;
    7. import org.bukkit.command.CommandSender;
    8. import org.bukkit.entity.Player;
    9.  
    10. public class Help implements CommandExecutor{
    11. public Main plugin;
    12. public Help(Main plugin){
    13. this.plugin = plugin;
    14. }
    15. public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args){
    16. Player player = (Player) sender;
    17. if (cmd.getName().equalsIgnoreCase("help")){
    18. if (!(player.hasPermission("command.help"))){
    19. player.sendMessage(ChatColor.RED + "You do not have permission");
    20. return true;
    21. }
    22. if (args.length == 0){
    23. player.sendMessage(plugin.getConfig().getList(config.help));
    24. return true;
    25. }
    26. }
    27. }
    28. }
    29.  


    config:
    Code:
    config:
      help:
        - "help"
        - "hello"
     
  2. Offline

    The_Doctor_123

    Code:java
    1. player.sendMessage(plugin.getConfig().getList(config.help));

    When you get the list, you're getting it from a String.
    Code:java
    1. player.sendMessage(plugin.getConfig().getList("config.help"));


    EDIT: Also, I don't believe you can send players a List.. if you can, it will look really bad.
     
  3. Offline

    metalhedd

    not a List, but a String[] is possible, it just displays them as if they were separate Player.sendMessage(String) calls, each on a new line.
     
  4. Offline

    The_Doctor_123

  5. Offline

    metalhedd

    so if you could pass a List<String> there's no reason to think it would look bad.. You could also just call toArray on the list you back.
     
  6. Offline

    Blah1

    So...what should I use?
     
  7. Offline

    Gopaintman

    Code:java
    1. String[] name = this.getConfig().getStringList("nameoflist").toArray(new String[0);


    Then to display to user
    Code:java
    1. for(int i = 0; i <name.length; i++){
    2. player.sendmessage(name[i]);
    3. }[/i]
     
  8. Offline

    DAZ3DNDC0NFUS3D

    Or you could do

    Code:java
    1. List<String> list = this.getConfig().getStringList("Key.To.List");
    2. for (String string : list){
    3. player.sendMessage(ChatColor.DARKAQUA + string);
    4. }
     
    Blah1 likes this.
  9. Offline

    Blah1

    Also, is there an easy way to change the chatcolors so that they are compatible with the &x way?
    I know that you can replace "&7" (or whatever) with ChatColor.GRAY but is there an easier way?
     
  10. Offline

    The_Doctor_123

    metalhedd
    If you could(but you can't, the argument for sendMessage is a String), it would look like an ID of the class's instance. But yes, you could just convert it to an array.
     
  11. Offline

    DAZ3DNDC0NFUS3D

    The way I do colors like that in configs is as followed:
    Code:
    config.yml
    StringList:
        - &a test1
        - &a test2
    Code:java
    1. public class Test extends JavaPlugin{
    2. public static Test test;
    3.  
    4. @Override
    5. public void onDisable(){
    6.  
    7. }
    8. @Override
    9. public void onEnable(){
    10.  
    11. }
    12. @EventHandler
    13. public boolean onCommand(CommandSender s, Command c, String s, String[] a){
    14. List<String> test1 = this.getConfig().getStringList("StringList");
    15. test1 = ChatColor.translateAlternateColorCodes('&', test1)
    16. if (s.equalsIgnoreCase("test"){
    17. for( String string : test1){
    18. s.sendMessage(test1);
    19. }
    20. }
    21. }
    22.  
    23. }


    I believe that this snippet of code is what you are looking for:
    test1 = ChatColor.translateAlternateColorCodes('&', test1)
     
Thread Status:
Not open for further replies.

Share This Page