Skip to content

Commit ff26a4f

Browse files
authored
Merge pull request #46 from PlaceholderAPI/remove-caffine
2 parents 73e5fc0 + d6bc40a commit ff26a4f

File tree

2 files changed

+49
-70
lines changed

2 files changed

+49
-70
lines changed

pom.xml

+5-37
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
<modelVersion>4.0.0</modelVersion>
33
<groupId>com.extendedclip.papi.expansion.server</groupId>
44
<artifactId>server-expansion</artifactId>
5-
<version>2.6.1</version>
5+
<version>2.6.2</version>
66
<name>PAPI-Expansion-Server</name>
77
<description>PlaceholderAPI expansion for server placeholders</description>
88

@@ -27,35 +27,25 @@
2727
<dependency>
2828
<groupId>me.clip</groupId>
2929
<artifactId>placeholderapi</artifactId>
30-
<version>2.10.10</version>
30+
<version>2.11.2</version>
3131
<scope>provided</scope>
3232
<exclusions>
33-
<exclusion>
34-
<groupId>me.rayzr522</groupId>
35-
<artifactId>jsonmessage</artifactId>
36-
</exclusion>
3733
<exclusion>
3834
<groupId>org.bstats</groupId>
3935
<artifactId>bstats-bukkit</artifactId>
4036
</exclusion>
4137
</exclusions>
4238
</dependency>
43-
<dependency>
44-
<groupId>com.github.ben-manes.caffeine</groupId>
45-
<artifactId>caffeine</artifactId>
46-
<version>1.3.3</version>
47-
<scope>compile</scope>
48-
</dependency>
4939
</dependencies>
5040

5141
<build>
5242
<plugins>
5343
<plugin>
5444
<groupId>org.apache.maven.plugins</groupId>
5545
<artifactId>maven-jar-plugin</artifactId>
56-
<version>2.3.2</version>
46+
<version>3.3.0</version>
5747
<configuration>
58-
<finalName>${name}</finalName>
48+
<finalName>${project.name}</finalName>
5949
<archive>
6050
<manifest>
6151
<addDefaultImplementationEntries>true</addDefaultImplementationEntries>
@@ -67,36 +57,14 @@
6757
<plugin>
6858
<groupId>org.apache.maven.plugins</groupId>
6959
<artifactId>maven-compiler-plugin</artifactId>
70-
<version>3.2</version>
60+
<version>3.11.0</version>
7161
<configuration>
7262
<target>1.8</target>
7363
<source>1.8</source>
7464
<encoding>UTF-8</encoding>
7565
<useIncrementalCompilation>false</useIncrementalCompilation>
7666
</configuration>
7767
</plugin>
78-
<plugin>
79-
<groupId>org.apache.maven.plugins</groupId>
80-
<artifactId>maven-shade-plugin</artifactId>
81-
<version>3.2.4</version>
82-
<configuration>
83-
<minimizeJar>true</minimizeJar>
84-
<relocations>
85-
<relocation>
86-
<pattern>com.github.benmanes.caffeine</pattern>
87-
<shadedPattern>com.extendedclip.papi.expansion.server.caffeine</shadedPattern>
88-
</relocation>
89-
</relocations>
90-
</configuration>
91-
<executions>
92-
<execution>
93-
<phase>package</phase>
94-
<goals>
95-
<goal>shade</goal>
96-
</goals>
97-
</execution>
98-
</executions>
99-
</plugin>
10068
</plugins>
10169
</build>
10270
</project>

src/main/java/com/extendedclip/papi/expansion/server/ServerExpansion.java

+44-33
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@
2020
*/
2121
package com.extendedclip.papi.expansion.server;
2222

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;
2525
import me.clip.placeholderapi.PlaceholderAPI;
2626
import me.clip.placeholderapi.PlaceholderAPIPlugin;
2727
import me.clip.placeholderapi.expansion.Cacheable;
@@ -33,16 +33,21 @@
3333
import org.bukkit.World;
3434
import org.bukkit.entity.Player;
3535
import org.jetbrains.annotations.NotNull;
36+
import org.jetbrains.annotations.Nullable;
3637

3738
import java.lang.management.ManagementFactory;
3839
import java.text.SimpleDateFormat;
3940
import java.time.Duration;
4041
import java.time.temporal.ChronoUnit;
4142
import java.util.Date;
4243
import java.util.HashMap;
44+
import java.util.List;
4345
import java.util.Map;
4446
import java.util.StringJoiner;
47+
import java.util.concurrent.Callable;
48+
import java.util.concurrent.ExecutionException;
4549
import java.util.concurrent.TimeUnit;
50+
import java.util.logging.Level;
4651

4752
public class ServerExpansion extends PlaceholderExpansion implements Cacheable, Configurable {
4853

@@ -58,7 +63,7 @@ public class ServerExpansion extends PlaceholderExpansion implements Cacheable,
5863
private String high = "&a";
5964
// -----
6065

61-
private final Cache<String, Integer> cache = Caffeine.newBuilder()
66+
private final Cache<String, Integer> cache = CacheBuilder.newBuilder()
6267
.expireAfterWrite(1, TimeUnit.MINUTES)
6368
.build();
6469

@@ -105,6 +110,38 @@ public Map<String, Object> getDefaults() {
105110
return defaults;
106111
}
107112

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+
108145
@Override
109146
public String onRequest(OfflinePlayer p, @NotNull String identifier) {
110147
final int MB = 1048576;
@@ -157,11 +194,11 @@ public String onRequest(OfflinePlayer p, @NotNull String identifier) {
157194
long seconds = TimeUnit.MILLISECONDS.toSeconds(ManagementFactory.getRuntimeMXBean().getUptime());
158195
return formatTime(Duration.of(seconds, ChronoUnit.SECONDS));
159196
case "total_chunks":
160-
return String.valueOf(cache.get("chunks", k -> getChunks()));
197+
return getCached("chunks", this::getChunks);
161198
case "total_living_entities":
162-
return String.valueOf(cache.get("livingEntities", k -> getLivingEntities()));
199+
return getCached("livingEntities", this::getLivingEntities);
163200
case "total_entities":
164-
return String.valueOf(cache.get("totalEntities", k -> getTotalEntities()));
201+
return getCached("totalEntities", this::getTotalEntities);
165202
case "has_whitelist":
166203
return Bukkit.getServer().hasWhitelist() ? PlaceholderAPIPlugin.booleanTrue() : PlaceholderAPIPlugin.booleanFalse();
167204
}
@@ -408,31 +445,5 @@ private String getPercent(double tps){
408445

409446
return (tps > 20.0 ? "*" : "") + finalPercent + "%";
410447
}
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+
438449
}

0 commit comments

Comments
 (0)