Most Efficient Way of Storing Time + Retrieving it in a Specific Format

Discussion in 'Spigot Plugin Development' started by Username1234, Jun 7, 2017.

Thread Status:
Not open for further replies.
  1. So, I would like to know a way to store the current System time in milliseconds somehow for a long period. Putting the time in to a configuration file and updating it every millisecond is just ridiculous. I think a database would be the best way. What are your thoughts on this?
    And my second question is, how would I retrieve that time and convert it to a specific format without modifying the original time in the database (if I use it)?

    I actually don't really need milliseconds. If I can convert the milliseconds in to the format before I store it, I think that would be better.

    Either way, I don't think storing information to something and reloading it every time is not a good idea.
    Any suggestions?
     
  2. why updating it every millisecond? isnt there an event for your stuff? like player leaves or server shuts down, etc?
     
  3. I am using this to store each player on the server's play time.
     
  4. just store the join-time in a hashmap and on leave event add the current time - jointime to the config/database
     
  5. Okay. About part 2?
     
  6. The server already stores each players playtime for you. Look in a player nbt file and you will see it. No need to recreate the wheel and all that, just read the value the server keeps track of for you.

    In fact, this might be a good PR if you wanted to contribute a way to get to this value from the Bukkit API. ;)
     
  7. just do some math to convert your time into an readable format
    or google for some other solutions like SimpleDateFormat
     
  8. No, that gets the world time of a player (you can set the world time different than the actual world time per player).

    There is no current API method to get to the players playtime that I'm aware of. But the value is there and stored in the "bukkit" section of the players nbt (.dat) file. Crack open the nbt file with nbt explorer and you'll see it and it's name (I can't remember it's name, it's been forever since I've messed with it).
     
  9. I think this would be the best answer in my case because I need to stop the timer at some time (because I want to exclude /afk time).
     
  10. Never mind, this won't work for me.
    I want players to be able to do a command that displays their play time in a Hours/Minutes (HH/MM) format, excluding /afk time.
     
  11. Mas

    Mas

    Code (Java):
    Player#getStatistic(Statistic.PLAY_ONE_TICK)
    ?
     
  12. So that would get the play time? If so, how would I convert the ticks into Hours/Minutes format? I am guessing it will involve division and multiplication, but I am HORRIBLE at math, so if anyone can help me with this, it would be great.
     
  13. Its simple math if you can convert to minutes/days/hours etc... or use DateFormat or Time classes I believe theyre called, not sure though.
     
  14. Im actually curious as to how to do that... never messed with .dat files before. Mind assisting? I would love to know how to get a players play time from that file, as currently I just store the system time in a database and update it on a player leaving / joining the server.
     
  15. Mas

    Mas

    20 ticks = 1 second, so ticks / 20 = seconds played.
    You can them use the handy methods in the TimeUnit enums to convert:
    eg: TimeUnit.SECONDS.toDays(ticksPlayed / 20) would give you the player's days played.
     
  16. Thank you, this is very helpful.
    ~Thread Locked (for now)
     
    • Like Like x 1
Thread Status:
Not open for further replies.