Issue with adding Longs

Discussion in 'Plugin Development' started by Loogeh, Aug 31, 2013.

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

    Loogeh

    I'm trying to track playtime by comparing milliseconds. I thought I knew how to do this but apparently not. I'm aware that using += with different data types doesn't do what it normally would do but i'm just using longs and it still does other stuff.

    I've tried doing it two ways.

    First way
    Code:
    public void updatePlayTime() {
            this.playTime += (System.currentTimeMillis() - getLastJoin());
        }
        
    Second way
    Code:
    public void updatePlayTime() {
            this.playTime = Long.valueOf(this.playTime) + (System.currentTimeMillis() - getLastJoin());
        }
    All this does is add 1-2 hours every time I use my command, no matter when I use it.

    Any help?
     
  2. Offline

    metalhedd

    is this.playTime an int or a long? it needs to be a long or it'll overflow, causing what appears to be math errors.
     
  3. Offline

    Loogeh

    metalhedd It's a long
    Code:
    private long playTime;
     
  4. Offline

    metalhedd

    well, the only possibility is that getLastJoin isn't returning what you expect from it...

    why not add: plugin.getLogger().info(this.playTime + " += (" + System.currentTimeMillis() + " - " getLastJoin() + ")");

    you'll see what the numbers are, punch them into a calculator and see which number is wrong.
     
  5. Offline

    Loogeh

    I'm in need of help with this, again. I was trying to find a way to make it work but became frustrated and bored with it, so I started doing something else. Now, i'm back to this. I know a little more about why it happens but not sure how to fix it. I tried casting both longs to (long) and (Long) but that didn't work. I also tried using the BigInteger class but I had no luck with that too.
    Any help?
     
  6. Offline

    Loogeh

    anyone?
     
  7. did you checked if the other values are valid?
     
  8. Offline

    Loogeh

    ferrybig Not sure what you mean by that.
     
  9. Offline

    ShadowDog007

    Loogeh

    Can I see 'getLastJoin()'?
     
  10. Offline

    Loogeh

    ShadowDog007

    getLastJoin
    Code:
        public long getLastJoin() {
            return this.lastJoin;
        }
        
    lastJoin
    Code:
    private long lastJoin;
     
  11. Offline

    ShadowDog007

    Loogeh

    You set lastJoin to the current time when a player joins the server with PlayerJoinEvent ?

    Could you give some examples of results?

    You should also try printing those values to console when 'updatePlayTime()' is called, so you can validate what is happening.
     
Thread Status:
Not open for further replies.

Share This Page