Reputation: 21
I am unsure on how to allow users of my plugin to enter color codes with &
and have them display properly. I know that I can use the constants in ChatColor
to put various colors in messages sent by the plugin, but I don't know how to allow users to enter their own colored messages.
Here is my code:
package me.avy.simplemotd;
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.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerJoinEvent;
import org.bukkit.plugin.java.JavaPlugin;
public class motd extends JavaPlugin implements Listener {
@EventHandler
public void onPlayerJoin(PlayerJoinEvent e) {
Player p = e.getPlayer();
p.sendMessage(ChatColor.AQUA + getConfig().getString("message"));
}
public void onEnable()
{
Bukkit.getServer().getLogger().info(" SimpleMotd enabled correctly!");
saveDefaultConfig();
Bukkit.getServer().getPluginManager().registerEvents(this, this);
}
public void onDisable()
{
Bukkit.getServer().getLogger().info(" SimpleMotd disabled correctly!");
}
public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) {
if (cmd.getName().equalsIgnoreCase("motd")) {
sender.sendMessage(ChatColor.AQUA + "MOTD: " + getConfig().getString("message"));
return true;
}
if (cmd.getName().equalsIgnoreCase("setmotd")) {
if (!sender.hasPermission("motd.set")) {
sender.sendMessage(ChatColor.RED + "You are not permitted to do this!");
return true;
}
if (args.length == 0) {
sender.sendMessage(ChatColor.RED + "Please specify a message!");
return true;
}
StringBuilder str = new StringBuilder();
for (int i = 0; i < args.length; i++) {
str.append(args[i] + " ");
}
String motd = str.toString();
getConfig().set("message", motd);
saveConfig();
sender.sendMessage(ChatColor.GREEN + "MOTD set to: " + motd);
return true;
}
return true;
}
}
I want players to be able to use /setmotd &cRed &aGreen &9Blue
and have the MOTD be displayed to them with the appropriate colors, using the Minecraft color code format. There are several other plugins that are capable of displaying messages set that way, so it must be possible.
How can I convert a message like &cRed &aGreen &9Blue
into one that displays correctly in-game?
Upvotes: 0
Views: 638
Reputation: 5046
You can use ChatColor.translateAlternateColorCodes
to convert color codes using &
to the format used by ChatColor. (Internally, that's §
).
translateAlternateColorCodes
takes a replacement character and then the text to replace in. For the replacement character, you would want to use '&'
(note the single quotes and not the double quotes). translateAlternateColorCodes
has special logic to only replace valid codes, so &eWelcome
becomes §eWelcome
(which, in-game, is "Welcome" in yellow text), but I like cats & dogs
does not get converted and displays in-game as I like cats & dogs
.
You probably will want to do the replacement in all cases where the message is displayed, but not when it is set. That way, if someone edits the config manually, &
can also be used there. As such, you'll want to edit your code to replace each of the following lines:
p.sendMessage(ChatColor.AQUA + getConfig().getString("message"));
sender.sendMessage(ChatColor.AQUA + "MOTD: " + getConfig().getString("message"));
sender.sendMessage(ChatColor.GREEN + "MOTD set to: " + motd);
With these:
p.sendMessage(ChatColor.AQUA + ChatColor.translateAlternateColorCodes('&', getConfig().getString("message")));
sender.sendMessage(ChatColor.AQUA + "MOTD: " + ChatColor.translateAlternateColorCodes('&', getConfig().getString("message")));
sender.sendMessage(ChatColor.GREEN + "MOTD set to: " + ChatColor.translateAlternateColorCodes('&', motd));
Better yet, you could write a getMotd
method that retrieves the MOTD from the configuration with correct formatting, and use that instead:
/**
* Gets the MOTD from the configuration with formatting that can be used in chat.
*/
private String getMotd()
{
String rawMessage = getConfig().getString("message");
return ChatColor.translateAlternateColorCodes('&', rawMessage);
}
You'd use getMotd()
in all places you'd want to display your MOTD, rather than directly retrieving it from the configuration.
Upvotes: 2