Solved Help using \n in config.yml for new line

Discussion in 'Spigot Plugin Development' started by floory565, Jun 26, 2016.

  1. floory565

    Moderator

    Hi people,

    Part of a plugin I'm developing involves sending a message to players that is specified in the config.yml.

    For example:
    Code (YAML):
    message: 'Hello buddy!'
    But I want to allow server owners to add new lines to the message using \n, like this:
    Code (Text):
    message: 'Hello buddy!\nHow are you?'
    so that the player would see this in chat:
    How would I be able to implement such a feature using \n to specify a new line?

    Thanks in advance!
     
  2. Instead, you could create a list of messages to send in the config. Then, loop through the list and send the player the messages in the list.
     
    • Agree Agree x 1
  3. You could split the String at '\n' which creates a String array. I wouldn't actually recommend that. In the config, you could use a list or an array which you can send to the player.
     
  4. ChatColor uses split("\n") on translateAlternateColors, so what's the big deal in a config?
     
  5. floory565

    Moderator

    I presume if I split the message like @Web_ mentioned then use the method that @Lightspeed360 suggested, that would do the trick. But I'm new to plugin development so I'm not entirely sure how I would split the message into a String array then use the method to send the player the messages. Any help would be appreciated :)
     
  6. floory565

    Moderator

    So if I did:
    Code (Text):
    String message = this.getConfig().getString("message");
    String[] messageSplit = message.split("\n")
    what would I do from there?
     
    #8 floory565, Jun 26, 2016
    Last edited: Jun 26, 2016
  7. floory565

    Moderator

    Oh I see, I'll give it a go and see how it goes :)
     
  8. floory565

    Moderator

    I did:
    Code (Text):
    String message = this.getConfig().getString("message");
    String[] messageSplit = messageConfig.split("\n")
    player.sendMessage(messageSplit);
    and it didn't work.

    I'm probably making a basic mistake, but from my perspective this is "new territory", so sorry if it's a simple problem!
     
  9. Either:
    Code (Java):
     for (String s : split) player.sendMessage(s);
    or (Java 8)
    Code (Java):
    Arrays.asList(split).forEach(Player::sendMessage);
     
  10. floory565

    Moderator

    Hey,
    Neither of them worked, BUT I think that they should - I believe that there may be a problem with the backslash:
    (https://bukkit.org/threads/kick-with-newline.107338/)

    I'll experiment with the points raised in that thread, if you come up with anything I'd be super appreciative if you could share it :)
     
  11. floory565

    Moderator

    Yep, using proper quotation marks in the config.yml solved the problem - thanks for your help!