How can i use Config Colors ??

Discussion in 'Plugin Development' started by plarsootje, Jun 20, 2012.

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

    Dragon252525

    this will convert a text with &-colorcodes to a text whit colors
    Code:
    String yourText = path in config
    String convertedText = ChatColor.translateAlternateColorCodes('&', yourText);
    for example:
    "&6This is an &lexample" in config will be converted to "This is an example"

    If this is not the answer you are looking for; Please describe your problem better (not in only six words).
     
    ferrybig likes this.
  2. how can i do that with a list
    because i hava a command rules like on the site i send on this topic
     
  3. Offline

    evilmidget38

    I just realized there was a method for this...... I'd been writing my own methods to do this in the past.... I guess my methods were more flexible permissions-wise, though.

    Code:
            for (String s : list){
                String converted = ChatColor.translateAlternateColorCodes('&', s);
                player.sendMessage(s);
            }
     
  4. no i keep getting null but you don't use converted where do i need that?
     
  5. Offline

    evilmidget38

    My bad, converted is the message that should be sent to the player, not the string s. And if you're getting Null, make sure you've renamed everything to what it should be. I doubt you called your List "list", and sending the player a message might not be what you want to do. If you haven't assigned a value to player(e.g. if you're in onCommand), it could be null. Maybe you want to do a sender.sendMessage(converted), or something like that.
     
  6. yeah i have checked everything but when i do the command i get the string rules so with the command there's nothing whrong only when i add a color code like &6 the string i placed after the &6 changed to null like that i think there is something else i do whrong maby something in my main class because without i do the command only if i reload the server the string changes to null

    before
    Code:
    Rules:
    - null
    - Rule2
    - null
    - &6rule4
    - rule5
    - rule6
    when i reload the server
    Rules:
    - null
    - Rule2
    - null
    - null
    - rule5
    - rule6
     
  7. place strings inside '' &is an spacial toen for YML
     
  8. I have deleted my commandclass and when i reload the server the string in the config still changes to null
    so there's nothing with my commandclass
    there need to be something with my main class

    no still wont work now i get that
    and the first line i don't get in game please help

    Code:
    Example:
      list:
        path:
        - '&6null'
        - Rule2
        - Rule3
        - rule4
        - rule5
        - rule6
    
    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 26, 2016
  9. Offline

    r0306

    plarsootje
    When adding to the config list, try this:
    Code:
    list.add("\u0026" + "6Rule1");
     
  10. no doesn't work
     
  11. Offline

    theguynextdoor

    Code:
    package tk.theguynextdoor.runenextdoor;
     
    import java.util.Arrays;
    import java.util.HashMap;
    import java.util.Map;
     
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.configuration.file.FileConfiguration;
    import org.bukkit.plugin.PluginManager;
    import org.bukkit.plugin.java.JavaPlugin;
     
    public class RuneNextDoor extends JavaPlugin {
        String[] rules = { "one", "&4Derp" };
     
        @Override
        public void onEnable() {
            FileConfiguration config = getConfig();
            config.addDefault("rules", Arrays.asList(rules));
            config.options().copyDefaults(true);
            saveConfig();
        }
     
        @Override
        public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
            if (command.getName().equalsIgnoreCase("rules")) {
                for (String string : getConfig().getStringList("rules")) {
                    sender.sendMessage(ChatColor.translateAlternateColorCodes('&', string));
                }
                return true;
            }
            return false;
        }
     
    }
    
    This works as expected. Now for the example from another class.

    This is my main class
    Code:
    package tk.theguynextdoor.runenextdoor;
     
    import java.util.Arrays;
    import java.util.HashMap;
    import java.util.Map;
     
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.configuration.file.FileConfiguration;
    import org.bukkit.plugin.PluginManager;
    import org.bukkit.plugin.java.JavaPlugin;
     
    public class RuneNextDoor extends JavaPlugin {
        String[] rules = { "one", "&4Derp" };
     
        @Override
        public void onEnable() {
            PluginManager pm = getServer().getPluginManager();
            pm.registerEvents(new RuneListener(this), this);
     
            FileConfiguration config = getConfig();
            config.addDefault("rules", Arrays.asList(rules));
            config.options().copyDefaults(true);
            saveConfig();
        }
     
        @Override
        public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
            if (command.getName().equalsIgnoreCase("rules")) {
                for (String string : getConfig().getStringList("rules")) {
                    sender.sendMessage(ChatColor.translateAlternateColorCodes('&', string));
                }
                return true;
            }
            return false;
        }
     
    }
    
    This is my listener.
    Code:
    package tk.theguynextdoor.runenextdoor;
     
    import org.bukkit.ChatColor;
    import org.bukkit.block.Sign;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.EventPriority;
    import org.bukkit.event.Listener;
    import org.bukkit.event.block.SignChangeEvent;
    import org.bukkit.event.player.PlayerJoinEvent;
     
    public class RuneListener implements Listener {
        public RuneNextDoor plugin;
     
        public RuneListener(RuneNextDoor instance) {
            plugin = instance;
        }
       
        @EventHandler
        public void join(PlayerJoinEvent e){
            for(String string : plugin.getConfig().getStringList("rules")){
                e.getPlayer().sendMessage(ChatColor.translateAlternateColorCodes('&', string));
            }
        }
    }
    
    And if you have a command executor from another class (this also includes adding to the list)

    main class:
    Code:
    package tk.theguynextdoor.runenextdoor;
     
    import java.util.Arrays;
    import java.util.HashMap;
    import java.util.Map;
     
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.configuration.file.FileConfiguration;
    import org.bukkit.plugin.PluginManager;
    import org.bukkit.plugin.java.JavaPlugin;
     
    public class RuneNextDoor extends JavaPlugin {
     
        String[] rules = { "one", "&4Derp" };
     
        @Override
        public void onEnable() {
            PluginManager pm = getServer().getPluginManager();
            pm.registerEvents(new RuneListener(this), this);
     
            FileConfiguration config = getConfig();
     
            config.addDefault("rules", Arrays.asList(rules));
            config.options().copyDefaults(true);
            saveConfig();
     
            getCommand("rules").setExecutor(new RulesCommand(this));
        }
    }
    and my RulesCommand class looks like this:
    Code:
    package tk.theguynextdoor.runenextdoor;
     
    import java.util.List;
     
    import org.bukkit.ChatColor;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
     
    public class RulesCommand implements CommandExecutor {
        RuneNextDoor plugin;
     
        public RulesCommand(RuneNextDoor instance) {
            plugin = instance;
        }
     
        @Override
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
            if (args.length == 0) {
                for (String s : plugin.getConfig().getStringList("rules")) {
                    sender.sendMessage(ChatColor.translateAlternateColorCodes('&', s));
                }
                return true;
            }
            else if (args.length == 2) {
                if (args[0].equalsIgnoreCase("add")) {
                    List<String> cRules = plugin.getConfig().getStringList("rules");
                    cRules.add(args[1]);
     
                    plugin.getConfig().set("rules", cRules);
                    return true;
                }
            }
            return false;
        }
     
    }
    
    If i have misunderstood the question, or you have other questions please say
     
  12. WOW thanks a lot man so the only thing i do whrong was the ' in front and afther the string?
     
  13. Offline

    theguynextdoor

    Where do you mean?
     
  14. in the config the only problem i got when i do /rules nothing shows up only if i send a message before for (.....
     
  15. Offline

    theguynextdoor

    I tested all the code i sent you before i sent it. If you were to copy and paste it all (and put a valid plugin.yml (with the command in it if you are using that)) then it should work without any troubles.

    If that still fails. Paste all of your code here so i can look at it, along with plugin.yml
     
  16. i got it to work thanks a lot man
    but now i need to place everything in my config between ' ' ?
     
  17. Offline

    theguynextdoor

    Can you show me your plugin.yml, the config this creates, and the result of using it?
    Because it mainly looks to be fine
     
  18. yeah i got it to work but something more

    when i use
    PHP:
            config.options().header("go to our wiki to get the color codes on http://dracinis.wikia.com/wiki/Colors_Codes" +
                    
    "Here you can place your own rules change Rule 1 to a rule" +
                    
    "Everything MUST be between the '    ' ");
    how can i get it in the config under eachother because now everything is on one line
     
  19. Offline

    theguynextdoor

    Not needed to do this myself, but try and input the new line character where you want a new line

    the new line character is \n
     
  20. thanks a lot man :D

    you're good in configs so i have a other question
    is it possible that players can do like onPlayerJoin that the can set something like playerName and that the code gets the displayname?

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

Share This Page