From c8f6b45357ff180700a83d88c255c8c8aa701a0c Mon Sep 17 00:00:00 2001 From: ShaneBee Date: Sat, 11 May 2024 08:01:44 -0700 Subject: [PATCH 01/20] EnchantmentType - prepare for MC 1.21 custom enchantments --- .../ch/njol/skript/util/EnchantmentType.java | 103 ++++++++++-------- src/main/resources/lang/default.lang | 55 ---------- 2 files changed, 55 insertions(+), 103 deletions(-) diff --git a/src/main/java/ch/njol/skript/util/EnchantmentType.java b/src/main/java/ch/njol/skript/util/EnchantmentType.java index 1ef13893fc9..a0455c0411a 100644 --- a/src/main/java/ch/njol/skript/util/EnchantmentType.java +++ b/src/main/java/ch/njol/skript/util/EnchantmentType.java @@ -18,30 +18,31 @@ */ package ch.njol.skript.util; +import java.util.ArrayList; import java.util.Collection; -import java.util.HashMap; -import java.util.Locale; -import java.util.Map; +import java.util.List; import java.util.regex.Pattern; +import ch.njol.skript.Skript; +import org.bukkit.NamespacedKey; +import org.bukkit.Registry; import org.bukkit.enchantments.Enchantment; import org.eclipse.jdt.annotation.Nullable; import ch.njol.skript.aliases.ItemType; -import ch.njol.skript.bukkitutil.EnchantmentUtils; -import ch.njol.skript.localization.Language; import ch.njol.yggdrasil.YggdrasilSerializable; +import org.jetbrains.annotations.NotNull; /** * @author Peter Güttinger */ public class EnchantmentType implements YggdrasilSerializable { - - private final static String LANGUAGE_NODE = "enchantments"; - + + private static final boolean HAS_REGISTRY = Skript.classExists("org.bukkit.Registry") && Skript.fieldExists(Registry.class, "ENCHANTMENT"); + private final Enchantment type; private final int level; - + /** * Used for deserialisation only */ @@ -50,7 +51,7 @@ private EnchantmentType() { type = null; level = -1; } - + public EnchantmentType(final Enchantment type) { assert type != null; this.type = type; @@ -61,26 +62,26 @@ public EnchantmentType(final Enchantment type, final int level) { this.type = type; this.level = level; } - + /** * @return level or 1 if level == -1 */ public int getLevel() { return level == -1 ? 1 : level; } - + /** * @return the internal level, can be -1 */ public int getInternalLevel() { return level; } - + @Nullable public Enchantment getType() { return type; } - + /** * Checks whether the given item type has this enchantment. * @param item the item to be checked. @@ -90,43 +91,32 @@ public Enchantment getType() { public boolean has(final ItemType item) { return item.hasEnchantments(type); } - + @Override public String toString() { return toString(type) + (level == -1 ? "" : " " + level); } - + @SuppressWarnings("null") - public static String toString(final Enchantment e) { - return NAMES.get(e); + public static String toString(final Enchantment enchantment) { + NamespacedKey key = enchantment.getKey(); + // If it's a minecraft enchant, just return the key + if (key.getNamespace().equalsIgnoreCase("minecraft")) + return key.getKey(); + // Else if it's a custom enchant, return with the namespace + // ex: `my_key:explosive` + return key.toString(); } - + // REMIND flags? @SuppressWarnings("null") - public static String toString(final Enchantment e, final int flags) { - return NAMES.get(e); + public static String toString(final Enchantment enchantment, final int flags) { + return toString(enchantment); } - - private final static Map NAMES = new HashMap<>(); - private final static Map PATTERNS = new HashMap<>(); - - static { - Language.addListener(() -> { - NAMES.clear(); - for (Enchantment e : Enchantment.values()) { - assert e != null; - final String[] names = Language.getList(LANGUAGE_NODE + ".names." + EnchantmentUtils.getKey(e)); - NAMES.put(e, names[0]); - - for (String name : names) - PATTERNS.put(name.toLowerCase(Locale.ENGLISH), e); - } - }); - } - + @SuppressWarnings("null") private final static Pattern pattern = Pattern.compile(".+ \\d+"); - + /** * Parses an enchantment type from string. This includes an {@link Enchantment} * and its level. @@ -150,17 +140,34 @@ public static EnchantmentType parse(final String s) { return null; return new EnchantmentType(ench, -1); } - + + @SuppressWarnings("deprecation") @Nullable - public static Enchantment parseEnchantment(final String s) { - return PATTERNS.get(s.toLowerCase(Locale.ENGLISH)); + public static Enchantment parseEnchantment(String s) { + s = s.replace(" ", "_"); + NamespacedKey key; + try { + if (s.contains(":")) + key = NamespacedKey.fromString(s); + else + key = NamespacedKey.minecraft(s); + } catch (IllegalArgumentException ignore) { + return null; + } + if (key == null) + return null; + if (HAS_REGISTRY) // Registry added in Bukkit 1.14 + return Registry.ENCHANTMENT.get(key); + return Enchantment.getByKey(key); } - + @SuppressWarnings("null") public static Collection getNames() { - return NAMES.values(); + List names = new ArrayList<>(); + Registry.ENCHANTMENT.forEach(enchantment -> names.add(enchantment.getKey().getKey().replace("_", " "))); + return names; } - + @Override public int hashCode() { final int prime = 31; @@ -169,7 +176,7 @@ public int hashCode() { result = prime * result + type.hashCode(); return result; } - + @Override public boolean equals(final @Nullable Object obj) { if (this == obj) @@ -183,5 +190,5 @@ public boolean equals(final @Nullable Object obj) { return false; return type.equals(other.type); } - + } diff --git a/src/main/resources/lang/default.lang b/src/main/resources/lang/default.lang index 21bfe5f6092..980641eac11 100644 --- a/src/main/resources/lang/default.lang +++ b/src/main/resources/lang/default.lang @@ -45,61 +45,6 @@ aliases: named: named -# -- Enchantments -- -enchantments: - of: of - names: - protection: Protection - fire_protection: Fire Protection - feather_falling: Feather Falling - blast_protection: Blast Protection - projectile_protection: Projectile Protection - respiration: Respiration - aqua_affinity: Aqua Affinity - sharpness: Sharpness - smite: Smite - bane_of_arthropods: Bane of Arthropods - knockback: Knockback - fire_aspect: Fire Aspect - looting: Looting - efficiency: Efficiency - silk_touch: Silk Touch - unbreaking: Unbreaking - fortune: Fortune - power: Power - punch: Punch - flame: Flame - infinity: Infinity - thorns: Thorns - luck: Luck of the Sea - lure: Lure - depth_strider: Depth Strider - mending: Mending - frost_walker: Frost Walker - vanishing_curse: Curse of Vanishing - binding_curse: Curse of Binding - # It's sweeping on 1.10 and below for some reason. - sweeping: Sweeping Edge - sweeping_edge: Sweeping Edge - # New 1.13 Enchantments - channeling: Channeling, Channelling - riptide: Riptide - impaling: Impaling - loyalty: Loyalty - luck_of_the_sea: Luck of The Sea - # new 1.14 Enchantments - multishot: Multishot, Multi-Shot - piercing: Piercing - quick_charge: Quick Charge - # new 1.16 Enchantment - soul_speed: Soul Speed - # new 1.19 Enchantment - swift_sneak: Swift Sneak - # new 1.21 Enchantments (added in 1.20.5 experimental) - density: Density - breach: Breach - wind_burst: Wind Burst - # -- Potion Effects -- potions: speed: speed, swiftness From fc2ad09c7ce21c526de1eb008274847045436d6c Mon Sep 17 00:00:00 2001 From: ShaneBee Date: Sat, 11 May 2024 08:06:00 -0700 Subject: [PATCH 02/20] EnchantmentType - change wording, key to namespace --- src/main/java/ch/njol/skript/util/EnchantmentType.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/ch/njol/skript/util/EnchantmentType.java b/src/main/java/ch/njol/skript/util/EnchantmentType.java index a0455c0411a..35d25a2dac1 100644 --- a/src/main/java/ch/njol/skript/util/EnchantmentType.java +++ b/src/main/java/ch/njol/skript/util/EnchantmentType.java @@ -104,7 +104,7 @@ public static String toString(final Enchantment enchantment) { if (key.getNamespace().equalsIgnoreCase("minecraft")) return key.getKey(); // Else if it's a custom enchant, return with the namespace - // ex: `my_key:explosive` + // ex: `some_namespace:explosive` return key.toString(); } From 10f20a00bde024ace4f75ef92c95021e1c73e99c Mon Sep 17 00:00:00 2001 From: ShaneBee Date: Sat, 11 May 2024 08:16:50 -0700 Subject: [PATCH 03/20] EnchantmentType - clean up repetitive code --- .../ch/njol/skript/util/EnchantmentType.java | 27 ++++++++++++------- 1 file changed, 18 insertions(+), 9 deletions(-) diff --git a/src/main/java/ch/njol/skript/util/EnchantmentType.java b/src/main/java/ch/njol/skript/util/EnchantmentType.java index 35d25a2dac1..afb0b760933 100644 --- a/src/main/java/ch/njol/skript/util/EnchantmentType.java +++ b/src/main/java/ch/njol/skript/util/EnchantmentType.java @@ -99,13 +99,7 @@ public String toString() { @SuppressWarnings("null") public static String toString(final Enchantment enchantment) { - NamespacedKey key = enchantment.getKey(); - // If it's a minecraft enchant, just return the key - if (key.getNamespace().equalsIgnoreCase("minecraft")) - return key.getKey(); - // Else if it's a custom enchant, return with the namespace - // ex: `some_namespace:explosive` - return key.toString(); + return getEnchantmentName(enchantment); } // REMIND flags? @@ -161,13 +155,28 @@ public static Enchantment parseEnchantment(String s) { return Enchantment.getByKey(key); } - @SuppressWarnings("null") + @SuppressWarnings({"null", "deprecation"}) public static Collection getNames() { List names = new ArrayList<>(); - Registry.ENCHANTMENT.forEach(enchantment -> names.add(enchantment.getKey().getKey().replace("_", " "))); + if (HAS_REGISTRY) + Registry.ENCHANTMENT.forEach(enchantment -> names.add(getEnchantmentName(enchantment))); + else + for (Enchantment enchantment : Enchantment.values()) { + names.add(getEnchantmentName(enchantment)); + } return names; } + private static String getEnchantmentName(Enchantment enchantment) { + NamespacedKey key = enchantment.getKey(); + // If it's a minecraft enchant, just return the key + if (key.getNamespace().equalsIgnoreCase("minecraft")) + return key.getKey(); + // Else if it's a custom enchant, return with the namespace + // ex: `some_namespace:explosive` + return key.toString(); + } + @Override public int hashCode() { final int prime = 31; From 6b694df42d6e7efdcdd5dc187deb8f9f210dab61 Mon Sep 17 00:00:00 2001 From: ShaneBee Date: Sat, 11 May 2024 12:45:55 -0700 Subject: [PATCH 04/20] EnchantmentType - to lower case --- src/main/java/ch/njol/skript/util/EnchantmentType.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/main/java/ch/njol/skript/util/EnchantmentType.java b/src/main/java/ch/njol/skript/util/EnchantmentType.java index afb0b760933..3e98d915d36 100644 --- a/src/main/java/ch/njol/skript/util/EnchantmentType.java +++ b/src/main/java/ch/njol/skript/util/EnchantmentType.java @@ -21,6 +21,7 @@ import java.util.ArrayList; import java.util.Collection; import java.util.List; +import java.util.Locale; import java.util.regex.Pattern; import ch.njol.skript.Skript; @@ -138,7 +139,7 @@ public static EnchantmentType parse(final String s) { @SuppressWarnings("deprecation") @Nullable public static Enchantment parseEnchantment(String s) { - s = s.replace(" ", "_"); + s = s.replace(" ", "_").toLowerCase(Locale.ROOT); NamespacedKey key; try { if (s.contains(":")) From 4d4f56bafe02c161c5e929e367fcd67e66372c83 Mon Sep 17 00:00:00 2001 From: ShaneBee Date: Sat, 11 May 2024 21:22:43 -0700 Subject: [PATCH 05/20] default.lang - add this back and deal with it later --- src/main/resources/lang/default.lang | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/main/resources/lang/default.lang b/src/main/resources/lang/default.lang index 980641eac11..2fdee9e179f 100644 --- a/src/main/resources/lang/default.lang +++ b/src/main/resources/lang/default.lang @@ -45,6 +45,10 @@ aliases: named: named +# -- Enchantments -- +enchantments: + of: of + # -- Potion Effects -- potions: speed: speed, swiftness From 09760cbc65a7478a800c0f1556abe5a18f9ee727 Mon Sep 17 00:00:00 2001 From: ShaneBee Date: Sun, 12 May 2024 16:35:32 -0700 Subject: [PATCH 06/20] Aliases/default.lang - change "of" location in lang file --- src/main/java/ch/njol/skript/aliases/Aliases.java | 2 +- src/main/resources/lang/default.lang | 5 +---- 2 files changed, 2 insertions(+), 5 deletions(-) diff --git a/src/main/java/ch/njol/skript/aliases/Aliases.java b/src/main/java/ch/njol/skript/aliases/Aliases.java index 56bffc24f9a..f1c1b9fd195 100644 --- a/src/main/java/ch/njol/skript/aliases/Aliases.java +++ b/src/main/java/ch/njol/skript/aliases/Aliases.java @@ -267,7 +267,7 @@ public static ItemType parseItemType(String s) { } String lc = s.toLowerCase(Locale.ENGLISH); - String of = Language.getSpaced("enchantments.of").toLowerCase(); + String of = Language.getSpaced("of").toLowerCase(); int c = -1; outer: while ((c = lc.indexOf(of, c + 1)) != -1) { ItemType t2 = t.clone(); diff --git a/src/main/resources/lang/default.lang b/src/main/resources/lang/default.lang index 2fdee9e179f..2abfa6bef0f 100644 --- a/src/main/resources/lang/default.lang +++ b/src/main/resources/lang/default.lang @@ -13,6 +13,7 @@ not: not # not a: not a(n) # @a:not a @an:not an @x:not neither: neither nor: nor +of: of genders: 0: @@ -45,10 +46,6 @@ aliases: named: named -# -- Enchantments -- -enchantments: - of: of - # -- Potion Effects -- potions: speed: speed, swiftness From f56f5292c614757ee2050a66507ba3817282e27a Mon Sep 17 00:00:00 2001 From: ShaneBee Date: Mon, 13 May 2024 11:56:25 -0700 Subject: [PATCH 07/20] EnchantmentUtils - more changes --- .../skript/bukkitutil/EnchantmentUtils.java | 82 ++++++++++++++++++- .../skript/classes/data/BukkitClasses.java | 10 +-- .../ch/njol/skript/util/EnchantmentType.java | 70 +--------------- src/main/resources/lang/default.lang | 53 ++++++++++++ 4 files changed, 142 insertions(+), 73 deletions(-) diff --git a/src/main/java/ch/njol/skript/bukkitutil/EnchantmentUtils.java b/src/main/java/ch/njol/skript/bukkitutil/EnchantmentUtils.java index fcebff78917..4d7bd499d91 100644 --- a/src/main/java/ch/njol/skript/bukkitutil/EnchantmentUtils.java +++ b/src/main/java/ch/njol/skript/bukkitutil/EnchantmentUtils.java @@ -18,19 +18,44 @@ */ package ch.njol.skript.bukkitutil; +import ch.njol.skript.localization.Language; import org.bukkit.NamespacedKey; +import org.bukkit.Registry; import org.bukkit.enchantments.Enchantment; import org.eclipse.jdt.annotation.Nullable; -import com.google.common.collect.BiMap; -import com.google.common.collect.HashBiMap; import ch.njol.skript.Skript; +import java.util.Arrays; +import java.util.Collection; +import java.util.HashMap; +import java.util.List; +import java.util.Locale; +import java.util.Map; + /** * Maps enchantments to their keys. */ public class EnchantmentUtils { + private static final Map NAMES = new HashMap<>(); + private static final Map PATTERNS = new HashMap<>(); + private static final boolean HAS_REGISTRY = Skript.classExists("org.bukkit.Registry") && Skript.fieldExists(Registry.class, "ENCHANTMENT"); + static { + Language.addListener(() -> { + NAMES.clear(); + List enchantments = HAS_REGISTRY ? Registry.ENCHANTMENT.stream().toList() : Arrays.asList(Enchantment.values()); + for (Enchantment enchantment : enchantments) { + assert enchantment != null; + final String[] names = Language.getList("enchantments." + getKey(enchantment)); + NAMES.put(enchantment, names[0]); + + for (String name : names) + PATTERNS.put(name.toLowerCase(Locale.ENGLISH), enchantment); + } + }); + } + public static String getKey(Enchantment enchantment) { return enchantment.getKey().getKey(); } @@ -40,4 +65,57 @@ public static Enchantment getByKey(String key) { return Enchantment.getByKey(NamespacedKey.minecraft(key)); } + @SuppressWarnings("deprecation") + @Nullable + public static Enchantment parseEnchantment(String s) { + s = s.toLowerCase(Locale.ROOT); + // First try to parse from the lang file + Enchantment enchantment = PATTERNS.get(s); + if (enchantment != null) + return enchantment; + + // If that fails, we move forward with getting from key + s = s.replace(" ", "_"); + NamespacedKey key; + try { + if (s.contains(":")) + key = NamespacedKey.fromString(s); + else + key = NamespacedKey.minecraft(s); + } catch (IllegalArgumentException ignore) { + return null; + } + if (key == null) + return null; + if (HAS_REGISTRY) // Registry added in Bukkit 1.14 + return Registry.ENCHANTMENT.get(key); + return Enchantment.getByKey(key); + } + + @SuppressWarnings("null") + public static Collection getNames() { + return NAMES.values(); + } + + @SuppressWarnings("null") + public static String toString(final Enchantment enchantment) { + // If we have a name in the lang file, return that first + if (NAMES.containsKey(enchantment)) + return NAMES.get(enchantment); + + NamespacedKey key = enchantment.getKey(); + // Else if it's a missing minecraft enchant, just return the key + if (key.getNamespace().equalsIgnoreCase("minecraft")) + return key.getKey(); + // Else if it's a custom enchant, return with the namespace + // ex: `some_namespace:explosive` + return key.toString(); + } + + // REMIND flags? + @SuppressWarnings("null") + public static String toString(final Enchantment enchantment, final int flags) { + return toString(enchantment); + } + } diff --git a/src/main/java/ch/njol/skript/classes/data/BukkitClasses.java b/src/main/java/ch/njol/skript/classes/data/BukkitClasses.java index 25000a2390d..4b9a3bae987 100644 --- a/src/main/java/ch/njol/skript/classes/data/BukkitClasses.java +++ b/src/main/java/ch/njol/skript/classes/data/BukkitClasses.java @@ -1218,7 +1218,7 @@ public boolean mustSyncDeserialization() { .name("Enchantment") .description("An enchantment, e.g. 'sharpness' or 'fortune'. Unlike enchantment type " + "this type has no level, but you usually don't need to use this type anyway.") - .usage(StringUtils.join(EnchantmentType.getNames(), ", ")) + .usage(StringUtils.join(EnchantmentUtils.getNames(), ", ")) .examples("") .since("1.4.6") .before("enchantmenttype") @@ -1227,12 +1227,12 @@ public boolean mustSyncDeserialization() { @Override @Nullable public Enchantment parse(final String s, final ParseContext context) { - return EnchantmentType.parseEnchantment(s); + return EnchantmentUtils.parseEnchantment(s); } @Override public String toString(final Enchantment e, final int flags) { - return EnchantmentType.toString(e, flags); + return EnchantmentUtils.toString(e, flags); } @Override @@ -1497,12 +1497,12 @@ public boolean canParse(ParseContext context) { @Override public String toString(EnchantmentOffer eo, int flags) { - return EnchantmentType.toString(eo.getEnchantment(), flags) + " " + eo.getEnchantmentLevel(); + return EnchantmentUtils.toString(eo.getEnchantment(), flags) + " " + eo.getEnchantmentLevel(); } @Override public String toVariableNameString(EnchantmentOffer eo) { - return "offer:" + EnchantmentType.toString(eo.getEnchantment()) + "=" + eo.getEnchantmentLevel(); + return "offer:" + EnchantmentUtils.toString(eo.getEnchantment()) + "=" + eo.getEnchantmentLevel(); } })); diff --git a/src/main/java/ch/njol/skript/util/EnchantmentType.java b/src/main/java/ch/njol/skript/util/EnchantmentType.java index 3e98d915d36..f302a7623c7 100644 --- a/src/main/java/ch/njol/skript/util/EnchantmentType.java +++ b/src/main/java/ch/njol/skript/util/EnchantmentType.java @@ -18,29 +18,20 @@ */ package ch.njol.skript.util; -import java.util.ArrayList; -import java.util.Collection; -import java.util.List; -import java.util.Locale; import java.util.regex.Pattern; -import ch.njol.skript.Skript; -import org.bukkit.NamespacedKey; -import org.bukkit.Registry; +import ch.njol.skript.bukkitutil.EnchantmentUtils; import org.bukkit.enchantments.Enchantment; import org.eclipse.jdt.annotation.Nullable; import ch.njol.skript.aliases.ItemType; import ch.njol.yggdrasil.YggdrasilSerializable; -import org.jetbrains.annotations.NotNull; /** * @author Peter Güttinger */ public class EnchantmentType implements YggdrasilSerializable { - private static final boolean HAS_REGISTRY = Skript.classExists("org.bukkit.Registry") && Skript.fieldExists(Registry.class, "ENCHANTMENT"); - private final Enchantment type; private final int level; @@ -95,18 +86,7 @@ public boolean has(final ItemType item) { @Override public String toString() { - return toString(type) + (level == -1 ? "" : " " + level); - } - - @SuppressWarnings("null") - public static String toString(final Enchantment enchantment) { - return getEnchantmentName(enchantment); - } - - // REMIND flags? - @SuppressWarnings("null") - public static String toString(final Enchantment enchantment, final int flags) { - return toString(enchantment); + return EnchantmentUtils.toString(type) + (level == -1 ? "" : " " + level); } @SuppressWarnings("null") @@ -123,61 +103,19 @@ public static EnchantmentType parse(final String s) { if (pattern.matcher(s).matches()) { String name = s.substring(0, s.lastIndexOf(' ')); assert name != null; - final Enchantment ench = parseEnchantment(name); + final Enchantment ench = EnchantmentUtils.parseEnchantment(name); if (ench == null) return null; String level = s.substring(s.lastIndexOf(' ') + 1); assert level != null; return new EnchantmentType(ench, Utils.parseInt(level)); } - final Enchantment ench = parseEnchantment(s); + final Enchantment ench = EnchantmentUtils.parseEnchantment(s); if (ench == null) return null; return new EnchantmentType(ench, -1); } - @SuppressWarnings("deprecation") - @Nullable - public static Enchantment parseEnchantment(String s) { - s = s.replace(" ", "_").toLowerCase(Locale.ROOT); - NamespacedKey key; - try { - if (s.contains(":")) - key = NamespacedKey.fromString(s); - else - key = NamespacedKey.minecraft(s); - } catch (IllegalArgumentException ignore) { - return null; - } - if (key == null) - return null; - if (HAS_REGISTRY) // Registry added in Bukkit 1.14 - return Registry.ENCHANTMENT.get(key); - return Enchantment.getByKey(key); - } - - @SuppressWarnings({"null", "deprecation"}) - public static Collection getNames() { - List names = new ArrayList<>(); - if (HAS_REGISTRY) - Registry.ENCHANTMENT.forEach(enchantment -> names.add(getEnchantmentName(enchantment))); - else - for (Enchantment enchantment : Enchantment.values()) { - names.add(getEnchantmentName(enchantment)); - } - return names; - } - - private static String getEnchantmentName(Enchantment enchantment) { - NamespacedKey key = enchantment.getKey(); - // If it's a minecraft enchant, just return the key - if (key.getNamespace().equalsIgnoreCase("minecraft")) - return key.getKey(); - // Else if it's a custom enchant, return with the namespace - // ex: `some_namespace:explosive` - return key.toString(); - } - @Override public int hashCode() { final int prime = 31; diff --git a/src/main/resources/lang/default.lang b/src/main/resources/lang/default.lang index 2abfa6bef0f..01e99560361 100644 --- a/src/main/resources/lang/default.lang +++ b/src/main/resources/lang/default.lang @@ -46,6 +46,59 @@ aliases: named: named +# -- Enchantments -- +enchantments: + protection: Protection + fire_protection: Fire Protection + feather_falling: Feather Falling + blast_protection: Blast Protection + projectile_protection: Projectile Protection + respiration: Respiration + aqua_affinity: Aqua Affinity + sharpness: Sharpness + smite: Smite + bane_of_arthropods: Bane of Arthropods + knockback: Knockback + fire_aspect: Fire Aspect + looting: Looting + efficiency: Efficiency + silk_touch: Silk Touch + unbreaking: Unbreaking + fortune: Fortune + power: Power + punch: Punch + flame: Flame + infinity: Infinity + thorns: Thorns + luck: Luck of the Sea + lure: Lure + depth_strider: Depth Strider + mending: Mending + frost_walker: Frost Walker + vanishing_curse: Curse of Vanishing + binding_curse: Curse of Binding + # It's sweeping on 1.10 and below for some reason. + sweeping: Sweeping Edge + sweeping_edge: Sweeping Edge + # New 1.13 Enchantments + channeling: Channeling, Channelling + riptide: Riptide + impaling: Impaling + loyalty: Loyalty + luck_of_the_sea: Luck of The Sea + # new 1.14 Enchantments + multishot: Multishot, Multi-Shot + piercing: Piercing + quick_charge: Quick Charge + # new 1.16 Enchantment + soul_speed: Soul Speed + # new 1.19 Enchantment + swift_sneak: Swift Sneak + # new 1.21 Enchantments (added in 1.20.5 experimental) + density: Density + breach: Breach + wind_burst: Wind Burst + # -- Potion Effects -- potions: speed: speed, swiftness From 620b354f95e7eaf3b57328820e8b827b4361a15c Mon Sep 17 00:00:00 2001 From: ShaneBee Date: Mon, 13 May 2024 12:16:55 -0700 Subject: [PATCH 08/20] EnchantmentUtils - fix stream issue on older java versions --- .../java/ch/njol/skript/bukkitutil/EnchantmentUtils.java | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/main/java/ch/njol/skript/bukkitutil/EnchantmentUtils.java b/src/main/java/ch/njol/skript/bukkitutil/EnchantmentUtils.java index 4d7bd499d91..fd47ded1319 100644 --- a/src/main/java/ch/njol/skript/bukkitutil/EnchantmentUtils.java +++ b/src/main/java/ch/njol/skript/bukkitutil/EnchantmentUtils.java @@ -26,6 +26,7 @@ import ch.njol.skript.Skript; +import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; import java.util.HashMap; @@ -44,7 +45,12 @@ public class EnchantmentUtils { static { Language.addListener(() -> { NAMES.clear(); - List enchantments = HAS_REGISTRY ? Registry.ENCHANTMENT.stream().toList() : Arrays.asList(Enchantment.values()); + List enchantments = new ArrayList<>(); + if (HAS_REGISTRY) { + Registry.ENCHANTMENT.forEach(enchantments::add); + } else { + enchantments.addAll(Arrays.asList(Enchantment.values())); + } for (Enchantment enchantment : enchantments) { assert enchantment != null; final String[] names = Language.getList("enchantments." + getKey(enchantment)); From b9ac52c500f61d6722428734d4d7cbdc8c0ad624 Mon Sep 17 00:00:00 2001 From: ShaneBee Date: Mon, 13 May 2024 12:23:18 -0700 Subject: [PATCH 09/20] EnchantmentUtils - small cleanup --- .../java/ch/njol/skript/bukkitutil/EnchantmentUtils.java | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/src/main/java/ch/njol/skript/bukkitutil/EnchantmentUtils.java b/src/main/java/ch/njol/skript/bukkitutil/EnchantmentUtils.java index fd47ded1319..68dec184a29 100644 --- a/src/main/java/ch/njol/skript/bukkitutil/EnchantmentUtils.java +++ b/src/main/java/ch/njol/skript/bukkitutil/EnchantmentUtils.java @@ -42,17 +42,16 @@ public class EnchantmentUtils { private static final Map NAMES = new HashMap<>(); private static final Map PATTERNS = new HashMap<>(); private static final boolean HAS_REGISTRY = Skript.classExists("org.bukkit.Registry") && Skript.fieldExists(Registry.class, "ENCHANTMENT"); + static { Language.addListener(() -> { NAMES.clear(); List enchantments = new ArrayList<>(); - if (HAS_REGISTRY) { + if (HAS_REGISTRY) Registry.ENCHANTMENT.forEach(enchantments::add); - } else { + else enchantments.addAll(Arrays.asList(Enchantment.values())); - } for (Enchantment enchantment : enchantments) { - assert enchantment != null; final String[] names = Language.getList("enchantments." + getKey(enchantment)); NAMES.put(enchantment, names[0]); From 2412afa789d6d8bbd406886ffc1b9d0be7138995 Mon Sep 17 00:00:00 2001 From: Shane Bee Date: Mon, 13 May 2024 13:04:02 -0700 Subject: [PATCH 10/20] Update src/main/java/ch/njol/skript/bukkitutil/EnchantmentUtils.java Co-authored-by: Patrick Miller --- .../java/ch/njol/skript/bukkitutil/EnchantmentUtils.java | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/main/java/ch/njol/skript/bukkitutil/EnchantmentUtils.java b/src/main/java/ch/njol/skript/bukkitutil/EnchantmentUtils.java index 68dec184a29..3ad7a617604 100644 --- a/src/main/java/ch/njol/skript/bukkitutil/EnchantmentUtils.java +++ b/src/main/java/ch/njol/skript/bukkitutil/EnchantmentUtils.java @@ -47,10 +47,11 @@ public class EnchantmentUtils { Language.addListener(() -> { NAMES.clear(); List enchantments = new ArrayList<>(); - if (HAS_REGISTRY) + if (HAS_REGISTRY) { Registry.ENCHANTMENT.forEach(enchantments::add); - else + } else { enchantments.addAll(Arrays.asList(Enchantment.values())); + } for (Enchantment enchantment : enchantments) { final String[] names = Language.getList("enchantments." + getKey(enchantment)); NAMES.put(enchantment, names[0]); From 8fc32460f431766753443ab9adb32dd8533d7638 Mon Sep 17 00:00:00 2001 From: Shane Bee Date: Mon, 13 May 2024 13:05:04 -0700 Subject: [PATCH 11/20] Update src/main/java/ch/njol/skript/bukkitutil/EnchantmentUtils.java Co-authored-by: Patrick Miller --- .../java/ch/njol/skript/bukkitutil/EnchantmentUtils.java | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/main/java/ch/njol/skript/bukkitutil/EnchantmentUtils.java b/src/main/java/ch/njol/skript/bukkitutil/EnchantmentUtils.java index 3ad7a617604..ceef991a85f 100644 --- a/src/main/java/ch/njol/skript/bukkitutil/EnchantmentUtils.java +++ b/src/main/java/ch/njol/skript/bukkitutil/EnchantmentUtils.java @@ -84,10 +84,11 @@ public static Enchantment parseEnchantment(String s) { s = s.replace(" ", "_"); NamespacedKey key; try { - if (s.contains(":")) + if (s.contains(":")) { key = NamespacedKey.fromString(s); - else + } else { key = NamespacedKey.minecraft(s); + } } catch (IllegalArgumentException ignore) { return null; } From 126607f0ae137a2b12acbb8176f5b9ba6d65e2cd Mon Sep 17 00:00:00 2001 From: ShaneBee Date: Mon, 13 May 2024 15:19:36 -0700 Subject: [PATCH 12/20] EnchantmentUtils - small changes as suggested - skip adding names if not in lang file - toString should return full namespaced key if no name found --- .../njol/skript/bukkitutil/EnchantmentUtils.java | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/src/main/java/ch/njol/skript/bukkitutil/EnchantmentUtils.java b/src/main/java/ch/njol/skript/bukkitutil/EnchantmentUtils.java index ceef991a85f..e0e7a2257a6 100644 --- a/src/main/java/ch/njol/skript/bukkitutil/EnchantmentUtils.java +++ b/src/main/java/ch/njol/skript/bukkitutil/EnchantmentUtils.java @@ -54,8 +54,11 @@ public class EnchantmentUtils { } for (Enchantment enchantment : enchantments) { final String[] names = Language.getList("enchantments." + getKey(enchantment)); - NAMES.put(enchantment, names[0]); + // Don't store the name/pattern if it doesn't exist, let toString handle it + if (names[0].contains("enchantments.")) + continue; + NAMES.put(enchantment, names[0]); for (String name : names) PATTERNS.put(name.toLowerCase(Locale.ENGLISH), enchantment); } @@ -110,13 +113,8 @@ public static String toString(final Enchantment enchantment) { if (NAMES.containsKey(enchantment)) return NAMES.get(enchantment); - NamespacedKey key = enchantment.getKey(); - // Else if it's a missing minecraft enchant, just return the key - if (key.getNamespace().equalsIgnoreCase("minecraft")) - return key.getKey(); - // Else if it's a custom enchant, return with the namespace - // ex: `some_namespace:explosive` - return key.toString(); + // If no name is available, return the namespaced key + return enchantment.getKey().toString(); } // REMIND flags? From 60b45b82b08492156dadce082e433f9527a3f160 Mon Sep 17 00:00:00 2001 From: ShaneBee Date: Mon, 13 May 2024 17:54:31 -0700 Subject: [PATCH 13/20] EnchantmentUtils - more changes --- .../skript/bukkitutil/EnchantmentUtils.java | 48 +++++++------------ 1 file changed, 16 insertions(+), 32 deletions(-) diff --git a/src/main/java/ch/njol/skript/bukkitutil/EnchantmentUtils.java b/src/main/java/ch/njol/skript/bukkitutil/EnchantmentUtils.java index e0e7a2257a6..61c97e92a58 100644 --- a/src/main/java/ch/njol/skript/bukkitutil/EnchantmentUtils.java +++ b/src/main/java/ch/njol/skript/bukkitutil/EnchantmentUtils.java @@ -46,6 +46,7 @@ public class EnchantmentUtils { static { Language.addListener(() -> { NAMES.clear(); + PATTERNS.clear(); List enchantments = new ArrayList<>(); if (HAS_REGISTRY) { Registry.ENCHANTMENT.forEach(enchantments::add); @@ -53,14 +54,20 @@ public class EnchantmentUtils { enchantments.addAll(Arrays.asList(Enchantment.values())); } for (Enchantment enchantment : enchantments) { - final String[] names = Language.getList("enchantments." + getKey(enchantment)); - // Don't store the name/pattern if it doesn't exist, let toString handle it - if (names[0].contains("enchantments.")) - continue; - - NAMES.put(enchantment, names[0]); - for (String name : names) - PATTERNS.put(name.toLowerCase(Locale.ENGLISH), enchantment); + NamespacedKey key = enchantment.getKey(); + final String[] names = Language.getList("enchantments." + key.getKey()); + + if (!names[0].startsWith("enchantments.")) { + NAMES.put(enchantment, names[0]); + // Add lang file names + for (String name : names) + PATTERNS.put(name.toLowerCase(Locale.ENGLISH), enchantment); + } + // If Minecraft provided, add key without namespace and underscores (ex: "fire aspect") + if (key.getNamespace().equalsIgnoreCase("minecraft")) + PATTERNS.put(key.getKey().replace("_", " "), enchantment); + // Add full namespaced key as pattern (ex: "minecraft:fire_aspect", "custom:floopy_floopy") + PATTERNS.put(key.toString(), enchantment); } }); } @@ -74,32 +81,9 @@ public static Enchantment getByKey(String key) { return Enchantment.getByKey(NamespacedKey.minecraft(key)); } - @SuppressWarnings("deprecation") @Nullable public static Enchantment parseEnchantment(String s) { - s = s.toLowerCase(Locale.ROOT); - // First try to parse from the lang file - Enchantment enchantment = PATTERNS.get(s); - if (enchantment != null) - return enchantment; - - // If that fails, we move forward with getting from key - s = s.replace(" ", "_"); - NamespacedKey key; - try { - if (s.contains(":")) { - key = NamespacedKey.fromString(s); - } else { - key = NamespacedKey.minecraft(s); - } - } catch (IllegalArgumentException ignore) { - return null; - } - if (key == null) - return null; - if (HAS_REGISTRY) // Registry added in Bukkit 1.14 - return Registry.ENCHANTMENT.get(key); - return Enchantment.getByKey(key); + return PATTERNS.get(s); } @SuppressWarnings("null") From 843c009b6664c44431bfc45c6de132e27e24c157 Mon Sep 17 00:00:00 2001 From: ShaneBee Date: Mon, 13 May 2024 18:14:52 -0700 Subject: [PATCH 14/20] EnchantmentUtils - update serialization --- .../skript/bukkitutil/EnchantmentUtils.java | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/src/main/java/ch/njol/skript/bukkitutil/EnchantmentUtils.java b/src/main/java/ch/njol/skript/bukkitutil/EnchantmentUtils.java index 61c97e92a58..54efe453e93 100644 --- a/src/main/java/ch/njol/skript/bukkitutil/EnchantmentUtils.java +++ b/src/main/java/ch/njol/skript/bukkitutil/EnchantmentUtils.java @@ -73,12 +73,25 @@ public class EnchantmentUtils { } public static String getKey(Enchantment enchantment) { - return enchantment.getKey().getKey(); + return enchantment.getKey().toString(); } @Nullable public static Enchantment getByKey(String key) { - return Enchantment.getByKey(NamespacedKey.minecraft(key)); + if (!key.contains(":")) { + // Old method for old variables + return Enchantment.getByKey(NamespacedKey.minecraft(key)); + } else { + NamespacedKey namespacedKey = NamespacedKey.fromString(key); + if (namespacedKey == null) + return null; + + if (HAS_REGISTRY) { + return Registry.ENCHANTMENT.get(namespacedKey); + } else { + return Enchantment.getByKey(namespacedKey); + } + } } @Nullable From ad813540c5b24b844be6fff69ebc3649ab635939 Mon Sep 17 00:00:00 2001 From: shanebeee Date: Mon, 1 Jul 2024 08:39:17 -0700 Subject: [PATCH 15/20] EnchantmentUtils - change up how classinfo is done --- .../skript/bukkitutil/EnchantmentUtils.java | 71 +++++++++++++++++- .../skript/classes/data/BukkitClasses.java | 75 +++---------------- .../ch/njol/skript/util/EnchantmentType.java | 31 ++++++-- 3 files changed, 102 insertions(+), 75 deletions(-) diff --git a/src/main/java/ch/njol/skript/bukkitutil/EnchantmentUtils.java b/src/main/java/ch/njol/skript/bukkitutil/EnchantmentUtils.java index 54efe453e93..3bd9c43f788 100644 --- a/src/main/java/ch/njol/skript/bukkitutil/EnchantmentUtils.java +++ b/src/main/java/ch/njol/skript/bukkitutil/EnchantmentUtils.java @@ -18,14 +18,18 @@ */ package ch.njol.skript.bukkitutil; +import ch.njol.skript.classes.ClassInfo; +import ch.njol.skript.classes.Parser; +import ch.njol.skript.classes.Serializer; +import ch.njol.skript.lang.ParseContext; import ch.njol.skript.localization.Language; +import ch.njol.yggdrasil.Fields; import org.bukkit.NamespacedKey; import org.bukkit.Registry; import org.bukkit.enchantments.Enchantment; -import org.eclipse.jdt.annotation.Nullable; - -import ch.njol.skript.Skript; +import org.jetbrains.annotations.Nullable; +import java.io.StreamCorruptedException; import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; @@ -41,7 +45,7 @@ public class EnchantmentUtils { private static final Map NAMES = new HashMap<>(); private static final Map PATTERNS = new HashMap<>(); - private static final boolean HAS_REGISTRY = Skript.classExists("org.bukkit.Registry") && Skript.fieldExists(Registry.class, "ENCHANTMENT"); + private static final boolean HAS_REGISTRY = BukkitUtils.registryExists("ENCHANTMENT"); static { Language.addListener(() -> { @@ -120,4 +124,63 @@ public static String toString(final Enchantment enchantment, final int flags) { return toString(enchantment); } + public static ClassInfo createClassInfo() { + return new ClassInfo<>(Enchantment.class, "enchantment") + .parser(new Parser<>() { + @Override + @Nullable + public Enchantment parse(final String s, final ParseContext context) { + return EnchantmentUtils.parseEnchantment(s); + } + + @Override + public String toString(final Enchantment e, final int flags) { + return EnchantmentUtils.toString(e, flags); + } + + @Override + public String toVariableNameString(final Enchantment e) { + return "" + EnchantmentUtils.getKey(e); + } + }).serializer(new Serializer<>() { + @Override + public Fields serialize(final Enchantment ench) { + final Fields f = new Fields(); + f.putObject("key", EnchantmentUtils.getKey(ench)); + return f; + } + + @Override + public boolean canBeInstantiated() { + return false; + } + + @Override + public void deserialize(final Enchantment o, final Fields f) { + assert false; + } + + @Override + protected Enchantment deserialize(final Fields fields) throws StreamCorruptedException { + final String key = fields.getObject("key", String.class); + assert key != null; // If a key happens to be null, something went really wrong... + final Enchantment e = EnchantmentUtils.getByKey(key); + if (e == null) + throw new StreamCorruptedException("Invalid enchantment " + key); + return e; + } + + @Override + @Nullable + public Enchantment deserialize(String s) { + return Enchantment.getByName(s); + } + + @Override + public boolean mustSyncDeserialization() { + return false; + } + }).supplier(Enchantment.values()); + } + } diff --git a/src/main/java/ch/njol/skript/classes/data/BukkitClasses.java b/src/main/java/ch/njol/skript/classes/data/BukkitClasses.java index ee8d52d9c95..f98ec41b844 100644 --- a/src/main/java/ch/njol/skript/classes/data/BukkitClasses.java +++ b/src/main/java/ch/njol/skript/classes/data/BukkitClasses.java @@ -100,7 +100,6 @@ import ch.njol.skript.localization.Language; import ch.njol.skript.registrations.Classes; import ch.njol.skript.util.BlockUtils; -import ch.njol.skript.util.EnchantmentType; import ch.njol.skript.util.PotionEffectUtils; import ch.njol.skript.util.StringMode; import ch.njol.util.StringUtils; @@ -1076,12 +1075,12 @@ protected boolean canBeInstantiated() { public PotionEffectType parse(final String s, final ParseContext context) { return PotionEffectUtils.parseType(s); } - + @Override public String toString(final PotionEffectType p, final int flags) { return PotionEffectUtils.toString(p, flags); } - + @Override public String toVariableNameString(final PotionEffectType p) { return "" + p.getName(); @@ -1221,8 +1220,14 @@ public boolean mustSyncDeserialization() { return true; } })); - - Classes.registerClass(new ClassInfo<>(Enchantment.class, "enchantment") + + ClassInfo enchantmentClassInfo; + if (BukkitUtils.registryExists("ENCHANTMENT")) { + enchantmentClassInfo = new RegistryClassInfo<>(Enchantment.class, Registry.ENCHANTMENT, "enchantment", "enchantments"); + } else { + enchantmentClassInfo = EnchantmentUtils.createClassInfo(); + } + Classes.registerClass(enchantmentClassInfo .user("enchantments?") .name("Enchantment") .description("An enchantment, e.g. 'sharpness' or 'fortune'. Unlike enchantment type " + @@ -1230,65 +1235,7 @@ public boolean mustSyncDeserialization() { .usage(StringUtils.join(EnchantmentUtils.getNames(), ", ")) .examples("") .since("1.4.6") - .before("enchantmenttype") - .supplier(Enchantment.values()) - .parser(new Parser() { - @Override - @Nullable - public Enchantment parse(final String s, final ParseContext context) { - return EnchantmentUtils.parseEnchantment(s); - } - - @Override - public String toString(final Enchantment e, final int flags) { - return EnchantmentUtils.toString(e, flags); - } - - @Override - public String toVariableNameString(final Enchantment e) { - return "" + EnchantmentUtils.getKey(e); - } - }) - .serializer(new Serializer() { - @Override - public Fields serialize(final Enchantment ench) { - final Fields f = new Fields(); - f.putObject("key", EnchantmentUtils.getKey(ench)); - return f; - } - - @Override - public boolean canBeInstantiated() { - return false; - } - - @Override - public void deserialize(final Enchantment o, final Fields f) { - assert false; - } - - @Override - protected Enchantment deserialize(final Fields fields) throws StreamCorruptedException { - final String key = fields.getObject("key", String.class); - assert key != null; // If a key happens to be null, something went really wrong... - final Enchantment e = EnchantmentUtils.getByKey(key); - if (e == null) - throw new StreamCorruptedException("Invalid enchantment " + key); - return e; - } - - // return "" + e.getId(); - @Override - @Nullable - public Enchantment deserialize(String s) { - return Enchantment.getByName(s); - } - - @Override - public boolean mustSyncDeserialization() { - return false; - } - })); + .before("enchantmenttype")); Material[] allMaterials = Material.values(); Classes.registerClass(new ClassInfo<>(Material.class, "material") diff --git a/src/main/java/ch/njol/skript/util/EnchantmentType.java b/src/main/java/ch/njol/skript/util/EnchantmentType.java index f302a7623c7..97f4b8d22d0 100644 --- a/src/main/java/ch/njol/skript/util/EnchantmentType.java +++ b/src/main/java/ch/njol/skript/util/EnchantmentType.java @@ -18,20 +18,24 @@ */ package ch.njol.skript.util; -import java.util.regex.Pattern; - +import ch.njol.skript.aliases.ItemType; import ch.njol.skript.bukkitutil.EnchantmentUtils; +import ch.njol.skript.classes.ClassInfo; +import ch.njol.skript.classes.Parser; +import ch.njol.skript.lang.ParseContext; +import ch.njol.skript.registrations.Classes; +import ch.njol.yggdrasil.YggdrasilSerializable; import org.bukkit.enchantments.Enchantment; -import org.eclipse.jdt.annotation.Nullable; +import org.jetbrains.annotations.Nullable; -import ch.njol.skript.aliases.ItemType; -import ch.njol.yggdrasil.YggdrasilSerializable; +import java.util.regex.Pattern; /** * @author Peter Güttinger */ public class EnchantmentType implements YggdrasilSerializable { + private static @Nullable Parser ENCHANTMENT_PARSER = null; private final Enchantment type; private final int level; @@ -49,6 +53,7 @@ public EnchantmentType(final Enchantment type) { this.type = type; this.level = -1; } + public EnchantmentType(final Enchantment type, final int level) { assert type != null; this.type = type; @@ -76,6 +81,7 @@ public Enchantment getType() { /** * Checks whether the given item type has this enchantment. + * * @param item the item to be checked. * @deprecated Use {@link ItemType#hasEnchantments(Enchantment...)} */ @@ -95,22 +101,33 @@ public String toString() { /** * Parses an enchantment type from string. This includes an {@link Enchantment} * and its level. + * * @param s String to parse. * @return Enchantment type, or null if parsing failed. */ @Nullable public static EnchantmentType parse(final String s) { + if (ENCHANTMENT_PARSER == null) { + ClassInfo classInfo = Classes.getExactClassInfo(Enchantment.class); + if (classInfo == null) { + throw new IllegalStateException("Enchantment ClassInfo not found"); + } + ENCHANTMENT_PARSER = (Parser) classInfo.getParser(); + if (ENCHANTMENT_PARSER == null) { + throw new IllegalStateException("Enchantment parser not found"); + } + } if (pattern.matcher(s).matches()) { String name = s.substring(0, s.lastIndexOf(' ')); assert name != null; - final Enchantment ench = EnchantmentUtils.parseEnchantment(name); + final Enchantment ench = ENCHANTMENT_PARSER.parse(name, ParseContext.DEFAULT); if (ench == null) return null; String level = s.substring(s.lastIndexOf(' ') + 1); assert level != null; return new EnchantmentType(ench, Utils.parseInt(level)); } - final Enchantment ench = EnchantmentUtils.parseEnchantment(s); + final Enchantment ench = ENCHANTMENT_PARSER.parse(s, ParseContext.DEFAULT); if (ench == null) return null; return new EnchantmentType(ench, -1); From f4c3a7a2425b7e354df54ad2db1661055cc3ff08 Mon Sep 17 00:00:00 2001 From: shanebeee Date: Mon, 1 Jul 2024 08:50:04 -0700 Subject: [PATCH 16/20] BukkitClasses - move usage to EnchantmentUtils --- .../java/ch/njol/skript/bukkitutil/EnchantmentUtils.java | 5 ++++- src/main/java/ch/njol/skript/classes/data/BukkitClasses.java | 1 - 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/src/main/java/ch/njol/skript/bukkitutil/EnchantmentUtils.java b/src/main/java/ch/njol/skript/bukkitutil/EnchantmentUtils.java index 3bd9c43f788..4760260ff61 100644 --- a/src/main/java/ch/njol/skript/bukkitutil/EnchantmentUtils.java +++ b/src/main/java/ch/njol/skript/bukkitutil/EnchantmentUtils.java @@ -23,6 +23,7 @@ import ch.njol.skript.classes.Serializer; import ch.njol.skript.lang.ParseContext; import ch.njol.skript.localization.Language; +import ch.njol.util.StringUtils; import ch.njol.yggdrasil.Fields; import org.bukkit.NamespacedKey; import org.bukkit.Registry; @@ -180,7 +181,9 @@ public Enchantment deserialize(String s) { public boolean mustSyncDeserialization() { return false; } - }).supplier(Enchantment.values()); + }) + .usage(StringUtils.join(EnchantmentUtils.getNames(), ", ")) + .supplier(Enchantment.values()); } } diff --git a/src/main/java/ch/njol/skript/classes/data/BukkitClasses.java b/src/main/java/ch/njol/skript/classes/data/BukkitClasses.java index f98ec41b844..4ba2579ca23 100644 --- a/src/main/java/ch/njol/skript/classes/data/BukkitClasses.java +++ b/src/main/java/ch/njol/skript/classes/data/BukkitClasses.java @@ -1232,7 +1232,6 @@ public boolean mustSyncDeserialization() { .name("Enchantment") .description("An enchantment, e.g. 'sharpness' or 'fortune'. Unlike enchantment type " + "this type has no level, but you usually don't need to use this type anyway.") - .usage(StringUtils.join(EnchantmentUtils.getNames(), ", ")) .examples("") .since("1.4.6") .before("enchantmenttype")); From 50b13bdf209b4ce09aad8d21beb5550f8a9ffd52 Mon Sep 17 00:00:00 2001 From: shanebeee Date: Mon, 1 Jul 2024 08:50:14 -0700 Subject: [PATCH 17/20] pull-6687-enchantment-update.sk - add test --- .../tests/regressions/pull-6687-enchantment-update.sk | 11 +++++++++++ 1 file changed, 11 insertions(+) create mode 100644 src/test/skript/tests/regressions/pull-6687-enchantment-update.sk diff --git a/src/test/skript/tests/regressions/pull-6687-enchantment-update.sk b/src/test/skript/tests/regressions/pull-6687-enchantment-update.sk new file mode 100644 index 00000000000..94f4d9a3c42 --- /dev/null +++ b/src/test/skript/tests/regressions/pull-6687-enchantment-update.sk @@ -0,0 +1,11 @@ +test "Enchantment Registry Update": + set {_i} to diamond sword of unbreaking 3 and sharpness 10 + + assert enchantment level of unbreaking of {_i} = 3 with "Unbreaking enchant on {_i} should have been 3" + assert enchantment level of sharpness of {_i} = 10 with "Sharpness enchant on {_i} should have been 10" + + set enchantment level of lure of {_i} to 5 + assert enchantment level of lure of {_i} = 5 with "Lure enchant on {_i} should have been 5" + + set enchantment level of sharpness of {_i} to 5 + assert enchantment level of sharpness of {_i} = 5 with "Sharpness enchant on {_i} should have been 5" From 70eff272e4b0378f516c90ec3d1fcdd1aadbeea5 Mon Sep 17 00:00:00 2001 From: shanebeee Date: Mon, 1 Jul 2024 12:26:55 -0700 Subject: [PATCH 18/20] BukkitClasses - add note regarding namespaces and custom enchants --- src/main/java/ch/njol/skript/classes/data/BukkitClasses.java | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/main/java/ch/njol/skript/classes/data/BukkitClasses.java b/src/main/java/ch/njol/skript/classes/data/BukkitClasses.java index 4ba2579ca23..bd8840d0e88 100644 --- a/src/main/java/ch/njol/skript/classes/data/BukkitClasses.java +++ b/src/main/java/ch/njol/skript/classes/data/BukkitClasses.java @@ -1231,7 +1231,9 @@ public boolean mustSyncDeserialization() { .user("enchantments?") .name("Enchantment") .description("An enchantment, e.g. 'sharpness' or 'fortune'. Unlike enchantment type " + - "this type has no level, but you usually don't need to use this type anyway.") + "this type has no level, but you usually don't need to use this type anyway.", + "NOTE: Minecraft namespaces are supported, ex: 'minecraft:basalt_deltas'.", + "As of Minecraft 1.21 this will also support custom enchantments using namespaces, ex: 'myenchants:explosive'.") .examples("") .since("1.4.6") .before("enchantmenttype")); From 80e81ffffaa2569c02b1c7fa288fb76d0f35fc0c Mon Sep 17 00:00:00 2001 From: shanebeee Date: Mon, 1 Jul 2024 13:23:37 -0700 Subject: [PATCH 19/20] EnchantmentUtils - only register listener if Registry is not found --- .../skript/bukkitutil/EnchantmentUtils.java | 49 ++++++++----------- 1 file changed, 21 insertions(+), 28 deletions(-) diff --git a/src/main/java/ch/njol/skript/bukkitutil/EnchantmentUtils.java b/src/main/java/ch/njol/skript/bukkitutil/EnchantmentUtils.java index 4760260ff61..a983b74f856 100644 --- a/src/main/java/ch/njol/skript/bukkitutil/EnchantmentUtils.java +++ b/src/main/java/ch/njol/skript/bukkitutil/EnchantmentUtils.java @@ -31,11 +31,8 @@ import org.jetbrains.annotations.Nullable; import java.io.StreamCorruptedException; -import java.util.ArrayList; -import java.util.Arrays; import java.util.Collection; import java.util.HashMap; -import java.util.List; import java.util.Locale; import java.util.Map; @@ -49,32 +46,28 @@ public class EnchantmentUtils { private static final boolean HAS_REGISTRY = BukkitUtils.registryExists("ENCHANTMENT"); static { - Language.addListener(() -> { - NAMES.clear(); - PATTERNS.clear(); - List enchantments = new ArrayList<>(); - if (HAS_REGISTRY) { - Registry.ENCHANTMENT.forEach(enchantments::add); - } else { - enchantments.addAll(Arrays.asList(Enchantment.values())); - } - for (Enchantment enchantment : enchantments) { - NamespacedKey key = enchantment.getKey(); - final String[] names = Language.getList("enchantments." + key.getKey()); - - if (!names[0].startsWith("enchantments.")) { - NAMES.put(enchantment, names[0]); - // Add lang file names - for (String name : names) - PATTERNS.put(name.toLowerCase(Locale.ENGLISH), enchantment); + if (!HAS_REGISTRY) { + Language.addListener(() -> { + NAMES.clear(); + PATTERNS.clear(); + for (Enchantment enchantment : Enchantment.values()) { + NamespacedKey key = enchantment.getKey(); + final String[] names = Language.getList("enchantments." + key.getKey()); + + if (!names[0].startsWith("enchantments.")) { + NAMES.put(enchantment, names[0]); + // Add lang file names + for (String name : names) + PATTERNS.put(name.toLowerCase(Locale.ENGLISH), enchantment); + } + // If Minecraft provided, add key without namespace and underscores (ex: "fire aspect") + if (key.getNamespace().equalsIgnoreCase("minecraft")) + PATTERNS.put(key.getKey().replace("_", " "), enchantment); + // Add full namespaced key as pattern (ex: "minecraft:fire_aspect", "custom:floopy_floopy") + PATTERNS.put(key.toString(), enchantment); } - // If Minecraft provided, add key without namespace and underscores (ex: "fire aspect") - if (key.getNamespace().equalsIgnoreCase("minecraft")) - PATTERNS.put(key.getKey().replace("_", " "), enchantment); - // Add full namespaced key as pattern (ex: "minecraft:fire_aspect", "custom:floopy_floopy") - PATTERNS.put(key.toString(), enchantment); - } - }); + }); + } } public static String getKey(Enchantment enchantment) { From c28da3711f4dfe83d3488162093d41ecf6655f38 Mon Sep 17 00:00:00 2001 From: Shane Bee Date: Mon, 1 Jul 2024 13:33:01 -0700 Subject: [PATCH 20/20] Update src/main/java/ch/njol/skript/bukkitutil/EnchantmentUtils.java Co-authored-by: Patrick Miller --- src/main/java/ch/njol/skript/bukkitutil/EnchantmentUtils.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/ch/njol/skript/bukkitutil/EnchantmentUtils.java b/src/main/java/ch/njol/skript/bukkitutil/EnchantmentUtils.java index a983b74f856..7247bba5d37 100644 --- a/src/main/java/ch/njol/skript/bukkitutil/EnchantmentUtils.java +++ b/src/main/java/ch/njol/skript/bukkitutil/EnchantmentUtils.java @@ -61,7 +61,7 @@ public class EnchantmentUtils { PATTERNS.put(name.toLowerCase(Locale.ENGLISH), enchantment); } // If Minecraft provided, add key without namespace and underscores (ex: "fire aspect") - if (key.getNamespace().equalsIgnoreCase("minecraft")) + if (key.getNamespace().equalsIgnoreCase(NamespacedKey.MINECRAFT)) PATTERNS.put(key.getKey().replace("_", " "), enchantment); // Add full namespaced key as pattern (ex: "minecraft:fire_aspect", "custom:floopy_floopy") PATTERNS.put(key.toString(), enchantment);