Add enchantment register api.

This commit is contained in:
alexcrea 2024-07-07 19:08:31 +02:00
parent 97f175462d
commit 962ce9cb48
No known key found for this signature in database
GPG key ID: 43FD265DB0DBF91F
2 changed files with 101 additions and 4 deletions

View file

@ -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);
}
}

View file

@ -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;
}
/**