MuteAll Plugin Won't Work

Discussion in 'Plugin Development' started by Blah1, Oct 5, 2013.

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

    Blah1

    Ok, when I do /muteall it says: "MirrorRealm muted the public chat" and when I do it again, instead of saying "MirrorRealm unmuted the public chat," it says that I muted it again. Also, it doesn't even mute the players (I tried it with my alt). And /unmute all just keeps saying "The public chat is not muted" Frustrated :p

    MuteAll:
    Code:java
    1. package me.MirrorRealm.kadmin;
    2.  
    3. import java.util.HashMap;
    4.  
    5. import org.bukkit.Bukkit;
    6. import org.bukkit.ChatColor;
    7. import org.bukkit.command.Command;
    8. import org.bukkit.command.CommandExecutor;
    9. import org.bukkit.command.CommandSender;
    10. import org.bukkit.entity.Player;
    11. import org.bukkit.event.EventHandler;
    12. import org.bukkit.event.Listener;
    13. import org.bukkit.event.player.AsyncPlayerChatEvent;
    14.  
    15. public class MuteAll implements CommandExecutor, Listener{
    16. public final HashMap<Player, Player> mute = new HashMap<Player, Player>();
    17. @EventHandler
    18. public void onPlayerChatEvent(AsyncPlayerChatEvent event){
    19. Player player = event.getPlayer();
    20. if(mute.containsKey(player)){
    21. event.setCancelled(true);
    22. }
    23. }
    24. public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args){
    25. Player player = (Player) sender;
    26. if (cmd.getName().equalsIgnoreCase("muteall")){
    27. if (!(player.hasPermission("command.muteall"))){
    28. player.sendMessage(ChatColor.RED + "You do not have permission.");
    29. return true;
    30. }
    31. if (args.length >= 1){
    32. player.sendMessage(ChatColor.RED + "/muteall");
    33. return true;
    34. }
    35. for (Player p : Bukkit.getOnlinePlayers()){
    36. if (mute.containsKey(p)){
    37. if (p.hasPermission("command.mute.exempt"))
    38. return true;
    39. }else{
    40. mute.remove(p);
    41. Bukkit.broadcastMessage(player.getDisplayName() + ChatColor.RED + " muted the public chat.");
    42. return true;
    43. }
    44. if (!(mute.containsKey(p))){
    45. if (p.hasPermission("command.mute.exempt"))
    46. return true;
    47. }else{
    48. mute.put(p, null);
    49. Bukkit.broadcastMessage(player.getDisplayName() + ChatColor.RED + " unmuted the public chat.");
    50. return true;
    51. }
    52. }
    53. }
    54. if (cmd.getName().equalsIgnoreCase("unmuteall")){
    55. if (!(player.hasPermission("command.unmuteall"))){
    56. player.sendMessage(ChatColor.RED + "You do not have permission.");
    57. return true;
    58. }
    59. if (args.length >= 1){
    60. player.sendMessage(ChatColor.RED + "/unmuteall");
    61. return true;
    62. }
    63. for (Player p : Bukkit.getOnlinePlayers()){
    64. if (!(mute.containsKey(p))){
    65. player.sendMessage(ChatColor.RED + "The public chat is not muted");
    66. return true;
    67. }
    68. if (mute.containsKey(p)){
    69. mute.remove(p);
    70. Bukkit.broadcastMessage(player.getDisplayName() + ChatColor.RED + " unmuted the public chat.");
    71. return true;
    72. }
    73. }
    74. }
    75. return true;
    76. }
    77. }
    78.  


    Main:
    Code:java
    1. package me.MirrorRealm.kadmin;
    2.  
    3. import org.bukkit.Bukkit;
    4. import org.bukkit.plugin.java.JavaPlugin;
    5.  
    6. public class Main extends JavaPlugin{
    7. public void onEnable(){
    8. Bukkit.getServer().getLogger().info("kAdmin has been enabled");
    9. getCommand("ban").setExecutor(new Ban());
    10. getCommand("broadcast").setExecutor(new Broadcast());
    11. getCommand("clear").setExecutor(new Clear());
    12. getCommand("feed").setExecutor(new Feed());
    13. getCommand("fly").setExecutor(new Fly());
    14. getCommand("speed").setExecutor(new Fly());
    15. getCommand("gamemode").setExecutor(new Gamemode());
    16. getCommand("heal").setExecutor(new Heal());
    17. getCommand("k").setExecutor(new K());
    18. getCommand("unban").setExecutor(new Unban());
    19. getCommand("tp").setExecutor(new Teleport());
    20. getCommand("cc").setExecutor(new ClearChat());
    21. getCommand("s").setExecutor(new S());
    22. getCommand("help").setExecutor(new Help(this));
    23. getCommand("kill").setExecutor(new Kill());
    24. getCommand("suicide").setExecutor(new Kill());
    25. getCommand("mute").setExecutor(new Mute());
    26. getCommand("unmute").setExecutor(new Mute());
    27. getCommand("muteall").setExecutor(new MuteAll());
    28. getCommand("unmuteall").setExecutor(new MuteAll());
    29. getCommand("msg").setExecutor(new Message());
    30. getCommand("r").setExecutor(new Message());
    31. Bukkit.getServer().getPluginManager().registerEvents(new Mute(), this);
    32. Bukkit.getServer().getPluginManager().registerEvents(new MuteAll(), this);
    33. Bukkit.getServer().getPluginManager().registerEvents(new Message(), this);
    34. getConfig().options().copyDefaults(true);
    35. saveConfig();
    36. }
    37. }


    Sorry if I'm posting a lot :p I'm still learning and need a lot of help.
     
  2. Offline

    adam753

    Blah1 likes this.
  3. Offline

    Goblom

    Ive never had much luck with AsyncPlayerChatEvent... You could try the depricated PlayerChatEvent...

    Code:java
    1. @EventHandler
    2. public void onPlayerChatEvent(PlayerChatEvent event){
    3. Player player = event.getPlayer();
    4. if(mute.containsKey(player)){
    5. event.setCancelled(true);
    6. }
    7. }


    Edit: Not Recommended
     
  4. Offline

    Blah1

    adam753 That worked for the Mute class but the MuteAll class still doesn't work. I think it has to do with muting EVERYONE on the server.

    Btw thanks for helping my on the Mute command :D
     
  5. Offline

    Goblom

    You muteall command doesnt add anyone to the HashMap... you are actually removing players from the hashmap.

    Change the for loop for /muteall to...

    Code:java
    1. for (Player p : Bukkit.getOnlinePlayers()) {
    2. if (!mute.containsKey(p)) {
    3. if (!p.hasPermission("command.mute.exempt")) {
    4. mute.put(p, null);
    5. p.sendMessage(ChatColor.RED + "You have been muted in the public chat.");
    6. }
    7. } else if (mute.containsKey(p)) {
    8. mute.remove(p);
    9. p.sendMessage(ChatColor.RED + "You have been unmuted in the public chat.");
    10. }
    11. }


    That is basically a condensed version of your fixed code. It is faster and in my opinion easier to read..

    There is also no abundance of "returns true" which normally ends the command ;)
     
  6. Offline

    Loogeh

    Blah1 Assuming that you're trying to do this so you can disable chat or something along those lines, this isn't a very good way to do it. A couple reasons why are
    • That there is no need to loop through every player and mute them, that's unnecessary
    • Players who join afterwards will be unmuted if they weren't previously muted therefore they'll be able to talk
    A much better way to do this is to have a boolean called chatEnable or something like that. Then, when a player uses the /muteall command the chatEnable boolean will be set to false meaning the chat is disabled. Next, in an AsyncPlayerChatEvent you would check if chatEnable is set to false and if that returns true, cancel the event and do whatever you want to after that.

    If this isn't what you were intending this command to be used for, just ignore this post.
     
  7. Offline

    Blah1

    Loogeh
    ok I did this, but it still doesn't work :(

    Code:java
    1. package me.MirrorRealm.kadmin;
    2.  
    3. import org.bukkit.Bukkit;
    4. import org.bukkit.ChatColor;
    5. import org.bukkit.command.Command;
    6. import org.bukkit.command.CommandExecutor;
    7. import org.bukkit.command.CommandSender;
    8. import org.bukkit.entity.Player;
    9. import org.bukkit.event.EventHandler;
    10. import org.bukkit.event.Listener;
    11. import org.bukkit.event.player.AsyncPlayerChatEvent;
    12.  
    13. public class MuteAll implements CommandExecutor, Listener{
    14. @EventHandler
    15. public void onPlayerChatEvent(AsyncPlayerChatEvent event){
    16. Player player = event.getPlayer();
    17. if(chatEnable == true){
    18. event.setCancelled(true);
    19. player.sendMessage(ChatColor.RED + "The public chat is muted");
    20. }
    21. }
    22. boolean chatEnable;
    23. public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args){
    24. Player player = (Player) sender;
    25. if (cmd.getName().equalsIgnoreCase("muteall")){
    26. if (!(player.hasPermission("command.muteall"))){
    27. player.sendMessage(ChatColor.RED + "You do not have permission.");
    28. return true;
    29. }
    30. if (args.length >= 1){
    31. player.sendMessage(ChatColor.RED + "/muteall");
    32. return true;
    33. }
    34. chatEnable = true;
    35. Bukkit.getServer().broadcastMessage(player.getDisplayName() + ChatColor.RED + " muted the public chat.");
    36. }
    37. if (cmd.getName().equalsIgnoreCase("unmuteall")){
    38. if (!(player.hasPermission("command.unmuteall"))){
    39. player.sendMessage(ChatColor.RED + "You do not have permission.");
    40. return true;
    41. }
    42. if (args.length >= 1){
    43. player.sendMessage(ChatColor.RED + "/unmuteall");
    44. return true;
    45. }
    46. if (chatEnable != true){
    47. player.sendMessage(ChatColor.RED + "The public chat is not muted.");
    48. return true;
    49. }else if (chatEnable == true){
    50. chatEnable = false;
    51. Bukkit.getServer().broadcastMessage(player.getDisplayName() + ChatColor.RED + " unmuted the public chat.");
    52. }
    53. }
    54. return true;
    55. }
    56. }
    57.  
     
  8. Offline

    Loogeh

    Blah1 Only thing I can think of is that you didn't register the event properly. This is how I do it

    I have a variable for my main class and a constructor for the listener class like this
    Code:
    public static YourMainClass plugin;
        public YourListener(YourMainClass instance) {
            plugin = instance;
        }
    Then in onEnable() I use this

    Code:
    PluginManager pm = getServer().getPluginManager();
    pm.registerEvents(new YourListener (this), this);
     
    Blah1 likes this.
  9. Offline

    Blah1

    Loogeh Works now! Thanks!
    One question: how do it allow chatting for people with the permission command.muteall.exempt
     
  10. Offline

    Loogeh

    Blah1 No worrries :D As for your question, in your AsyncPlayerChatEvent check if the player has the command.muteall.exempt permission and if they do, use event.setCancelled(false).
     
  11. Offline

    Blah1

    Loogeh Thanks :D Lllaaaast thing :p When I do /muteall it should "broadcast" that the chat is muted but it only tells me...

    Code:java
    1. package me.MirrorRealm.kadmin;
    2.  
    3. import org.bukkit.Bukkit;
    4. import org.bukkit.ChatColor;
    5. import org.bukkit.command.Command;
    6. import org.bukkit.command.CommandExecutor;
    7. import org.bukkit.command.CommandSender;
    8. import org.bukkit.entity.Player;
    9. import org.bukkit.event.EventHandler;
    10. import org.bukkit.event.Listener;
    11. import org.bukkit.event.player.AsyncPlayerChatEvent;
    12.  
    13. public class MuteAll implements CommandExecutor, Listener{
    14. public static Main plugin;
    15. public MuteAll(Main instance){
    16. plugin = instance;
    17. }
    18. @EventHandler
    19. public void onPlayerChatEvent(AsyncPlayerChatEvent event){
    20. Player player = event.getPlayer();
    21. if(chatEnable == true){
    22. event.setCancelled(true);
    23. player.sendMessage(ChatColor.RED + "The public chat is muted.");
    24. }
    25. if(player.hasPermission("command.muteall.exempt")){
    26. event.setCancelled(false);
    27. }
    28. }
    29. boolean chatEnable;
    30. public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args){
    31. Player player = (Player) sender;
    32. if (cmd.getName().equalsIgnoreCase("muteall")){
    33. if (!(player.hasPermission("command.muteall"))){
    34. player.sendMessage(ChatColor.RED + "You do not have permission.");
    35. return true;
    36. }
    37. if (args.length >= 1){
    38. player.sendMessage(ChatColor.RED + "/muteall");
    39. return true;
    40. }
    41. if (chatEnable == true){
    42. chatEnable = false;
    43. Bukkit.broadcastMessage(player.getDisplayName() + ChatColor.RED + " unmuted the public chat.");
    44. return true;
    45. }
    46. if (chatEnable == false){
    47. chatEnable = true;
    48. Bukkit.broadcastMessage(player.getDisplayName() + ChatColor.RED + " muted the public chat.");
    49. return true;
    50. }
    51. }
    52. if (cmd.getName().equalsIgnoreCase("unmuteall")){
    53. if (!(player.hasPermission("command.unmuteall"))){
    54. player.sendMessage(ChatColor.RED + "You do not have permission.");
    55. return true;
    56. }
    57. if (args.length >= 1){
    58. player.sendMessage(ChatColor.RED + "/unmuteall");
    59. return true;
    60. }
    61. if (chatEnable != true){
    62. player.sendMessage(ChatColor.RED + "The public chat is not muted.");
    63. return true;
    64. }else if (chatEnable == true){
    65. chatEnable = false;
    66. for (Player o : Bukkit.getOnlinePlayers()){
    67. o.sendMessage(player.getDisplayName() + ChatColor.RED + " unmuted the public chat.");
    68. }
    69. }
    70. }
    71. return true;
    72. }
    73. }
    74.  


    Line 43 and 48.

    I've noticed this happening with some other plugins too... I tried:
    Code:java
    1. for (Player o : Bukkit.getOnlinePlayers()){
    2. o.sendMessage(" ");
    3. }

    but that didn't work either...
     
  12. Offline

    jimuskin

    Blah1
    for the broadcasting messages, try
    Code:java
    1. Bukkit.getServer().broadcastMessage(message);


    EDIT:
    Also try
    Code:java
    1. plugin.getServer().broadcastMessage(message);
     
  13. Offline

    Blah1

    That didn't work either :/
     
  14. Offline

    Loogeh

    Blah1 What do you mean it didn't work? What happened when you tried?
     
  15. Offline

    Blah1

    Loogeh Still only message me. It doesn't broadcast it to other players.
     
  16. Offline

    mazentheamazin

    Blah1
    I am not sure whats wrong. However To broadcast a message I use this
    Code:java
    1. Bukkit.broadcastMessage("Message Here");

    This works like a charm for me.
    You can try this:
    Code:java
    1. plugin.getServer().broadcastMessage("Message");
    2. //However I have not tested that.


    Good luck and Happy Coding,
    Mazen
     
  17. Offline

    Loogeh

    Blah1 How do you know that it only sends it to you?
     
  18. Offline

    Blah1

    I have an alt and it doesn't show up there.
     
  19. Offline

    Loogeh

    Blah1 post the code please
     
  20. Offline

    Blah1

    Loogeh
    Code:java
    1. package me.MirrorRealm.kadmin;
    2.  
    3. import org.bukkit.Bukkit;
    4. import org.bukkit.ChatColor;
    5. import org.bukkit.command.Command;
    6. import org.bukkit.command.CommandExecutor;
    7. import org.bukkit.command.CommandSender;
    8. import org.bukkit.entity.Player;
    9. import org.bukkit.event.EventHandler;
    10. import org.bukkit.event.Listener;
    11. import org.bukkit.event.player.AsyncPlayerChatEvent;
    12.  
    13. public class MuteAll implements CommandExecutor, Listener{
    14. public static Main plugin;
    15. public MuteAll(Main instance){
    16. plugin = instance;
    17. }
    18. @EventHandler
    19. public void onPlayerChatEvent(AsyncPlayerChatEvent event){
    20. Player player = event.getPlayer();
    21. if(chatEnable == true){
    22. event.setCancelled(true);
    23. player.sendMessage(ChatColor.RED + "The public chat is muted.");
    24. }
    25. if(player.hasPermission("command.muteall.exempt")){
    26. event.setCancelled(false);
    27. }
    28. }
    29. boolean chatEnable;
    30. public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args){
    31. Player player = (Player) sender;
    32. if (cmd.getName().equalsIgnoreCase("muteall")){
    33. if (!(player.hasPermission("command.muteall"))){
    34. player.sendMessage("Unknown command. Type " + '"' + "/help" + '"' + " for help.");
    35. return true;
    36. }
    37. if (args.length >= 1){
    38. player.sendMessage(ChatColor.RED + "/muteall");
    39. return true;
    40. }
    41. if (chatEnable == true){
    42. Bukkit.getServer().broadcastMessage(player.getDisplayName() + ChatColor.RED + " unmuted the public chat.");
    43. chatEnable = false;
    44. return true;
    45. }
    46. if (chatEnable == false){
    47. Bukkit.getServer().broadcastMessage(player.getDisplayName() + ChatColor.RED + " muted the public chat.");
    48. chatEnable = true;
    49. return true;
    50. }
    51. }
    52. if (cmd.getName().equalsIgnoreCase("unmuteall")){
    53. if (!(player.hasPermission("command.unmuteall"))){
    54. player.sendMessage("Unknown command. Type " + '"' + "/help" + '"' + " for help.");
    55. return true;
    56. }
    57. if (args.length >= 1){
    58. player.sendMessage(ChatColor.RED + "/unmuteall");
    59. return true;
    60. }
    61. if (chatEnable != true){
    62. player.sendMessage(ChatColor.RED + "The public chat is not muted.");
    63. return true;
    64. }else if (chatEnable == true){
    65. Bukkit.getServer().broadcastMessage(player.getDisplayName() + ChatColor.RED + " unmuted the public chat.");
    66. chatEnable = false;
    67. }
    68. }
    69. return true;
    70. }
    71. }
    72.  
     
  21. Offline

    holysteward

    @Blah1 the if statement on line 37 probably shouldn't return true yet, its causing you to leave before checking the "if (chatEnable == true)" section.

    Little tip, you don'r have to check if a Boolean resolves to true or false just by itself, it is the true/false
     
  22. Offline

    Goblom

    Blah1 Expanding on what holysteward said about checking if true if false... you could just do
    Code:java
    1. if (chatEnable) {
    2. // what to do if chat is enabled
    3. } else {
    4. // what to do if chat is not enabled
    5. }
     
  23. Offline

    Blah1

    holysteward Goblom
    Well the code works fine. It's just the broadcast thing that isn't working (it only sends it to the command's sender).
     
  24. Offline

    Goblom

    Blah1 Well yes.. Because you have
    Code:java
    1. player.sendMessage(ChatColor.RED + "The public chat is not muted.");
    2.  
    3. player.sendMessage(ChatColor.RED+"The public chat is muted.");
    Which will only send it to a single player.

    Use
    Code:java
    1. Bukkit.broadcast("");
    to send it to every player on the server
     
  25. Offline

    Blah1

    Goblom
    No those are supposed to be sent to the player. The problem is that it wont broadcast the message on line 42 and 47. It just sends that message to the sender.
     
  26. Offline

    billman555555

    To mute all players just do:
    Code:java
    1. boolean muted = false;

    Then in onCommand for /mute do:
    Code:java
    1. if (muted == false) {
    2. muted = true;
    3. } else {
    4. muted = false;
    5. }

    In your AsyncPlayerChatEvent do:
    Code:java
    1. event.setCancelled(muted);
     
  27. Offline

    Blah1

    billman555555 Yeah...but again, the code works fine. It's just the bukkit.broadcastMessage that doesn't work
     
Thread Status:
Not open for further replies.

Share This Page