diff options
| author | Krakenied <Krakenied1@gmail.com> | 2024-02-20 11:00:11 +0100 |
|---|---|---|
| committer | Leonardo Bishop <13875753+LMBishop@users.noreply.github.com> | 2024-02-22 16:33:26 +0000 |
| commit | 33c409dcb3139c18fa1304b1c881b66cbac9745f (patch) | |
| tree | 97b548c3157fb2b299a72e40aa1b3d6d886d592f | |
| parent | 32aa65fa349837c1af172d22aa9c61e4a00e01d9 (diff) | |
Add namespaced enchantments support to 1.13+ and 1.14+ item getters
| -rw-r--r-- | bukkit/src/main/java/com/leonardobishop/quests/bukkit/hook/itemgetter/ItemGetter13.java | 35 | ||||
| -rw-r--r-- | bukkit/src/main/java/com/leonardobishop/quests/bukkit/hook/itemgetter/ItemGetter14.java | 33 |
2 files changed, 46 insertions, 22 deletions
diff --git a/bukkit/src/main/java/com/leonardobishop/quests/bukkit/hook/itemgetter/ItemGetter13.java b/bukkit/src/main/java/com/leonardobishop/quests/bukkit/hook/itemgetter/ItemGetter13.java index 8b89bdfe..b72894fa 100644 --- a/bukkit/src/main/java/com/leonardobishop/quests/bukkit/hook/itemgetter/ItemGetter13.java +++ b/bukkit/src/main/java/com/leonardobishop/quests/bukkit/hook/itemgetter/ItemGetter13.java @@ -1,8 +1,10 @@ package com.leonardobishop.quests.bukkit.hook.itemgetter; import com.leonardobishop.quests.bukkit.BukkitQuestsPlugin; +import com.leonardobishop.quests.bukkit.util.NamespacedKeyUtils; import com.leonardobishop.quests.bukkit.util.chat.Chat; import org.bukkit.Material; +import org.bukkit.NamespacedKey; import org.bukkit.attribute.Attribute; import org.bukkit.attribute.AttributeModifier; import org.bukkit.configuration.ConfigurationSection; @@ -24,7 +26,7 @@ import java.util.UUID; * <li>type (<b>without</b> data support, <b>without</b> namespace support)</li> * <li>name</li> * <li>lore</li> - * <li>enchantments (<b>without</b> namespace support)</li> + * <li>enchantments (<b>with</b> namespace support)</li> * <li>item flags</li> * <li>unbreakability (<b>with</b> CraftBukkit support)</li> * <li>attribute modifiers</li> @@ -86,22 +88,33 @@ public class ItemGetter13 extends ItemGetter { continue; } - Enchantment enchantment = Enchantment.getByName(parts[0]); + boolean namespaced = parts.length >= 2 && parts[0].startsWith("(") && parts[1].endsWith(")"); + + Enchantment enchantment; + if (namespaced) { + String namespacedKeyString = enchantmentString.substring(1, parts[0].length() + parts[1].length()); + NamespacedKey namespacedKey = NamespacedKeyUtils.fromString(namespacedKeyString); + enchantment = Enchantment.getByKey(namespacedKey); + } else { + enchantment = Enchantment.getByName(parts[0]); + } + if (enchantment == null) { continue; } - int level; - if (parts.length == 2) { + // (namespace:key):level + // 0 1 2 + // SOME_ENUM_NAME:level + // 0 1 + int levelIndex = namespaced ? 2 : 1; + + int level = 1; + if (parts.length >= levelIndex + 1) { try { - level = Integer.parseUnsignedInt(parts[1]); - } catch (NumberFormatException e) { - continue; + level = Integer.parseUnsignedInt(parts[levelIndex]); + } catch (NumberFormatException ignored) { } - } else if (parts.length == 1) { - level = 1; - } else { - continue; } meta.addEnchant(enchantment, level, true); diff --git a/bukkit/src/main/java/com/leonardobishop/quests/bukkit/hook/itemgetter/ItemGetter14.java b/bukkit/src/main/java/com/leonardobishop/quests/bukkit/hook/itemgetter/ItemGetter14.java index 351a7dd4..c90ca99c 100644 --- a/bukkit/src/main/java/com/leonardobishop/quests/bukkit/hook/itemgetter/ItemGetter14.java +++ b/bukkit/src/main/java/com/leonardobishop/quests/bukkit/hook/itemgetter/ItemGetter14.java @@ -27,7 +27,7 @@ import java.util.UUID; * <li>type (<b>without</b> data support, <b>with</b> namespace support)</li> * <li>name</li> * <li>lore</li> - * <li>enchantments (<b>without</b> namespace support)</li> + * <li>enchantments (<b>with</b> namespace support)</li> * <li>item flags</li> * <li>unbreakability (<b>with</b> CraftBukkit support)</li> * <li>attribute modifiers</li> @@ -90,22 +90,33 @@ public class ItemGetter14 extends ItemGetter { continue; } - Enchantment enchantment = Enchantment.getByName(parts[0]); + boolean namespaced = parts.length >= 2 && parts[0].startsWith("(") && parts[1].endsWith(")"); + + Enchantment enchantment; + if (namespaced) { + String namespacedKeyString = enchantmentString.substring(1, parts[0].length() + parts[1].length()); + NamespacedKey namespacedKey = NamespacedKeyUtils.fromString(namespacedKeyString); + enchantment = Enchantment.getByKey(namespacedKey); + } else { + enchantment = Enchantment.getByName(parts[0]); + } + if (enchantment == null) { continue; } - int level; - if (parts.length == 2) { + // (namespace:key):level + // 0 1 2 + // SOME_ENUM_NAME:level + // 0 1 + int levelIndex = namespaced ? 2 : 1; + + int level = 1; + if (parts.length >= levelIndex + 1) { try { - level = Integer.parseUnsignedInt(parts[1]); - } catch (NumberFormatException e) { - continue; + level = Integer.parseUnsignedInt(parts[levelIndex]); + } catch (NumberFormatException ignored) { } - } else if (parts.length == 1) { - level = 1; - } else { - continue; } meta.addEnchant(enchantment, level, true); |
