Mute Command

Discussion in 'Spigot Plugin Development' started by _xXProDudeXx_, May 29, 2016.

  1. Hello guys,

    I am coding a BanManager, and in that Manager, I also want to be able to Mute players. To do this, I coded the following class:

    Code (Text):
    package banmanager.Commands;

    import java.util.ArrayList;

    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.AsyncPlayerChatEvent;

    import banmanager.Strings.Strings;
    import me.ZeusReksYou_.BanManager.BanManager;
    import me.ZeusReksYou_.BanManager.Enforcer;

    public class MuteCommand implements CommandExecutor, Listener {
       
        public BanManager plugin;
        public MuteCommand(BanManager plugin){
            this.plugin = plugin;
        }
       
        ArrayList<String> Muted = new ArrayList<String>();

        @EventHandler
        public void PlayerChatEvent(AsyncPlayerChatEvent event) {
            Player target = event.getPlayer();
            if (plugin.getConfig().getStringList("Muted Players").contains(target)){
                event.setCancelled(true);
                event.getPlayer().sendMessage(Strings.MuteMsg);
            }
        }

        @SuppressWarnings("deprecation")
        @Override
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
            Player s = (Player) sender;
            Player target = Bukkit.getPlayer(args[0]);
            if (cmd.getName().equalsIgnoreCase("bmmute")) {
                if (!(s.hasPermission("bm.mod"))) {
                    if (s instanceof Player) {
                        s.sendMessage(Strings.ConsoleMsg);
                        return true;
                    }
                    s.sendMessage(Strings.NoPermMsg);
                }
                if (args.length < 1) {
                    s.sendMessage(Strings.MuteUsage);
                    return true;
                }
                if (target == null) {
                    s.sendMessage(Strings.SpecifyMsg);
                    return true;
                }
                String player = args[0];
                if (!(plugin.getConfig().getStringList("Muted Players").contains(target))) {
                    //plugin.getConfig().getStringList("Muted Players").add(player);
                    plugin.getConfig().addDefault("Muted Players", player);
                    s.sendMessage(Strings.HasMuted);
                    Bukkit.getPluginManager().callEvent(new Enforcer(target, me.ZeusReksYou_.BanManager.Type.MUTE));
                    for (Player staff : Bukkit.getOnlinePlayers()) {
                        if (staff.hasPermission("bm.admin")) {
                            staff.sendMessage(Strings.BMP + ChatColor.LIGHT_PURPLE + "The player " + target.getName()
                                    + " has muted by " + sender.getName());
                            return true;
                        }
                    }
                    return true;
                }
            }

            return true;
        }

    }
     
    As you can see, I tried to code it with a ArrayList, which didn't work. I also tried a Config-StringList, but that also didn't work. My problem seems to be that if I execute my Mute-Command, the player will recieve a message that he is muted, but still will be able to talk.

    How should I make that I can add players to something like a muted list, and then mute everybody on that list?

    ~ZeusReksYou_
     
  2. You should make an array list of UUIDs then check if the list contains the player's UUID, then on server stop save those UUIDs to the config and on server start load them from it and change the string list to a UUID list by iterating through it and doing UUID.fromString(String) then check if the player is on the list and do what you needa do(I'm thinking about making a ban-manager as well)
     
    • Agree Agree x 1
  3. What you're doing is right, but you are not creating the list in the config in the first place. Also, you didn't register your events.

    Make an onEnable() method and type something like this:
    Code (Java):
    getConfig().addDefault("muted", new ArrayList<String>());
    Then, you have to copy what you wrote over to the config file.
    Code (Java):
    getConfig().options().copyDefaults(true);
    saveConfig();
    reloadConfig();
    Then, somewhere else in the method, type
    Code (Java):
    getServer().getPluginManager().registerEvents(this, this)
    Then, in your chat method, where you are checking if the config contains the target's name, replace it with whatever you called it in addDefault and it should work :)
     
  4. Except he should use UUIDs not player names ;)
     
    • Agree Agree x 1
  5. Yes ;)
     
    • Agree Agree x 1
  6. @TechBug2012 @ExoticCode Thank you very much ! I am going to change that right now, and hope it will work. How to use UUID's btw?

    Just like player p = Bukkit.getPlayerUUID(); ?
     
  7. Player#getUniqueId() read up on UUIDs as well, they're pretty interesting.
    Edit: Sniped
     
  8. Hello !

    I now have this, however I know it is wrong. I just don't know how to fix this.

    Code (Text):
    package me.ZeusReksYou_.Mute;

    import org.bukkit.Bukkit;
    import org.bukkit.plugin.java.JavaPlugin;

    import java.util.ArrayList;
    import java.util.UUID;

    import org.bukkit.ChatColor;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.AsyncPlayerChatEvent;

    public class Mute extends JavaPlugin implements Listener {
       
        public void onEnable()
        {
            Bukkit.getPluginManager().registerEvents(this, this);
        }      
            ArrayList<String> Muted = new ArrayList<String>();

            @EventHandler
            public void PlayerChatEvent(AsyncPlayerChatEvent event) {
                Player target = event.getPlayer();
                if (getConfig().getStringList("Muted Players").contains(target)){
                    event.setCancelled(true);
                    event.getPlayer().sendMessage(ChatColor.RED + "You are muted");
                }
            }

            @SuppressWarnings("deprecation")
            @Override
            public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
                Player s = (Player) sender;
                Player target = Bukkit.getPlayer(args[0]);
                if (cmd.getName().equalsIgnoreCase("bmmute")) {
                    if (!(s.hasPermission("bm.mod"))) {
                        if (s instanceof Player) {
                            s.sendMessage(ChatColor.RED + "No Perm");
                            return true;
                        }
                        s.sendMessage(ChatColor.RED + "No Perm");
                    }
                    if (args.length < 1) {
                        s.sendMessage(ChatColor.RED + "/bmmute <player>");
                        return true;
                    }
                    if (target == null) {
                        s.sendMessage(ChatColor.RED + "Specify a player");
                        return true;
                    }
                    String MutePlayer = args[0];
                    if (!(getConfig().getStringList("Muted Players").contains(target))) {
                        //plugin.getConfig().getStringList("Muted Players").add(player);
                        getConfig().addDefault("Muted Players", MutePlayer);
                        s.sendMessage(ChatColor.GREEN + "You succesfully muted this player");
                        for (Player staff : Bukkit.getOnlinePlayers()) {
                            if (staff.hasPermission("bm.admin")) {
                                staff.sendMessage(ChatColor.LIGHT_PURPLE + "The player " + target.getName()
                                        + " has muted by " + sender.getName());
                                return true;
                            }
                        }
                        return true;
                    }
                }

                return true;
            }

        }
         
     
    I don't know what to do with the UUID parts.

    ~Zeus
     
  9. You're trying to pass a player through a string list, do Player#getName(); if you want to do it that way
     
  10. UUIDs are not that difficult, just instead of getPlayer().getName(), you use getUniqueId().toString(). Either way it return a string. Pogo has a very good explanation from when the switch was first announced: