55import org .broken .arrow .library .itemcreator .utility .ConvertToItemStack ;
66import org .broken .arrow .library .itemcreator .utility .ServerVersion ;
77import org .broken .arrow .library .itemcreator .utility .builders .ItemBuilder ;
8+ import org .broken .arrow .library .logging .Validate ;
89import org .broken .arrow .library .logging .Validate .ValidateExceptions ;
910import org .broken .arrow .library .nbt .RegisterNbtAPI ;
1011import org .bukkit .Material ;
12+ import org .bukkit .NamespacedKey ;
13+ import org .bukkit .enchantments .Enchantment ;
1114import org .bukkit .inventory .ItemStack ;
1215import org .bukkit .plugin .Plugin ;
1316
17+ import javax .annotation .Nonnull ;
1418import javax .annotation .Nullable ;
1519import java .util .Arrays ;
1620import java .util .List ;
@@ -48,7 +52,7 @@ public ItemCreator(final Plugin plugin) {
4852 /**
4953 * Constructs an ItemCreator instance associated with the given plugin.
5054 *
51- * @param plugin the plugin instance
55+ * @param plugin the plugin instance
5256 * @param turnOffLogger whether to disable logging for NBT manager
5357 */
5458 public ItemCreator (final Plugin plugin , boolean turnOffLogger ) {
@@ -63,8 +67,6 @@ public ItemCreator(final Plugin plugin, boolean turnOffLogger) {
6367 } catch (NoClassDefFoundError ignore ) {
6468 haveTextTranslator = false ;
6569 }
66-
67-
6870 }
6971
7072 /**
@@ -245,15 +247,6 @@ public void setEnableColorTranslation(boolean enableColorTranslation) {
245247 this .enableColorTranslation = enableColorTranslation ;
246248 }
247249
248- /**
249- * Returns the current server version as a float.
250- *
251- * @return the server version
252- */
253- public static float getServerVersion () {
254- return serverVersion .getServerVersion ();
255- }
256-
257250 /**
258251 * Gets the NBTManager instance used to manipulate item NBT data.
259252 *
@@ -281,6 +274,7 @@ public ConvertToItemStack getConvertItems() {
281274 public Plugin getPlugin () {
282275 return plugin ;
283276 }
277+
284278 /**
285279 * Returns true if the correct module is imported
286280 * for color translations, it will use bukkits methods
@@ -292,6 +286,88 @@ public boolean isHaveTextTranslator() {
292286 return haveTextTranslator ;
293287 }
294288
289+
290+ /**
291+ * Returns the current server version as a float.
292+ *
293+ * @return the server version
294+ */
295+ public static float getServerVersion () {
296+ return serverVersion .getServerVersion ();
297+ }
298+
299+ /**
300+ * Return the enchantment's name across Minecraft versions.
301+ *
302+ * <p>On Minecraft 1.13 and newer this returns the NamespacedKey key
303+ * (enchantment.getKey().getKey()). On older versions this returns
304+ * {@link Enchantment#getName()}, which on legacy servers may be a numeric id string
305+ * or a legacy name. {@link Enchantment#getByName(String)} can resolve either form.
306+ *
307+ * @param enchantment the enchantment to get the name for
308+ * @return the enchantment name ({@link NamespacedKey#getKey()} on 1.13+, otherwise {@link Enchantment#getName()}
309+ * @throws NullPointerException if {@code enchantment} is null
310+ */
311+ @ Nonnull
312+ public static String getEnchantmentName (@ Nonnull final Enchantment enchantment ) {
313+ Validate .checkNotNull (enchantment , "Enchantment must be set to use this method." );
314+
315+ final String enhancementName ;
316+ if (ItemCreator .getServerVersion () > 12.2F )
317+ enhancementName = enchantment .getKey ().getKey ();
318+ else
319+ enhancementName = enchantment .getName ();
320+
321+ return enhancementName ;
322+ }
323+
324+ /**
325+ * Resolve an Enchantment by name, using a NamespacedKey on modern servers and legacy names on older servers.
326+ *
327+ * <p>Resolution order:
328+ * <ol>
329+ * <li>On Minecraft 1.13+ this tries {@link Enchantment#getByKey(NamespacedKey)} with
330+ * {@link NamespacedKey#minecraft(String)} constructed from {@code name}.</li>
331+ * <li>If unresolved (or on older servers) this tries {@link Enchantment#getByName(String)} using {@code name}.</li>
332+ * <li>If still unresolved returns {@link Enchantment#VANISHING_CURSE} as a safe default.</li>
333+ * </ol>
334+ *
335+ * <p>Notes:
336+ * <ul>
337+ * <li>The {@code enhancementName} parameter should be the NamespacedKey key for modern servers (e.g. "sharpness")
338+ * or the legacy enchantment name/id for older servers. Use {@link #getEnhancement(NamespacedKey)} on modern
339+ * Minecraft versions if you want access to enchantments outside the Minecraft-provided ones.</li>
340+ * </ul>
341+ *
342+ * @param enhancementName the enchantment name or key (non-null)
343+ * @return the resolved Enchantment, or {@link Enchantment#VANISHING_CURSE} if none found
344+ * @throws NullPointerException if {@code enhancementName} is null
345+ */
346+ @ Nonnull
347+ public static Enchantment getEnhancement (@ Nonnull final String enhancementName ) {
348+ return getEnchantment (null , enhancementName );
349+ }
350+
351+ /**
352+ * Resolve an Enchantment using a provided {@link NamespacedKey}.
353+ *
354+ * <p>Resolution order:
355+ * <ol>
356+ * <li>Try {@link Enchantment#getByKey(NamespacedKey)} with {@code key}.</li>
357+ * <li>If unresolved, returns {@link Enchantment#VANISHING_CURSE} as a safe default.</li>
358+ * </ol>
359+ *
360+ * @param key the NamespacedKey to try first (non-null)
361+ * @return the resolved Enchantment, or {@link Enchantment#VANISHING_CURSE} if none found
362+ * @throws NullPointerException if {@code key} is null
363+ */
364+ @ Nonnull
365+ public static Enchantment getEnhancement (@ Nonnull final NamespacedKey key ) {
366+ Validate .checkNotNull (key , "key must not be null" );
367+ return getEnchantment (key , null );
368+ }
369+
370+
295371 /**
296372 * Sets the server version based on the plugin's server version.
297373 *
@@ -301,4 +377,44 @@ private static void setServerVersion(Plugin plugin) {
301377 serverVersion = new ServerVersion (plugin );
302378 }
303379
380+ /**
381+ * Resolve an Enchantment with an optional {@link NamespacedKey} and a name fallback.
382+ *
383+ * <p>Resolution order:
384+ * <ol>
385+ * <li>If {@code key} is non-null, try {@link Enchantment#getByKey(NamespacedKey)} with {@code key}.</li>
386+ * <li>If that fails and the server is 1.13+, try {@link Enchantment#getByKey(NamespacedKey)}
387+ * with {@link NamespacedKey#minecraft(String)} constructed from {@code name}.</li>
388+ * <li>If still unresolved (or on older servers) try {@link Enchantment#getByName(String)} with {@code name}.</li>
389+ * <li>If unresolved return {@link Enchantment#VANISHING_CURSE} as a safe default.</li>
390+ * </ol>
391+ *
392+ * <p>Notes:
393+ * <ul>
394+ * <li>The {@code name} parameter should be the NamespacedKey key for modern servers (e.g. "sharpness")
395+ * or the legacy enchantment name/id for older servers.</li>
396+ * </ul>
397+ *
398+ * @param key the NamespacedKey to try first (maybe null)
399+ * @param name the enchantment name or key (maybe null)
400+ * @return the resolved Enchantment, or {@link Enchantment#VANISHING_CURSE} if none found
401+ */
402+ @ Nonnull
403+ private static Enchantment getEnchantment (@ Nullable final NamespacedKey key , @ Nullable final String name ) {
404+ if (key != null ) {
405+ Enchantment enchantment = Enchantment .getByKey (key );
406+ if (enchantment != null ) return enchantment ;
407+ else return Enchantment .VANISHING_CURSE ;
408+ }
409+ if (name == null ) return Enchantment .VANISHING_CURSE ;
410+
411+ if (ItemCreator .getServerVersion () > 12.2F ) {
412+ Enchantment enchantment = Enchantment .getByKey (NamespacedKey .minecraft (name ));
413+ if (enchantment != null ) return enchantment ;
414+ }
415+
416+ Enchantment enchantment = Enchantment .getByName (name );
417+ return enchantment == null ? Enchantment .VANISHING_CURSE : enchantment ;
418+ }
419+
304420}
0 commit comments