Inaccurate results when comparing a long value

Discussion in 'Plugin Development' started by elementalgodz11, Apr 11, 2014.

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

    elementalgodz11

    I am trying to get the total online time a player has, using a config.

    I decided to approach this by taking away the config System.currentTimeMillis from a defined value in the config which is set when a player joins and is set to the value of System.currentTimeMillis.

    I have managed to give this a go, and I am able to get the players current online session time, but the total online time I couldn't manage to do:

    Firstly here's the code I'm returning the time using (the millisToReadable method just parses time into a seconds/minutes string):
    Code:java
    1. Long newTime = System.currentTimeMillis() / 1000 - plugin.getConfig().getLong("OnlineTime.Current." + target.getName()) / 1000;
    2. Long totalTime = plugin.getConfig().getLong("OnlineTime.Total." + target.getName());
    3.  
    4. player.sendMessage(ChatColor.AQUA + target.getName() + " current online session time is " + ChatColor.GRAY + plugin.methods.millisToReadable(player, newTime) + ChatColor.AQUA + ".");
    5. player.sendMessage(ChatColor.AQUA + target.getName() + " has been online in total for " + ChatColor.GRAY + plugin.methods.millisToReadable(player, totalTime) + ChatColor.AQUA + ".");


    So when a player joins, I set the current time:
    Code:java
    1. plugin.getConfig().set("OnlineTime.Current." + player.getName(), Long.valueOf(System.currentTimeMillis()));
    2. plugin.saveConfig();


    And when a player quits (I believe this is where the error occurs), I set the total value to the current online time - the system.currentTimeMilis:
    Code:java
    1. Long current = plugin.getConfig().getLong("OnlineTime.Current." + player.getName());
    2. Long longNow = plugin.getConfig().getLong("OnlineTime.Total." + player.getName());
    3.  
    4. Long newLong = current - longNow;
    5.  
    6. plugin.getConfig().set("OnlineTime.Total." + player.getName(), current);
    7. plugin.getConfig().set("OnlineTime.Current." + player.getName(), newLong);
    8. plugin.saveConfig();


    And by inaccurate results in the title, here's what I was talking about:
    Screenshot_3.png

    If anyone could help me with this, that would be appreciated.

    Thanks.
     
  2. Offline

    coasterman10

    You shouldn't be storing and pulling values from a configuration like that to be doing time comparisons. Just get the join time from the config, and call System.nanoTime() directly. Overall the logic seems broken, if you could post the actual code so I could follow it I may be able to understand it.
     
  3. Offline

    xTrollxDudex

    coasterman10
    nanoTime is not necessary for this, overtime it will overflow and usage of BigInteger will need to be used. nanoTime also is used for benchmarking and cryptography where a very, very high value of precision is essential to result type. +/- 15 millis is a small in inaccuracy to pay for performance over nanoTime.
     
  4. Offline

    RawCode

    never nanotime, you do not understand what that native does.
     
  5. Offline

    elementalgodz11

    coasterman10 Every snippet of code I have posted in my original post is generally my actual code.
     
  6. Offline

    coasterman10

    nanoTime bashing aside, you should probably be using a class optimized for this sort of work, such as the partially deprecated Date, or Calendar. I'm not very keen on how these classes work, so I'll stop there, but I can say that a proper time class would work much better and save you the writing of a lot of code that someone else has already written.

    If that's the case, it looks like you're trying to use the config to get around an instance variable. The only thing that need be in the config is the time the player joined. If you're not storing that through server reboots/etc you may also be better off just using a Map.
     
  7. Offline

    elementalgodz11

    coasterman10
    Thanks.

    I'm updating the "OnlineTime.Current" path for the player in the config everytime I player joins, so if I'm correct I'd have to create an "OnlineTime.FirstJoined" value and only set it if the path is null so the comparison is correct.

    Is that correct?
     
  8. Offline

    coasterman10

    Not quite. The way I see it is that you should have a value for the total running time the player has been online until the last login, and then a value for the time they joined their current session. When the player disconnects, you get the difference between the time they joined the current session and the time they disconnected, then add that to the running total. When the player asks for their current online time, you would get the difference between the time they joined the current session and the current time, add that to their running total, and then give them the result.

    EDIT: Re-reading the OP I think I better understand what you're doing and it appears your original method was sound in theory, but you were using nanoTime incorrectly.
     
  9. Offline

    elementalgodz11

    coasterman10
    Okay thank you, I think I understand, but how would I return the difference? I have tried using Long - Long, but that returns a negative long value.
     
  10. Offline

    coasterman10

    You should try using java.util.Date instead of milliseconds. And I think the error was that you were dividing the millisecond value for one but not the other.
     
  11. Offline

    RawCode

    add debug output to your code and try again.

    issue is extremely easy to spot if you just try to debug.
     
Thread Status:
Not open for further replies.

Share This Page