making colored mentions in chat

Discussion in 'Plugin Development' started by MaplePlaysMC, Jun 21, 2014.

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

    MaplePlaysMC

    I want to make a plugin where if a player sends a msg in chat with another users ign in the msg the name in that msg will turn a certain color so if i did "Hi <name> how are you doing today?" the <name> would turn a different color from the other part of the msg but it would only turn color for the player whos name the msg says, is that possible? if so can somebody point me in the right direction on how to start that?

    Thanks!
     
  2. Offline

    es359

    You might want to use the PlayerAsyncChatEvent for this. MaplePlaysMC
     
  3. Offline

    jthort

    MaplePlaysMC listen to the chat event, and loop through each I the words in te string. If it is one of the online players names, then change the color and continue. At the end of the word set the new string to the message
     
  4. Offline

    ReggieMOL

    Also make sure it work's only if the player is online.
     
  5. Offline

    MaplePlaysMC

    an you give me a example of how i would do this? please
     
  6. Offline

    ReggieMOL

  7. Offline

    MaplePlaysMC

  8. Offline

    ReggieMOL

    "I want to make a plugin where if a player sends a message in chat with another users ign in the msg the name in that msg will turn a certain color so if i did "Hi <name> how are you doing today?" the <name> would turn a different color from the other part of the msg but it would only turn color for the player whos name the msg says, is that possible? if so can somebody point me in the right direction on how to start that?"

    Well you could first define the target player. Check if the arg after the command used to message is a valid online player:
    Code:java
    1. @Override
    2. public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
    3. Player p = (Player) sender;
    4. if (cmd.getName().equalsIgnoreCase("message")) {
    5. if (p.hasPermission("message.use"))
    6. if (args.length != 1) {
    7. return false;
    8. }
    9.  
    10. if (!(sender instanceof Player)) {
    11. sender.sendMessage("Only players can send messages.");
    12. return true;
    13. }
    14. Player target = Bukkit.getServer().getPlayer(args[0]);
    15. String message = args[1];
    16.  
    17. if (target == null) {
    18. sender.sendMessage(ChatColor.DARK_RED + "ERROR :" + ChatColor.RED + args[0] + " is not currently online.");
    19. return true;
    20. }
    21.  
    22. sender.sendMessage(ChatColor.COLOR + target + ": " + args[1]);
    23. target.sendMessage(ChatColor.COLOR + sender + ": " + args[1]);
    24. return true;
    25. }
    26. return false;
    27. }

    http://jd.bukkit.org/rb/doxygen/da/d86/TellCommand_8java_source.html
    This should lead you in the correct direction.

    NOTE: I haven't tested it.
     
  9. Offline

    MaplePlaysMC

    I probably should have made it more clear but i dont want it to be like /message i just mean that when somebody says something in chat with another players ign Sorry

    bumb*

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 9, 2016
  10. Offline

    ZanderMan9

    I could try and make this for you...
    if you want to dev it yourself, you need to listen to the chat event, as mentioned. If you have basic knowledge you can do this. Then it becomes complicated, because the chat I believe is a single string... You'll have to have some interesting code to break the string down into a string for each word... after that, you'll have to reverse the process and rebuild the string (I have some code for this part, as I made a plugin that needed it), with the name removed and replaced with ChatColor.COLOR + player.getName().
     
  11. Offline

    MaplePlaysMC

    Could you try to make it for me? if not is there anyway i can see the code you already have for rebuilding the string? :p
     
  12. Offline

    Traks

    ZanderMan9 Sounds a bit overcomplicated...
    Just create an AsyncPlayerChatEvent listener. Split the string in words and loop through them and check if each word resembles a player's name. If it does, add that player to a Set and remove the player from the Set returned by AsyncPlayerChatEvent.getRecipients(). Then send a message to that player using the event's format and message, in which you replace the player's name with "§c" + Player.getName() + "§f".
     
  13. Offline

    MaplePlaysMC

    Can you show me how i would go about Splitting the string into words and looping through them and checking for a online player?
     
  14. Offline

    Traks

    MaplePlaysMC
    If event represents an instance of the AsyncPlayerChatEvent
    Code:java
    1. if(event.getMessage() != null) {
    2. for(String word : event.getMessage().split(" ")) {
    3. Player p = Bukkit.getPlayerExact(word);
    4.  
    5. if(p != null) {
    6. // Player with this name exists, do as I mentioned earlier...
    7. }
    8. }
    9. }
     
  15. Offline

    ZanderMan9

    Code:java
    1. StringBuilder sb = new StringBuilder();
    2. for (int i = 1; i < args.length; i++)
    3. {
    4. sb.append(args[I]).append(" ");[/I]
    5. [I] }[/I]
    6. [I] String argsConjugated = sb.toString().trim();[/I]

    Here's the builder, it's used in the context of a command, just change the sting array name to fit your needs. Get rid of the [/I]
     
Thread Status:
Not open for further replies.

Share This Page