From 39d6fdd05d058bfcde182b5929d80566ba4c38cc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tim=20Vis=C3=A9e?= Date: Sun, 1 Nov 2015 17:42:46 +0100 Subject: [PATCH] Implemented lastlogin command --- .../xephi/authme/command/CommandManager.java | 13 ++++ .../command/executable/LastLoginCommand.java | 74 +++++++++++++++++++ 2 files changed, 87 insertions(+) create mode 100644 src/main/java/fr/xephi/authme/command/executable/LastLoginCommand.java diff --git a/src/main/java/fr/xephi/authme/command/CommandManager.java b/src/main/java/fr/xephi/authme/command/CommandManager.java index 715c3637..1a62a036 100644 --- a/src/main/java/fr/xephi/authme/command/CommandManager.java +++ b/src/main/java/fr/xephi/authme/command/CommandManager.java @@ -123,6 +123,19 @@ public class CommandManager { dungeonMazeCommand); listWorldCommand.setCommandPermissions("dungeonmaze.command.listworlds", CommandPermissions.DefaultPermission.OP_ONLY);*/ + // Register the purge command + CommandDescription lastLoginCommand = new CommandDescription( + new LastLoginCommand(), + new ArrayList() {{ + add("lastlogin"); + add("ll"); + }}, + "Player's last login", + "View the date of the specified players last login", + authMeCommand); + lastLoginCommand.setCommandPermissions("authme.admin.lastlogin", CommandPermissions.DefaultPermission.OP_ONLY); + lastLoginCommand.addArgument(new CommandArgumentDescription("player", "Player name", true)); + // Register the purge command CommandDescription purgeCommand = new CommandDescription( new PurgeCommand(), diff --git a/src/main/java/fr/xephi/authme/command/executable/LastLoginCommand.java b/src/main/java/fr/xephi/authme/command/executable/LastLoginCommand.java new file mode 100644 index 00000000..cc184cbb --- /dev/null +++ b/src/main/java/fr/xephi/authme/command/executable/LastLoginCommand.java @@ -0,0 +1,74 @@ +package fr.xephi.authme.command.executable; + +import fr.xephi.authme.AuthMe; +import fr.xephi.authme.ConsoleLogger; +import fr.xephi.authme.cache.auth.PlayerAuth; +import fr.xephi.authme.command.CommandParts; +import fr.xephi.authme.command.ExecutableCommand; +import fr.xephi.authme.settings.Messages; +import fr.xephi.authme.settings.Settings; +import fr.xephi.authme.util.Profiler; +import org.bukkit.command.CommandSender; + +//import org.bukkit.ChatColor; +import java.util.Date; + +public class LastLoginCommand extends ExecutableCommand { + + /** + * Execute the command. + * + * @param sender The command sender. + * @param commandReference The command reference. + * @param commandArguments The command arguments. + * + * @return True if the command was executed successfully, false otherwise. + */ + @Override + public boolean executeCommand(CommandSender sender, CommandParts commandReference, CommandParts commandArguments) { + // Profile the reload process + Profiler p = new Profiler(true); + + // AuthMe plugin instance + AuthMe plugin = AuthMe.getInstance(); + // Messages instance + Messages m = Messages.getInstance(); + + // Get the player + String playerName = sender.getName(); + if(commandArguments.getCount() >= 1) + playerName = commandArguments.get(0); + + // Validate the player + PlayerAuth auth; + try { + auth = plugin.database.getAuth(args[1].toLowerCase()); + } catch (NullPointerException e) { + m.send(sender, "unknown_user"); + return true; + } + if (auth == null) { + m.send(sender, "user_unknown"); + return true; + } + + // Get the last login date + long lastLogin = auth.getLastLogin(); + Date date = new Date(lastLogin); + + // Get the difference + final long diff = System.currentTimeMillis() - lastLogin; + + // Build the message + final String msg = (int) (diff / 86400000) + " days " + (int) (diff / 3600000 % 24) + " hours " + (int) (diff / 60000 % 60) + " mins " + (int) (diff / 1000 % 60) + " secs."; + + // Get the player's last IP + String lastIP = auth.getIp(); + + // Show the player status + sender.sendMessage("[AuthMe] " + args[1] + " last login : " + date.toString()); + sender.sendMessage("[AuthMe] The player " + auth.getNickname() + " is unlogged since " + msg); + sender.sendMessage("[AuthMe] Last Player's IP: " + lastIP); + return true; + } +}