-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathTimeSince.java
82 lines (65 loc) · 2.08 KB
/
TimeSince.java
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
72
73
74
75
76
77
78
79
80
81
82
import java.lang.management.ManagementFactory;
import xyz.derkades.ssx_connector.Addon;
public class TimeSince extends Addon {
@Override
public String getAuthor() {
return "Derkades";
}
@Override
public String getDescription() {
return "Add placeholders which count the time after a specific date";
}
@Override
public String getLicense() {
return "MIT";
}
@Override
public String getVersion() {
return "1.1.2";
}
@Override
public void onLoad() {
final int placeholderType = this.config.getInt("placeholder-type");
for (final String placeholderName : this.config.getConfigurationSection("placeholders").getKeys(false)) {
addPlaceholder(placeholderName, () -> {
long diff; // In seconds
if (this.config.isLong("placeholders." + placeholderName) || this.config.isInt("placeholders." + placeholderName)) {
final long timestamp = this.config.getLong("placeholders." + placeholderName);
diff = System.currentTimeMillis() / 1000 - timestamp;
} else {
final String type = this.config.getString("placeholders." + placeholderName);
if (type.equals("uptime")) {
diff = ManagementFactory.getRuntimeMXBean().getUptime() / 1000;
} else {
return "error: invalid value";
}
}
if (diff < 0) {
return "in the future";
}
int weeks = -1;
int days = -1;
if (placeholderType >= 3) {
weeks = (int) (diff / (60*60*24*7));
diff -= weeks*60*60*24*7;
}
if (placeholderType >= 2) {
days = (int) (diff / (60*60*24));
diff -= days*60*60*24;
}
final int hours = (int) (diff / (60*60));
diff -= hours*60*60;
final int minutes = (int) (diff / 60);
diff -= minutes*60;
final int seconds = (int) diff;
final String formattedDiff = this.config.getString("format")
.replace("{weeks}", weeks + "")
.replace("{days}", days + "")
.replace("{hours}", hours + "")
.replace("{minutes}", minutes + "")
.replace("{seconds}", seconds + "");
return formattedDiff;
});
}
}
}