fix /ca enchant
Some checks failed
Java CI with Gradle / build (push) Has been cancelled

This commit is contained in:
alexcrea 2026-07-08 17:36:47 +02:00
parent d469171e4d
commit 497a3e84bc
Signed by: alexcrea
GPG key ID: E59DF23EE2A2266C
4 changed files with 28 additions and 15 deletions

View file

@ -128,7 +128,7 @@ public class EnchantmentApi {
* @return The custom anvil enchantment of this key. null if not found.
*/
@Nullable
public static CAEnchantment getByKey(@NotNull NamespacedKey key) {
public static CAEnchantment getByKey(@Nullable NamespacedKey key) {
return CAEnchantment.getByKey(key);
}

View file

@ -230,7 +230,7 @@ public interface CAEnchantment {
* @param key The enchantment key
* @return Array of enchantment.
*/
static @Nullable CAEnchantment getByKey(@NotNull NamespacedKey key){
static @Nullable CAEnchantment getByKey(@Nullable NamespacedKey key){
return CAEnchantmentRegistry.getInstance().getByKey(key);
}

View file

@ -151,7 +151,7 @@ public class CAEnchantmentRegistry {
* @return Registered enchantment. null if absent.
*/
@Nullable
public CAEnchantment getByKey(@NotNull NamespacedKey key) {
public CAEnchantment getByKey(@Nullable NamespacedKey key) {
return byKeyMap.get(key);
}

View file

@ -2,10 +2,14 @@ package xyz.alexcrea.cuanvil.command
import io.delilaheve.CustomAnvil
import io.delilaheve.util.ConfigOptions
import io.delilaheve.util.ItemUtil.isEnchantedBook
import org.bukkit.Material
import org.bukkit.NamespacedKey
import org.bukkit.command.Command
import org.bukkit.command.CommandSender
import org.bukkit.entity.HumanEntity
import xyz.alexcrea.cuanvil.api.EnchantmentApi
import xyz.alexcrea.cuanvil.enchant.CAEnchantment
import xyz.alexcrea.cuanvil.util.MaterialUtil.isAir
class EnchantExecutor : CASubCommand() {
@ -37,22 +41,18 @@ class EnchantExecutor : CASubCommand() {
sender.sendMessage("Missing enchantment parameter")
return true
}
1 -> {
sender.sendMessage("Missing level parameter")
return true
}
}
val enchants = EnchantmentApi.getListByName(args[0].lowercase())
if (enchants.isEmpty()) {
val enchant = firstEnchantment(args[0])
if (enchant == null) {
sender.sendMessage("Enchantment not found: ${args[0]}")
return true
}
val enchant = enchants.iterator().next()
val level = if (args.size > 1)
args[1].toIntOrNull()?.coerceIn(0, ConfigOptions.ENCHANT_LIMIT)
else 1
val level = args[1].toIntOrNull()?.coerceIn(0, ConfigOptions.ENCHANT_LIMIT)
if (level == null) {
sender.sendMessage("Invalid number: ${args[1]}")
return true
@ -66,8 +66,15 @@ class EnchantExecutor : CASubCommand() {
if (level == 0) {
enchant.removeFrom(inHand)
if (inHand.isEnchantedBook() && EnchantmentApi.getEnchantments(inHand).isEmpty())
inHand.type = Material.BOOK
sender.sendMessage("${enchant.prettyName} removed")
} else {
if (Material.BOOK == inHand.type)
inHand.type = Material.ENCHANTED_BOOK
enchant.addEnchantmentUnsafe(inHand, level)
sender.sendMessage("${enchant.prettyName} set to level $level")
}
@ -92,10 +99,8 @@ class EnchantExecutor : CASubCommand() {
sender: HumanEntity,
name: String
): Collection<String> {
val enchants = EnchantmentApi.getListByName(name.lowercase())
if (enchants.isEmpty()) return listOf()
val enchant = firstEnchantment(name) ?: return listOf()
val enchant = enchants.iterator().next()
val limit = ConfigOptions.enchantLimit(enchant)
val result = mutableListOf<String>()
@ -110,4 +115,12 @@ class EnchantExecutor : CASubCommand() {
return result
}
private fun firstEnchantment(name: String): CAEnchantment? {
val enchants = EnchantmentApi.getListByName(name.lowercase())
if (!enchants.isEmpty())
return enchants.iterator().next()
return EnchantmentApi.getByKey(NamespacedKey.fromString(name))
}
}