i made this
may you can fix it for your needs
public static Map<String,Integer> getEnchantments(ItemStack stack){
Map<String,Integer> spells = new HashMap<>();
if( stack != null && stack.isEmpty() ) {
//System.out.println("~~~~~ Stack is empty");
}
if( stack != null && !stack.isEmpty() ) {
ItemEnchantmentsComponent itemEnchantmentsComponent = stack.getOrDefault(DataComponentTypes.ENCHANTMENTS, ItemEnchantmentsComponent.DEFAULT);
for (Object2IntMap.Entry<RegistryEntry<Enchantment>> entry : itemEnchantmentsComponent.getEnchantmentEntries()) {
spells.put(((RegistryEntry<Enchantment>) entry.getKey()).getIdAsString(), entry.getIntValue());
System.out.println(
((RegistryEntry<Enchantment>)entry.getKey()).getIdAsString()
+ " -> "
+ entry.getIntValue()
+ " x");
}
}
return spells;
}