|
1 | 1 | package at.helpch.placeholderapi.expansion.server.util;
|
2 | 2 |
|
| 3 | +import io.papermc.paper.ServerBuildInfo; |
3 | 4 | import org.bukkit.Bukkit;
|
4 | 5 |
|
5 | 6 | import java.lang.reflect.Field;
|
6 | 7 | import java.lang.reflect.InvocationTargetException;
|
7 | 8 | import java.lang.reflect.Method;
|
8 | 9 | import java.util.Map;
|
| 10 | +import java.util.OptionalInt; |
9 | 11 | import java.util.TreeMap;
|
10 | 12 |
|
11 | 13 | public final class ServerUtil {
|
@@ -35,6 +37,9 @@ public final class ServerUtil {
|
35 | 37 | }
|
36 | 38 |
|
37 | 39 | private static String findVariant() {
|
| 40 | + if (hasServerBuildInfo()) |
| 41 | + return getVariant0(); |
| 42 | + |
38 | 43 | for (final Map.Entry<String, String> entry : variants.entrySet()) {
|
39 | 44 | try {
|
40 | 45 | Class.forName(entry.getKey());
|
@@ -99,6 +104,9 @@ private static Field getTpsHandler() {
|
99 | 104 | }
|
100 | 105 |
|
101 | 106 | private static String findBuild() {
|
| 107 | + if (hasServerBuildInfo()) |
| 108 | + return getBuild0(); |
| 109 | + |
102 | 110 | String[] buildParts = Bukkit.getVersion().split("-");
|
103 | 111 |
|
104 | 112 | switch (getVariant().toLowerCase()) {
|
@@ -159,4 +167,25 @@ public static double[] getTps() {
|
159 | 167 | }
|
160 | 168 | }
|
161 | 169 |
|
| 170 | + // Available since recent 1.20.6 versions of Paper. Allows easier retrieval of certain info. |
| 171 | + private static boolean hasServerBuildInfo() { |
| 172 | + try { |
| 173 | + Class.forName("io.papermc.paper.ServerBuildInfo"); |
| 174 | + return true; |
| 175 | + } catch(ClassNotFoundException ignored) { |
| 176 | + return false; |
| 177 | + } |
| 178 | + } |
| 179 | + |
| 180 | + private static String getVariant0() { |
| 181 | + return ServerBuildInfo.buildInfo().brandName(); |
| 182 | + } |
| 183 | + |
| 184 | + private static String getBuild0() { |
| 185 | + OptionalInt buildNumber = ServerBuildInfo.buildInfo().buildNumber(); |
| 186 | + if (buildNumber.isEmpty()) |
| 187 | + return "Unknown"; |
| 188 | + |
| 189 | + return String.valueOf(buildNumber.getAsInt()); |
| 190 | + } |
162 | 191 | }
|
0 commit comments