ColorCodes in config

Discussion in 'Plugin Development' started by mcaustin1, May 10, 2014.

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

    mcaustin1

    How do you use color codes in your enable color codes in your config with out doing §?
     
  2. Offline

    SainttX

  3. Offline

    mcaustin1

    SainttX So would you do
    Code:
    ChatColor.translateAlternateColorCodes(&, String);
    ?
     
  4. Offline

    SainttX

    mcaustin1
    Code:java
    1. ChatColor.translateAlternateColorCodes('&', "Some string");
     
  5. Offline

    mcaustin1

    SainttX So what Color code you want would go in the string?
     
  6. Offline

    SainttX

    mcaustin1 Basically what it does it it parses the string and returns a new string with all instances of the char (in this case &, or whatever you send it too) replaced with the §/chatcolor symbol
     
  7. Offline

    tryy3

    mcaustin1
    No string that you want to be converted (the string that includes the color codes)
    example
    Code:java
    1. String message = "&5Hello &6World";
    2. ChatColor.trasnlateAlternateColorCodes('&', message);
     
  8. Offline

    mcaustin1

    SainttX tryy3 The way I am using isint working.
    Code:
    public void showBarChanging(final Player p) {
            getServer().getScheduler().scheduleSyncRepeatingTask(this,
                    new Runnable() {
                        public void run() {
                            Random random = new Random();
                            List<String> list = new ArrayList<>();
                 
                            ChatColor.translateAlternateColorCodes('&', "§");
                            list.add(getConfig().getString("announcment1"));
                            list.add(getConfig().getString("announcment2"));
                            list.add(getConfig().getString("announcment3"));
                            list.add(getConfig().getString("announcment4"));
                            list.add(getConfig().getString("announcment5"));
                            list.add(getConfig().getString("announcment6"));
                       
                       
                            String message = (String) list.get(random.nextInt(list
                                    .size()));
     
                            BarAPI.setMessage(p, message);
                        }
                    }, 0, 45 * 20);
    I am using BarAPI I dont know if that is effecting it at all or what.
     
  9. Offline

    SainttX

    mcaustin1
    Code:java
    1. BarAPI.setMessage(p, ChatColor.translateAlternateColorCodes('&', message));
     
  10. Offline

    mcaustin1

    SainttX Here's my full code.
    Code:
    public class Main extends JavaPlugin implements Listener {
        PluginDescriptionFile pdfFile = getDescription();
        public final Logger logger = Logger.getLogger("Minecraft");
        public void onEnable() {
            this.logger.info(pdfFile.getName() + " Version " + pdfFile.getVersion() + " Has been enabled!");
            getServer().getPluginManager().registerEvents(this, this);
            saveConfig();
         
        }
    public void onDisable(){
        this.logger.info(pdfFile.getName() + " Version " + pdfFile.getVersion() + " Has been disabled!");
        saveConfig();
    }
     
        public void initialiseConfig(){
            FileConfiguration config = getConfig();
         
            config.addDefault("Path.Sub.String", "String");
            config.addDefault("Path.Sub.Int", "1");
            config.addDefault("Path.Sub.Boolean", true);
            saveConfig();
        }
     
     
     
     
     
        @EventHandler
        public void onJoin(PlayerJoinEvent e) {
            Player p = e.getPlayer();
            // BarAPI.setMessage(p, "Confuser is really cool");
            showBarChanging(p,ChatColor.translateAlternateColorCodes('&', '§'));
        }
     
     
     
     
        public void showBarChanging(final Player p) {
            getServer().getScheduler().scheduleSyncRepeatingTask(this,
                    new Runnable() {
                        public void run() {
                            Random random = new Random();
                            List<String> list = new ArrayList<>();
                           
                            ChatColor.translateAlternateColorCodes('&', "§");
                            list.add(getConfig().getString("announcment1"));
                            list.add(getConfig().getString("announcment2"));
                            list.add(getConfig().getString("announcment3"));
                            list.add(getConfig().getString("announcment4"));
                            list.add(getConfig().getString("announcment5"));
                            list.add(getConfig().getString("announcment6"));
                         
                         
                            String message = (String) list.get(random.nextInt(list
                                    .size()));
     
                            BarAPI.setMessage(p, message);
                        }
                    }, 0, 45 * 20);
        }
     
    }
    I dont know were to insert the code.
     
  11. Offline

    tryy3

    Under the
    Code:java
    1. String message = (String) list.get(random.nextInt(list
    2. .size()));

    you replace
    Code:java
    1. BarAPI.setMessage(p, message);

    with
    Code:
    BarApi.setMessage(p, ChatColor.translateAlternateColorCodes('&', message));
     
  12. Offline

    mcaustin1

    It created a null pointer exeption. http://pastebin.com/5wLhzsnn
     
  13. Offline

    tryy3

    add some debugging info
    like, debug what the message actually says before its sent to translateAlternateColorCodes
     
  14. Offline

    mcaustin1

  15. Offline

    Drkmaster83

    Post your current class in a pastebin.
     
  16. Offline

    rsod

    you may just put § in your configuration
    like
    SomeString: "§4Red text"
     
  17. Offline

    mcaustin1

  18. Offline

    rsod

    p.s. you'll be hated for using spigot and your tread will be closed without permissions to create same threads in future.
     
  19. Offline

    mcaustin1

    Okay
     
  20. Offline

    rsod

    Anyway it seems that error isn't in ChatColor method. Is BarAPI.setMessage(p, message); working correctly?
     
  21. Offline

    mcaustin1

    Yes
     
  22. Offline

    Drkmaster83

    Code:
    String message = (String) list.get(random.nextInt(list.size()));
    BarAPI.setMessage(p, ChatColor.translateAlternateColorCodes('&', message));
    This is the code that should be throwing the error. Perhaps try list.size() - 1?
     
  23. Offline

    rsod

    Well, I don't really know what's going on here. First of all,
    Code:
    List<String> list = new ArrayList<>();
    must be replaced with
    Code:
    List<String> list = new ArrayList<String>();
    Try use this instead:
    Code:
    BarAPI.setMessage(p, message.replaceAll("&", "§"));[/core]
     
  24. Offline

    mcaustin1

    Gives me an error when I try to put it in the code: http://prntscr.com/3ip9rf

    rsod
    No errors show up in console but message dose not appear on the server.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 8, 2016
  25. Offline

    rsod

    try this code
    Code:
    public void showBarChanging1(Player p){
      getServer().getScheduler().scheduleSyncRepeatingTask(this, new Runnable(){
        @Override
        public void run(){
          BarAPI.setMessage(p, ChatColor.translateAlternateColorCodes('&', getConfig().getString("announcment" + ((int)(Math.random()*6)+1) )) );
        }  
      }, 0, 1 * 20);
    }
    UPD oh wait! You should create separate class for task
    Code:
    public class NotifyPlayer implements Runnable {
        private Player p;    
        public NotifyPlayer (Player p){
            this.p = p;
        }
        @Override
        public void run(){
          BarAPI.setMessage(p, ChatColor.translateAlternateColorCodes('&', getConfig().getString("announcment" + ((int)(Math.random()*6)+1) )) );
        }   
    }
    
    then use something like
    Code:
    getServer().getScheduler().scheduleSyncRepeatingTask(this, new NotifyPlayer (p), 0, 1 * 20);
    
    in your main class.[/code][/code][/code]
     
  26. Offline

    mcaustin1

    Nope doesnt work. Good try though.
    rsod
     
  27. Offline

    Drkmaster83

    If that doesn't work, try creating the list of messages and printing their contents out to the console. The elements inside the ArrayList may be null.
     
    rsod likes this.
  28. Offline

    rsod

    mcaustin1 well I think last one will, if not then I can't do anything without complete sources.
     
  29. Offline

    mcaustin1

    Okay thanks all I will just edit the code from inside the plugin.
     
  30. Offline

    Drkmaster83

    That's not necessary. When you leave the <> on the side of instantiation, it takes the Generic from the left side, I believe.

    Try this method to see if the elements in the ArrayList are null:
    Code:
    public void showBarChanging(final Player p) {
    /*getServer().getScheduler().scheduleSyncRepeatingTask(this,
    new Runnable() {
    public void run() {*/
    Random random = new Random();
    List<String> list = new ArrayList<>();
     
    ChatColor.translateAlternateColorCodes('&', "§");
    list.add(getConfig().getString("announcment1"));
    list.add(getConfig().getString("announcment2"));
    list.add(getConfig().getString("announcment3"));
    list.add(getConfig().getString("announcment4"));
    list.add(getConfig().getString("announcment5"));
    list.add(getConfig().getString("announcment6"));
     
    for(String s : list) {
    System.out.println(s);
    }
     
    //String message = (String) list.get(random.nextInt(list.size() - 1));
     
    BarAPI.setMessage(p, message);
    /*}
    }, 0, 45 * 20);*/
    }
    
     
Thread Status:
Not open for further replies.

Share This Page