Hi, I am looking to create a plugin that displays the login info for a specific player but i cannot work out how to do this. If you look at my code, you will see that i have used the target.getFirstPlayed() and target.getLastPlayed() but it does not display a date, only a random number. I also am not sure how to get the total amount of times a player has logged on. Thanks for any help! Spoiler: What I am looking for Login Info For <Player>: - First Login: 05/01/2019 - Last Login: 14/02/2020 - Total Logins: 152 Spoiler: My Code Code (Java): public class LoginCommand implements CommandExecutor, Listener { private Main plugin; public LoginCommand(Main plugin) { this.plugin = plugin; plugin.getCommand("logins").setExecutor(this); } @Override public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) { if (!(sender instanceof Player)){ sender.sendMessage("This command cannot be used through console!"); return true; } Player plr = (Player) sender; Player target = Bukkit.getPlayerExact(args[0]); if (plr.hasPermission("techcraft.command.logins")){ if (args.length == 0) { plr.sendMessage(Utils.chat("&cPlayer Login Info Command")); plr.sendMessage(Utils.chat("&cCorrect Usage: /logins <Player>")); return false; } if (args.length == 1) { plr.sendMessage(Utils.chat(" ")); plr.sendMessage(Utils.chat("&6Display Name: &f" + target.getDisplayName())); plr.sendMessage(Utils.chat("&6First Login: &f" + target.getFirstPlayed())); plr.sendMessage(Utils.chat("&6Last Login: &f" + target.getLastPlayed())); plr.sendMessage(Utils.chat("&6Total Logins: &f" + ?? )); plr.sendMessage(Utils.chat("&6Online: &f" + target.isOnline())); plr.sendMessage(Utils.chat(" ")); return true; } if (args.length >= 2) { plr.sendMessage(Utils.chat("&cPlayer Login Info Command")); plr.sendMessage(Utils.chat("&cCorrect Usage: /logins <Player>")); return false; } return true; } else { plr.sendMessage(Utils.chat(plugin.getConfig().getString("NoPermission"))); } return false; } }
#getFirstPlayed doesnt display a random number. its likely a long of the system time it was at. think a Date object can take that long. Code (Text): new Date(player.getfirstplayed) /e as for amt of logins, probably have to track that manually. dunno if theres a statistic for that.
As you can see from the docs (in OfflinePlayer): So you will have to format that amount of milliseconds to date. You can do this, for example, using the constructor of java's Date
Code (Java): Date date = new Date(milis); String dateString = date.toString(); or use DateTimeFormatter
https://www.geeksforgeeks.org/program-to-convert-milliseconds-to-a-date-format-in-java/ And for the total logins, you will need to make your own counting system, Every time the player joins add 1 to his score, A better idea will be to increase his score only after he been online for 5 minutes straight.
Another method that produces a more pretty result would be like: Code (Text): DateFormat dateFormat = DateFormat.getDateInstance(DateFormat.DEFAULT, new Locale("en", "EN")); sender.sendMessage("Joined: " + dateFormat.format(new Date(PlayerObject.getFirstPlayed()))); Obv your locale would be set to wherever you are from.
Thanks for replies, This worked perfectly but it only works for online players. When I try to search for offline players, it says "An internal error occurred while attempting to perform this command" any help?