[solved] Chat color from the config file

Discussion in 'Plugin Development' started by vYN, Mar 7, 2012.

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

    vYN

    Hi. In my config file i got this:

    This is only an example.

    "&1Hi &aAnd &2Welcome!"

    Any suggestion on how to make that work? So the message that get printed in-game will be those colors?

    My plugin prints the lines just fine... But i can't figure out how i can change the color from the config.
     
  2. Offline

    HON95

    You need to replace yout color codes with ChatColor.<whateverColor>, e.g. ChatColor.RED.
    Easiest way of doing so; copy / paste this:

    Code:
            line = line.replaceAll("&0", ChatColor.BLACK + "");
            line = line.replaceAll("&1", ChatColor.DARK_BLUE + "");
            line = line.replaceAll("&2", ChatColor.DARK_GREEN + "");
            line = line.replaceAll("&3", ChatColor.DARK_AQUA + "");
            line = line.replaceAll("&4", ChatColor.DARK_RED + "");
            line = line.replaceAll("&5", ChatColor.DARK_PURPLE + "");
            line = line.replaceAll("&6", ChatColor.GOLD + "");
            line = line.replaceAll("&7", ChatColor.GRAY + "");
            line = line.replaceAll("&8", ChatColor.DARK_GRAY+ "");
            line = line.replaceAll("&9", ChatColor.BLUE + "");
            line = line.replaceAll("&a", ChatColor.GREEN + "");
            line = line.replaceAll("&b", ChatColor.AQUA + "");
            line = line.replaceAll("&c", ChatColor.RED + "");
            line = line.replaceAll("&d", ChatColor.LIGHT_PURPLE + "");
            line = line.replaceAll("&e", ChatColor.YELLOW + "");
            line = line.replaceAll("&f", ChatColor.WHITE + "");
            line = line.replaceAll("&g", ChatColor.MAGIC + "");
     
  3. Offline

    vYN

    hmm ok. How would i use this? I'm new to all this config text print out in-game. xD
     
  4. Offline

    HON95

    :p Let's say "line" is the string you got and stored from the config. Then just use line.replace(x, y) on all of them.

    The first line wil replace all the "&0"s in your line with the color black + a string to add the color to. The rest of your line will now use this color until a new one is provided.
     
  5. Offline

    vYN

    Ahh ok. I will test now. Got no errors in eclipse with it now xD
     
  6. Offline

    HON95

    Awesome :)
     
  7. Offline

    vYN

    Hmm. getting a big error in the console... i guess I'm doing something wrong....

    Code:
    org.bukkit.configuration.InvalidConfigurationException: while parsing a block mapping
    in "<string>", line 7, column 3:
          How many lines: 4
          ^
    expected <block end>, but found Anchor
    in "<string>", line 8, column 13:
          '1': &aHi &1and &2welcome1
                    ^
     
        at org.bukkit.configuration.file.YamlConfiguration.loadFromString(YamlConfiguration.java:55)
        at org.bukkit.configuration.file.FileConfiguration.load(FileConfiguration.java:143)
        at org.bukkit.configuration.file.FileConfiguration.load(FileConfiguration.java:109)
        at org.bukkit.configuration.file.YamlConfiguration.loadConfiguration(YamlConfiguration.java:177)
        at com.CMDbox.Config.MessagesTextFile.reloadMessages(MessagesTextFile.java:31)
        at com.CMDbox.Config.MessagesTextFile.getMessages(MessagesTextFile.java:36)
        at com.CMDbox.Config.MessagesTextFile.loadMessages(MessagesTextFile.java:23)
        at com.CMDbox.CMDbox.onEnable(CMDbox.java:49)
        at org.bukkit.plugin.java.JavaPlugin.setEnabled(JavaPlugin.java:215)
        at org.bukkit.plugin.java.JavaPluginLoader.enablePlugin(JavaPluginLoader.java:336)
        at org.bukkit.plugin.SimplePluginManager.enablePlugin(SimplePluginManager.java:363)
        at org.bukkit.craftbukkit.CraftServer.loadPlugin(CraftServer.java:250)
        at org.bukkit.craftbukkit.CraftServer.enablePlugins(CraftServer.java:232)
        at net.minecraft.server.MinecraftServer.t(MinecraftServer.java:374)
        at net.minecraft.server.MinecraftServer.a(MinecraftServer.java:361)
        at net.minecraft.server.MinecraftServer.init(MinecraftServer.java:187)
        at net.minecraft.server.MinecraftServer.run(MinecraftServer.java:425)
        at net.minecraft.server.ThreadServerApplication.run(SourceFile:490)
    Caused by: while parsing a block mapping
    in "<string>", line 7, column 3:
          How many lines: 4
          ^
    expected <block end>, but found Anchor
    in "<string>", line 8, column 13:
          '1': &aHi &1and &2welcome1
                    ^
     
        at org.yaml.snakeyaml.parser.ParserImpl$ParseBlockMappingKey.produce(ParserImpl.java:575)
        at org.yaml.snakeyaml.parser.ParserImpl.peekEvent(ParserImpl.java:161)
        at org.yaml.snakeyaml.parser.ParserImpl.checkEvent(ParserImpl.java:146)
        at org.yaml.snakeyaml.composer.Composer.composeMappingNode(Composer.java:230)
        at org.yaml.snakeyaml.composer.Composer.composeNode(Composer.java:160)
        at org.yaml.snakeyaml.composer.Composer.composeMappingNode(Composer.java:237)
        at org.yaml.snakeyaml.composer.Composer.composeNode(Composer.java:160)
        at org.yaml.snakeyaml.composer.Composer.composeDocument(Composer.java:123)
        at org.yaml.snakeyaml.composer.Composer.getSingleNode(Composer.java:106)
        at org.yaml.snakeyaml.constructor.BaseConstructor.getSingleData(BaseConstructor.java:121)
        at org.yaml.snakeyaml.Yaml.loadFromReader(Yaml.java:480)
        at org.yaml.snakeyaml.Yaml.load(Yaml.java:399)
        at org.bukkit.configuration.file.YamlConfiguration.loadFromString(YamlConfiguration.java:53)
        ... 17 more
     
  8. Offline

    HON95

    That's an error in your config. If you post it or something, I can try and find it for you.
     
  9. Offline

    vYN

    Code:
    # CMDbox BroadCaster settings!
    BroadCaster:
      Enable: true
      prefix: '[HC]'
      interval: 45
    Messages:
      How many lines: 2
      '1': &aHi &1and &2welcome1
      '2': &aHi &1and &2welcome2
     
  10. Offline

    HON95

    This should fix it:

    Code:
    # CMDbox BroadCaster settings!
    BroadCaster:
      Enable: true
      prefix: '[HC]'
      interval: 45
    Messages:
      HowManyLines: 2
      '1': '&aHi &1and &2welcome1'
      '2': '&aHi &1and &2welcome2'
    3 last lines changed.
    Really don't think you need "How many lines: 2" for anything.
     
  11. Offline

    vYN

    Ok. i will test it. I need the "How many lines" to what I'm doing... I'm not sure on any other way i can count the lines and print the first line in-game and then the second line after 45 sec.

    EDIT: it fixed my error! thanks
     
  12. Offline

    HON95

    You could use a List<?>, but whatever. As long as it works :)
     
  13. Offline

    vYN

    I guess i can. Ye it works :). Thanks for the help!
     
Thread Status:
Not open for further replies.

Share This Page