|
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.util.Map;
|
| 9 | +import java.util.OptionalInt; |
8 | 10 | import java.util.TreeMap;
|
9 | 11 |
|
10 | 12 | public final class ServerUtil {
|
@@ -32,6 +34,9 @@ public final class ServerUtil {
|
32 | 34 | }
|
33 | 35 |
|
34 | 36 | private static String findVariant() {
|
| 37 | + if (hasServerBuildInfo()) |
| 38 | + return getVariant0(); |
| 39 | + |
35 | 40 | for (final Map.Entry<String, String> entry : variants.entrySet()) {
|
36 | 41 | try {
|
37 | 42 | Class.forName(entry.getKey());
|
@@ -97,6 +102,9 @@ private static Field getTpsHandler() {
|
97 | 102 | }
|
98 | 103 |
|
99 | 104 | private static String findBuild() {
|
| 105 | + if (hasServerBuildInfo()) |
| 106 | + return getBuild0(); |
| 107 | + |
100 | 108 | String[] buildParts = Bukkit.getVersion().split("-");
|
101 | 109 |
|
102 | 110 | switch (getVariant().toLowerCase()) {
|
@@ -153,4 +161,25 @@ public static double[] getTps() {
|
153 | 161 | }
|
154 | 162 | }
|
155 | 163 |
|
| 164 | + // Available since recent 1.20.6 versions of Paper. Allows easier retrieval of certain info. |
| 165 | + private static boolean hasServerBuildInfo() { |
| 166 | + try { |
| 167 | + Class.forName("io.papermc.paper.ServerBuildInfo"); |
| 168 | + return true; |
| 169 | + } catch(ClassNotFoundException ignored) { |
| 170 | + return false; |
| 171 | + } |
| 172 | + } |
| 173 | + |
| 174 | + private static String getVariant0() { |
| 175 | + return ServerBuildInfo.buildInfo().brandName(); |
| 176 | + } |
| 177 | + |
| 178 | + private static String getBuild0() { |
| 179 | + OptionalInt buildNumber = ServerBuildInfo.buildInfo().buildNumber(); |
| 180 | + if (buildNumber.isEmpty()) |
| 181 | + return "Unknown"; |
| 182 | + |
| 183 | + return String.valueOf(buildNumber.getAsInt()); |
| 184 | + } |
156 | 185 | }
|
0 commit comments