Started by
dreamingdreamer
on
Topic category: Help with Minecraft modding (Java Edition)
I tried writing code with ChatGPT.
But it still shows up as a limited enchantment with ...? in the Enchant Table.
How can I put vanilla enchantments on items?
//custom element
@EventBusSubscriber
public class AllowCustomBookEnchant {
@SubscribeEvent
public static void onEnchantabilityCheck(EnchantmentLevelSetEvent event) {
ItemStack stack = event.getItem();
if (stack.getItem() instanceof BlessedBookItem || stack.getItem() instanceof AdvancedBlessedBookItem) {
event.setEnchantLevel(event.getEnchantLevel());
ItemEnchantments enchantments = stack.getEnchantments();
stack.set(DataComponents.ENCHANTMENTS, enchantments);
}
}
//book.java
public class BlessedBookItem extends Item {
public BlessedBookItem(Item.Properties properties) {
super(properties.stacksTo(1).component(DataComponents.ENCHANTABLE, new Enchantable(7))
.component(DataComponents.ENCHANTMENTS, ItemEnchantments.EMPTY)
.component(DataComponents.REPAIR_COST, 1));
}
public static void addStoredEnchant(ItemStack stack, ItemEnchantments enchants) {
stack.set(DataComponents.ENCHANTMENTS, enchants);
}
public static boolean hasStoredEnchantments(ItemStack stack) {
return stack.has(DataComponents.ENCHANTMENTS);
}
}
do you change the ItemEnchantments.EMPTY part for the item? also, for the question in your text, custom tools already allow enchants provided they are in the correct enchantable tag (can be seen through the element tags tab on the left (should already default to the enchantable tag of the set item tool type))