Solved Using Colour in config

Discussion in 'Plugin Development' started by Conor015, Aug 31, 2017.

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

    Conor015

    Hello everyone, is was making a Heal/Feed plugin and everything works fine. But only one thing is ,that I want to be able to use colour codes for the prefix. Here is an image - https://prnt.sc/gfk2dk Where I boxed of is where I want to be able to use colour code (eg: change green text to blue text) I made the config and it works, I am able to change the "test" to what ever I want but if I try putting "&" in front of it and reload, the config just resets.

    Here is my code:
    Code:
    package me.Conor015.HealAndFeed;
    
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.event.Listener;
    import org.bukkit.plugin.java.JavaPlugin;
    
    public class Main extends JavaPlugin implements Listener{
    
        public void onEnable() {
            getConfig().options().copyDefaults(true);
            saveConfig();
            Bukkit.getServer().getLogger().info("HealAndFeed plugin Enabled");
            Bukkit.getServer().getPluginManager().registerEvents(this, this);
        }
    
        @Override
        public void onDisable() {
            Bukkit.getServer().getLogger().info("HealAndFeed plugin Disabled");
        }
    
        @Override
        public boolean onCommand(CommandSender sender, Command cmd, String commandlabel, String[] args) {
    
            if (!(sender instanceof Player)) {
                sender.sendMessage(ChatColor.RED + "The console can not do this command!");
                return true;
            }
    
            Player player = (Player) sender;
    
            String heal = ChatColor.GREEN + getConfig().getString("HealingPrefix");
            String Feed = ChatColor.GREEN + getConfig().getString("EatingPrefix");
    
            if (cmd.getName().equalsIgnoreCase("heal")) {
    
                if (args.length == 0){
                    player.setHealth(20);
                    player.sendMessage( heal + ChatColor.GREEN + " You have been healed!");
                    return true;
    
                }
                Player target = Bukkit.getServer().getPlayer(args[0]);
                if (target == null) {
                    player.sendMessage(ChatColor.RED + "Could not find Player!");
                    return true;
                }
                target.setHealth(20);
                target.sendMessage(ChatColor.DARK_PURPLE + heal + ChatColor.GREEN + " You have been healed!");
                player.sendMessage(ChatColor.DARK_PURPLE + heal + ChatColor.AQUA + target.getName() + " have been healed!");
    
            }
    
            if (cmd.getName().equalsIgnoreCase("feed")) {
                if (args.length == 0){
                    player.setFoodLevel(20);
                    player.sendMessage(ChatColor.DARK_PURPLE + Feed  + ChatColor.GREEN + " Your appetite was sated!");
                    return true;
    
                }
                Player target = Bukkit.getServer().getPlayer(args[0]);
                if (target == null) {
                    player.sendMessage(ChatColor.DARK_PURPLE + Feed  + ChatColor.RED + "Could not find Player!");
                    return true;
                }
                target.setFoodLevel(20);
                target.sendMessage(ChatColor.DARK_PURPLE + Feed  + ChatColor.GREEN + " Your appetite was sated!");
                player.sendMessage(ChatColor.DARK_PURPLE + Feed  + ChatColor.GREEN + target.getName() + " Your appetite was sated!");
    
            }
    return true;
        }
    }

    Thank you.
     
  2. Offline

    Dnyce72799

    ChatColor.translateAlternateColorCodes('&', this.getConfig().getString(""));

    Sent from my LGLS775 using Tapatalk
     
  3. Offline

    FrostDevStudios

    @Conor015
    PHP:
    String heal ChatColor.GREEN getConfig().getString("HealingPrefix");
    String Feed ChatColor.GREEN getConfig().getString("EatingPrefix");
    Should Equal ↓
    PHP:
    String heal ChatColor.translateAlternateColorCodes('&'getConfig().getString("HealingPrefix"))
    String Feed ChatColor.translateAlternateColorCodes('&'getConfig().getString("EatingPrefix"));
    Also what do you mean by this? ↓
     
  4. Offline

    Conor015

    The code didnt really work. When i did, /eat, it was purple but /heal was white. I want to make it customisable in the config but when i tried to use a colour code in config, nothing happens.
    This is my config:

    Code:
    HealingPrefix: test
    EatingPrefix: test
    
    if i do this with the colour codes:
    Code:
    HealingPrefix: &4test
    EatingPrefix: &4test
    
    and reload the server, the &4 goes away and just goes back the the original config
     
  5. Offline

    FrostDevStudios

    @Conor015
    This is the configuration setup i use to load and save my configurations
    PHP:
    private FileConfiguration config getConfig();
    private 
    File configFile = new File(getDataFolder(), "config.yml");

    private 
    void loadFile()
    {
        try {
            if(!
    configFile.exists()) {
                
    plugin.saveResource("config.yml"false);  // CREATES FILE IF NOT FOUND
            
    }
            else {
                
    // FILE FOUND
            
    }
            
    config YamlConfiguration.loadConfiguration(configFile); // LOADS FILE VALUES
        
    }
        catch(
    IllegalArgumentException error) {
            throw new 
    IllegalArgumentException("Failed to load our configuration files"error);
        }
    }
     
    Last edited: Aug 31, 2017
  6. Offline

    Dnyce72799

    Don't reload the server, add a command that reloads the plugin's config file.
    Code:
    @Override
    public void onEnable(){
        //You don't have to use getConfig().options().copyDefaults(true); and saveConfig();
        //Instead use this:
        this.saveDefaultConfig();
    }
     
  7. Offline

    Reflxction

    It's a string, you have to put the text in ""
     
  8. Offline

    RRGamingZ

    Try using saveDefaultConfig() on the onEnable instead of copyDefaults...
    Also, you might want to save the files onDisable since you are using /reload
     
  9. Offline

    Conor015

    Yes, im just after figuring that out. Thank you :)

    -Solved
     
Thread Status:
Not open for further replies.

Share This Page