Getting how many blocks a player mined the last hour?

Discussion in 'Spigot Plugin Development' started by Sean0402, Dec 28, 2019.

  1. Hey everyone, Here again, to ask another question.

    As the title suggests, I'm trying to figure out how to get the information of the player's blocks mined in the last hour. I store my player's blocks in a MySQL database and can grab them etc.. usual stuff, but I need to know how to check how much they have done in the last hour.

    I know how to do Timestamps and progressive time (Such as playtime stuff) but I've got no idea how to go backwards and get the last hour.

    If anyone can point me in the right direction, that'd be much appreciated. :)
     
    • Optimistic Optimistic x 1
  2. Well, I guess you just let the mined blocks count for an hour and delete the count after an hour.
     
  3. FrostedSnowman

    Resource Staff

    i believe theirs a statistic for this, double check. if there is, place a time stamp (start) and track the amount of blocks you have right then and there, then after an hour subtract the current statistic of mined blocks from the one you initially cached
     
    • Creative Creative x 1
  4. Log all block break with system millis, and check if that millis is within the last hour, if not delete it
     
  5. 1. Potentially use EZBlocks and reset the counter each hour after grabbing?

    2. For each block broke add it to the database of a custom object that is getting updated to the database every so often while the player is on rather than each time a block is broken?
     
  6. Are you already storing the blocks in a database or do you want to cache them locally?

    If you want mysql for this but haven't make a primary key using uuid and time stamp and saving the block on the other side.
    If you want an in memory solution, just use a construct like Map<UUID, Map<Date, YourBlockData>>.

    For counting in mysql all you need to do is do a count selecting all entries with the given uuid and a timestamp larger/equal than current time- 60 min.

    If you delete entries older than one our you can just count the entries of that uuid.

    Same for the inmemory solution.

    I'd recommend you the in memory solution as it is faster due mysql saving and loading causing lots of IO time.
    If you want mysql for cross server support you'd probably want to use both and than just save entries to the database. This would get very complex but if the player is online for long would reduce mysql queries.

    Check out the vanilla mc statistics first though.
     
    • Informative Informative x 1
  7. Will the hour always start at :00 or can the minutes be arbitrary? If the latter, then you can't use a re-settable counter. If I understand correctly, you said you were already storing block mining data in the database. If that's the case, then just store the timestamp as well. To get all the blocks mined in the last hour, we know that we have to filter our timestamps to be greater than
    Code (Text):
    currentTimeMillis - 60 * 60 * 1000
    Here is a good tutorial on how to sort that data and find the entries we want through MySQL.
     
  8. How would I go about getting them on every hour?

    Example: 6-7, 7-8, 8-9. Like real life hour times.
     
  9. There are multiple ways to go about this, the worst would be to set a delayed function to remove the count after one hour.
    You can save each block with a timestamp, and each time you check you get the ones in a specific hour and delete the previous ones since you dont need them. I think there is a better way but I don't know any more
     
    • Funny Funny x 1
  10. If that's the case, then you could just use a counter. Reset the counter every hour and increment it for each block mined. Like what @GodCipher said.