Hey guys! Today I will be explaining the benefits, and how to make a MessageManager. For larger plugins that send a lot of messages, you may want to have a message manager. The end result will be: Code:java MM.message(player, Kind.Good, "You've been a good boy"); The message manager that we are going to make, is going to look for a enum type, and send a player a message with the enum prefix. First lets create a new class, named MM. Lets create are Enums. I created three, but you can create what ever one's you like. Code:java public static enum MessageType {Good, Bad, Error} I made them static and public so they can be used. Lets create our message method, lets name it message. Code: public static void message(Player player, Enum type, String message) { } To use the code, you will have to type a Player, your enum type and then your message. Now lets check for the enum and send the player a message acording on the enum type. Code:java public static void message(Player player, Enum type, String message) {if (type == MessageType.Good) {player.sendMessage(ChatColor.GRAY + "[TestPlugin] " + ChatColor.GREEN + message);return;}if (type == MessageType.Bad) {player.sendMessage(ChatColor.GRAY + "[TestPlugin] " + ChatColor.RED + message);return;}if (type == MessageType.Error) {player.sendMessage(ChatColor.GRAY + "[TestPlugin] " + ChatColor.DARK_RED + "Error: " + message);return;}} That is my quick message manager. I find that it ends up saving lots of time. To recap, create your static enums, create your static method and send a message depending on the enum type.
You could do this way sexier by adding a "formatting" field to the "Type" enum (undescriptive name btw, MessageType would be better fit), and have that formatting field dynamically added to the final message. Then, when you want to add a new message field, you just add something else to the enum. This would also make it pretty easy to make it read from a configuration file (the formatting, that is)
JPG2000 The parameter type in the msg method shouldn't be enum instead it should be Type because that's the name of your enumeration. You probably know that but it may confuse some people. Also you create a message method and then changed it to msg.
Iwould suggest changing Code:java if (type == MessageType.Bad) { to Code:java switch(type) { case Bad: break;} Or, if you won't make it a switch, at least use if - else
Usually an enum like that is fully caps? Not sure though, I usually see that a lot. So it'd be Kind.ERROR rather than Kind.Error - I guess it doesn't mean much anyways
I just add a few methods to my custom player object. Code:java p.sendWarning("This is a warning!");p.sendAlert("This is an alert!");p.sendPrefixedMessage("This is a message with the plugin's prefix!");