20
20
*/
21
21
package com .extendedclip .papi .expansion .server ;
22
22
23
- import com .github . benmanes . caffeine .cache .Cache ;
24
- import com .github . benmanes . caffeine . cache .Caffeine ;
23
+ import com .google . common .cache .Cache ;
24
+ import com .google . common . cache .CacheBuilder ;
25
25
import me .clip .placeholderapi .PlaceholderAPI ;
26
26
import me .clip .placeholderapi .PlaceholderAPIPlugin ;
27
27
import me .clip .placeholderapi .expansion .Cacheable ;
33
33
import org .bukkit .World ;
34
34
import org .bukkit .entity .Player ;
35
35
import org .jetbrains .annotations .NotNull ;
36
+ import org .jetbrains .annotations .Nullable ;
36
37
37
38
import java .lang .management .ManagementFactory ;
38
39
import java .text .SimpleDateFormat ;
39
40
import java .time .Duration ;
40
41
import java .time .temporal .ChronoUnit ;
41
42
import java .util .Date ;
42
43
import java .util .HashMap ;
44
+ import java .util .List ;
43
45
import java .util .Map ;
44
46
import java .util .StringJoiner ;
47
+ import java .util .concurrent .Callable ;
48
+ import java .util .concurrent .ExecutionException ;
45
49
import java .util .concurrent .TimeUnit ;
50
+ import java .util .logging .Level ;
46
51
47
52
public class ServerExpansion extends PlaceholderExpansion implements Cacheable , Configurable {
48
53
@@ -58,7 +63,7 @@ public class ServerExpansion extends PlaceholderExpansion implements Cacheable,
58
63
private String high = "&a" ;
59
64
// -----
60
65
61
- private final Cache <String , Integer > cache = Caffeine .newBuilder ()
66
+ private final Cache <String , Integer > cache = CacheBuilder .newBuilder ()
62
67
.expireAfterWrite (1 , TimeUnit .MINUTES )
63
68
.build ();
64
69
@@ -105,6 +110,38 @@ public Map<String, Object> getDefaults() {
105
110
return defaults ;
106
111
}
107
112
113
+ private @ Nullable String getCached (String key , Callable <Integer > callable ) {
114
+ try {
115
+ return String .valueOf (cache .get (key , callable ));
116
+ } catch (ExecutionException e ) {
117
+ if (getPlaceholderAPI ().getPlaceholderAPIConfig ().isDebugMode ()) {
118
+ getPlaceholderAPI ().getLogger ().log (Level .SEVERE , "[server] Could not access cache key " + key , e );
119
+ }
120
+ return "" ;
121
+ }
122
+ }
123
+
124
+ private int getChunks (){
125
+ return Bukkit .getWorlds ()
126
+ .stream ()
127
+ .mapToInt (world -> world .getLoadedChunks ().length )
128
+ .sum ();
129
+ }
130
+
131
+ private int getLivingEntities (){
132
+ return Bukkit .getWorlds ()
133
+ .stream ()
134
+ .mapToInt (world -> world .getLivingEntities ().size ())
135
+ .sum ();
136
+ }
137
+
138
+ private Integer getTotalEntities (){
139
+ return Bukkit .getWorlds ()
140
+ .stream ()
141
+ .mapToInt (world -> world .getEntities ().size ())
142
+ .sum ();
143
+ }
144
+
108
145
@ Override
109
146
public String onRequest (OfflinePlayer p , @ NotNull String identifier ) {
110
147
final int MB = 1048576 ;
@@ -157,11 +194,11 @@ public String onRequest(OfflinePlayer p, @NotNull String identifier) {
157
194
long seconds = TimeUnit .MILLISECONDS .toSeconds (ManagementFactory .getRuntimeMXBean ().getUptime ());
158
195
return formatTime (Duration .of (seconds , ChronoUnit .SECONDS ));
159
196
case "total_chunks" :
160
- return String . valueOf ( cache . get ( "chunks" , k -> getChunks ()) );
197
+ return getCached ( "chunks" , this :: getChunks );
161
198
case "total_living_entities" :
162
- return String . valueOf ( cache . get ( "livingEntities" , k -> getLivingEntities ()) );
199
+ return getCached ( "livingEntities" , this :: getLivingEntities );
163
200
case "total_entities" :
164
- return String . valueOf ( cache . get ( "totalEntities" , k -> getTotalEntities ()) );
201
+ return getCached ( "totalEntities" , this :: getTotalEntities );
165
202
case "has_whitelist" :
166
203
return Bukkit .getServer ().hasWhitelist () ? PlaceholderAPIPlugin .booleanTrue () : PlaceholderAPIPlugin .booleanFalse ();
167
204
}
@@ -408,31 +445,5 @@ private String getPercent(double tps){
408
445
409
446
return (tps > 20.0 ? "*" : "" ) + finalPercent + "%" ;
410
447
}
411
-
412
- private Integer getChunks (){
413
- int loadedChunks = 0 ;
414
- for (final World world : Bukkit .getWorlds ()) {
415
- loadedChunks += world .getLoadedChunks ().length ;
416
- }
417
-
418
- return loadedChunks ;
419
- }
420
-
421
- private Integer getLivingEntities (){
422
- int livingEntities = 0 ;
423
- for (final World world : Bukkit .getWorlds ()) {
424
- livingEntities += world .getLivingEntities ().size ();
425
- }
426
-
427
- return livingEntities ;
428
- }
429
-
430
- private Integer getTotalEntities (){
431
- int allEntities = 0 ;
432
- for (World world : Bukkit .getWorlds ()) {
433
- allEntities += world .getEntities ().size ();
434
- }
435
-
436
- return allEntities ;
437
- }
448
+
438
449
}
0 commit comments