Jump to content

... per hour giving negative ammount?


blabla123

Recommended Posts

public void onStart() {
     startTime = System.currentTimeMillis();
     planksCreated = 0;
}

onLoop() {
     planksCreated += client.getInventory().getAmount(userChoiceP);
}

onPaint() {
     long planksPerHour = (planksCreated * 3600000) / (System.currentTimeMillis() - startTime);
}

Maybe I'm overlooking something, but why is this giving me correct results up till around 30 mins running the script (1500/hr) and after 30 mins it goes to (-1500/hr) and goes towards 0... ?

 

Time counter is running ok, planks counter running ok, even trips per hour counter running ok, just this one went crazy.

 

Only way '(planksCreated * 3600000) / (System.currentTimeMillis() - startTime)' would be giving negative result is when startTime > System.currentTimeMillis() but how is that possible?

Link to comment
Share on other sites

public void onStart() {
     startTime = System.currentTimeMillis();
     planksCreated = 0;
}

onLoop() {
     planksCreated += client.getInventory().getAmount(userChoiceP);
}

onPaint() {
     long planksPerHour = (planksCreated * 3600000) / (System.currentTimeMillis() - startTime);
}

Maybe I'm overlooking something, but why is this giving me correct results up till around 30 mins running the script (1500/hr) and after 30 mins it goes to (-1500/hr) and goes towards 0... ?

 

Time counter is running ok, planks counter running ok, even trips per hour counter running ok, just this one went crazy.

 

Only way '(planksCreated * 3600000) / (System.currentTimeMillis() - startTime)' would be giving negative result is when startTime > System.currentTimeMillis() but how is that possible?

 

 

You have not supplied enough information. Also, I'm assuming that's not everything you have for determining planksCreated

planksCreated += client.getInventory().getAmount(userChoiceP);

You haven't even shown us what data types those all are.

Link to comment
Share on other sites

Declared at the beginning, outside any methods.

int userChoiceP;
long startTime;
int planksCreated;

And actually that line is all I use for counting planks created and it works fine everytime. As I said before, all other counters work fine.

 

Now that you asked about data types I see that startTime is long, so maybe I should do

long planksPerHour = ((long)planksCreated * 3600000) / (System.currentTimeMillis() - startTime);
Edited by blabla123
Link to comment
Share on other sites

 

Declared at the beginning, outside any methods.

int userChoiceP;
long startTime;
int planksCreated;

And actually that line is all I use for counting planks created and it works fine everytime. As I said before, all other counters work fine.

 

Now that you asked about data types I see that startTime is long, so maybe I should do

long planksPerHour = ((long)planksCreated * 3600000) / (System.currentTimeMillis() - startTime);
public int getPerHour(int value) {
if (System.currentTimeMillis() - startTime > 0) {
return (int) (value * 3600000d / System.currentTimeMillis() - startTime);
} else {
return 0;
    }
}

 and use in onPaint() like this:
g.drawString("" + getPerHour(planksCreated), x,y);
Link to comment
Share on other sites

As @TheScrub stated, you're going over Integer.MAX_VALUE and thus overflowing. Just change the order of operations.

You might have some errors if you store some values as integers, but you should be able to work those out.

long planksPerHour = (long)(planksCreated * (3600000.0 / (System.currentTimeMillis() - startTime));
Edited by Eliot
Link to comment
Share on other sites

 

As @TheScrub stated, you're going over Integer.MAX_VALUE and thus overflowing. Just change the order of operations.

You might have some errors if you store some values as integers, but you should be able to work those out.

long planksPerHour = (long)(planksCreated * (3600000.0 / (System.currentTimeMillis() - startTime));

 

He can hold planksPerHour as integer, there's no need for long, as it won't even exceed 2k.

Also, he doesn't need to reverse order, if he changed the number to double. (3600000.0) or (3600000d).

The resulting double will be converted back to integer without any problems.

int planksPerHour = (int)(3600000d * planksCreated  / System.currentTimeMillis() - startTime);
Link to comment
Share on other sites

Guest
This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...