[SOLVED]Config Color Formatting

Discussion in 'Plugin Development' started by catchaser9620, Jul 14, 2012.

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

    catchaser9620

    Does anyone know how to set up colors in the plugins config file?
     
  2. Offline

    EnvisionRed

    So let's say you have colors enabled with the "&" sign.
    A string in your config looks like:
    Code:
    message: &3I &4am &5 a &6message
    You can then do in your plugin:
    Code:
    String message = getConfig().getString("message").replaceAll("(&([a-f0-9]))", "\u00A7$2");
    The
    Code:
    .replaceAll("(&([a-f0-9]))", "\u00A7$2")
    part takes stuff that comes after the "&" sign and replaces it with the colors/formatting.

    You can add more lines after so you can have colors with "%", "&", and "$"
    Code:
    String message = getConfig().getString("message").replaceAll("(&([a-f0-9]))", "\u00A7$2").replaceAll("($([a-f0-9]))", "\u00A7$2").replaceAll("(%([a-f0-9]))", "\u00A7$2")
     
  3. Offline

    catchaser9620

    I tried that but it just gets rid of the word its in front of and doesn't change the color
     
  4. Offline

    EnvisionRed

  5. Offline

    catchaser9620

    now it wont even show
     
  6. Offline

    EnvisionRed

    Show your code. These methods are the only way to do it.
     
  7. Offline

    catchaser9620

    Code:
    public void onEnable() {
                PluginDescriptionFile pdfFile = this.getDescription();
                this.log.info(pdfFile.getName() + " Version: " + pdfFile.getVersion() + " has been Enabled!");
                Extras ex = new Extras("BaseCommands");
                this.getConfig().options().copyDefaults(true);
                this.saveConfig();
                getServer().getPluginManager().registerEvents(new Listener() {
                    @EventHandler(priority = EventPriority.NORMAL)
                    public void playerJoin(PlayerJoinEvent event) {
                        String message = getConfig().getString("MOTD");
                        message = ChatColor.translateAlternateColorCodes('&', message);
                        message = ChatColor.translateAlternateColorCodes('$', message);
                        message = ChatColor.translateAlternateColorCodes('%', message);
                        event.getPlayer().sendMessage(message);
                    }
                }, this);
     
  8. Offline

    sayaad

    How about this :

    Code:java
    1. String string = getConfig().getString("message");
    2. String string2 = string.replaceAll("&" , "§");
    3. String message = string2.replaceAll("§§" , "&");


    Replaces "&" with the corresponding color.
    To display the "&" , the value in the config must be "&&"

    EDIT :

    extend JavaPlugin and implement Listener in your main class.
    Try changing our code to this :

    Code:java
    1. public void onEnable() {
    2. PluginDescriptionFile pdfFile = this.getDescription();
    3. this.log.info(pdfFile.getName() + " Version: " + pdfFile.getVersion() + " has been Enabled!");
    4. Extras ex = new Extras("BaseCommands");
    5. this.getConfig().options().copyDefaults(true);
    6. this.saveConfig();
    7. getServer().getPluginManager().registerEvents(this, this);
    8. }
    9.  
    10. @EventHandler
    11. public void playerJoin(PlayerJoinEvent event) {
    12.  
    13. String string = getConfig().getString("message");
    14. String string2 = string.replaceAll("&" , "§");
    15. String message = string2.replaceAll("§§" , "&");
    16.  
    17. event.getPlayer().sendMessage(message);
    18. }
     
  9. Offline

    EnvisionRed

    Why...why are you putting a Listener inside your onEnable? You could at least make a method in your class and then register events from your main class.

    Anyway, the reason it isn't working is because it doesn't know where to get the config from. You need to instantiate the plugin and use:
    Code:
    plugin.getConfig.getString("message");
     
  10. Offline

    catchaser9620

    yay it works now thank you and i will move that listener

    ok nvm its sorta working it keeps on erroring if I do &3 or &2 or if i put like &7 at the start of the string it gets rid of that word

    nvm got it to work

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 27, 2016
Thread Status:
Not open for further replies.

Share This Page