Time since first login on server.

Discussion in 'Plugin Development' started by yupie_123, Jul 30, 2014.

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

    yupie_123

    Hello, I'm trying to figure out how to get the time the player has been playing on the server.

    I tried getting the players' first time join by doing:
    Code:java
    1. Long firstPlayed = p.getFirstPlayed();

    This returns a long value, I assume ticks?

    After that I need to get the total amount of time between that variable and the current time.

    Code:java
    1. Long Total;
    2. Total = currTime - firstPlayed;
    3.  


    I have some questions:

    1) How would I get "currTime"? I assume I would have to get it as a long to be able to subtract firstPlayed from it.
    2) How would I broadcast "Total" in days, minutes and seconds?
     
  2. Offline

    Bobit

    currTime=player.getWorld().getFullTime() might work
    You can "broadcast" using broadcastmessage(String)
    You can put the calculations for converting to days, minutes, and seconds yourself.

    Actually, getFirstPlayed() says in the API:
    So you'd have to calculate exactly how long it has been since Jan 1, 1970 if using this method. If this plugin is for a new server, I'd recommend instead marking the time they joined when they first join manually.

    Edit: Seriously, why 1970? Why not 2000?

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 9, 2016
  3. Offline

    yupie_123

    Bobit
    Ok, I will try that. Thanks for the info
    But I do have one question: Won't p.getWorld().getFullTime(); show the Minecraft ingame time?
     
  4. Offline

    Necrodoom

    Because it uses a Unix timestamp but with milliseconds.
     
    AdamQpzm likes this.
  5. Offline

    Bobit

    Not the minecraft ingame time, that's getWorld().getTime(). You know how to read the API, right?
    It returns the time since the server was last launched though. You probably don't want that, my bad.
    Instead, use System.currentTimeMillis(); I found this one on google, the java API says
    So if you have System.currentTimeMillis()-p.getFirstPlayed(), that would be the "total" you want.
     
  6. Offline

    AoH_Ruthless

    Bobit
    Are you sure that would work? If I played on a server for 24 hours, and then logged for 24 hours, and logged back in, wouldn't System.currentTimeMillis() - Player#getFirstPlayed() return 48 hours in milliseconds? That isn't correct if I am understanding OP correctly.

    If I'm right, then every time they login, you have to timestamp it, timestamp their logout, compare the timestamps and cache it to either a database or flatfile. Then when they login again you can retrieve this cached long.
     
    Bobit likes this.
  7. Offline

    Bobit

    Right, I guess so. So his total=timenow-timejoined thing isn't really what he wants...
     
  8. Offline

    yupie_123

    AoH_Ruthless
    No, what Bobit is correct. I did want the total IRL time since the player has first joined the server. Not play time ;)
    I haven't been able to test this yet, but as soon as I get on my PC and get the math figured out, I'll try it and reply!
    Thanks for the help
     
    Bobit likes this.
Thread Status:
Not open for further replies.

Share This Page