From c8f6b45357ff180700a83d88c255c8c8aa701a0c Mon Sep 17 00:00:00 2001 From: ShaneBee Date: Sat, 11 May 2024 08:01:44 -0700 Subject: [PATCH] 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