YAML quotes for string

Discussion in 'Plugin Development' started by deasertman, Mar 24, 2014.

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

    deasertman

    Hello there! I've been testing my plugin and I've recently came on with a bug which I'm not being able to fix. Well, my plugin has a config, and it takes a string from it. However, if the string starts with a color code or a symbol, it's a YML error causing the plugin to stop working properly. So I tried to add quotes ' ' to my config, and it worked, without showing them in chat. So I tried to
    Code:
    config.addDefault("OldPlayer.Message.WelcomeMsg", "'Message'"
    But then it showed as '''Message''', ingame. So how do I fix this?

    I don't believe the code is needed in order to fix it, as my code is correct. If it is actually required I will post it.
     
  2. Offline

    Alshain01

    From within your code, adding a String to YAML will automatically add the quotes as needed. You should only manually add them if your directly editing the file. And because you mentioned it, if your using 'ยง' for colors, don't. That symbol is non-standard and has trouble moving between operating systems (actually it has more to do with ANSII vs. UTF file encoding, but different OS will make it obvious). Instead use a different character such as '&' and then use ChatColor.translateAlternateColorCodes('&', message);
     
  3. Offline

    deasertman

    No, I am using the & symbol. However it is not automatically adding the quotes, and I've tried deleting the config multiple times.

    Code:
    Code:java
    1. config.addDefault("OldPlayer.Message.WelcomeMsg", "Welcome back, %player!");

    Config:
    OldPlayer:
    Message:
    WelcomeMsg: Text

    If it matters, here's the code I use to send the message:
    Code:java
    1. String msg = getConfig().getString("OldPlayer.Message.WelcomeMsg").replace("%player", name);
    2. msg = ChatColor.translateAlternateColorCodes('&', msg);
    3. player.sendMessage(msg);

    The variable "name" is defined, don't worry.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 7, 2016
  4. Offline

    Alshain01

    deasertman Well, I don't see anything your doing different than my plugins, except I don't use % for my replacements, i use brackets {player}, I don't think that should make a difference but give it a try, maybe there is something I'm not aware of.

    I also don't use addDefault(), I save my defaults from resources and then change them with .set(). Again I would expect that to be different.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 7, 2016
  5. Offline

    deasertman

    Oh, well that didn't exactly help. But thanks anyways. Anyone else knows what's wrong?

    Bump. Anyone mind helping please?

    Ah, noone replied. Does anyone know what's wrong?

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 7, 2016
  6. deasertman I don't see anywhere in your code that you're using a string that contains &
     
  7. Offline

    DrEinsteinium

    deasertman I would try using String.format() and make %player a format specifier. Unless this isn't what you're looking for.
    Code:
    String msg = config.getString("OldPlayer.Message.WelcomeMsg", "%player", "<string to replace %player with>"
    Edit; I'm not sure if this code works properly... I only read the formatting javadocs for about 30 seconds and went to an assumption :p
     
  8. Offline

    deasertman

    AdamQpzm I'm giving examples, plus this isn't the point. The point is I am using 'translateAlternateColorCodes'. And anyways, I don't think that really matters from what I want.
    DrEinsteinium Thanks, but that's not really what I want.

    Guys, you're missing out on the point. What I want is for the string to have quotes in the YML file, or a solution to allow symbols to be used in the beginning of a string in the YML, so people can use color codes in the beginning of a String.
     
  9. Offline

    Alshain01

    deasertman You need to show us the actual code. There has to be something else at play here because the SnakeYAML implementation handles that for you.
     
  10. Offline

    deasertman

    I've solved this by inserting the config file manually, and inserting single quotes before and after strings. They work perfectly and do not show ingame. Plus, I changed from saveConfig(); to saveDefaultConfig(); Thanks Alshain01!
     
Thread Status:
Not open for further replies.

Share This Page