Improve ServerUtils and TPS resolving
This commit is contained in:
1 parent
daeb8a88cc
commit
78003bc532
3 files changed
+160
-116
No files matched your search
@@ -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>
|
||||
@@ -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>
|
||||
|
||||
@@ -35,7 +35,6 @@ import org.bukkit.entity.Player;
|
||||
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;
|
||||
@@ -44,16 +43,13 @@ import java.util.HashMap;
|
||||
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 final ServerUtils serverUtils;
|
||||
|
||||
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
|
||||
@@ -70,20 +66,9 @@ 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];
|
||||
this.serverUtils = new ServerUtils();
|
||||
|
||||
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();
|
||||
this.variant = serverUtils.getServerVariant();
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -97,9 +82,7 @@ public class ServerExpansion extends PlaceholderExpansion implements Cacheable,
|
||||
|
||||
@Override
|
||||
public void clear() {
|
||||
craftServer = null;
|
||||
tps = null;
|
||||
version = null;
|
||||
serverUtils.clear();
|
||||
dateFormats.clear();
|
||||
|
||||
cache.invalidateAll();
|
||||
@@ -146,12 +129,12 @@ public class ServerExpansion extends PlaceholderExpansion implements Cacheable,
|
||||
|
||||
// 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
|
||||
@@ -296,7 +279,7 @@ public class ServerExpansion extends PlaceholderExpansion implements Cacheable,
|
||||
public String getTps(String arg) {
|
||||
if (arg == null || arg.isEmpty()) {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
for (double t : tps()) {
|
||||
for (double t : serverUtils.getTps()) {
|
||||
sb.append(getColoredTps(t))
|
||||
.append(ChatColor.GRAY)
|
||||
.append(", ");
|
||||
@@ -306,26 +289,26 @@ public class ServerExpansion extends PlaceholderExpansion implements Cacheable,
|
||||
switch (arg) {
|
||||
case "1":
|
||||
case "one":
|
||||
return String.valueOf(fix(tps()[0]));
|
||||
return String.valueOf(fix(serverUtils.getTps()[0]));
|
||||
case "5":
|
||||
case "five":
|
||||
return String.valueOf(fix(tps()[1]));
|
||||
return String.valueOf(fix(serverUtils.getTps()[1]));
|
||||
case "15":
|
||||
case "fifteen":
|
||||
return String.valueOf(tps()[2]);
|
||||
return String.valueOf(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));
|
||||
}
|
||||
|
||||
@@ -333,22 +316,22 @@ public class ServerExpansion extends PlaceholderExpansion implements Cacheable,
|
||||
}
|
||||
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;
|
||||
}
|
||||
@@ -409,18 +392,6 @@ public class ServerExpansion extends PlaceholderExpansion implements Cacheable,
|
||||
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);
|
||||
}
|
||||
@@ -469,24 +440,4 @@ public class ServerExpansion extends PlaceholderExpansion implements Cacheable,
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,51 +1,148 @@
|
||||
package com.extendedclip.papi.expansion.server;
|
||||
|
||||
import me.clip.placeholderapi.PlaceholderAPIPlugin;
|
||||
import org.bukkit.Bukkit;
|
||||
|
||||
import java.lang.reflect.Field;
|
||||
import java.util.HashMap;
|
||||
import java.util.Locale;
|
||||
import java.util.Map;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
public final class ServerUtils {
|
||||
|
||||
public static final String VERSION = Bukkit.getBukkitVersion().split("-")[0];
|
||||
public static final String BUILD;
|
||||
|
||||
private static final Map<String, String> variants = new HashMap<>();
|
||||
|
||||
static {
|
||||
boolean isPaper = false;
|
||||
variants.put("Purpur", "net.pl3x.purpur.PurpurConfig");
|
||||
variants.put("Airplane", "gg.airplane.AirplaneConfig");
|
||||
variants.put("Tuinity", "com.tuinity.tuinity.config.TuinityConfig");
|
||||
private String version = null;
|
||||
private String build = null;
|
||||
private String variant = null;
|
||||
|
||||
private final Map<String, String> variants = new HashMap<>();
|
||||
|
||||
private Object craftServer = null;
|
||||
private Field tps = null;
|
||||
|
||||
private boolean hasTpsMethod = false;
|
||||
|
||||
public ServerUtils() {
|
||||
variants.put("Spigot", "org.spigotnc.SpigotConfig");
|
||||
variants.put("Paper", "com.destroystokyo.paper.PaperConfig");
|
||||
variants.put("Spigot", "org.spigotmc.SpigotConfig");
|
||||
|
||||
try {
|
||||
Class.forName("com.destroystokyo.paper.PaperConfig");
|
||||
isPaper = true;
|
||||
} catch (ClassNotFoundException ignored) { }
|
||||
|
||||
String[] buildParts = Bukkit.getVersion().split("-");
|
||||
|
||||
if (buildParts.length >= 3) {
|
||||
BUILD = isPaper ? buildParts[2].substring(0, buildParts[2].indexOf(" ")) : buildParts[0];
|
||||
} else {
|
||||
BUILD = "UNKNOWN";
|
||||
}
|
||||
variants.put("Tuinity", "com.tuinity.tuinity.config.TuinityConfig");
|
||||
variants.put("Airplane", "gg.airplane.AirplaneConfig");
|
||||
variants.put("Purpur", "net.pl3x.purpur.PurpurConfig");
|
||||
|
||||
resolveTPSHandler();
|
||||
}
|
||||
|
||||
private ServerUtils() { }
|
||||
|
||||
public static String getServerVariant() {
|
||||
|
||||
for (Map.Entry<String, String> variant : variants.entrySet()) {
|
||||
|
||||
public String getServerVariant() {
|
||||
if (variant != null) {
|
||||
return variant;
|
||||
}
|
||||
|
||||
for(Map.Entry<String, String> variant : variants.entrySet()) {
|
||||
try {
|
||||
Class.forName(variant.getValue());
|
||||
return variant.getKey();
|
||||
} catch (ClassNotFoundException ignored) { }
|
||||
|
||||
return (this.variant = variant.getKey());
|
||||
} catch (ClassNotFoundException ignored) {}
|
||||
}
|
||||
|
||||
return (this.variant = "Unknown");
|
||||
}
|
||||
|
||||
public String getVersion() {
|
||||
if (version != null) {
|
||||
return version;
|
||||
}
|
||||
|
||||
return (version = Bukkit.getBukkitVersion().split("-")[0]);
|
||||
}
|
||||
|
||||
public String getBuild() {
|
||||
if (build != null) {
|
||||
return build;
|
||||
}
|
||||
|
||||
String[] buildParts = Bukkit.getVersion().split("-");
|
||||
switch (getServerVariant().toLowerCase(Locale.ROOT)) {
|
||||
case "spigot":
|
||||
// TODO: Find out what those variants return.
|
||||
case "tuinity":
|
||||
case "airplane":
|
||||
case "purpur":
|
||||
return (build = buildParts[0]);
|
||||
|
||||
case "paper":
|
||||
if (buildParts.length >= 3) {
|
||||
if (buildParts[2].contains(" ")) {
|
||||
return (build = buildParts[2].substring(0, buildParts[2].indexOf(" ")));
|
||||
} else {
|
||||
return (build = buildParts[2]);
|
||||
}
|
||||
} else {
|
||||
return (build = "Unknown");
|
||||
}
|
||||
|
||||
default:
|
||||
return (build = "Unknown");
|
||||
}
|
||||
}
|
||||
|
||||
public void clear() {
|
||||
craftServer = null;
|
||||
tps = null;
|
||||
}
|
||||
|
||||
public double[] getTps() {
|
||||
if (hasTpsMethod) {
|
||||
return Bukkit.getTPS();
|
||||
}
|
||||
|
||||
if (craftServer == null || tps == null) {
|
||||
return new double[]{0, 0, 0};
|
||||
}
|
||||
|
||||
try {
|
||||
return (double[]) tps.get(craftServer);
|
||||
} catch (IllegalAccessException ignored) {
|
||||
return new double[]{0, 0, 0};
|
||||
}
|
||||
}
|
||||
|
||||
private void resolveTPSHandler() {
|
||||
try {
|
||||
// If this throws is the server not a fork...
|
||||
Bukkit.getTPS();
|
||||
hasTpsMethod = true;
|
||||
} catch (Exception ignored) {
|
||||
final String mcVersion = Bukkit.getServer().getClass().getPackage().getName().split("\\.")[3];
|
||||
|
||||
try {
|
||||
if (getMajorVersion() >= 17) {
|
||||
craftServer = Class.forName("net.minecraft.server.MinecraftServer")
|
||||
.getMethod("getServer").invoke(null);
|
||||
} else {
|
||||
craftServer = Class.forName("net.minecraft.server." + mcVersion + ".MinecraftServer")
|
||||
.getMethod("getServer").invoke(null);
|
||||
}
|
||||
|
||||
tps = craftServer.getClass().getField("recentTps");
|
||||
} catch (Exception ex) {
|
||||
PlaceholderAPIPlugin.getInstance().getLogger().warning("Could not resolve TPS handling!");
|
||||
ex.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private int getMajorVersion() {
|
||||
final Matcher matcher = Pattern.compile("\\(MC: (\\d)\\.(\\d+)\\.?(\\d+?)?\\)").matcher(Bukkit.getVersion());
|
||||
if (matcher.find()) {
|
||||
try{
|
||||
return Integer.parseInt(matcher.toMatchResult().group(2), 10);
|
||||
} catch (NumberFormatException ignored) {
|
||||
return -1;
|
||||
}
|
||||
} else {
|
||||
return -1;
|
||||
}
|
||||
|
||||
return "Unknown";
|
||||
}
|
||||
|
||||
}
|
||||
Reference in new issue
Block a user