Skip to content

Commit 78003bc

Browse files
committed
Improve ServerUtils and TPS resolving
1 parent daeb8a8 commit 78003bc

3 files changed

Lines changed: 159 additions & 115 deletions

File tree

pom.xml

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,8 @@
88

99
<repositories>
1010
<repository>
11-
<id>spigot-repo</id>
12-
<url>https://hub.spigotmc.org/nexus/content/repositories/snapshots/</url>
13-
</repository>
14-
<repository>
15-
<id>sonatype</id>
16-
<url>https://oss.sonatype.org/content/groups/public/</url>
11+
<id>paper-repo</id>
12+
<url>https://papermc.io/repo/repository/maven-public/</url>
1713
</repository>
1814
<repository>
1915
<id>placeholderapi</id>
@@ -23,9 +19,9 @@
2319

2420
<dependencies>
2521
<dependency>
26-
<groupId>org.spigotmc</groupId>
27-
<artifactId>spigot-api</artifactId>
28-
<version>1.17-R0.1-SNAPSHOT</version>
22+
<groupId>io.papermc.paper</groupId>
23+
<artifactId>paper-api</artifactId>
24+
<version>1.17.1-R0.1-SNAPSHOT</version>
2925
<scope>provided</scope>
3026
</dependency>
3127
<dependency>

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

Lines changed: 23 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@
3535
import org.jetbrains.annotations.NotNull;
3636

3737
import java.lang.management.ManagementFactory;
38-
import java.lang.reflect.Field;
3938
import java.text.SimpleDateFormat;
4039
import java.time.Duration;
4140
import java.time.temporal.ChronoUnit;
@@ -44,16 +43,13 @@
4443
import java.util.Map;
4544
import java.util.StringJoiner;
4645
import java.util.concurrent.TimeUnit;
47-
import java.util.regex.Matcher;
48-
import java.util.regex.Pattern;
4946

5047
public class ServerExpansion extends PlaceholderExpansion implements Cacheable, Configurable {
51-
48+
49+
private final ServerUtils serverUtils;
50+
5251
private final Map<String, SimpleDateFormat> dateFormats = new HashMap<>();
5352
private final Runtime runtime = Runtime.getRuntime();
54-
private Object craftServer;
55-
private Field tps;
56-
private String version;
5753
private final String variant;
5854

5955
// config stuff
@@ -70,20 +66,9 @@ public class ServerExpansion extends PlaceholderExpansion implements Cacheable,
7066
private final String VERSION = getClass().getPackage().getImplementationVersion();
7167

7268
public ServerExpansion() {
73-
this.version = Bukkit.getServer().getClass().getPackage().getName().split("\\.")[3];
69+
this.serverUtils = new ServerUtils();
7470

75-
try {
76-
if (minecraftVersion() >= 17) {
77-
craftServer = Class.forName("net.minecraft.server.MinecraftServer").getMethod("getServer").invoke(null);
78-
} else {
79-
craftServer = Class.forName("net.minecraft.server." + version + ".MinecraftServer").getMethod("getServer").invoke(null);
80-
}
81-
tps = craftServer.getClass().getField("recentTps");
82-
} catch (Exception e) {
83-
e.printStackTrace();
84-
}
85-
86-
this.variant = ServerUtils.getServerVariant();
71+
this.variant = serverUtils.getServerVariant();
8772
}
8873

8974
@Override
@@ -97,9 +82,7 @@ public boolean canRegister() {
9782

9883
@Override
9984
public void clear() {
100-
craftServer = null;
101-
tps = null;
102-
version = null;
85+
serverUtils.clear();
10386
dateFormats.clear();
10487

10588
cache.invalidateAll();
@@ -146,12 +129,12 @@ public String onRequest(OfflinePlayer p, String identifier) {
146129

147130
// Version placeholders
148131
case "version":
149-
return ServerUtils.VERSION;
132+
return serverUtils.getVersion();
150133
case "build":
151-
return ServerUtils.BUILD;
134+
return serverUtils.getBuild();
152135
case "version_build":
153136
case "version_full":
154-
return ServerUtils.VERSION + '-' + ServerUtils.BUILD;
137+
return serverUtils.getVersion() + '-' + serverUtils.getBuild();
155138
// -----
156139

157140
// Ram placeholders
@@ -296,7 +279,7 @@ public String onRequest(OfflinePlayer p, String identifier) {
296279
public String getTps(String arg) {
297280
if (arg == null || arg.isEmpty()) {
298281
StringBuilder sb = new StringBuilder();
299-
for (double t : tps()) {
282+
for (double t : serverUtils.getTps()) {
300283
sb.append(getColoredTps(t))
301284
.append(ChatColor.GRAY)
302285
.append(", ");
@@ -306,49 +289,49 @@ public String getTps(String arg) {
306289
switch (arg) {
307290
case "1":
308291
case "one":
309-
return String.valueOf(fix(tps()[0]));
292+
return String.valueOf(fix(serverUtils.getTps()[0]));
310293
case "5":
311294
case "five":
312-
return String.valueOf(fix(tps()[1]));
295+
return String.valueOf(fix(serverUtils.getTps()[1]));
313296
case "15":
314297
case "fifteen":
315-
return String.valueOf(tps()[2]);
298+
return String.valueOf(serverUtils.getTps()[2]);
316299
case "1_colored":
317300
case "one_colored":
318-
return getColoredTps(tps()[0]);
301+
return getColoredTps(serverUtils.getTps()[0]);
319302
case "5_colored":
320303
case "five_colored":
321-
return getColoredTps(tps()[1]);
304+
return getColoredTps(serverUtils.getTps()[1]);
322305
case "15_colored":
323306
case "fifteen_colored":
324-
return getColoredTps(tps()[2]);
307+
return getColoredTps(serverUtils.getTps()[2]);
325308
case "percent": {
326309
final StringJoiner joiner = new StringJoiner(ChatColor.GRAY + ", ");
327310

328-
for (double t : tps()) {
311+
for (double t : serverUtils.getTps()) {
329312
joiner.add(getColoredTpsPercent(t));
330313
}
331314

332315
return joiner.toString();
333316
}
334317
case "1_percent":
335318
case "one_percent":
336-
return getPercent(tps()[0]);
319+
return getPercent(serverUtils.getTps()[0]);
337320
case "5_percent":
338321
case "five_percent":
339-
return getPercent(tps()[1]);
322+
return getPercent(serverUtils.getTps()[1]);
340323
case "15_percent":
341324
case "fifteen_percent":
342-
return getPercent(tps()[2]);
325+
return getPercent(serverUtils.getTps()[2]);
343326
case "1_percent_colored":
344327
case "one_percent_colored":
345-
return getColoredTpsPercent(tps()[0]);
328+
return getColoredTpsPercent(serverUtils.getTps()[0]);
346329
case "5_percent_colored":
347330
case "five_percent_colored":
348-
return getColoredTpsPercent(tps()[1]);
331+
return getColoredTpsPercent(serverUtils.getTps()[1]);
349332
case "15_percent_colored":
350333
case "fifteen_percent_colored":
351-
return getColoredTpsPercent(tps()[2]);
334+
return getColoredTpsPercent(serverUtils.getTps()[2]);
352335
}
353336
return null;
354337
}
@@ -409,18 +392,6 @@ public static String formatTime(final Duration duration) {
409392
return builder.toString();
410393
}
411394

412-
private double[] tps() {
413-
if (version == null || craftServer == null || tps == null) {
414-
return new double[] { 0, 0, 0 };
415-
}
416-
try {
417-
return ((double[]) tps.get(craftServer));
418-
} catch (IllegalAccessException e) {
419-
e.printStackTrace();
420-
}
421-
return new double[] { 0, 0, 0 };
422-
}
423-
424395
private double fix(double tps) {
425396
return Math.min(Math.round(tps * 100.0) / 100.0, 20.0);
426397
}
@@ -469,24 +440,4 @@ private String getPercent(double tps){
469440
return Math.min(Math.round(100 / 20.0 * tps), 100.0) + "%";
470441
}
471442

472-
/**
473-
* Helper method to return the major version that the server is running.
474-
*
475-
* This is needed because in 1.17, NMS is no longer versioned.
476-
*
477-
* @return the major version of Minecraft the server is running
478-
*/
479-
public static int minecraftVersion() {
480-
try {
481-
final Matcher matcher = Pattern.compile("\\(MC: (\\d)\\.(\\d+)\\.?(\\d+?)?\\)").matcher(Bukkit.getVersion());
482-
if (matcher.find()) {
483-
return Integer.parseInt(matcher.toMatchResult().group(2), 10);
484-
} else {
485-
throw new IllegalArgumentException(String.format("No match found in '%s'", Bukkit.getVersion()));
486-
}
487-
} catch (final IllegalArgumentException ex) {
488-
throw new RuntimeException("Failed to determine Minecraft version", ex);
489-
}
490-
}
491-
492443
}

0 commit comments

Comments
 (0)