Skip to content

Commit

Permalink
Merge pull request #38 from PlaceholderAPI/feature/improve-serverutils
Browse files Browse the repository at this point in the history
  • Loading branch information
darbyjack authored Apr 19, 2022
2 parents daeb8a8 + 4ec200a commit 8ad875e
Show file tree
Hide file tree
Showing 4 changed files with 175 additions and 140 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@
build/
target/

*.iml
*.iml
/dependency-reduced-pom.xml
14 changes: 5 additions & 9 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,8 @@

<repositories>
<repository>
<id>spigot-repo</id>
<url>https://hub.spigotmc.org/nexus/content/repositories/snapshots/</url>
</repository>
<repository>
<id>sonatype</id>
<url>https://oss.sonatype.org/content/groups/public/</url>
<id>paper-repo</id>
<url>https://papermc.io/repo/repository/maven-public/</url>
</repository>
<repository>
<id>placeholderapi</id>
Expand All @@ -23,9 +19,9 @@

<dependencies>
<dependency>
<groupId>org.spigotmc</groupId>
<artifactId>spigot-api</artifactId>
<version>1.17-R0.1-SNAPSHOT</version>
<groupId>io.papermc.paper</groupId>
<artifactId>paper-api</artifactId>
<version>1.17.1-R0.1-SNAPSHOT</version>
<scope>provided</scope>
</dependency>
<dependency>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@
import org.jetbrains.annotations.NotNull;

import java.lang.management.ManagementFactory;
import java.lang.reflect.Field;
import java.text.SimpleDateFormat;
import java.time.Duration;
import java.time.temporal.ChronoUnit;
Expand All @@ -44,17 +43,13 @@
import java.util.Map;
import java.util.StringJoiner;
import java.util.concurrent.TimeUnit;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class ServerExpansion extends PlaceholderExpansion implements Cacheable, Configurable {


private ServerUtils serverUtils = null;

private final Map<String, SimpleDateFormat> dateFormats = new HashMap<>();
private final Runtime runtime = Runtime.getRuntime();
private Object craftServer;
private Field tps;
private String version;
private final String variant;

// config stuff
private String serverName;
Expand All @@ -69,23 +64,6 @@ public class ServerExpansion extends PlaceholderExpansion implements Cacheable,

private final String VERSION = getClass().getPackage().getImplementationVersion();

public ServerExpansion() {
this.version = Bukkit.getServer().getClass().getPackage().getName().split("\\.")[3];

try {
if (minecraftVersion() >= 17) {
craftServer = Class.forName("net.minecraft.server.MinecraftServer").getMethod("getServer").invoke(null);
} else {
craftServer = Class.forName("net.minecraft.server." + version + ".MinecraftServer").getMethod("getServer").invoke(null);
}
tps = craftServer.getClass().getField("recentTps");
} catch (Exception e) {
e.printStackTrace();
}

this.variant = ServerUtils.getServerVariant();
}

@Override
public boolean canRegister() {
serverName = this.getString("server_name", "A Minecraft Server");
Expand All @@ -97,11 +75,8 @@ public boolean canRegister() {

@Override
public void clear() {
craftServer = null;
tps = null;
version = null;
dateFormats.clear();

serverUtils = null;
cache.invalidateAll();
}

Expand Down Expand Up @@ -131,8 +106,11 @@ public Map<String, Object> getDefaults() {
}

@Override
public String onRequest(OfflinePlayer p, String identifier) {
public String onRequest(OfflinePlayer p, @NotNull String identifier) {
final int MB = 1048576;
if (serverUtils == null) {
serverUtils = new ServerUtils();
}

switch (identifier) {
// Players placeholders
Expand All @@ -146,12 +124,12 @@ public String onRequest(OfflinePlayer p, String identifier) {

// Version placeholders
case "version":
return ServerUtils.VERSION;
return serverUtils.getVersion();
case "build":
return ServerUtils.BUILD;
return serverUtils.getBuild();
case "version_build":
case "version_full":
return ServerUtils.VERSION + '-' + ServerUtils.BUILD;
return serverUtils.getVersion() + '-' + serverUtils.getBuild();
// -----

// Ram placeholders
Expand All @@ -169,7 +147,7 @@ public String onRequest(OfflinePlayer p, String identifier) {
case "name":
return serverName == null ? "" : serverName;
case "variant":
return variant;
return serverUtils.getServerVariant();
// -----

// Other placeholders
Expand Down Expand Up @@ -295,60 +273,58 @@ public String onRequest(OfflinePlayer p, String identifier) {

public String getTps(String arg) {
if (arg == null || arg.isEmpty()) {
StringBuilder sb = new StringBuilder();
for (double t : tps()) {
sb.append(getColoredTps(t))
.append(ChatColor.GRAY)
.append(", ");
StringJoiner joiner = new StringJoiner(ChatColor.GRAY + ", ");
for (double tps : serverUtils.getTps()) {
joiner.add(getColoredTps(tps));
}
return sb.toString();
return joiner.toString();
}
switch (arg) {
switch (arg) {
case "1":
case "one":
return String.valueOf(fix(tps()[0]));
case "one":
return fix(serverUtils.getTps()[0]);
case "5":
case "five":
return String.valueOf(fix(tps()[1]));
return fix(serverUtils.getTps()[1]);
case "15":
case "fifteen":
return String.valueOf(tps()[2]);
return fix(serverUtils.getTps()[2]);
case "1_colored":
case "one_colored":
return getColoredTps(tps()[0]);
return getColoredTps(serverUtils.getTps()[0]);
case "5_colored":
case "five_colored":
return getColoredTps(tps()[1]);
return getColoredTps(serverUtils.getTps()[1]);
case "15_colored":
case "fifteen_colored":
return getColoredTps(tps()[2]);
return getColoredTps(serverUtils.getTps()[2]);
case "percent": {
final StringJoiner joiner = new StringJoiner(ChatColor.GRAY + ", ");

for (double t : tps()) {
for (double t : serverUtils.getTps()) {
joiner.add(getColoredTpsPercent(t));
}

return joiner.toString();
}
case "1_percent":
case "one_percent":
return getPercent(tps()[0]);
return getPercent(serverUtils.getTps()[0]);
case "5_percent":
case "five_percent":
return getPercent(tps()[1]);
return getPercent(serverUtils.getTps()[1]);
case "15_percent":
case "fifteen_percent":
return getPercent(tps()[2]);
return getPercent(serverUtils.getTps()[2]);
case "1_percent_colored":
case "one_percent_colored":
return getColoredTpsPercent(tps()[0]);
return getColoredTpsPercent(serverUtils.getTps()[0]);
case "5_percent_colored":
case "five_percent_colored":
return getColoredTpsPercent(tps()[1]);
return getColoredTpsPercent(serverUtils.getTps()[1]);
case "15_percent_colored":
case "fifteen_percent_colored":
return getColoredTpsPercent(tps()[2]);
return getColoredTpsPercent(serverUtils.getTps()[2]);
}
return null;
}
Expand Down Expand Up @@ -409,25 +385,14 @@ public static String formatTime(final Duration duration) {
return builder.toString();
}

private double[] tps() {
if (version == null || craftServer == null || tps == null) {
return new double[] { 0, 0, 0 };
}
try {
return ((double[]) tps.get(craftServer));
} catch (IllegalAccessException e) {
e.printStackTrace();
}
return new double[] { 0, 0, 0 };
}

private double fix(double tps) {
return Math.min(Math.round(tps * 100.0) / 100.0, 20.0);
private String fix(double tps) {
double finalTps = Math.min(Math.round(tps), 20.0);

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

private String color(double tps) {
return ChatColor.translateAlternateColorCodes('&', (tps > 18.0) ? high : (tps > 16.0) ? medium : low)
+ ((tps > 20.0) ? "*" : "");
return ChatColor.translateAlternateColorCodes('&', (tps > 18.0) ? high : (tps > 16.0) ? medium : low);
}

private String getColoredTps(double tps) {
Expand All @@ -438,6 +403,12 @@ private String getColoredTpsPercent(double tps){
return color(tps) + getPercent(tps);
}

private String getPercent(double tps){
double finalPercent = Math.min(Math.round(100 / 20.0 * tps), 100.0);

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

private Integer getChunks(){
int loadedChunks = 0;
for (final World world : Bukkit.getWorlds()) {
Expand All @@ -464,29 +435,4 @@ private Integer getTotalEntities(){

return allEntities;
}

private String getPercent(double tps){
return Math.min(Math.round(100 / 20.0 * tps), 100.0) + "%";
}

/**
* Helper method to return the major version that the server is running.
*
* This is needed because in 1.17, NMS is no longer versioned.
*
* @return the major version of Minecraft the server is running
*/
public static int minecraftVersion() {
try {
final Matcher matcher = Pattern.compile("\\(MC: (\\d)\\.(\\d+)\\.?(\\d+?)?\\)").matcher(Bukkit.getVersion());
if (matcher.find()) {
return Integer.parseInt(matcher.toMatchResult().group(2), 10);
} else {
throw new IllegalArgumentException(String.format("No match found in '%s'", Bukkit.getVersion()));
}
} catch (final IllegalArgumentException ex) {
throw new RuntimeException("Failed to determine Minecraft version", ex);
}
}

}
Loading

0 comments on commit 8ad875e

Please sign in to comment.