-
Notifications
You must be signed in to change notification settings - Fork 155
Expand file tree
/
Copy pathCooldown.java
More file actions
71 lines (65 loc) · 1.68 KB
/
Cooldown.java
File metadata and controls
71 lines (65 loc) · 1.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
package engine;
/**
* Imposes a cooldown period between two actions.
*
* @author <a href="mailto:RobertoIA1987@gmail.com">Roberto Izquierdo Amo</a>
*
*/
public class Cooldown {
/** Cooldown duration. */
private int milliseconds;
/** Maximum difference between durations. */
private int variance;
/** Duration of this run, varies between runs if variance > 0. */
private int duration;
/** Beginning time. */
private long time;
/**
* Constructor, established the time until the action can be performed
* again.
*
* @param milliseconds
* Time until cooldown period is finished.
*/
protected Cooldown(final int milliseconds) {
this.milliseconds = milliseconds;
this.variance = 0;
this.duration = milliseconds;
this.time = 0;
}
/**
* Constructor, established the time until the action can be performed
* again, with a variation of +/- variance.
*
* @param milliseconds
* Time until cooldown period is finished.
* @param variance
* Variance in the cooldown period.
*/
protected Cooldown(final int milliseconds, final int variance) {
this.milliseconds = milliseconds;
this.variance = variance;
this.time = 0;
}
/**
* Checks if the cooldown is finished.
*
* @return Cooldown state.
*/
public final boolean checkFinished() {
if ((this.time == 0)
|| this.time + this.duration < System.currentTimeMillis())
return true;
return false;
}
/**
* Restarts the cooldown.
*/
public final void reset() {
this.time = System.currentTimeMillis();
if (this.variance != 0)
this.duration = (this.milliseconds - this.variance)
+ (int) (Math.random()
* (this.milliseconds + this.variance));
}
}