About chat manipulating

Discussion in 'Spigot Plugin Development' started by Kyllian, Apr 25, 2017.

  1. Hey,

    I'm wondering what the best way is to manipulate chat, to disallow a player from receiving chat when the player is in a hashmap.

    I tought that I should cancel all messages and send them to the players that arent in the hashmap. But that would deny things like autobroadcasts.

    Soo...??
     
  2. AsyncPlayerChatEvent.getRecipients().remove(player)
     
  3. Hmm yes that would work. But not for things like autobroadcast
     
  4. If you want to cancel every single message, use a protocollib send listener and cancel every message packet.
     
    • Agree Agree x 1
  5. AsyncPlayerChatEvent easily solves your issue. It is async only if the sender is a player, the same event works as sync if its sender is a plugin. So you can do the following,
    Code (Text):
    if (e.isAsynchronous()){
    //remove player from recipents
    }
     
    • Informative Informative x 1
  6. Mas

    Mas

    Wow, didn't know that, that's really useful.
     
  7. I think you're misunderstanding the documentation. It says that the event is synchronous when a plugin makes a player chat, not when a plugin makes the server broadcast a message. If what you are saying were the case, the event would not have the word 'player' in it.
     
    • Like Like x 1
  8. Oh yea, Sorry for the confusion. I was a bit sleepy. Tbh I dont think broadcasts even go through the playerchatevent/asyncplayerchatevent. If it does do that, one other way of checking would be by using a format check.
    Code (Text):
    String mess = ChatColor.StripColor(e.getMessage());
    for (Player p:Bukkit.getserver.getOnlinePlayers()){
    String displayn = p.getDisplayName();
    boolean isPlayer = e.getformat().contains(displayn);
    If (isPlayer){
    return ;
    }
    U may need to correct some stuff from the above code. I just wrote it out of my mind. Good luck either way
     
  9. We could test this by setting up a asyncplayerchatevent and saying something in the console
     
  10. This is what you need. The player chat events are only called when a player chats, nothing else. The only way to intercept all messages sent to them is by using protocollib and cancelling the sent message packets. If you google you'll probably find what you need.
     
  11. He wants to cancel chat messages not broadcasts. He was asking if the broadcast would be blocked as well by what he was doing. Which im 90% sure that broadcasts dont go through playerchatevent or its async alter
     
  12. But stopping the broadcasts will be needed too because the chat messages need a delay. For my plugin, so it stops them for (x) amount of seconds and after them it will send them all
     
  13. Ah alright. Then packets are your way
     
  14. I dont like using protocollib because I hate dependencies. Isnt there another way around?
     
  15. Rewrite every plugin you wish to work with this hash map... Or rewrite the player.sendMessage method to support your hash map.


    Sent from my iPhone using Tapatalk
     
  16. I don't get why this wasn't clearly mentioned yet. When you're broadcasting a message, just insert a loop and check if the HashMap contains the player. This shouldn't affect normal chat, and vice-versa.

    Here's an example using an ArrayList (you can use a HashMap, I just didn't see the need to):
    Code (Text):
    public ArrayList<Player> cannotRecieveChat = new ArrayList<Player>();

    for(Player all : Bukkit.getServer().getOnlinePlayers()) {
            if(!(cannotRecieveChat.contains(all))) {
                    // Send your message or whatever
            }
            return;
    }
     
  17. Op wishes to remove ALL messages not just sending a message to a specific player or excluding them from a message.

    Op wants to be able to exclude the player even from methods sent by other plugins like your example. Which are not playerchatevents.


    Sent from my iPhone using Tapatalk
     
    • Like Like x 1
  18. Wouldn't they need to import every plugin they want to support, and create methods to exclude that player individually?
     
  19. Or rewrite player.SendMessage() but it's definitely NOT in the player chat event


    Sent from my iPhone using Tapatalk
     
    • Agree Agree x 1