4040
4141public final class LevelToolsUtil {
4242 public static final int MID_VERSION ;
43+ private static final Pattern MINECRAFT_VERSION_PATTERN =
44+ Pattern .compile ("^(\\ d+)(?:\\ .(\\ d+))?(?:\\ .(\\ d+))?(?:[-+].*)?$" );
4345 private static final String LORE_PREFIX = "§§" ;
4446 private static final boolean IS_PAPER = hasClass ("com.destroystokyo.paper.PaperConfig" ) || hasClass ("io.papermc.paper.configuration.Configuration" );
47+ private static final MinecraftVersion MINECRAFT_VERSION ;
4548
4649 static {
47- // Code from https://github.com/boxbeam/RedLib/blob/master/src/redempt/redlib/java
48- final Pattern pattern = Pattern .compile ("1\\ .([0-9]+)" );
49- final Matcher matcher = pattern .matcher (Bukkit .getBukkitVersion ());
50- matcher .find ();
51- MID_VERSION = Integer .parseInt (matcher .group (1 ));
50+ MINECRAFT_VERSION = parseMinecraftVersion (Bukkit .getBukkitVersion ());
51+ MID_VERSION = MINECRAFT_VERSION .getCompatibilityMajor ();
5252 }
5353
5454 private static boolean hasClass (String name ) {
@@ -86,13 +86,13 @@ public static ItemProfile getItemProfile(Material material) {
8686 }
8787
8888 public static ItemStack getHand (Player player ) {
89- return MID_VERSION >= 9
89+ return supportsDualWielding ()
9090 ? player .getInventory ().getItemInMainHand ().clone ()
9191 : player .getItemInHand ().clone ();
9292 }
9393
9494 public static void setHand (Player player , ItemStack stack ) {
95- if (MID_VERSION >= 9 ) {
95+ if (supportsDualWielding () ) {
9696 player .getInventory ().setItemInMainHand (stack );
9797 } else {
9898 player .setItemInHand (stack );
@@ -106,7 +106,7 @@ public static void setItemInSlot(@NotNull Player player, @Nullable TriggerSlot s
106106 }
107107
108108 if (slot == TriggerSlot .OFF_HAND ) {
109- if (MID_VERSION >= 9 ) {
109+ if (supportsDualWielding () ) {
110110 player .getInventory ().setItemInOffHand (stack );
111111 } else {
112112 setHand (player , stack );
@@ -156,8 +156,8 @@ public static int roundDown(double value) {
156156 }
157157
158158 public static LevelToolsItem createLevelToolsItem (ItemStack stack ) {
159- if (MID_VERSION >= 14 ) {
160- if (MID_VERSION < 18 ) {
159+ if (supportsPersistentDataContainer () ) {
160+ if (shouldCheckLegacyNbtData () ) {
161161 final NBTItem nbt = new NBTItem (stack );
162162 if (nbt .getKeys ().stream ().anyMatch (s -> s .startsWith ("levelTools" ))) {
163163 return new NBTLevelToolsItem (
@@ -177,6 +177,62 @@ public static String getServerVersion() {
177177 return split [split .length - 1 ].trim ().replace (")" , "" );
178178 }
179179
180+ public static boolean requiresLegacyAnvilListener () {
181+ return isMinecraftVersionBefore (1 , 9 );
182+ }
183+
184+ public static boolean supportsDualWielding () {
185+ return isMinecraftVersionAtLeast (1 , 9 );
186+ }
187+
188+ public static boolean supportsTranslatableItemDisplayNames () {
189+ return isMinecraftVersionAtLeast (1 , 13 );
190+ }
191+
192+ public static boolean supportsBlockData () {
193+ return isMinecraftVersionAtLeast (1 , 13 );
194+ }
195+
196+ public static boolean supportsPersistentDataContainer () {
197+ return isMinecraftVersionAtLeast (1 , 14 );
198+ }
199+
200+ public static boolean shouldCheckLegacyNbtData () {
201+ return supportsPersistentDataContainer () && isMinecraftVersionBefore (1 , 18 );
202+ }
203+
204+ public static boolean supportsSpigotActionBar () {
205+ return isMinecraftVersionAtLeast (1 , 13 );
206+ }
207+
208+ private static boolean isMinecraftVersionAtLeast (int major , int minor ) {
209+ return MINECRAFT_VERSION .compareTo (new MinecraftVersion (major , minor , 0 )) >= 0 ;
210+ }
211+
212+ private static boolean isMinecraftVersionBefore (int major , int minor ) {
213+ return MINECRAFT_VERSION .compareTo (new MinecraftVersion (major , minor , 0 )) < 0 ;
214+ }
215+
216+ static MinecraftVersion parseMinecraftVersion (@ NotNull String version ) {
217+ final Matcher matcher = MINECRAFT_VERSION_PATTERN .matcher (version .trim ());
218+ if (!matcher .matches ()) {
219+ throw new IllegalStateException ("Unable to parse Bukkit Minecraft version: " + version );
220+ }
221+
222+ return new MinecraftVersion (
223+ Integer .parseInt (matcher .group (1 )),
224+ parseVersionPart (matcher .group (2 )),
225+ parseVersionPart (matcher .group (3 ))
226+ );
227+ }
228+
229+ private static int parseVersionPart (@ Nullable String value ) {
230+ if (value == null ) {
231+ return 0 ;
232+ }
233+ return Integer .parseInt (value );
234+ }
235+
180236 public static ItemStack buildItemStack (
181237 ItemStack stack , Map <Enchantment , Integer > enchantments , int level , double xp , double maxXp ) {
182238 DisplayProfile displayProfile = getDisplayProfileForMaterial (stack .getType ());
@@ -202,7 +258,9 @@ public static ItemStack buildItemStack(
202258 .replace ("{xp_formatted}" , formatMoney (xp ))
203259 .replace ("{progress_bar}" , progressBar ));
204260
205- if (nameDisplay .getText ().contains ("{item}" ) && MID_VERSION >= 13 && IS_PAPER ) {
261+ if (nameDisplay .getText ().contains ("{item}" )
262+ && supportsTranslatableItemDisplayNames ()
263+ && IS_PAPER ) {
206264 AdventureHelper .setDisplayNameWithTranslatable (meta , text , stack );
207265 } else {
208266 meta .setDisplayName (text );
@@ -342,7 +400,7 @@ public static void handleReward(LevelToolsItem tool, Player player) {
342400 }
343401
344402 public static void sendActionBar (Player player , String msg ) {
345- if (MID_VERSION > 12 ) {
403+ if (supportsSpigotActionBar () ) {
346404 player .spigot ().sendMessage (ChatMessageType .ACTION_BAR , TextComponent .fromLegacyText (msg ));
347405 } else {
348406 ActionBar .sendActionBar (player , msg );
@@ -365,4 +423,34 @@ private static boolean isFolia() {
365423 return false ;
366424 }
367425 }
426+
427+ static final class MinecraftVersion implements Comparable <MinecraftVersion > {
428+ private final int major ;
429+ private final int minor ;
430+ private final int patch ;
431+
432+ private MinecraftVersion (int major , int minor , int patch ) {
433+ this .major = major ;
434+ this .minor = minor ;
435+ this .patch = patch ;
436+ }
437+
438+ private int getCompatibilityMajor () {
439+ if (major == 1 ) {
440+ return minor ;
441+ }
442+ return major ;
443+ }
444+
445+ @ Override
446+ public int compareTo (@ NotNull MinecraftVersion other ) {
447+ if (major != other .major ) {
448+ return Integer .compare (major , other .major );
449+ }
450+ if (minor != other .minor ) {
451+ return Integer .compare (minor , other .minor );
452+ }
453+ return Integer .compare (patch , other .patch );
454+ }
455+ }
368456}
0 commit comments