Update gui on enchantment registering.

This commit is contained in:
alexcrea 2024-07-08 21:53:19 +02:00
parent 0761c70286
commit 1bd3328281
No known key found for this signature in database
GPG key ID: 43FD265DB0DBF91F
2 changed files with 29 additions and 15 deletions

View file

@ -8,6 +8,8 @@ import xyz.alexcrea.cuanvil.enchant.CAEnchantment;
import xyz.alexcrea.cuanvil.enchant.CAEnchantmentRegistry;
import xyz.alexcrea.cuanvil.enchant.EnchantmentRarity;
import xyz.alexcrea.cuanvil.enchant.wrapped.CAVanillaEnchantment;
import xyz.alexcrea.cuanvil.gui.config.global.EnchantCostConfigGui;
import xyz.alexcrea.cuanvil.gui.config.global.EnchantLimitConfigGui;
import java.util.Collections;
import java.util.Map;
@ -27,7 +29,13 @@ public class EnchantmentApi {
* @return True if successful.
*/
public static boolean registerEnchantment(@NotNull CAEnchantment enchantment){
return CAEnchantmentRegistry.getInstance().register(enchantment);
if(!CAEnchantmentRegistry.getInstance().register(enchantment)) return false;
// Add enchantment to gui.
EnchantCostConfigGui.INSTANCE.updateValueForGeneric(enchantment, true);
EnchantLimitConfigGui.INSTANCE.updateValueForGeneric(enchantment, true);
return true;
}
/**
@ -56,6 +64,20 @@ public class EnchantmentApi {
return registerEnchantment(new CAVanillaEnchantment(enchantment));
}
/**
* Unregister an enchantment.
*
* @param enchantment The enchantment to unregister
* @return True if successful.
*/
public static boolean unregisterEnchantment(@Nullable CAEnchantment enchantment){
// Remove from gui
EnchantCostConfigGui.INSTANCE.removeGeneric(enchantment);
EnchantLimitConfigGui.INSTANCE.removeGeneric(enchantment);
return CAEnchantmentRegistry.getInstance().unregister(enchantment);
}
/**
* Unregister an enchantment by its key.
*
@ -64,17 +86,7 @@ public class EnchantmentApi {
*/
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);
return unregisterEnchantment(enchantment);
}
/**

View file

@ -89,7 +89,7 @@ public class CAEnchantmentRegistry {
* @return If the operation was successful.
*/
public boolean unregister(CAEnchantment enchantment){
public boolean unregister(@Nullable CAEnchantment enchantment){
if(enchantment == null) return false;
byKeyMap.remove(enchantment.getKey());
byNameMap.remove(enchantment.getName());
@ -103,7 +103,8 @@ public class CAEnchantmentRegistry {
* @param key Key to fetch.
* @return Registered enchantment. null if absent.
*/
public @Nullable CAEnchantment getByKey(@NotNull NamespacedKey key){
@Nullable
public CAEnchantment getByKey(@NotNull NamespacedKey key){
return byKeyMap.get(key);
}
@ -112,7 +113,8 @@ public class CAEnchantmentRegistry {
* @param name Name to fetch.
* @return Registered enchantment. null if absent.
*/
public @Nullable CAEnchantment getByName(@NotNull String name){
@Nullable
public CAEnchantment getByName(@NotNull String name){
return byNameMap.get(name);
}