2525import org .bukkit .Bukkit ;
2626import org .jetbrains .annotations .Nullable ;
2727
28+ import java .util .Arrays ;
29+ import java .util .regex .Matcher ;
30+ import java .util .regex .Pattern ;
31+
2832/**
2933 * Enum for major Minecraft versions where Brewery needs
3034 * to handle things differently.
@@ -55,8 +59,10 @@ public enum MinecraftVersion {
5559 V1_21_11 ("1.21.11" ),
5660 UNKNOWN ("Unknown" );
5761
62+ private static final Pattern VERSION_PATTERN = Pattern .compile ("^([0-9]+)\\ .([0-9]+)(?:\\ .([0-9]+))?" );
5863
59- private @ Getter static final boolean isFolia = MinecraftVersion .checkFolia ();
64+ private @ Getter static final boolean isFolia = ClassUtil .exists ("io.papermc.paper.threadedregions.RegionizedServer" );
65+ private @ Getter static final boolean isCanvas = ClassUtil .exists ("io.canvasmc.canvas.Config" ); // Popular Folia fork
6066 private @ Getter static final boolean useNBT = NBTUtil .initNbt ();
6167
6268 private final String [] versions ;
@@ -91,12 +97,14 @@ public static MinecraftVersion get(String major, String minor, @Nullable String
9197 public static MinecraftVersion getIt () {
9298 String rawVersion = Bukkit .getVersion ();
9399 String rawVersionParsed = rawVersion .substring (rawVersion .indexOf ("(MC: " ) + 5 , rawVersion .indexOf (")" ));
94- String [] versionSplit = rawVersionParsed .split ("\\ ." );
95-
96- Preconditions .checkState (versionSplit .length == 3 || versionSplit .length == 2 , "Unexpected Minecraft version format: " + rawVersionParsed );
97100
101+ Matcher matcher = VERSION_PATTERN .matcher (rawVersionParsed );
102+ if (!matcher .find ()) {
103+ throw new IllegalStateException ("Could not parse Minecraft version from: " + rawVersion );
104+ }
105+ Preconditions .checkState (matcher .groupCount () == 3 || matcher .groupCount () == 2 , "Unexpected Minecraft version format: " + rawVersionParsed );
98106
99- return get (versionSplit [ 0 ], versionSplit [ 1 ], versionSplit [ 2 ] );
107+ return get (matcher . group ( 1 ), matcher . group ( 2 ), matcher . group ( 3 ) );
100108 }
101109
102110 public boolean isOrLater (MinecraftVersion version ) {
@@ -110,13 +118,4 @@ public boolean isOrEarlier(MinecraftVersion version) {
110118 public String getVersion () {
111119 return versions [0 ];
112120 }
113-
114- private static boolean checkFolia () {
115- try {
116- Class .forName ("io.papermc.paper.threadedregions.RegionizedServer" );
117- return true ;
118- } catch (ClassNotFoundException ignored ) {
119- return false ;
120- }
121- }
122121}
0 commit comments