send List<String>

Discussion in 'Plugin Development' started by stamline, Sep 16, 2015.

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

    stamline

    Hi, i would like to do a /help command. The player will get a return from config file within the list "HelpMessages".
    All the "Messages" in the list will be send to the player. I would also like to implement color codes.

    My current code looks like this:
    Code:
    public class Help implements CommandExecutor {
    
        @Override
        public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
            if(args.length == 0){
                @SuppressWarnings("unchecked")
                List<String> helpmessages = (List<String>)Main.plugin.getConfig().getList("HelpMessages");
                sender.sendMessage(helpmessages);
            }
            sender.sendMessage("Will be added soon");
            return false;
        }
    
    }
    

    And o would like to have my config like this:
    PHP:
    HelpMessages:
    'Line1'
    'Line2'
    'Should be avalible'
    'to add how many'
    'lines you want'
    Thank you so much :D

    is this how i should do it?
    Code:
    @SuppressWarnings("unchecked")
                List<String> helpmessages = (List<String>)Main.plugin.getConfig().getList("HelpMessages");
                String Message = helpmessages.toString();
                sender.sendMessage(Message);
    merged
     
    Last edited by a moderator: Sep 16, 2015
  2. Offline

    RoboticPlayer

    Does it do anything when you run the command?
     
  3. Offline

    stamline

    all of the content in the list are added into one line i minecraft.
    I want it to be seperated lines i minecraft too...

    yes, everything is comming out in one line. What i wanted was on seperated lines

    EDIT by Timtower: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Sep 16, 2015
  4. 1. Don't cast to List<String>. You can use .getStringList("path") instead of .getList("path")
    2. Loop through the list and send the player the message of the current String you are iterating over
     
    mine-care likes this.
  5. Offline

    teej107

  6. Offline

    Zombie_Striker

    @stamline
    There are two ways you can do this. 1:
    1. Loop through all the strings, use the methods listed above for color codes, and send each one individually.
    2. Create a String field and create a loop for all the strings. Inside that loop you would take the string field, add "\n" (\n means new line) and then the next String.
     
  7. Offline

    stamline

    how do i loop through it?
     
  8. Offline

    teej107

    @stamline loop through your list of strings and send each message individually with the colors
     
  9. Offline

    stamline

    @teej107
    but if i have 1000 messages i will send, do i have to make a new "player.sendMessage();" every time?
     
  10. Offline

    Zombie_Striker

    @stamline
    If you use the first way that I mentioned, yes. If you use the second way I mentioned (using \n), then you would only need to use the .sendmessage once.
     
  11. Offline

    rbrick

  12. There is .sendMessage(String str) and .sendMessage(String[] str)... so you could just turn that list into an array:
    Code:
    .sendMessage((String[]) list.toArray());
    I think this would be the easiest solution but it won't support color codes. If you want color codes you need to use @Zombie_Striker's or @teej107's answers
     
Thread Status:
Not open for further replies.

Share This Page