Defining a color depending on a (boolean) value

Viewed 42

I have this code to display when a user issues "/settings". It shows the settings the user can control and their current status (true/false), which I want to color accordingly. It works, but I want to ask for ideas to improve it's performance.

String BlockBreakingColor = "§7";

Boolean BlockBreakValue = plugin.getConfig().getBoolean("settings.block-break");

if(BlockBreakValue) {
    BlockBreakingColor = "§a";
} else {
    BlockBreakingColor = "§c";
}

p.sendMessage("§8» §7block-breaking §8- " + BlockBreakingColor + BlockBreakValue.toString());

My question would be if there is a better, more dynamic way of assigning the color because I would have to set this code up for every single setting and this way my code would end up having a lot of lines and be harder to read/understand.

1 Answers

I originally only developed in JavaScript, but now I wanted to try some Java too. Using my JS experience, I got the idea of simply using a function with the setting as input, shortening my code. This is what it looks like:

public String checkBooleanValueForColor(String setting) {
     Boolean SettingValue = plugin.getConfig().getBoolean(setting);
     if(SettingValue) {
         return "§atrue";
     } else {
        return "§cfalse";
     }
 }

It's very simple yet effective.

Related