Solved Chat Blocking

Discussion in 'Plugin Development' started by Deleted user, Aug 28, 2013.

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

    Deleted user

    Is it possible to block the chat from view for a specific player?

    Then, is it possible that if a player says a special player's name in chat (or part of their name), it'll send them a message?

    Thanks,
    JHG0

    Assist
     
  2. Offline

    Axe2760

    You can cancel the event, and use sendMessage(event.getMessage()) to players who should get the message.
     
  3. Offline

    Deleted user

    Axe2760

    Yes, vut how do I get the EXACT message? Plus my plugin shod also be conpatible with chatrooms
     
  4. Offline

    Axe2760

    JHG0 I think maybe something with the AsyncPlayerChatEvent, as according to the javadocs, it fires for plugins as well.
     
  5. Offline

    Deleted user

    Axe2760
    Hrmm...how would I use it

    Assist xTrollxDudex

    Bump

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 4, 2016
  6. Looked this up myself, here is what I found. Since there is no event called on receiving a message or global chat, here would be another alternative, taken from a post by @Mitsugaru

    Code:java
    1. public void chatEvent(final PlayerChatEvent event) {
    2. final Player target = Bukkit.getServer().getPlayer(nameOfPlayerToRemove);
    3. event.getRecipients().remove(target);
    4. }

     
  7. Offline

    Deleted user

    Rob_____
    Thanks, who would nameOfPlayer...be?

    Also, how do I get a list of player's in the hashmap and cycle through them for target?
     
  8. Offline

    JPG2000

    Axe2760 I don't think cancelling and sending the message would work, becuase it wouldn't show the players name and stuff.
     
  9. Offline

    Deleted user

    JPG2000
    Any idea abt he hashmaps above?
     
  10. Why would you need to use hashmaps, just add a command like /mutechat [player] which uses the argument as the name of a player.

    I am going to sleep now and won't be answering further questions till tomorrow, Thanks
     
  11. Offline

    JPG2000

    JHG0 Okay. Heres my idea. Use the player chat event. By defualt, block it, and send the player the message(Use the player.chat becuase of what Is aid before)
    Code:java
    1. public void onTalk(AsyncPlayerChatEvent event) {
    2. event.setCancelled(true);
    3. event.getPlayer().chat(event.getMessage());
    4. }


    Then use a Hashmap, with the key being the player who did /block, and the value being the one who is blocked.
    Code:java
    1. HashMap<String, String> blocked = new HashMap<String, String>();


    Then do the rest :D
     
  12. Offline

    Deleted user

    Rob_____ JPG2000

    Confused

    I want it to block chat for every player that types the command /silence

    I got almost everything else so far...just wanna know how to get every player out of an areay lost and cycle through them
     
  13. Offline

    Axe2760

    Code:
    //for ArrayList
    Iterator<String> it = stringarray.iterator();
    while (it.hasNext()){
      String name = it.next();
      //insert code
    }
    
    //for HashMap
    Iterator<Set<String>> it = hash.keySet().iterator();
    //same thing
    
     
  14. Offline

    Deleted user

    Axe2760
    Thanks...
    Would you by any chance know how if a player /msg-s a certain player it will stop the event?
     
  15. Offline

    Axe2760

    Try the PlayerCommandPreProcessEvent, I'm looking at the jd for it ATM. It has a method getRecipients(), not sure if this will work. If not,

    Pseudo code:
    Code:
     
    on PlayerCommandPreprocessEvent event:
      String[] words = event.getMessage().split(" ");
      if (words.length >=3){
        if (words[0].equalsIgnoreCase("/msg") && whitelist.contains(words[1])){
          //send message
        }
        else if (words[0].equalsIgnoreCase("/msg") && !whitelist.contains(words[1])){
          event.setCancelled(true);
        }
      }
    
    However as it is case sensitive, I recommend using .toLowerCase() when putting/getting/seeing if names are in the whitelist.
     
  16. Offline

    Deleted user

    Axe2760
    What is wrong with this?
    It won't silence the chat...
    Code:java
    1. //EventHandler
    2. @EventHandler
    3. public void chatEvent(final PlayerChatEvent e){
    4. //Player chatter = e.getPlayer();
    5. int i = 0;
    6. int isLength = isS.size();
    7. while(i < isLength){
    8. Player sPlayer = isS.get(i);
    9. e.getRecipients().remove(sPlayer);
    10. i++;
    11. }
    12. }
    13.  
    14. //onCommand
    15. public boolean onCommand(CommandSender sender, Command cmd,
    16. String commandLabel, String[] args) {
    17. if (commandLabel.equalsIgnoreCase("silence")) {
    18. Player player = (Player) sender;
    19. if (player.hasPermission(new Permissions().All)
    20. || player.hasPermission(new Permissions().Silence)) {
    21. if(isS.contains(player.getName())){
    22. isS.remove(player);
    23. player.sendMessage(ChatColor.DARK_GRAY + "[" + ChatColor.GRAY
    24. + "Silence" + ChatColor.DARK_GRAY + "] "
    25. + ChatColor.GRAY + "Your chat has now been un-silenced!");
    26. } else {
    27. player.sendMessage(ChatColor.DARK_GRAY + "[" + ChatColor.GRAY
    28. + "Silence" + ChatColor.DARK_GRAY + "] "
    29. + ChatColor.GRAY + "Your chat has now been silenced!");
    30. isS.add(player);
    31. }
    32. } else {
    33. player.sendMessage(ChatColor.DARK_GRAY + "[" + ChatColor.GRAY
    34. + "Silence" + ChatColor.DARK_GRAY + "] "
    35. + ChatColor.GRAY + "You do not have permission to silence chat!");
    36. }
    37. }
    38. return false;
    39. }
    40.  
    41. //ArrayList
    42. ArrayList<Player> isS= new ArrayList<Player>();
     
  17. Offline

    Drkmaster83

    You're using PlayerChatEvent, which is and has been deprecated since about 1.2.5. Use AsyncPlayerChatEvent.
     
    Axe2760 likes this.
  18. Offline

    Deleted user

  19. Offline

    Drkmaster83

    Sorry for a double post, but the edit button won't actually allow me to edit at this time.

    On top of that, your event is doing it in an improper way.

    Code:
    @EventHandler
    public void onAsyncPlayerChat(AsyncPlayerChatEvent event)
    {
      for(Player p : Bukkit.getOnlinePlayers())
      {
        if(isS.contains(p.getName())
        {
          event.getRecipients().remove(p);
        }
      }
    }
    Lastly, change your ArrayList<Player> to an ArrayList<String>, because of my signature. NEVER, EVER, STORE A PLAYER OBJECT IN A LIST/MAP/SET.
     
    Deleted user likes this.
  20. Offline

    Deleted user

    Drkmaster83

    Thank you...would that all be why it wasn't working?

    And can you also explain what the Player p : Bukkit.getOnlinePlayers() does?


    Forgot this...
    Would you happen to know how to take p's name...convert it to the first 3 characters, then check if the message contains those characters?

    JHG0
    PHP:
    e.setCancelled(true);
    Set<Playerrecipients = new HashSet<Player>();
    for(
    Player p Bukkit.getOnlinePlayers()){
        if(!
    isS.contains(p)){
            
    recipients.add(p);
        }
    }
    AsyncPlayerChatEvent event = new AsyncPlayerChatEvent(truee.getPlayer(), e.getMessage(), recipients);
    plugin.getServer().getPluginManager().callEvent(event);
    recipients.clear();
    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Nov 25, 2021
  21. Offline

    Drkmaster83

    The Player p : Bukkit.getOnlinePlayers() in a for-loop is called a "for-each" loop. It allows you to use a single object variable to access a whole array of variables. "p" represents every online player.

    String newString = p.getName().substring(0, 2); should work in taking the first 3 characters. If not, change the 2 to a 3, and yeah.
    Code:
    if(event.getMessage().split(" ").contains(newString))
    {
      //Do something, preferably.
    }
    
    Err, not .contains(), but make a for-each loop using the event.getMessage().split(" ") as the array. Check if the String variable that you create in the for-each loop is equal to newString.

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

    Deleted user

    Drkmaster83
    Hrmm?

    Dark,

    Thanks for fixing the main code, it works now :)

    Drkmaster83

    Wait so how should I do it?

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

    Drkmaster83

    What do you mean?
     
  24. Offline

    Deleted user

    Drkmaster83

    How would I check and see if the chat message contains the first 3-5 letters of a player's name and check if they (the mentioned player) are on the hashmap.
     
  25. Offline

    Deleted user

    Bump

    Can anyone help with the issue directly above this post?
    Assist
     
  26. JHG0
    Loop through the names in map, and check if the message contains the index 3-5 of name from map.
     
  27. Offline

    Deleted user

    Assist
    The players are stored in a HashMap
     
  28. Yeah, sorry. I edited my post just before you posted yours.
     
  29. Offline

    Deleted user

    Assist

    If you hang on a minute I'll post the code
    I tried some things but am confused

    Assist
    Code:java
    1. ArrayList<String> isS= new ArrayList<String>();
    2.  
    3. @EventHandler
    4. public void onAsyncPlayerChat(AsyncPlayerChatEvent e){
    5. Player chatter = e.getPlayer();
    6. for(Player p : Bukkit.getOnlinePlayers()){
    7. if((chatter.getName() == p.getName()) && isS.contains(p.getName())){
    8. isS.remove(p.getName());
    9. p.sendMessage(ChatColor.DARK_GRAY + "[" + ChatColor.GRAY
    10. + "Silence" + ChatColor.DARK_GRAY + "] "
    11. + ChatColor.GRAY + "Your chat has now been un-silenced!");
    12. } else {
    13. if(isS.contains(p.getName())){
    14. e.getRecipients().remove(p);
    15. }
    16. }
    17. }
    18. }


    So, I want it to be if a PLAYER says the first three letters of a SILENCED player's name, it sends them a message...but if a STAFF (it'll just be permission based) says the first three letters of a SILENCED player's name, it sends the silenced player that message and un-mutes chat

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 4, 2016
  30. JHG0
    You shouldn't compare strings with '=='. I'd just check if index 1-3 of 'p' name is equal to the event message, if so, I'd build the whole name with Bukkit.matchPlayer(string of index 1-3), and do stuff. I'll attempt to write some code for you later, it's really late here where I live.
     
Thread Status:
Not open for further replies.

Share This Page