Time online

Discussion in 'Plugin Development' started by harhar4932843098234, May 1, 2016.

Thread Status:
Not open for further replies.
  1. Hi, I am currently coding a Bukkit plugin and need to see how long the user has been logged onto the server for, for example if someone stays on the server for a certain amount of time this will happen... So, basically I need the time that they have been on the server for.

    Thanks :)
     
  2. Offline

    timtower Administrator Administrator Moderator

    Moved to plugin development.
    @harhar4932843098234 Many ways to do this, total online time or that session?
     
  3. That session time of the player.
     
  4. Offline

    Zombie_Striker

    @harhar4932843098234
    1. Create a Map storing a value related to the player (UUID or Name) as the key, and a Long which will be when the player joined in milliseconds.
    2. Create a RepeatingTask. Inside this task, loop through every player online and see if the Current-Time-in-Milliseconds - Milliseconds in the map is greater than the amount of time you want to check for.
    3. If so, re-add the current milliseconds to the map. This will allow for you to have these checks multiple times per session. If you do not want this to happen multiple time per session, then remove the player from the Map.
    4. You can set the delay for this for however long you want. If you need it to be precise, then set it to repeat every second, else you can wait minutes before repeating to save memory.
     
  5. Offline

    timtower Administrator Administrator Moderator

    @harhar4932843098234 Save the time that they went online in a hashmap, every ... time go through that hashmap and see who has been online long enough.
     
  6. How would I go about getting the system time to add to my hashmap?
     
  7. Offline

    Zombie_Striker

    @harhar4932843098234
    System.currentTimeMillis returns a long which is the current time in milliseconds.
     
  8. This does not seem to be working, please help. (No hate, I only started coding Java yesterday and this is all mine.)


    Code:
        @EventHandler
        public void onPlayerJoin(PlayerJoinEvent e) {
            final HashMap<UUID, Long> hashmap = new HashMap<UUID, Long>();
            Player player = e.getPlayer();
            //Date now = new Date();
            hashmap.put(player.getUniqueId(), System.currentTimeMillis());
            //Added systime and UUID to hashmap
           
            if ((System.currentTimeMillis() - hashmap.get(System.currentTimeMillis())) == 60000) {
                //Run get key code
                player.sendMessage(ChatColor.BLUE + "You have been on the server long enough to earn yourself a key!");
            } else {
                player.sendMessage(ChatColor.RED + "False.");
            }
                   
        }
     
  9. Offline

    Zombie_Striker

    And this is your problem. Fully understanding Java is a requirement for making Bukkit plugins, as not knowing Java allows you to make mistakes and almost always leads to confusion. Please learn Java before working on Java.

    If you do not have a Java tutorial already, you should select one from the following link. Stick with this for a few weeks until you got a good understanding of Java and it's classes and methods.
    https://bukkit.org/threads/plugin-dev-sticky-learning-java-where-to-learn.395662/
     
  10. Thank you. Honestly.

    Which one would you recommend?

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 1, 2016
  11. Offline

    Zombie_Striker

Thread Status:
Not open for further replies.

Share This Page