How to properly add color from config

Discussion in 'Plugin Development' started by Ruptur, Feb 12, 2015.

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

    Ruptur

    Upon plenty research i have found dozens of methods doing basically the same thing; which is to get in the chat.


    Code:
    publicstaticvoid sendPlayer(Player p,String s){if(sm.getPrefixUse()){
    
    // - Method 1 String string = s.replaceAll("(&([a-f0-9]))","\u00A7$2");
    p.sendMessage("["+ sm.getPrefixName().replaceAll("(&([a-f0-9]))","\u00A7$2")+"]"+ string);// This is one method but does not work - no colour in chat
    
    // - Method 2
    p.sendMessage(ChatColor.translateAlternateColorCodes('&', sm.getPrefixName())+ChatColor.translateAlternateColorCodes('&', s));
    // Heres another- no colour in chat
    
    // - Method 3 String working ="&4This works ".replaceAll("(&([a-f0-9]))","\u00A7$2");
    p.sendMessage(working);
    
    // - Method 4 String thistoo ="&4This too works";
    p.sendMessage(ChatColor.translateAlternateColorCodes('&', thistoo));}else{
    p.sendMessage(s);}}
    Method 1 and 2 doesnt show color in chat - when i get a string from the config

    Method 3 and 4 works, basically same as above but the string is defined here

    How do i get a string from the config and send it to the player with the correct color
     
  2. #sendMessage(ChatColor.translateAltenateColorCodes('&', configMessage))
     
  3. Offline

    Ruptur

    @bwfcwalshy
    The prefix is the message from the config.

    Code:
    p.sendMessage(ChatColor.translateAlternateColorCodes('&', sm.getPrefixName())+ChatColor.translateAlternateColorCodes('&', s));
    
     
  4. @Ruptur You could make do

    Code:
    String msg = sm.getPrefixName() + " " + s;
    
    p.sendMessage(ChatColor.translateAlternateColorCodes('&', msg);
     
  5. Offline

    Ruptur

    @bwfcwalshy

    Lets say this was my config
    Code:
    prefix:
        use: true # By default it is true [true/false]
        # true = Adds a prefix to the messages
        # false = Doesnt add a prefix to the messages 
       
        name: &4Hub
        # Plugin prefix - prefix to appear before messages
    
    on the message it would return null if i add the color code to it

    @bwfcwalshy

    Code:
    public void onPlayerJoin(PlayerJoinEvent e){
    
    ..... something ...
    
    
            if(sm.getMotdUse()){
                ChatManager.sendPlayer(p, sm.getMotdMessage());
                return;
            }
    ..... something ...
    
    then
    
        public static void sendPlayer(Player p, String s){
    
    ..... something ...
    
            if (sm.getPrefixUse()) {
    
                String msg = sm.getPrefixName() + " " + s;
                p.sendMessage(ChatColor.translateAlternateColorCodes('&', msg));
            } else{
                p.sendMessage(s);
            }
    
    ..... something ...
    
        }
    
    

    basic summary if motd true then send motd colored message + prefix
    but prefix returns null
    message returns with one less word

    So lets say if motd was '&4Welcome to server'
    it sends 'to server' without color

    EDIT by Timtower: merged posts
     
    Last edited by a moderator: Feb 12, 2015
  6. @Ruptur Please provide getPrefixName method.
     
  7. Offline

    Ruptur

    @bwfcwalshy
    Code:
        public ConfigurationSection getPrefix(){
            return config.getConfigurationSection("prefix");
        }
        public String getPrefixName(){
            return getPrefix().getString("name");
        }
    
     
Thread Status:
Not open for further replies.

Share This Page