From 962ce9cb4838e7e80dddc050de5a297d4329c590 Mon Sep 17 00:00:00 2001 From: alexcrea <42614139+alexcrea@users.noreply.github.com> Date: Sun, 7 Jul 2024 19:08:31 +0200 Subject: [PATCH] Add enchantment register api. --- .../alexcrea/cuanvil/api/EnchantmentApi.java | 92 +++++++++++++++++++ .../enchant/CAEnchantmentRegistry.java | 13 ++- 2 files changed, 101 insertions(+), 4 deletions(-) create mode 100644 src/main/java/xyz/alexcrea/cuanvil/api/EnchantmentApi.java diff --git a/src/main/java/xyz/alexcrea/cuanvil/api/EnchantmentApi.java b/src/main/java/xyz/alexcrea/cuanvil/api/EnchantmentApi.java new file mode 100644 index 0000000..5176eef --- /dev/null +++ b/src/main/java/xyz/alexcrea/cuanvil/api/EnchantmentApi.java @@ -0,0 +1,92 @@ +package xyz.alexcrea.cuanvil.api; + +import org.bukkit.NamespacedKey; +import org.bukkit.enchantments.Enchantment; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; +import xyz.alexcrea.cuanvil.enchant.CAEnchantment; +import xyz.alexcrea.cuanvil.enchant.CAEnchantmentRegistry; +import xyz.alexcrea.cuanvil.enchant.wrapped.CAVanillaEnchantment; + +/** + * Custom Anvil api for enchantment registry. + */ +@SuppressWarnings("unused") +public class EnchantmentApi { + + private EnchantmentApi() {} + + /** + * Register an enchantment. + * + * @param enchantment the enchantment to register + * @return true if successful + */ + public static boolean registerEnchantment(@NotNull CAEnchantment enchantment){ + return CAEnchantmentRegistry.getInstance().register(enchantment); + } + + /** + * Register an enchantment by minecraft registered enchantment. + * + * @param enchantment the enchantment to register + * @return true if successful + */ + public static boolean registerEnchantment(@NotNull Enchantment enchantment){ + return registerEnchantment(new CAVanillaEnchantment(enchantment)); + } + + /** + * Unregister an enchantment by its key. + * + * @param key the enchantment key to unregister + * @return true if successful + */ + public static boolean unregisterEnchantment(@NotNull NamespacedKey key){ + CAEnchantment enchantment = CAEnchantmentRegistry.getInstance().getByKey(key); + return CAEnchantmentRegistry.getInstance().unregister(enchantment); + } + + /** + * Unregister an enchantment. + * + * @param enchantment the enchantment to unregister + * @return true if successful + */ + public static boolean unregisterEnchantment(@NotNull CAEnchantment enchantment){ + return CAEnchantmentRegistry.getInstance().unregister(enchantment); + } + + /** + * Unregister an enchantment by his bukkit enchantment. + * + * @param enchantment the enchantment to unregister + * @return true if successful + */ + public static boolean unregisterEnchantment(@NotNull Enchantment enchantment){ + return unregisterEnchantment(enchantment.getKey()); + } + + /** + * Get by key the enchantment. + * + * @param key the key used to fetch + * @return the custom anvil enchantment + */ + @Nullable + public static CAEnchantment getByKey(@NotNull NamespacedKey key){ + return CAEnchantmentRegistry.getInstance().getByKey(key); + } + + /** + * Get by name the enchantment. + * + * @param name the name used to fetch + * @return the custom anvil enchantment + */ + @Nullable + public static CAEnchantment getByName(@NotNull String name){ + return CAEnchantmentRegistry.getInstance().getByName(name); + } + +} diff --git a/src/main/java/xyz/alexcrea/cuanvil/enchant/CAEnchantmentRegistry.java b/src/main/java/xyz/alexcrea/cuanvil/enchant/CAEnchantmentRegistry.java index c00c7c2..1f29b4e 100644 --- a/src/main/java/xyz/alexcrea/cuanvil/enchant/CAEnchantmentRegistry.java +++ b/src/main/java/xyz/alexcrea/cuanvil/enchant/CAEnchantmentRegistry.java @@ -56,13 +56,14 @@ public class CAEnchantmentRegistry { * No guarantee that the enchantment will be present on the config gui if registered late. * (By late I mean after custom anvil startup.) * @param enchantment The enchantment to be registered. + * @return If the operation was successful. */ - public void register(@NotNull CAEnchantment enchantment){ + public boolean register(@NotNull CAEnchantment enchantment){ if(byKeyMap.containsKey(enchantment.getKey())){ CustomAnvil.instance.getLogger().log(Level.WARNING, "Duplicate registered enchantment. This should NOT happen.", new IllegalStateException(enchantment.getKey()+" enchantment was already registered")); - return; + return false; } if(byNameMap.containsKey(enchantment.getName())){ CustomAnvil.instance.getLogger().log(Level.WARNING, @@ -77,6 +78,7 @@ public class CAEnchantmentRegistry { if(!enchantment.isOptimised()){ unoptimisedValues.add(enchantment); } + return true; } /** @@ -87,13 +89,16 @@ public class CAEnchantmentRegistry { * No guarantee that the enchantment will absent if the config guis if unregistered late. * (By late I mean after custom anvil startup.) * @param enchantment The enchantment to be unregistered. + * @return If the operation was successful. */ - public void unregister(CAEnchantment enchantment){ - if(enchantment == null) return; + + public boolean unregister(CAEnchantment enchantment){ + if(enchantment == null) return false; byKeyMap.remove(enchantment.getKey()); byNameMap.remove(enchantment.getName()); unoptimisedValues.remove(enchantment); + return true; } /**