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

    S1ant

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

    RcExtract

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

    timtower Administrator Administrator Moderator

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

    S1ant

    @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

    FrostedSnowman

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

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

    timtower Administrator Administrator Moderator

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

    Horsey

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

    S1ant

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

    }
     
  9. Online

    timtower Administrator Administrator Moderator

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

    S1ant

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

    timtower Administrator Administrator Moderator

  12. Offline

    FrostedSnowman

    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. Online

    timtower Administrator Administrator 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

    S1ant

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

    timtower Administrator Administrator Moderator

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

    mccrafter1212

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

    S1ant

    Now added A poll!
     
  18. Offline

    Caderape2

    @S1ant dude, sorry but it's useless. It's just a basic method.
     
    CeramicTitan likes this.
  19. @Caderape2
    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

    MomentHCF

    I like the idea but can't you just type

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

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

    timtower Administrator Administrator Moderator

  22. Offline

    S1ant

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

    PhantomUnicorns

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

    CeramicTitan

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

    PHP:
    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!");
     
        private 
    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