Best way to log a players time on a server


#1

The method I was planning to try, was to have an event scheduled to run every mintue, and then it would get a list of all players and add a minute to their onTime.

I just want to see what the best method of this would be, and to see if my idea would be fine.


#2

How about record the join and leave time, then get a delta?
Also I think the vanilla server actually tracks play time as a statistic (see https://minecraft.gamepedia.com/Statistics)

You might be able to use that instead.


#3

And that makes even more sense. I will tap into that and then detect when a player goes afk and subtract that from the onTime.

I think that (since my pluging will log when they go afk, and print to a log) It would be easier to just read the log and find out when the player went afk and figure out the difference in the time.

Would that make sense or would it be better to store that info somewhere on the server and call it when the player is no longer afk?


#4

I’m not sure what you’re referring to by a log, I assume it’s some kind of text file you’re writing to?

What I’d do is store a mapping of player to time data, like so:

Map<UUID, PlayerTimeData> timeMap;

Where PlayerTimeData stores these attributes:

class PlayerTimeData {
    private long commitedTime;
    private long recordStartTime;
}

Then have a function startRecording() that sets recordStartTime to the current time.
A function stopRecording() gets the current time and computes a delta with recordStartTime, and adds the delta to commitedTime.

call startRecording() when a player joins or when they resume from AFK
call stopRecording() when the player leaves or goes AFK

commitedTime is the total time recorded, and can be serialized to a file so it persists across server shutdown.


#5

yeah the log is a text file, and I will give that a go, thanks for that tip.


#6

Are there any events that would detect a player entering/ exiting AFK state? If there was an /afk command, im sure that could server as an entry trigger, but what about non-activity… The vanilla server is able to detect non-keyboard, non-mouse, non-hand-swinging etc activity and time it to kick players in that state after a given amount of time, if such a configureation is specified in the server.properties.

Or would it be a matter of having to see what plugin is handling AFK on a system (custom, Nucleus, etc) and see if / ask plugin author to provide throwing events when the player is put into and taken out of afk mode, then letting a system like this thread listen for entry/exit to record time spent in afk mode to deduct


#7

If i remeber right, the ones that i used were from nucleus. But im not home atm to show you the code. When I get home in about a hour I will post what I use.