Solved How can I override chat events?

Discussion in 'Plugin Development' started by TfT_02, Nov 26, 2012.

Thread Status:
Not open for further replies.
  1. Hi everyone,

    I'm looking for a way to override playernames in chat. So when a certain player sends a message or when he died / kills someone, his name will be changed.

    I'm looking for a way that will override every chatplugin, so that their displayname doesn't get displayed. How would I do this? Is it possible?

    Edit: Can Vault help me with this?

    Edit2: Solved.
    Added non existent plugin to softdepend in plugin.yml
    Did this onPlayerChat.
    Code:java
    1. @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = false)
    2. public void onAsyncPlayerChatEvent(AsyncPlayerChatEvent event) {
    3. Player player = event.getPlayer();
    4. String pName = ChatColor.DARK_RED + "[ASSASSIN]: " + ChatColor.RESET;
    5. String msg = event.getMessage();
    6.  
    7. if (msg == null)
    8. return;
    9.  
    10. if (data.isAssassin(player)){
    11. event.setFormat(pName + msg);
    12. }
    13. }
     
  2. Offline

    andf54

    player.setDisplayName()?
     
  3. Yeah that works, until another chat plugin overrides that..
     
  4. Set your event priority to Highest or monitor. That might work.
     
  5. But what if I'm using player.setDisplayName() in a command?
     
  6. Test if you changed that persons name in the command, you could do this by adding that players name to a list or something like that. If you did change his name in the command, don't change it in the event.
     
  7. I'm not sure if I understand what you mean. I'm trying to edit a player name, so I've used player.setDisplayName in a command. That works perfectly fine, but when I add a chat plugin to my test server, that plugin overrides my plugin's display name.

    I know you can set event priority's, but because I'm setting the display name in a command I don't know how I could use this. I can't just set the priority of my command event to HIGHEST.


    I'm now trying to use the AsyncPlayerChatEvent event) { and changing the message with event.setFormat() but I'm not sure if I'm doing this the right way.

    Could someone provide me with a simple example which listens to AsyncPlayerChatEvent and changes name displayed before the chat message with event.setFormat()?
     
  8. Offline

    linkrock4

    Are you trying to make something just like the /nick in essentials?
     
  9. Offline

    skipperguy12

    No, hes not... This is a problem I ran into on my team plugin... I put in bChatManager and it kept overriding my plugins beautiful colored names. I haven't found a solution either, but the event idea was nice. @TfT_02, your in a chat event. Soo, above that put @EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
     
  10. Offline

    toothplck1

    You shouldn't edit events while on monitor, set it to HIGHEST
     
  11. Ok, well, in that case you should make sure your plugin loads before that chat plugin does, which means editing your plugin.yml
    If you set loadbefore in your plugin.yml it will load before the plugins you put in that list, and thus your plugin should be the first and last one to change the names. (see: http://wiki.bukkit.org/Plugin_YAML)
    I am not 100% sure about this, but i think this could work.
     
  12. Offline

    Comphenix

    That sounds like a bit of a hack. But I don't think it's necessary - all you need to do, is change the display name in the AsyncPlayerChatEvent. The new name should be kept in a hash table, allowing you to change it in a command:
    Code:java
    1. public class ExampleMod extends JavaPlugin implements Listener {
    2. // Or you could use synchronization
    3. private Map<String, String> overridenNames = new ConcurrentHashMap<String, String>();
    4.  
    5. public void onEnable() {
    6. PluginManager pm = getServer().getPluginManager();
    7. pm.registerEvents(this, this);
    8. }
    9.  
    10. // Only use MONITOR if you absolutely must in order to override the Plugin
    11. @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = false)
    12. public void onAsyncPlayerChatEvent(AsyncPlayerChatEvent event) {
    13. Player player = event.getPlayer();
    14. String overriden = overridenNames.get(player.getName());
    15.  
    16. if (overriden != null) {
    17. event.getPlayer().setDisplayName(overriden);
    18. }
    19. }
    20.  
    21. private void performAction(CommandSender sender, String[] args) {
    22. String player = args[0];
    23. String newName = StringUtils.join(args, " ", 1, args.length);
    24.  
    25. // Warn - but still allow it
    26. if (getServer().getPlayerExact(player) == null) {
    27. sender.sendMessage(ChatColor.GOLD + "Warning: Player " + player + " is not online.");
    28. }
    29.  
    30. overridenNames.put(player, newName);
    31. }
    32.  
    33. @Override
    34. public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
    35. if (args.length < 2) {
    36. sender.sendMessage("This command takes at least two arguments");
    37. return true;
    38. }
    39.  
    40. if (command.getName().equals("performaction")) {
    41. performAction(sender, args);
    42. return true;
    43. }
    44.  
    45. return false;
    46. }
    47. }

    Note that I'm using a ConcurrentHashMap as we're dealing with asynchronous events. Though, it is possible for the display name to change in the main thread after the event handler, but before the chat message is broadcasted. So if you're really worried, you might want to use the setFormat() trick after all:
    Code:java
    1. @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = false)
    2. public void onAsyncPlayerChatEvent(AsyncPlayerChatEvent event) {
    3. Player player = event.getPlayer();
    4. String overriden = overridenNames.get(player.getName());
    5.  
    6. if (overriden != null) {
    7. // Half-evaluate the format now, using our own overriden name
    8. event.setFormat("<" + overriden + "> %2$s");
    9. }
    10. }
     
    CeramicTitan likes this.
  13. Well, you would actually load after the other plugin so you overwrite what it did. Just softdepend on ThisPluginDoesNotExist should be fine. But better change the chat plugin and/or configure it to not change display names.
     
  14. Yes, I'm trying to do something similar. My plugin will also put players in a team and change their display names.


    Yes.

    So I would have to add every possible Chat plugin there is (there are quite a few)...


    Yeah its a bit hacky, but I thought it might be the best idea. I would like it to work with every possible chat plugin.

    I will have a go with this.

    This would require me to add every possible chat plugin there is to softdepend.. Doesn't really sound like the best solution, but I'll do it if there's no other way.


    @ Everyone, thank you for your replies and help. Much appreciated :)

    I would like to add that I'm also looking for a way to change a players name in PVP death events, this might be similar to changing names in chat.
     
  15. No, re-read what I wrote: You put a non-exising plugin into softdepend. That way your plugin will load after all others. ;)
     
  16. Ohhh you actually meant to put that there :p I thought you wanted to say that I would have to replace that with an excisting plugin name. I will give it a go!

    Edit: This did the job! Such an easy solution! Thanks! :)
     
Thread Status:
Not open for further replies.

Share This Page