Creating a Message Manager! [Easy]

Discussion in 'Resources' started by JPG2000, Aug 23, 2013.

Thread Status:
Not open for further replies.
  1. Offline


    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:
    1. 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.
    1. public static enum MessageType {
    2. Good, Bad, Error
    3. }

    I made them static and public so they can be used.

    Lets create our message method, lets name it message.
    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.
    1. public static void message(Player player, Enum type, String message) {
    2. if (type == MessageType.Good) {
    3. player.sendMessage(ChatColor.GRAY + "[TestPlugin] " + ChatColor.GREEN + message);
    4. return;
    5. }
    6. if (type == MessageType.Bad) {
    7. player.sendMessage(ChatColor.GRAY + "[TestPlugin] " + ChatColor.RED + message);
    8. return;
    9. }
    10. if (type == MessageType.Error) {
    11. player.sendMessage(ChatColor.GRAY + "[TestPlugin] " + ChatColor.DARK_RED + "Error: " + message);
    12. return;
    13. }
    14. }

    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.
  2. Offline


    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)
    glen3b likes this.
  3. Offline


    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.
  4. Offline


    Loogeh I fixed the errors, thanks for your imput!
  5. Offline


    Iwould suggest changing
    1. if (type == MessageType.Bad) {

    1. switch(type) {
    2. case Bad:
    3. break;
    4. }

    Or, if you won't make it a switch, at least use if - else :)
  6. Offline


    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 :p
  7. Offline


    I just add a few methods to my custom player object.
    1. p.sendWarning("This is a warning!");
    2. p.sendAlert("This is an alert!");
    3. p.sendPrefixedMessage("This is a message with the plugin's prefix!");
  8. Offline


    which will break java 6 compatibility and forces java 7
  9. Offline


    yup, and have a method/function/whatever to titlecase it automagically ^_^
  10. Offline


    No it won't. It's switching on an Enum, not a string. Will work perfectly fine on Java 6.
    glen3b likes this.
  11. Offline


    oh, I guess I was "confused"
    confuserr likes this.
Thread Status:
Not open for further replies.

Share This Page