diff options
| author | Krakenied <Krakenied1@gmail.com> | 2024-01-07 06:21:08 +0100 |
|---|---|---|
| committer | Leonardo Bishop <13875753+LMBishop@users.noreply.github.com> | 2024-01-09 15:55:18 +0000 |
| commit | 554bb10f8df3b506a2e3cc11b717ab17fd61af70 (patch) | |
| tree | 99341c22471cf6dcdbfb82e47851255ebb3d8b09 /bukkit/src/main/java/com | |
| parent | 369ded87d21ffee99649fad27ab7d930d020b6b2 (diff) | |
Add namespaced types support to the 1.14+ item getter
Diffstat (limited to 'bukkit/src/main/java/com')
| -rw-r--r-- | bukkit/src/main/java/com/leonardobishop/quests/bukkit/hook/itemgetter/ItemGetter14.java | 30 | ||||
| -rw-r--r-- | bukkit/src/main/java/com/leonardobishop/quests/bukkit/util/NamespacedKeyUtils.java | 69 |
2 files changed, 95 insertions, 4 deletions
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 bdab3ad4..630b0fbd 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 @@ -1,8 +1,11 @@ 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.Registry; import org.bukkit.attribute.Attribute; import org.bukkit.attribute.AttributeModifier; import org.bukkit.configuration.ConfigurationSection; @@ -21,7 +24,7 @@ import java.util.UUID; /** * Reads the following: * <ul> - * <li>type (<b>without</b> data support, <b>without</b> namespace support)</li> + * <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> @@ -224,15 +227,34 @@ public class ItemGetter14 extends ItemGetter { } Material type = Material.getMaterial(typeString); - if (type == null) { + if (type != null) { + return new ItemStack(type, 1); + } + + NamespacedKey typeKey = NamespacedKeyUtils.fromString(typeString); + if (typeKey == null) { return invalidItemStack; } - return new ItemStack(type, 1); + type = Registry.MATERIAL.get(typeKey); + if (type != null) { + return new ItemStack(type, 1); + } + + return invalidItemStack; } @Override public boolean isValidMaterial(String typeString) { - return Material.getMaterial(typeString) != null; + if (Material.getMaterial(typeString) != null) { + return true; + } + + NamespacedKey typeKey = NamespacedKeyUtils.fromString(typeString); + if (typeKey == null) { + return false; + } + + return Registry.MATERIAL.get(typeKey) != null; } } diff --git a/bukkit/src/main/java/com/leonardobishop/quests/bukkit/util/NamespacedKeyUtils.java b/bukkit/src/main/java/com/leonardobishop/quests/bukkit/util/NamespacedKeyUtils.java new file mode 100644 index 00000000..7a65c452 --- /dev/null +++ b/bukkit/src/main/java/com/leonardobishop/quests/bukkit/util/NamespacedKeyUtils.java @@ -0,0 +1,69 @@ +package com.leonardobishop.quests.bukkit.util; + +import org.bukkit.NamespacedKey; + +/** + * Utility class (for compatibility reasons) with method to get {@link NamespacedKey} from string. + * Contains validation methods from the <a href="https://hub.spigotmc.org/stash/projects/SPIGOT/repos/bukkit/browse/src/main/java/org/bukkit/NamespacedKey.java"> + * original Spigot server implementation</a> introduced in 1.16.5. + * + * @see NamespacedKey#fromString(String) + */ +@SuppressWarnings("BooleanMethodIsAlwaysInverted") +public class NamespacedKeyUtils { + + public static NamespacedKey fromString(String string) { + if (string == null) { + return null; + } + + String[] parts = string.split(":", 3); + return switch (parts.length) { + case 1 -> isValidKey(parts[0]) ? NamespacedKey.minecraft(parts[0]) : null; + case 2 -> isValidNamespace(parts[0]) && isValidKey(parts[1]) ? new NamespacedKey(parts[0], parts[1]) : null; + default -> null; + }; + } + + private static boolean isValidNamespace(String namespace) { + int len = namespace.length(); + if (len == 0) { + return false; + } + + for (int i = 0; i < len; i++) { + char c = namespace.charAt(i); + + if (!isValidNamespaceChar(c)) { + return false; + } + } + + return true; + } + + private static boolean isValidNamespaceChar(char c) { + return (c >= 'a' && c <= 'z') || (c >= '0' && c <= '9') || c == '.' || c == '_' || c == '-'; + } + + private static boolean isValidKey(String key) { + int len = key.length(); + if (len == 0) { + return false; + } + + for (int i = 0; i < len; i++) { + char c = key.charAt(i); + + if (!isValidKeyChar(c)) { + return false; + } + } + + return true; + } + + private static boolean isValidKeyChar(char c) { + return isValidNamespaceChar(c) || c == '/'; + } +} |
