Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 5 additions & 37 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>com.extendedclip.papi.expansion.server</groupId>
<artifactId>server-expansion</artifactId>
<version>2.6.1</version>
<version>2.6.2</version>
<name>PAPI-Expansion-Server</name>
<description>PlaceholderAPI expansion for server placeholders</description>

Expand All @@ -27,35 +27,25 @@
<dependency>
<groupId>me.clip</groupId>
<artifactId>placeholderapi</artifactId>
<version>2.10.10</version>
<version>2.11.2</version>
<scope>provided</scope>
<exclusions>
<exclusion>
<groupId>me.rayzr522</groupId>
<artifactId>jsonmessage</artifactId>
</exclusion>
<exclusion>
<groupId>org.bstats</groupId>
<artifactId>bstats-bukkit</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>com.github.ben-manes.caffeine</groupId>
<artifactId>caffeine</artifactId>
<version>1.3.3</version>
<scope>compile</scope>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.3.2</version>
<version>3.3.0</version>
<configuration>
<finalName>${name}</finalName>
<finalName>${project.name}</finalName>
<archive>
<manifest>
<addDefaultImplementationEntries>true</addDefaultImplementationEntries>
Expand All @@ -67,36 +57,14 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.2</version>
<version>3.11.0</version>
<configuration>
<target>1.8</target>
<source>1.8</source>
<encoding>UTF-8</encoding>
<useIncrementalCompilation>false</useIncrementalCompilation>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.2.4</version>
<configuration>
<minimizeJar>true</minimizeJar>
<relocations>
<relocation>
<pattern>com.github.benmanes.caffeine</pattern>
<shadedPattern>com.extendedclip.papi.expansion.server.caffeine</shadedPattern>
</relocation>
</relocations>
</configuration>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@
*/
package com.extendedclip.papi.expansion.server;

import com.github.benmanes.caffeine.cache.Cache;
import com.github.benmanes.caffeine.cache.Caffeine;
import com.google.common.cache.Cache;
import com.google.common.cache.CacheBuilder;
import me.clip.placeholderapi.PlaceholderAPI;
import me.clip.placeholderapi.PlaceholderAPIPlugin;
import me.clip.placeholderapi.expansion.Cacheable;
Expand All @@ -33,16 +33,21 @@
import org.bukkit.World;
import org.bukkit.entity.Player;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.lang.management.ManagementFactory;
import java.text.SimpleDateFormat;
import java.time.Duration;
import java.time.temporal.ChronoUnit;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.StringJoiner;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.logging.Level;

public class ServerExpansion extends PlaceholderExpansion implements Cacheable, Configurable {

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

private final Cache<String, Integer> cache = Caffeine.newBuilder()
private final Cache<String, Integer> cache = CacheBuilder.newBuilder()
.expireAfterWrite(1, TimeUnit.MINUTES)
.build();

Expand Down Expand Up @@ -105,6 +110,38 @@ public Map<String, Object> getDefaults() {
return defaults;
}

private @Nullable String getCached(String key, Callable<Integer> callable) {
try {
return String.valueOf(cache.get(key, callable));
} catch (ExecutionException e) {
if (getPlaceholderAPI().getPlaceholderAPIConfig().isDebugMode()) {
getPlaceholderAPI().getLogger().log(Level.SEVERE, "[server] Could not access cache key " + key, e);
}
return "";
}
}

private int getChunks(){
return Bukkit.getWorlds()
.stream()
.mapToInt(world -> world.getLoadedChunks().length)
.sum();
}

private int getLivingEntities(){
return Bukkit.getWorlds()
.stream()
.mapToInt(world -> world.getLivingEntities().size())
.sum();
}

private Integer getTotalEntities(){
return Bukkit.getWorlds()
.stream()
.mapToInt(world -> world.getEntities().size())
.sum();
}

@Override
public String onRequest(OfflinePlayer p, @NotNull String identifier) {
final int MB = 1048576;
Expand Down Expand Up @@ -157,11 +194,11 @@ public String onRequest(OfflinePlayer p, @NotNull String identifier) {
long seconds = TimeUnit.MILLISECONDS.toSeconds(ManagementFactory.getRuntimeMXBean().getUptime());
return formatTime(Duration.of(seconds, ChronoUnit.SECONDS));
case "total_chunks":
return String.valueOf(cache.get("chunks", k -> getChunks()));
return getCached("chunks", this::getChunks);
case "total_living_entities":
return String.valueOf(cache.get("livingEntities", k -> getLivingEntities()));
return getCached("livingEntities", this::getLivingEntities);
case "total_entities":
return String.valueOf(cache.get("totalEntities", k -> getTotalEntities()));
return getCached("totalEntities", this::getTotalEntities);
case "has_whitelist":
return Bukkit.getServer().hasWhitelist() ? PlaceholderAPIPlugin.booleanTrue() : PlaceholderAPIPlugin.booleanFalse();
}
Expand Down Expand Up @@ -408,31 +445,5 @@ private String getPercent(double tps){

return (tps > 20.0 ? "*" : "") + finalPercent + "%";
}

private Integer getChunks(){
int loadedChunks = 0;
for (final World world : Bukkit.getWorlds()) {
loadedChunks += world.getLoadedChunks().length;
}

return loadedChunks;
}

private Integer getLivingEntities(){
int livingEntities = 0;
for (final World world : Bukkit.getWorlds()) {
livingEntities += world.getLivingEntities().size();
}

return livingEntities;
}

private Integer getTotalEntities(){
int allEntities = 0;
for (World world : Bukkit.getWorlds()) {
allEntities += world.getEntities().size();
}

return allEntities;
}

}