Solved Why isnt the player getting muted?

Discussion in 'Plugin Development' started by football70500, Sep 26, 2013.

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

    football70500

    I made a mute command and i have everything set up, however the target specified doesn't get muted, it sends the message but doesn't mute them.


    Heres my code:

    Main class:
    Code:java
    1. public class KJustShutUp extends JavaPlugin{
    2. public ArrayList<String> mute = new ArrayList<String>();
    3. private static KJustShutUp instance;
    4.  
    5. @Override
    6. public void onDisable()
    7. {
    8. getLogger().info("kJustShutUp has been disabled!");
    9. instance = null;
    10. }
    11.  
    12. @Override
    13. public void onEnable()
    14. {
    15. instance = this;
    16. getLogger().info("kJustShutUp has been enabled!");
    17. register();
    18. getConfig().addDefault("kJustShutUp.muted", new String("false"));
    19. getConfig().addDefault("kJustShutUp.mute-message", new String("&aThe chat was muted by %player"));
    20. getConfig().addDefault("kJustShutUp.unmute-message", new String("&aThe chat was unmuted by %player"));
    21. getConfig().addDefault("kJustShutUp.muted-message", new String("&cThe chat is currently muted!"));
    22. getConfig().options().copyDefaults(true);
    23. saveConfig();
    24.  
    25. PluginManager pm = getServer().getPluginManager();
    26. }
    27. public void register() {
    28. getCommand("mute").setExecutor(new MuteCommand(this));
    29. getCommand("unmute").setExecutor(new MuteCommand(this));
    30. getCommand("silence").setExecutor(new MuteItCommand(this));
    31. getCommand("clearchat").setExecutor(new ClearChatCommands(this));
    32. getServer().getPluginManager().registerEvents(new ChatListener(this), this);
    33. }
    34. public static void getCommand(String command, CommandExecutor commandexecutor) {
    35. Bukkit.getServer().getPluginCommand(command).setExecutor(commandexecutor);
    36. }
    37. public static KJustShutUp getInstance(){
    38. return instance;
    39. }
    40. }
    41.  


    My command class:
    Code:java
    1. public class MuteCommand implements CommandExecutor{
    2.  
    3. private KJustShutUp plugin;
    4.  
    5. public MuteCommand(KJustShutUp plugin)
    6. {
    7. this.plugin = plugin;
    8. }
    9.  
    10. public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args){
    11. Player target = null;
    12. Player send = (Player) sender;
    13. if(cmd.getName().equalsIgnoreCase("mute") && send.hasPermission("kjustshutup.mute") || send.isOp()){
    14. target = Bukkit.getServer().getPlayer(args [0]);
    15. if(args.length == 1){
    16. KJustShutUp.getInstance().mute.add(target.getDisplayName());
    17. send.sendMessage(ChatColor.GREEN + target.getDisplayName() + " has been muted!");
    18. target.sendMessage(ChatColor.GREEN + "You have been muted!");
    19. }
    20. }
    21. if(cmd.getName().equalsIgnoreCase("unmute") && send.hasPermission("kjustshutup.mute")){
    22. if(args.length == 1){
    23. target = Bukkit.getServer().getPlayer(args [0]);
    24. if(KJustShutUp.getInstance().mute.contains(target.getDisplayName())){
    25. KJustShutUp.getInstance().mute.remove(target.getDisplayName());
    26. send.sendMessage(ChatColor.GREEN + target.getDisplayName() + " has been unmuted!");
    27. target.sendMessage(ChatColor.GREEN + "You have been unmuted!");
    28. }
    29. }
    30. }
    31. return false;
    32. }
    33.  
    34.  
    35. }
    36.  


    My ChatListener:
    Code:java
    1. package me.cheezburger7.kjustshutup;
    2.  
    3. import java.util.ArrayList;
    4. import java.util.HashMap;
    5.  
    6. import org.bukkit.ChatColor;
    7. import org.bukkit.block.Block;
    8. import org.bukkit.event.EventHandler;
    9. import org.bukkit.event.EventPriority;
    10. import org.bukkit.event.Listener;
    11. import org.bukkit.event.player.AsyncPlayerChatEvent;
    12.  
    13. public class ChatListener implements Listener{
    14. private KJustShutUp plugin;
    15. public ChatListener(KJustShutUp plugin)
    16. {
    17. this.plugin = plugin;
    18. }
    19.  
    20. @EventHandler(priority=EventPriority.HIGHEST)
    21. public void onPlayerChat(AsyncPlayerChatEvent e) {
    22. if ((!e.getMessage().toLowerCase().startsWith("/")) && (!e.getMessage().toLowerCase().equals("/")) && (!e.getPlayer().isOp()) && (!e.getPlayer().hasPermission("kjustshutup.chat")) && (!this.plugin.getConfig().getString("KJustShutUp.muted").equalsIgnoreCase("false")))
    23. {
    24. if (this.plugin.getConfig().getString("KJustShutUp.muted").equalsIgnoreCase("true")) {
    25. e.setCancelled(true);
    26. e.getPlayer().sendMessage(this.plugin.getConfig().getString("KJustShutUp.muted-message"));
    27. }
    28. }
    29. if(KJustShutUp.getInstance().mute.contains(e.getPlayer().getName())){
    30. e.getPlayer().sendMessage(ChatColor.GREEN + "You can't speak while muted!");
    31. e.setCancelled(true);
    32. }
    33. }
    34. }
    35.  
     
  2. Offline

    james137137

    have you thought about using a hashmap or something to store if they are muted or not?
    somewhere along the line e.setCancelled(true); isn't being called. I am personally having a hard time reading your code. (lines are too long)
     
  3. Offline

    amhokies

    Try just making the ArrayList static, then you can access it from your command class with just KJustShutUp.mute.
     
  4. Offline

    football70500

    I had a hashmap for a while but my code wasnt working with it so I switched to an arraylist

    still nope :/

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

    james137137

    well EventPriority isn't that important as all the plugin get a turn so you could leave it to normal priority.

    (I suggest debugging with lots of System.out/println e.g. System.out/println("debug: Line # - player has been muted");

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

    amhokies

    Well, I copied and pasted your code exactly into eclipse and ran it on my server, and it works fine.
     
  7. Offline

    football70500

    ... the mute does?
     
  8. Offline

    amhokies

    Yes, when I try to talk it just says "You are muted" or something.
     
  9. Offline

    football70500

    amhokies Give me your exact code in there
     
  10. Offline

    amhokies

    It's the exact thing you have in your first post, except that you didn't provide the other two command executors, so I just left them out of the main class. I literally copy and pasted what you have into eclipse, then deleted those two lines.
     
  11. Offline

    football70500

    ... thats so weird, then how does it work for you but not for me?
     
  12. Offline

    xCyanide

    football70500
    It could be some sort of compiling issue or something. Try to delete the project and re create it(I am not responsible if you delete it and something gets screwed up, this is just a suggestion)
     
  13. Offline

    amhokies

    Well, I don't see how it would be the case, but it could be the other two command executors. Comment out lines 30 and 31, then try it.
     
  14. Offline

    football70500

    amhokies SOLVED IT ESSENTIALS WAS MY PROBLEM!

    Now however how can I get it to unmute them? I this is my code currently :

    Code:java
    1. public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args){
    2. Player target = null;
    3. Player send = (Player) sender;
    4. if(cmd.getName().equalsIgnoreCase("mute") && send.hasPermission("kjustshutup.mute") || send.isOp()){
    5. target = Bukkit.getServer().getPlayer(args [0]);
    6. if(args.length == 1){
    7. KJustShutUp.getInstance().mute.add(target.getDisplayName());
    8. send.sendMessage(ChatColor.GREEN + target.getDisplayName() + " has been muted!");
    9. target.sendMessage(ChatColor.GREEN + "You have been muted!");
    10. }else if(KJustShutUp.getInstance().mute.contains(target.getDisplayName())){
    11. target = Bukkit.getServer().getPlayer(args [0]);
    12. KJustShutUp.getInstance().mute.remove(target.getDisplayName());
    13. send.sendMessage(ChatColor.GREEN + target.getDisplayName() + " has been unmuted!");
    14. target.sendMessage(ChatColor.GREEN + "You have been unmuted!");
    15. }
    16. }
    17. return false;
    18. }
    19.  
    20. }
    21.  

    It just mutes them it sends the message: you have been muted! still and they cant talk

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

    amhokies

    By doing /mute again, it should toggle being muted and being unmuted.
     
  16. Offline

    football70500

    but it doesn't :/ it just sends the message: "you have been muted!" and doesnt let them talk and sends them the message you cant speak when muted! when they try to talk

    Someone help, doing /mute again won't unmute the player if hes already muted :/

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

    Wazup93

    I didn't read any of your code But when I wanna make something like this .. I use Arraylists ..
    it would be like if the name of player is in the array list .. it removes it (Remove = Un Mute) And if the player name wasn't so it adds it (Add = Mute) This is the only way for me :) Wish I helped
    The Killer
     
  18. Offline

    football70500

    How can I fix this?

    Wazup93 amhokies how can I fix this

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

    Wazup93

    Sorry But I was very bust .. Exams :)
    The way is very easy .. All you need to do is to make
    protected ArrayList<String> Muted;
    And you get the PlayerName As string : String playerName = player.getName();
    And when the command is made like /mute it checks if the playerName is in the arraylist or no .. Check command : if(plugin.Muted.contains(playerName)) {
    Codes when the player is muted ..
    }else{
    Code When player in NOT Muted
    }
    And if you got an error this should be from the plugin. ( if it was in your main class so put it .. this. ... and maybe you don't even need it in your main class )
    The Killer
     
Thread Status:
Not open for further replies.

Share This Page