Storing player time to file

Discussion in 'Spigot Plugin Development' started by Waldxn, Apr 19, 2017.

  1. I have read up on how to get a player's play time in a server.

    Basically get the currentTimeMillis when they join the server then subtract the same thing when they leave. I understand the concept but I want to know where and how I'm supposed to store the time and the most neat/effective way to do it.

    Here's what I have so far
    Code (Text):
    public class PlayerTime {

        public void PlayerJoin(PlayerJoinEvent je, Player p)
        p = je.getPlayer();
        long time = System.currentTimeMillis();

  2. I would suggest using a HashMap like:
    Code (Text):

    HashMap<Player, Long> playTime = new HashMap<Player, Long>();
    and then:
    on your playerJoinEvent you do:
    Code (Text):

    playTime.put(player, System.currentTimeMillis();
    To get the time, the player played, you could do this:
    Code (Text):

    long time = (playTime.get(player).getValue() - System.currentTimeMillis());
    To save the data in a config you can use this:
    Code (Text):

    File file = new File("folder", "fileName");
    FileConfiguration configuration = YamlConfiguration.load(file);

    configuration.set(playTime.get(player).getKey(), playTime.get(player).getValue());
    • Like Like x 1
  3. Isn't it easier if you use player.getStatistic(Statistic.PLAY_ONE_TICK)? This basically returns the player time in ticks. You only have to make a method for calculating the player time and you're done
    • Informative Informative x 1
  4. I didn't know that this exists :O
    Sure, this is way better than mine so.
  5. Thanks for this!

    OnPlayerJoinEvent(Logs the login time)
    OnPlayerleaveEvent(Logs the logout time)
    Then compare the difference and save the data somewhere.
    Then combine old data with new = total time.

    Any idea on how to do this though?

    Can't figure out how to store the times and in what format.
  6. This is the issue I'm now having:


    EDIT: Also once I fix the file creation code, how would I grab the minutes value from the file to subtract from the minutes captured on quit then add to the file.
    #6 Waldxn, Apr 19, 2017
    Last edited: Apr 19, 2017
  7. Please I still need help with this!
  8. To get the value out of a config, you can use this:
    Code (Text):