Solved Logging command after permission check

Discussion in 'Spigot Plugin Development' started by MrFishCakes, May 22, 2017.

Thread Status:
Not open for further replies.
  1. Hello,
    Just as a little feature that I wanted to try, I want to log commands but once the command is executed it is logged if that player has permission or not. What is the best way I can to see if they have the permission or not?

    Code:
    Code (Java):
    public class CommandLogger implements Listener {

        static Configuration config = Main.plugin.getConfig();

        Date now = new Date();
        SimpleDateFormat format = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss z");

        @EventHandler
        public void logCommand(PlayerCommandPreprocessEvent e) {
            String cmd = e.getMessage().toLowerCase();
            List<String> commands = config.getStringList("CommandsToLog");
            for (final String command : commands) {
                if (cmd.contains(command) || cmd.equals(command)) {
                    List logged;
                    if (config.getStringList("LoggedCommands") != null)
                        logged = config.getStringList("LoggedCommands");
                    else {
                        logged = new ArrayList();
                    }
                    final String finalCommand = "[" + this.format.format(now) + "] " + e.getPlayer().getName() + ": " + e.getMessage();
                    logged.add(finalCommand);
                    config.set("LoggedCommands", logged);
                    Main.plugin.saveConfig();
                }
            }
        }

    }

    Thanks,
    MrFishCakes
     
  2. You could use:
    Code (Text):
    Player p = (Player) e.getPlayer();
    //and then
    if (p.hasPermission("perm.node"){
    //code}
     
  3. Can't believe I didn't think of that. Thanks!
     
Thread Status:
Not open for further replies.