Creating a Message Manager! [Easy]

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

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

    JPG2000

    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
    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.
    Code:java
    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.
    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
    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

    Jumla

    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

    Loogeh

    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

    JPG2000

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

    TomFromCollege

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

    to
    Code:java
    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

    Paper

    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

    Ultimate_n00b

    I just add a few methods to my custom player object.
    Code:java
    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

    MTN


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

    hintss

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

    confuserr

    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

    MTN

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

Share This Page