Solved Played time

Discussion in 'Plugin Development' started by Just_Jitse, May 25, 2015.

Thread Status:
Not open for further replies.
  1. Offline

    Just_Jitse

    Hello people,

    I am making a plugin which makes use of the played time of a player.
    But the problem came early:
    - I can't use the stats of mc itself because these will be deleted when players re-install mc.
    - I thought that I couldn't use a scheduler for it either, because it would become laggy if there are a ton of players on the server.

    Does somebody know how this works, and help me out? :D
     
  2. Offline

    Gamecube762

    @Just_Jitse
    1. Create a HashMap with UUID and Long
    2. When they log in, store their UUID and System.currentTimeMillis()
    3. When they log out, use their UUID to grab their login time
    4. Subtract the login time from a new System.currentTimeMillis()
    5. Now you have how long they played!
    6. Store that info somewhere(yaml or database) and add to it next time you reach step 5!
     
    Just_Jitse likes this.
  3. Offline

    Just_Jitse

    Last edited: May 25, 2015
  4. Offline

    Gamecube762

    @Just_Jitse Well where are you at and what do you need help doing?
     
  5. Offline

    Zombie_Striker

    @Just_Jitse
    If you know what you're doing, it will not be laggy. This would actually the best way, and the way that uses the least amount of memory if done correctly.
     
  6. Offline

    nverdier

    It would not be the best way... Why would you want to run a bit of code every x amount of time when you could just store the time they log on, and the time they disconnect, and then get the time online from that?
     
    Gamecube762 likes this.
  7. Offline

    caderape

    @Just_Jitse offlineplayer has an option getFirstPlayed
     
  8. Offline

    Konato_K

    @caderape Unless the player will stay online 24 hours everyday, this will not give their play time, but the time since they joined for the first time.
     
  9. Offline

    nverdier

    @Just_Jitse
    What don't you understand about this? It's really quite specific.
     
  10. Offline

    567legodude

    @Just_Jitse Using what @Gamecube762 said would look like this: (I didn't use an IDE so forgive any mistakes)
    Code:
    HashMap<UUID, Long> time = new HashMap<UUID, Long>();
    
    //event
    @EventHandler
    public void onLogin(PlayerLoginEvent event) {
        time.put(event.getPlayer().getUniqueId(), System.currentTimeMillis());
    }
    
    //other event
    @EventHandler
    public void onLeave(PlayerQuitEvent event) {
        Player player = event.getPlayer();
        long t = System.currentTimeMillis() - time.get(player.getUniqueId());
        // t is now their time in milliseconds that they have been on.
    }
     
  11. Offline

    nverdier

    567legodude, teej107 and Gamecube762 like this.
  12. Offline

    567legodude

    @nverdier That makes it too easy. You think I would know about all this if someone just said "do this" and didn't explain any of it. No, I learn each part and how it works, then combine the parts to make the whole.
     
  13. Offline

    nverdier

    It's still spoonfeeding and it's still not recommended.
     
  14. Offline

    Gamecube762

    @567legodude It's the job of the one who is learning to figure out how to code, not the ones trying to teach it. If he never learns how to create the code himself, how will he ever make something new?


    If you must create code, use pseudo code("Not" code). Pseudo is fake code used to develop understanding and is not copy-able(to an extent). A computer will not be able to read pseudo code since its not real code, which will make it so it can't be copy/pasted.

    Example:
    Code:
    Public void giveMeCookies(Player, amount)
    if (player.inventory.contains(Cookie)) then Player.tell("You already have a cookie!!!")
    else Player.inventory.add(Cookie, amount)
    end
     
    nverdier likes this.
  15. Offline

    567legodude

  16. Offline

    nverdier

    Then why'd you do it??!!?
     
    teej107 likes this.
  17. Offline

    Just_Jitse

    ow, okay. Got it.
    Thanks all!
     
Thread Status:
Not open for further replies.

Share This Page