Muting a player trouble :(

Discussion in 'Plugin Development' started by thehutch, Oct 21, 2011.

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

    thehutch

    Ok so I have been requested to make a plugin which mutes a player but I am stuck on one part and I will probably start waffling ok so...
    This is my main class:
    Code:
    public class ZipIt extends JavaPlugin {
    
        public FileConfiguration config;
    
        @Override
        public void onDisable() {
            System.out.println("[ZipIt] has been disabled");
        }
    
        @Override
        public void onEnable() {
            setupConfig();
            PluginManager pm = getServer().getPluginManager();
            PluginDescriptionFile pdf = this.getDescription();
            getCommand("zipit").setExecutor(new CmdExecutor(this));
            pm.registerEvent(Event.Type.PLAYER_CHAT, new PListener(this), Event.Priority.Highest, this);
            System.out.println("[" + pdf.getName() + "] version " + pdf.getVersion() + " has been enabled");
        }
    
        public void setupConfig() {
            config = this.getConfig();
            config.options().header(" ZipIt configuration file ");
            config.addDefault("MutedPlayers.testplayer", "muted");
            config.options().copyDefaults(true);
            saveConfig();
        }
    
        public List<String> getMutedList(Player p) {
            List<String> mutedPlayers = config.getList("MutedPlayers");
            return mutedPlayers;
        }
    
        public void addMutedPlayer(Player p) {
            config.addDefault("MutedPlayers." + p, "muted");
        }
    
        public void removeMutedPlayer(Player p) {
            config.set("MutedPlayers." + p, null);
        }
    
        public void clearPlayers() {
            config.set("MutedPlayers.", null);
        }
    
        public Player[] getPlayersOnline() {
            return this.getServer().getOnlinePlayers();
        }
    
        public Player stringToPlayer(String player) {
             return this.getServer().getPlayer(player);
        }
    
    }
    This is my commandExecutor:
    Code:
    class CmdExecutor implements CommandExecutor {
    
        public ZipIt plugin;
        public CmdExecutor(ZipIt instance) {
            plugin = instance;
        }
    
        @Override
        public boolean onCommand(CommandSender cs, Command cmd, String aliases, String[] args) {
    
            if (cmd.getName().equalsIgnoreCase("mute")) {
    
            if ((!cs.hasPermission("zipit.mute")) || !cs.isOp()) {
                cs.sendMessage(ChatColor.RED + "You do not have permission");
                    return false;
            }
                if (args.length !=1) {
                    return false;
                }
    
                String player = args[1];
                plugin.stringToPlayer(player).sendMessage("You have been muted and can not longer talk");
                plugin.config.set("MutedPlayers." + player, "muted");
                plugin.reloadConfig();
                cs.sendMessage(player + " has been muted");
            }
    
            if (cmd.getName().equalsIgnoreCase("zipit")) {
    
                if ((!cs.hasPermission("zipit.clear")) || !cs.isOp()) {
                    cs.sendMessage(ChatColor.RED + "You do not have permission");
                        return false;
                    }
    
                if (args.length !=1) {
                    return false;
                }
    
                if (args[0].equalsIgnoreCase("clear")) {
                    plugin.clearPlayers();
                    plugin.reloadConfig();
                }
      
            } else if (args[0].equalsIgnoreCase("remove")) {
    
                if ((!cs.hasPermission("zipit.remove")) || !cs.isOp()) {
                    cs.sendMessage(ChatColor.RED + "You do not have permission");
                        return false;
                }
    
                if (args.length !=2) {
                    return false;
                }
                String player = args[1];
                Player p = plugin.stringToPlayer(player);
                plugin.removeMutedPlayer(p);
                plugin.reloadConfig();
            }
     
            return false;
        } // ends onCommand method
    
    }
    And my PlayerChatEvent class:
    Code:
    class PListener extends PlayerListener {
    
        public ZipIt plugin;
        public PListener(ZipIt instance) {
            plugin = instance;
        }
    
        @Override
        public void onPlayerChat(PlayerChatEvent event) {
    
            Player p = event.getPlayer();
    
            if (plugin.config.getList("MutedPlayers").contains(p)) {
                event.setCancelled(true);
                p.sendMessage("You have been muted and cannot talk");
            } else {
                return;
            }
        }
    
    }
    Ok and all of this is my plugin.. However idk if I am doing this the correct way but a way to describe it would be.
    I am trying to read a players name from the config if they are on that list then they are muted if not then they can talk. My commands add and remove a player and clear deletes the whole list however I get an NPE from this line of code which is obviously wrong since I was guessing stuff :p
    Code:
    if (plugin.config.getList("MutedPlayers").contains(p)) {
    ^^ is situated in my PListener class
    I hope you understand it and if anyone can help it would be great thanks.
     
  2. Offline

    nicholasntp

    Oh cool. Yeah i think thats correct. And, you could have asked me xD
     
  3. Offline

    Ahniolator

    Part of your problem, I believe, is that you are checking the list for a player object, and not the player name. Even if you put the players in a list somewhere, as soon as you try to save it to a file it will /fail. If you just put the player names in it, it will /work. (That is, if you are saving and loading the files correctly as well.)
     
  4. Offline

    thehutch

    hmm my other problem might be that I am not actually getting a list but keys and I want a list so if anyone could tell me how to create one that would be awesome. (PS dont link the intro to configuration thing its not on there :( (I think just woke up :p))
     
Thread Status:
Not open for further replies.

Share This Page