Solved YAML Configurations (Advanced?)

Discussion in 'Plugin Development' started by SourceForums, Feb 20, 2014.

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

    SourceForums

    Hey there, just a quick question with the YAML configurations. Is it possible to log something like:
    '&7Hello, my name is %Player.' for instance and allow the code to swap the '%Player' part with another variable? I've seen this in other plugins and would like to learn how it works. I tried a couple of codes but they don't seem to be working, which was quite expected. Tips? Thanks again! Please reply ASAP! :D
     
  2. plugin.getConfig().getString("message.hello").replace("%Player",player.getName());
     
  3. Offline

    TheE

    You need to replace the value before writing it to the file. That is actually pretty basic:

    Code:java
    1. String str = "&7Hello, my name is %Player.";
    2. str = str.replace("%Player", player.getDisplayName()); //or whatever you want it to say


    For performance purposes I would however recommend to use String.format() instead of replace:
    Code:java
    1. String str = "&7Hello, my name is %s.";
    2. str = String.format(str, player.getDisplayName()); //or whatever you want it to say


    Or, if you are dealing with translatable messages, the MessageFormat might help as it provides more flexible variables for different types.
     
  4. Offline

    SourceForums

    TheE
    Great! Thanks!
     
Thread Status:
Not open for further replies.

Share This Page