Solved What is the proper way to get a timestamp?

Discussion in 'Spigot Plugin Development' started by Tarluin, Sep 13, 2017.

  1. Code (Text):
    long lastLogin = System.currentTimeMillis();
    This is what I have now, will this cause any issues?
    Is there a better/more preferred way of doing this?
     
  2. Nope thats perfect, if you want to see how long they've been online:

    HashMap <Player,Long> loginTime = new HashMap<Player,Long>();

    onJoinEvent
    loginTime.put(Player,System.currentTimeMills();



    sometime later or a command
    getTimePlayed()
    {
    Long loginAt = loginTime.get(player);
    Long diff = System.currentTimeMills() - loginAt;
    int seconds = (int) diff / 1000;
    //gives you the number of seconds they've been logged in.
    }
     
    • Informative Informative x 1
  3. Maybe use the player's UUID rather than the entire object?
     
  4. For something like this yeah the uuid would probably be a sleeker solution, suppose it depends on the end goal of the plugin


    Sent from my iPhone using Tapatalk
     
  5. Entire object? It'll check the UUID and entity ID, nothing more, nothing less. Moreover, you'd only store a reference to an object, not the whole object itself.

    I'd actually advise to just stick with Player in this case, since the data is only relevant to a single login session. He should be using Map rather than HashMap as field type though.
     
  6. I had just assumed the player object gets garbage collected once the "onjoin" event method is complete, hence the reason to only store the UUID rather than keep the entire player object in memory. If the player object stays in memory anyway because Spigot keeps it there as a reference for all methods which need the player object, then yea, storing the player object works just as well.
     
  7. Again as mentioned before you're not actually storing the player object in the list/map/hash map. Spigot is storing that, you are just storing a pointer that points to it in memory. So it's not resource intensive to do Player, long and look up the player object for sending messages, kicking etc


    Sent from my iPhone using Tapatalk
     

Share This Page