1.8.8 Login Plugin

Discussion in 'Spigot Plugin Development' started by TechyGaming, Feb 14, 2020.

  1. 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!

    Login Info For <Player>:
    - First Login: 05/01/2019
    - Last Login: 14/02/2020
    - Total Logins: 152

    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;
           
        }

    }
     
  2. #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.
     
  3. 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
     
  4. Where would I insert a Date object?
     
  5. Code (Java):
    Date date = new Date(milis);
    String dateString = date.toString();
    or use DateTimeFormatter
     
    • Agree Agree x 1
  6. Strahan

    Benefactor

    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.
     
  7. 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?
     
  8. Are you actually storing the last login or just assuming bukkit remembers it?
     
  9. Strahan

    Benefactor

    Post the code and the stack trace, and mark the line indicated by the stack trace.