4343import java .util .HashMap ;
4444import java .util .Map ;
4545import java .util .concurrent .TimeUnit ;
46+ import java .util .regex .Matcher ;
47+ import java .util .regex .Pattern ;
4648
4749public class ServerExpansion extends PlaceholderExpansion implements Cacheable , Configurable {
4850
@@ -66,7 +68,11 @@ public class ServerExpansion extends PlaceholderExpansion implements Cacheable,
6668 public ServerExpansion () {
6769 try {
6870 version = Bukkit .getServer ().getClass ().getPackage ().getName ().split ("\\ ." )[3 ];
69- craftServer = Class .forName ("net.minecraft.server." + version + ".MinecraftServer" ).getMethod ("getServer" ).invoke (null );
71+ if (minecraftVersion () >= 17 ) {
72+ craftServer = Class .forName ("net.minecraft.server.MinecraftServer" ).getMethod ("getServer" ).invoke (null );
73+ } else {
74+ craftServer = Class .forName ("net.minecraft.server." + version + ".MinecraftServer" ).getMethod ("getServer" ).invoke (null );
75+ }
7076 tps = craftServer .getClass ().getField ("recentTps" );
7177 variant = initializeVariant ();
7278 } catch (Exception e ) {
@@ -462,4 +468,24 @@ private String getPercent(double tps){
462468 return Math .min (Math .round (100 / 20.0 * tps ), 100.0 ) + "%" ;
463469 }
464470
471+ /**
472+ * Helper method to return the major version that the server is running.
473+ *
474+ * This is needed because in 1.17, NMS is no longer versioned.
475+ *
476+ * @return the major version of Minecraft the server is running
477+ */
478+ public static int minecraftVersion () {
479+ try {
480+ final Matcher matcher = Pattern .compile ("\\ (MC: (\\ d)\\ .(\\ d+)\\ .?(\\ d+?)?\\ )" ).matcher (Bukkit .getVersion ());
481+ if (matcher .find ()) {
482+ return Integer .parseInt (matcher .toMatchResult ().group (2 ), 10 );
483+ } else {
484+ throw new IllegalArgumentException (String .format ("No match found in '%s'" , Bukkit .getVersion ()));
485+ }
486+ } catch (final IllegalArgumentException ex ) {
487+ throw new RuntimeException ("Failed to determine Minecraft version" , ex );
488+ }
489+ }
490+
465491}
0 commit comments