Hello, y name is Jimfutsu and I have recently found a easy way to work around a problem that I have found annoying. Color Codes. First, you have to go somewhere in your Main class where a method wont be affected by anything else(I prefer right underneath the Class). The code that you will want to type is: Code:java public String colorize(String msg) { String coloredMsg = ""; for(int i = 0; i < msg.length(); i++) { if(msg.charAt(i) == '&') coloredMsg += '§'; else coloredMsg += msg.charAt(i); } return coloredMsg; } This will allow you to use: Code:java Player.sendMessage(colorize("&3Hello")); An example of command implementation and usage would be like this: Code:java public class Firehelm extends JavaPlugin implements Listener{ /** Methods **/ public String colorize(String msg) { String coloredMsg = ""; for(int i = 0; i < msg.length(); i++) { if(msg.charAt(i) == '&') coloredMsg += '§'; else coloredMsg += msg.charAt(i); } return coloredMsg; } /** End Methods **/ public void onEnable(){ getServer().getPluginManager().registerEvents(this, this); } public void onDisable(){ } public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args){ Player p = (Player) sender; if(commandLabel.equalsIgnoreCase("Hi")) {p.sendMessage(colorize("&5Hi!!!!")); } return true; } Thanks for reading hope this helped!
Cirno ArthurMaker The ting though is that for me, when I code custom abilities or items, the owner usually wants color codes that are not found within ChatColor. Also, using translateAlernateColorCodes means that when you send a player a message or whatever, you have to put that in for every message. Using this method, all you have to do is (colorize("string")).
I just usually create a method that can be accessed from any class. Code:java public void sms(Player p, String msg) { p.sendMessage(ChatColor.translateAlternateColorCodes('&', msg)); }
Cirno Well not if they are trying to get the colors from a Config, you could but it would look ugly. Jimfutsu Just use ChatColor.translateAlternateColorCodes('char', String);
If that's the case: Code:java public String colorize(String str){ return ChatColor.translateAlternateColorCodes('&', str);} or if you want to do this without using ChatColor: Code:java public String colorize(String str){ return str.replace('&', '§');} Curious though; what colors are you using if Bukkit doesn't support them? Bukkit supports all vanilla client chat colors. I'm going to point out all of the "mistakes" that the code has; it's merely my point of view, so do not take it as an insult. 1) str +=str2 is horribly inefficient. Every time you run that statement, internelly, Java creates a new StringBuilder/Buffer and runs append("str2"). I suggest switching to a StringBuilder (StringBuffer if you're threading). 2) Why are you even going through all the characters if String already has a built-in replace()? It would be hypocritical to say this (since I do this sometimes without knowing it), but don't re-invent the wheel. 3) By using String.replace or your method, it automatically assumes that all characters are chat color characters.
extended_clip I like that idea as it is efficient, I might change the way in the thread, but still personally, I think that this way is a lot easier.
I feel with my way, I make it "easier" as I don't even have to type Code:java p.sendMessage(msg); I can simply call my method: Code:java if (!p.hasPermission("plugin.reload") {plugin.sms(p, "&cYou do not have permission!");} or if I wanted to even add my plugins message prefix to it, Code:java //this method sends a player a message with the plugin prefix attached to itpublic void smsP(Player p, String msg) {String prefix = plugin.prefix();p.sendMessage(ChatColor.transAlternateColorCodes('&', prefix + " &r" + msg));} It is all about how you choose to do it though, find what ever works best for you!
Jimfutsu Never concat String with +=. Use StringBuilder, internally, += clones the current string and modifies the backing array to create the new String. Bukkit's method modifies the char array itself and returns a new String from the char array, which isn't all that bad of an idea until it returns a new String object.
Oh my god. Just use this. Code:java public static String colorize(String message){ return ChatColor.translateAlternateColorCodes('&', message);} You are basically making life harder by using your method. It is not simpler.
This is me every time I view either the plugin dev forums, or more recently, the resources section of said forums.
To be more accurate, never use += (or String#concat(), which is the same thing) on Strings within a loop, which is what the OP was doing here, and it is indeed atrociously bad for performance. String concatenation outside a loop is generally OK, because the compiler will almost certainly spot that and convert it to use StringBuilder internally. It can't do that with code inside a loop.
Jimfutsu Another problem: Use your code and try to use a phrase like "fish & chips". It won't work out too well.
Cirno Dont use .replace() use .replaceAll() This way if theres more than 1 '&' symbol, which there probably is, it will replace them all. Also, to get the § symbol, hold down alt, and press 2, 1, then release alt. It doesnt work in Minecraft chat.
Plo124 Nope! You might think that from the name of it, but replace() actually does replace all occurrences. The only difference is that replaceAll() uses regex, whereas replace() doesn't. Which means that you should always use replace() unless you're using regex.