Potato
Potato

Reputation: 49

Converting from ChatColor to Color Codes

I'm trying to make a minecraft plugin with the Spigot/Bukkit API. The plugin I'm trying to make is a bigger enderchest.

I'm currently working on a save method and I'm stuck on getting it to save the names of items. I can't get the color codes to work correctly. What I'm asking, is how you can translate from a ChatColor to a color code.

For example, from ChatColor.GREEN + ChatColor.BOLD.toString() + "foo" to &a&lfoo.

I have it so when the player opens their enderchest, it translates the saved name from &a&lfoo to foo that is green and bold. It uses #translateAlternateColorCodes. I have tried using #lastColors but that didn't seem to work.

Does anyone know a method to translate from colors to minecraft's '&' format. Thanks in advance! :)

Upvotes: 0

Views: 3358

Answers (1)

Carson Aurum
Carson Aurum

Reputation: 56

I'm not sure if you ever got the answer to your question, so let me give this a go.

Bukkit API's built in alternate color codes method offers easy conversion. Just add a method that looks something like this:

public static String format (String str) {
    return ChatColor.translateAlternateColorCodes('&', str);
}

If this doesn't work for you, there's a couple other ways you could do this with String manipulation. Just tell me if you need those, but if this works, this is the much easier way around.

Upvotes: 1

Related Questions