[Tutorial] Chat Format and Messaging

Discussion in 'Resources' started by AlexHH251997, Oct 5, 2013.

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

    AlexHH251997

    So you have probably seen this threads title and wanted to know how to format the chat in Bukkit. Well it is actually quite simple and I am going to show you how with the aid of the Latest Bukkit Version and Eclipse.

    Getting the EventHandler (Required for using a event):
    Code:java
    1. @EventHandler

    Getting the Event (Required for setting the format):
    Code:java
    1. public void chatFormat(AsyncPlayerChatEvent event)

    Getting the Default Chat Format (Only use if you wish to see the default format):
    Code:java
    1. event.getFormat();

    Getting the Player (Required for setting the format):
    Code:java
    1. Player p = event.getPlayer();

    Setting the Format:
    Code:java
    1. event.setFormat(ChatColor.WHITE + "<" + p.getDisplayName() + ">" + event.getMessage());
    2. //This is the default layout for Bukkit Chat Format.
    3.  
    4. event.setFormat(ChatColor.GRAY + "<" + p.getDisplayName() + ChatColor.GRAY + "> " + ChatColor.WHITE + event.getMessage());
    5. //This is the default layout for Bukkit Chat Format but it has Gray characters on the name instead of White.
    6.  
    7. event.setFormat(p.getDisplayName() + ChatColor.GRAY + ": " + ChatColor.WHITE + event.getMessage());
    8. //This is a more standard look on the Minecraft Chat Format.
    9.  
    10. event.setFormat(ChatColor.DARK_GRAY + "[" + ChatColor.GREEN + "MEMBER" + ChatColor.DARK_GRAY + "] " + p.getDisplayName() + ChatColor.GRAY + ": " + ChatColor.WHITE + event.getMessage());
    11. //This is a rank format. It will show "[MEMBER]" infront of the name. It is known to be used on Faction servers.

    Setting the Format with Permissions:

    Code:java
    1. if(p.hasPermission("chat.format.member"){
    2. event.setFormat(ChatColor.DARK_GRAY + "[" + ChatColor.DARK_GREEN + "MEMBER" + ChatColor.DARK_GRAY + "] " + ChatColor.DARK_GREEN + p.getDisplayName());
    3. //This will give the player with that permission node that Chat format.
    4. }else if(p.hasPermission("chat.format.mod"){
    5. event.setFormat(ChatColor.DARK_GRAY + "[" + ChatColor.RED + "MODERATOR" + ChatColor.DARK_GRAY + "] " + ChatColor.RED + p.getDisplayName());
    6. //This will give the player with that permission node that Chat format.
    7. }else{
    8. event.setFormat(ChatColor.DARK_GRAY + "[" + ChatColor.GRAY + "UNKNOWN" + ChatColor.DARK_GRAY + "] " + ChatColor.GRAY + p.getDisplayName());
    9. //If the player has non of the above permission nodes they will have this Chat Format.
    10. }

    Using it all together:
    Code:java
    1. @EventHandler
    2. public void chatFormat(AsyncPlayerChatEvent event){
    3. Player p = event.getPlayer();
    4. event.setFormat(p.getDisplayName() + ChatColor.DARK_GRAY + ": " + ChatColor.WHITE + event.getMessage());
    5. }

    Using it all together (with permissions):
    Code:java
    1. @EventHandler
    2. public void chatFormat(AsyncPlayerChatEvent event){
    3. Player p = event.getPlayer();
    4. if(p.hasPermission("chat.format.member"){
    5. event.setFormat(ChatColor.DARK_GRAY + "[" + ChatColor.DARK_GREEN + "MEMBER" + ChatColor.DARK_GRAY + "] " + ChatColor.DARK_GREEN + p.getDisplayName() + ChatColor.DARK_GRAY + ": " + ChatColor.WHITE + event.getMessage();
    6. //This will give the player with that permission node that Chat format.
    7. }else if(p.hasPermission("chat.format.moderator"){
    8. event.setFormat(ChatColor.DARK_GRAY + "[" + ChatColor.RED + "MODERATOR" + ChatColor.DARK_GRAY + "] " + ChatColor.RED + p.getDisplayName() + ChatColor.DARK_GRAY + ": " + ChatColor.WHITE + event.getMessage();
    9. //This will give the player with that permission node that Chat format.
    10. }else if(p.hasPermission("chat.format.admin"){
    11. event.setFormat(ChatColor.DARK_GRAY + "[" + ChatColor.DARK_RED + "ADMIN" + ChatColor.DARK_GRAY + "] " + ChatColor.DARK_RED + p.getDisplayName() + ChatColor.DARK_GRAY + ": " + ChatColor.WHITE + event.getMessage();
    12. //This will give the player with that permission node that Chat format.
    13. }else{
    14. event.setFormat(ChatColor.DARK_GRAY + "[" + ChatColor.GRAY + "UNKNOWN" + ChatColor.DARK_GRAY + "] " + ChatColor.GRAY + p.getDisplayName() + ChatColor.DARK_GRAY + ": " + ChatColor.WHITE + event.getMessage();
    15. //If the player has non of the above permission nodes they will have this Chat format.
    16. }


    NOTE: If you are using the permission aspect of this code remember to write down the permission nodes in your plugin.yml otherwise this plugin will not work. If you are not using the permission aspect you will not need to add anything to your plugin.yml as this does not affect it.

    I hope this helps you Bukkit developers out there. If this did help be sure to like the thread, check out my BukkitDev plugin page and share this tutorial with other developers you may know that will need this in some of their plugins.
     
    TheArabianCoder and KingFaris11 like this.
  2. you don't have to add the permission nodes to the plugin.yml.
     
  3. Offline

    AlexHH251997

    To use permission nodes in a plugin they have to be identified in the plugin.yml
     
  4. Nope, they don't. If you use .hasPermission() it works, also when you didn't register the permissions in your plugin.yml
     
  5. Offline

    AlexHH251997

    I identify them in the plugin.yml just to be sure anyway. It has never worked in the past without doing it...
     
  6. Offline

    Aqua

    My plugins with permissions work, even without defining them in plugin.yml
     
  7. Offline

    AlexHH251997

    I have always done it anyway...
     
  8. Offline

    Ty Cleere

    Prob gonna use this in my plugin! :)
     
    AlexHH251997 likes this.
  9. Offline

    macguy8

    You should always use keywords (%s) and such for 2 reasons.
    1) If another plugin changes the message after your plugin, the change won't go through.
    2) If I go on your server and type anything with % in it how it currently is, it'll throw an exception into the console.

    Please make the following changes to your tutorial to avoid giving some not really wrong, but not proper information to newer developers :)
     
    StefanXX likes this.
Thread Status:
Not open for further replies.

Share This Page