Skip to content

Commit 8ad875e

Browse files
authored
Merge pull request #38 from PlaceholderAPI/feature/improve-serverutils
2 parents daeb8a8 + 4ec200a commit 8ad875e

File tree

4 files changed

+175
-140
lines changed

4 files changed

+175
-140
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,5 @@
22
build/
33
target/
44

5-
*.iml
5+
*.iml
6+
/dependency-reduced-pom.xml

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: 42 additions & 96 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,17 +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 ServerUtils serverUtils = null;
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;
57-
private final String variant;
5853

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

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

72-
public ServerExpansion() {
73-
this.version = Bukkit.getServer().getClass().getPackage().getName().split("\\.")[3];
74-
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();
87-
}
88-
8967
@Override
9068
public boolean canRegister() {
9169
serverName = this.getString("server_name", "A Minecraft Server");
@@ -97,11 +75,8 @@ public boolean canRegister() {
9775

9876
@Override
9977
public void clear() {
100-
craftServer = null;
101-
tps = null;
102-
version = null;
10378
dateFormats.clear();
104-
79+
serverUtils = null;
10580
cache.invalidateAll();
10681
}
10782

@@ -131,8 +106,11 @@ public Map<String, Object> getDefaults() {
131106
}
132107

133108
@Override
134-
public String onRequest(OfflinePlayer p, String identifier) {
109+
public String onRequest(OfflinePlayer p, @NotNull String identifier) {
135110
final int MB = 1048576;
111+
if (serverUtils == null) {
112+
serverUtils = new ServerUtils();
113+
}
136114

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

147125
// Version placeholders
148126
case "version":
149-
return ServerUtils.VERSION;
127+
return serverUtils.getVersion();
150128
case "build":
151-
return ServerUtils.BUILD;
129+
return serverUtils.getBuild();
152130
case "version_build":
153131
case "version_full":
154-
return ServerUtils.VERSION + '-' + ServerUtils.BUILD;
132+
return serverUtils.getVersion() + '-' + serverUtils.getBuild();
155133
// -----
156134

157135
// Ram placeholders
@@ -169,7 +147,7 @@ public String onRequest(OfflinePlayer p, String identifier) {
169147
case "name":
170148
return serverName == null ? "" : serverName;
171149
case "variant":
172-
return variant;
150+
return serverUtils.getServerVariant();
173151
// -----
174152

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

296274
public String getTps(String arg) {
297275
if (arg == null || arg.isEmpty()) {
298-
StringBuilder sb = new StringBuilder();
299-
for (double t : tps()) {
300-
sb.append(getColoredTps(t))
301-
.append(ChatColor.GRAY)
302-
.append(", ");
276+
StringJoiner joiner = new StringJoiner(ChatColor.GRAY + ", ");
277+
for (double tps : serverUtils.getTps()) {
278+
joiner.add(getColoredTps(tps));
303279
}
304-
return sb.toString();
280+
return joiner.toString();
305281
}
306-
switch (arg) {
282+
switch (arg) {
307283
case "1":
308-
case "one":
309-
return String.valueOf(fix(tps()[0]));
284+
case "one":
285+
return fix(serverUtils.getTps()[0]);
310286
case "5":
311287
case "five":
312-
return String.valueOf(fix(tps()[1]));
288+
return fix(serverUtils.getTps()[1]);
313289
case "15":
314290
case "fifteen":
315-
return String.valueOf(tps()[2]);
291+
return fix(serverUtils.getTps()[2]);
316292
case "1_colored":
317293
case "one_colored":
318-
return getColoredTps(tps()[0]);
294+
return getColoredTps(serverUtils.getTps()[0]);
319295
case "5_colored":
320296
case "five_colored":
321-
return getColoredTps(tps()[1]);
297+
return getColoredTps(serverUtils.getTps()[1]);
322298
case "15_colored":
323299
case "fifteen_colored":
324-
return getColoredTps(tps()[2]);
300+
return getColoredTps(serverUtils.getTps()[2]);
325301
case "percent": {
326302
final StringJoiner joiner = new StringJoiner(ChatColor.GRAY + ", ");
327303

328-
for (double t : tps()) {
304+
for (double t : serverUtils.getTps()) {
329305
joiner.add(getColoredTpsPercent(t));
330306
}
331307

332308
return joiner.toString();
333309
}
334310
case "1_percent":
335311
case "one_percent":
336-
return getPercent(tps()[0]);
312+
return getPercent(serverUtils.getTps()[0]);
337313
case "5_percent":
338314
case "five_percent":
339-
return getPercent(tps()[1]);
315+
return getPercent(serverUtils.getTps()[1]);
340316
case "15_percent":
341317
case "fifteen_percent":
342-
return getPercent(tps()[2]);
318+
return getPercent(serverUtils.getTps()[2]);
343319
case "1_percent_colored":
344320
case "one_percent_colored":
345-
return getColoredTpsPercent(tps()[0]);
321+
return getColoredTpsPercent(serverUtils.getTps()[0]);
346322
case "5_percent_colored":
347323
case "five_percent_colored":
348-
return getColoredTpsPercent(tps()[1]);
324+
return getColoredTpsPercent(serverUtils.getTps()[1]);
349325
case "15_percent_colored":
350326
case "fifteen_percent_colored":
351-
return getColoredTpsPercent(tps()[2]);
327+
return getColoredTpsPercent(serverUtils.getTps()[2]);
352328
}
353329
return null;
354330
}
@@ -409,25 +385,14 @@ public static String formatTime(final Duration duration) {
409385
return builder.toString();
410386
}
411387

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-
424-
private double fix(double tps) {
425-
return Math.min(Math.round(tps * 100.0) / 100.0, 20.0);
388+
private String fix(double tps) {
389+
double finalTps = Math.min(Math.round(tps), 20.0);
390+
391+
return (tps > 20.0 ? "*" : "") + finalTps;
426392
}
427393

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

433398
private String getColoredTps(double tps) {
@@ -438,6 +403,12 @@ private String getColoredTpsPercent(double tps){
438403
return color(tps) + getPercent(tps);
439404
}
440405

406+
private String getPercent(double tps){
407+
double finalPercent = Math.min(Math.round(100 / 20.0 * tps), 100.0);
408+
409+
return (tps > 20.0 ? "*" : "") + finalPercent + "%";
410+
}
411+
441412
private Integer getChunks(){
442413
int loadedChunks = 0;
443414
for (final World world : Bukkit.getWorlds()) {
@@ -464,29 +435,4 @@ private Integer getTotalEntities(){
464435

465436
return allEntities;
466437
}
467-
468-
private String getPercent(double tps){
469-
return Math.min(Math.round(100 / 20.0 * tps), 100.0) + "%";
470-
}
471-
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-
492438
}

0 commit comments

Comments
 (0)