Solved making colour codes work in a config file

Discussion in 'Plugin Development' started by Drakonn, Jan 19, 2018.

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

    Drakonn

    Hey guys, This is probably going to sound super noobish but I've been trying to make a plugin that when you do a command (in this case /fix-neme) shows a message in chat that only can be seen by whoever ran the command and that can be editable from a config file. I've done all that but my only problem is I cant for the life of me get color codes to work in the config file. I've spent the last 3 days googling watching videos trying to make it work all to no success, if anyone here can help me out by explaining what I've done wrong so i can learn (please don't just post a fixed code, I want to be able to improve)

    Anyway without further ado here is the code,

    Code:
    package aiden.e;
    
    import org.bukkit.ChatColor;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.configuration.file.FileConfiguration;
    import org.bukkit.plugin.java.JavaPlugin;
    
    import static org.bukkit.ChatColor.translateAlternateColorCodes;
    
    public class Main extends JavaPlugin {
        FileConfiguration config = getConfig();
        public void onEnable () {
            config.options().copyDefaults(true);
            saveConfig();
        }
        @Override
        public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
            if(command.getName().equalsIgnoreCase("FIX-NEME")){
                String nonColoredText = getConfig().getString("message"); //get the raw string from "MemberText"
                String coloredText = translateAlternateColorCodes('&', nonColoredText);
                sender.sendMessage(coloredText);
            return true;
            }
            return true;
            }
            }
     
    Last edited: Jan 19, 2018
  2. Offline

    timtower Moderator Moderator

    @Drakonn And what isn't working then? Code seems good.
     
  3. Offline

    FlaveDrake

    @Drakonn

    Put this in front of the class as a method.

    Code:java
    1.  
    2. public static String colorize(String msg) {
    3. String coloredMsg = "";
    4. for (int i = 0; i < msg.length(); i++) {
    5. if (msg.charAt(i) == '&')
    6. coloredMsg += 'ยง';
    7. else
    8. coloredMsg += msg.charAt(i);
    9. }
    10. return coloredMsg;
    11. }
    12.  
    13.  
    14. String nonColoredText = getConfig().getString("message"); //get the raw string from "MemberText"
    15. sender.sendMessage(colorize(nonColoredText));
    16.  
    17.  


    This should work probably, and it's way easier.
     
  4. Offline

    timtower Moderator Moderator

    @FlaveDrake How is that easier than translateAlternateColorCode ?
     
  5. Offline

    FlaveDrake

    @timtower
    because you only have to put colorize(STRING) to color it.

    translateAlternateColorCode hasn't worked for me yet..
     
  6. Offline

    timtower Moderator Moderator

    @FlaveDrake It does the exact same thing...
    You can also make your colorize with calling translatealternatecolorcode in it.
     
  7. Offline

    FlaveDrake

    @timtower
    I know but it doesn't work, it gives errors when i'm trying to "translate" it.
     
  8. Offline

    timtower Moderator Moderator

    What errors?
     
  9. Offline

    FlaveDrake

    @timtower
    I think it was a NullPointerException, i'm not sure i've fixed it about 2 months ago.
     
  10. Offline

    timtower Moderator Moderator

    Chances are that it wasn't the translate but the input for it.
     
  11. Offline

    FlaveDrake

    Could be, don't know but this works ^^
     
  12. Offline

    timtower Moderator Moderator

    @Drakonn Spotted the issue, remove the config variable, you don't need it.
     
  13. Offline

    Drakonn

    Isn't the config variable needed to generate the config file?

    @timtower
     
  14. Offline

    timtower Moderator Moderator

    Nope, the variable itself does nothing, you can just call getConfig() instead
     
  15. Offline

    Drakonn

    Still having the same issue, Getting the text from config works fine color codes just don't
    Code:
    package aiden.e;
    
    import org.bukkit.ChatColor;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.plugin.java.JavaPlugin;
    
    
    
    public class Main extends JavaPlugin {
        public void onEnable () {
            getConfig().options().copyDefaults(true);
            saveConfig();
        }
        @Override
        public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
            if(command.getName().equalsIgnoreCase("FIX-NEME")){
                String nonColoredText = getConfig().getString("message");
                String coloredText = ChatColor.translateAlternateColorCodes('&', nonColoredText);
                sender.sendMessage(coloredText);
            return true;
            }
            return true;
            }
            }
     
  16. Offline

    FlaveDrake

    @Drakonn

    Have you tried it out using my code?
     
  17. Offline

    Conor015

    can you send what your config looks like please
     
  18. Offline

    Drakonn

    Code:
    message:
       &1 Hi
    That's the config


    This is the plugin.yml
    Code:
    name: fix-neme
    
    version: 1.9
    main: aiden.e.Main
    description: memes
    
    commands:
      fix-neme:
        usage: /<command>
        description: memes
    @Conor015
     
  19. Offline

    timtower Moderator Moderator

  20. Offline

    Conor015

    Write your config like this and it should work.

    Code:
    message: '&1 Hi'
    I think its because you didnt use ' '
     
  21. Offline

    Drakonn

    @Conor015 thankyou so much, I know it was going to be something super simple and annoying like just didn't know what.
     
  22. Offline

    Conor015

    Your welcome. Please mark this thread as solved :)
     
Thread Status:
Not open for further replies.

Share This Page