sendMessage() Method! Easier than player.sendMessage()

Discussion in 'Plugin Development' started by S1ant, May 23, 2017.


Would you use this?

  1. Yes!

  2. No.

  3. Yes, but a modified Version.

Results are only viewable after voting.
Thread Status:
Not open for further replies.
  1. Offline


    Hello, today I want to show you a method I've been using. I think that this cool method would come in handy when creating plugins. So, what are we waiting for! Let us get right into it!
    1. public void sendMessage(Player p, String s) {
    2. p.sendMessage(ChatColor.DARK_GRAY + "[" + ChatColor.RED + "<PLUGIN_NAME>" + ChatColor.DARK_GRAY + "] " + ChatColor.RED + s);
    3. }

    At least that is usually what I do, hope it helps and makes your life Easier!

    EDIT: Another Method someone suggested:
    2. public void sendMessage(Player p, String s, boolean usesPrefix, ChatColor messageColor) {
    3. if (usesPrefix == true) {
    4. plr.sendMessage(ChatColor.GOLD + "[<PLUGINNAME>]" + messageColor + s);
    5. } else {
    6. plr.sendMessage(messageColor + s);
    7. }
    8. }
    Last edited: May 29, 2017
  2. Offline


    @S1ant what help do u need? I don't understand the sentence after P.M.
  3. Offline

    timtower Moderator Moderator

    @RcExtract There is no question.
    He is showing off something.
  4. Offline


    @RcExtract Exactly what
    @timtower said, I am showing off something that could help people when developing, by making it much easier then writing out the whole plugin name and the brackets every time.
  5. Offline


    but you could easily make a constant string, and have it accessible :p

    public class Example {
         public static String PREFIX = "[PREFIX]";
    public class AnotherExample extends JavaPlugin {
         public void onEnable() {
              System.out.println(Example.PREFIX + " Hello world!");
  6. Offline

    timtower Moderator Moderator

    @FrostedSnowman Or a language file.
    There are many ways to do this stuff.
  7. Offline


    How about using commandsender instead of player, it would work for console too.
  8. Offline


    @Horsey You could do that, you can make two different methods, Just make it sendMessage(CommandSender sender, String s) {

  9. Offline

    timtower Moderator Moderator

    A player is a commandsender so you only need 1 method
  10. Offline


    @timtower It has a red line under it if you put sender...
  11. Offline

    timtower Moderator Moderator

  12. Offline


    No, player is not the only instance of command sender. There is also ConsoleCommandSender. In reality, you could just easily make the two methods, no real big deal.

    Unless you want to check for the object everytime if you're going to be using a method with CommandSender as the parameter.
  13. Offline

    timtower Moderator Moderator

    @FrostedSnowman I never said that it was the only commandsender. And there is no need to check the object as sendMessage is a CommandSender method, so the console and the player use that.
  14. Offline


    @timtower Should I move this over to the Resources Section?
  15. Offline

    timtower Moderator Moderator

    @S1ant If I got to be honest: not worth it in my opinion.
  16. Offline


    To be honest, this doesn't make like much easier unless you always want to use those same colors.

    I would of made it more user friendly:
    public void sendMessage(Player plr, String s, boolean usesPrefix, ChatColor messageColor) {
        if (usesPrefix == true) {
            plr.sendMessage(ChatColor.GOLD + "[<PLUGINNAME>]" + messageColor + s);
        } else {
            plr.sendMessage(messageColor + s);
  17. Offline


    Now added A poll!
  18. Offline


    @S1ant dude, sorry but it's useless. It's just a basic method.
    CeramicTitan likes this.
  19. Offline


    I wouldn't say it's useless. Even though it is rather basic, it's quite a useful tip. Even the most experienced programmer may sometimes sit and repeat himself, in which case this can be quite a useful insight.
    S1ant likes this.
  20. Offline


    I like the idea but can't you just type

    Player p = (Player) sender; and be able to put

    p.sendMessage(<stuff here>); ?
  21. Offline

    timtower Moderator Moderator

  22. Offline


    Yes, you could, But this is in one method, and it has the Prefix and colors Included.
  23. Offline


    @S1ant I use
    public void sendMessage(CommandSender anything, String message) {
        anything.sendMessage(ChatColor.translateAlternateColorCodes('&', "&7[&6&oPluginName&r&7] &f" + message));
  24. Offline

    CeramicTitan Retired Staff

    A far more elegant solution to this would be something along these lines:

    public enum Messages {
    //A centralised unit for all messages, reducing duplication.
    INVALID_PERMISSION(ChatColor.RED+"You have insufficient permission!"),
    GREETING(ChatColor.BLUE+"Welcome to our server!");
    String message;
    //You can add more 'customisability', if needed by altering the enum forms and the constructor.
    private Messages(String message){
    this.message message;
    //Your sending method using these dynamic messages
    public void send(CommandSender sender,boolean tag){
    String prefix tag "[PREFIX]" "";
    sender.sendMessage(prefix message);
    EDIT: You don't even need to use Enums. You can achieve a similar outcome with a static class with messages and methods.
    S1ant likes this.
Thread Status:
Not open for further replies.

Share This Page