Stop showing chat to specific player

Discussion in 'Plugin Development' started by aidan573, Nov 12, 2013.

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

    aidan573

    Hello, I'm trying to make a plugin that will hide all players when they type /quiet, In addition it will stop the player receiving chat, but recieveing chat does not work.

    When the payer is in Quiet, i.e /quiet playersnotshown is true

    Code:java
    1. @EventHandler (priority = EventPriority.MONITOR)
    2. public void chatEvent(final AsyncPlayerChatEvent event) {
    3.  
    4. Player Player = event.getPlayer();
    5.  
    6.  
    7. boolean playersnotshown = cfg.getBoolean( "visibility." + Player.getDisplayName() );
    8. event.getPlayer().sendMessage(ChatColor.GREEN + "" + playersnotshown + "");
    9.  
    10. for(Player players : Bukkit.getServer().getOnlinePlayers()){
    11. if (playersnotshown == true) {
    12. event.getRecipients().remove(players);
    13. event.setCancelled(true);
    14. event.getPlayer().sendMessage("You said a Thing");
    15.  
    16.  
    17. }
    18.  
    19. }
    20. }



    The test messages also don't show and the player is still recieveing chat from other players, what is wrong?
     
  2. Offline

    Nibbur

    Did you implement Listener and register events?
     
  3. Offline

    aidan573

    Yes,
    Code:java
    1. @Override
    2. public void onEnable(){
    3. Bukkit.getServer().getPluginManager().registerEvents(this, this);
    4. cfg = getConfig();
    5. getLogger().info("Lobby Visiblity plugin Enabled!");
    6. }
     
  4. Offline

    Noxyro

    1) You shouldn't load the value from the config everytime. Create a List with all players that have the visibility set to not shown.
    2) When you still want to do this over the config, don't use .getDisplayName() as this gets the modified player name (with colors and stuff) - use .getName() instead.
    3) You shouldn't start your variables with uppercase letters. (See: http://www.oracle.com/technetwork/java/javase/documentation/codeconvtoc-136057.html)

    4) When you cancel the AsyncPlayerChatEvent you cancel it for all players.
     
  5. Offline

    aidan573

    Noxyro I am now using getName(); and understand when you say I cancel it for all players, which i see. But my problem is that is does not cancel anything, or show the messages.
    Code:java
    1. @EventHandler (priority = EventPriority.MONITOR)
    2. public void chatEvent(final AsyncPlayerChatEvent event) {
    3.  
    4. Player player = event.getPlayer();
    5.  
    6.  
    7. boolean playersnotshown = cfg.getBoolean( "visibility." + player.getName() );
    8. event.getPlayer().sendMessage(ChatColor.GREEN + "" + playersnotshown + "");
    9.  
    10. for(Player players : Bukkit.getServer().getOnlinePlayers()){
    11. if (playersnotshown == true) {
    12. event.getRecipients().remove(players);
    13. event.setCancelled(true);
    14. event.getPlayer().sendMessage("You said a Thing");
    15.  
    16.  
    17. }
    18.  
    19. }
    20. }
     
  6. Offline

    Noxyro

    There is also a PlayerCommandPreprocessEvent, might be that you have to cancel this instead, because AsyncPlayerChatEvent is triggered when the messages is already send (i guess - not sure about that).
     
  7. If you want to hide chat to make it quiet don't you do this (I am typing this from your code view, I would NEVER do it this way):
    Code:
    @EventHandler (ignoreCancelled = true)
    public void onPlayerChat(AsyncPlayerChatEvent event) {
        if (!cfg.getBoolean("visibility." + event.getPlayer().getName()) {
            event.setCancelled(true);
        } else {
            try {
                List<Player> playerRecipients = new ArrayList<Player>();
                for (Player recipient : event.getRecipients()) {
                    if (cfg.getBoolean("visibility." + event.getPlayer().getName())) playerRecipients.add(recipient);
                }
                event.getRecipients().clear();
                event.getRecipients().addAll(playerRecipients);
            } catch (Exception ex) {
            }
        }
    }
    
    Note: I did this off the top of my head, I don't know if the methods exist such as addAll() in a Set.

    If you wondering, I would have an ArrayList of hidden players and then do:
    Code:
    if (this.hiddenPlayers.contains(event.getPlayer().getName()) {
     
  8. Offline

    aidan573

    KingFaris11 Thanks, how would you do it? because what you have given me, i have tried and It didn't work (No Errors, No Canceled messages)(Also, I added test messages to test the stages it got to, but it didn't show them either SEE below) so I am left to assume that AsyncPlayerChatEvent is not the event I'm looking for?
    Code:java
    1. //KingFaris11
    2.  
    3. @EventHandler (ignoreCancelled = true)
    4. public void onPlayerChat(AsyncPlayerChatEvent event) {
    5. event.getPlayer().sendMessage("You said a Thing no 1");
    6. if (cfg.getBoolean("visibility." + event.getPlayer().getName())) {
    7. event.getPlayer().sendMessage("You said a Thing no 2");
    8. event.setCancelled(true);
    9. } else {
    10. try {
    11. List<Player> playerRecipients = new ArrayList<Player>();
    12. for (Player recipient : event.getRecipients()) {
    13. if (!cfg.getBoolean("visibility." + event.getPlayer().getName())) playerRecipients.add(recipient);
    14. }
    15. event.getRecipients().clear();
    16. event.getRecipients().addAll(playerRecipients);
    17. event.getPlayer().sendMessage("You said a Thing");
    18. } catch (Exception ex) {
    19. } }
    20. }
     
  9. Offline

    Noxyro

  10. Offline

    aidan573

    Ok, what I'm trying to do is stop chat from being shown for players who have decided they don't want to see other players, i.e /quiet.
     
  11. This should definitely work unless there's something wrong with the configuration. Check if the boolean "getBoolean("visibility." + event.getPlayer().getName())" is actually returning false when the player is invisible. Also, I changed the code a tiny bit:

    Code:
    @EventHandler (ignoreCancelled = true)
    public void onPlayerChat(AsyncPlayerChatEvent event) {
        if (!cfg.getBoolean("visibility." + event.getPlayer().getName()) {
            event.setCancelled(true);
        } else {
            try {
                List<Player> playerRecipients = new ArrayList<Player>();
                for (Player recipient : event.getRecipients()) {
                    if (cfg.getBoolean("visibility." + event.getPlayer().getName())) playerRecipients.add(recipient);
                }
                event.getRecipients().clear();
                event.getRecipients().addAll(playerRecipients);
            } catch (Exception ex) {
            }
        }
    }
    
     
  12. Offline

    aidan573

    KingFaris11 Thanks so much, that works to a degree, but when a player is in /quiet all other players cant talk, and the player can, also the player cannot leave /quiet, I really appreciate your help you have practically done this for me and frankly without you I would be lost.
     
Thread Status:
Not open for further replies.

Share This Page