Posted April 26, 20205 yr I'm new on modding and i have a question. I'm trying to add an enchantment to a custom tool by default , I've already done it but I think its not the best way. Here is my code: package littlemonge.rpgcraft.item.tools.icite; import java.util.List; import javax.annotation.Nullable; import net.minecraft.client.util.ITooltipFlag; import net.minecraft.enchantment.EnchantmentHelper; import net.minecraft.enchantment.Enchantments; import net.minecraft.entity.Entity; import net.minecraft.entity.player.PlayerEntity; import net.minecraft.item.IItemTier; import net.minecraft.item.ItemGroup; import net.minecraft.item.ItemStack; import net.minecraft.item.PickaxeItem; import net.minecraft.nbt.CompoundNBT; import net.minecraft.nbt.ListNBT; import net.minecraft.util.NonNullList; import net.minecraft.util.text.ITextComponent; import net.minecraft.world.World; import net.minecraftforge.api.distmarker.Dist; import net.minecraftforge.api.distmarker.OnlyIn; public class IcitePickaxeItem extends PickaxeItem { public IcitePickaxeItem(IItemTier tier, int attackDamageIn, float attackSpeedIn, Properties builder, String name) { super(tier, attackDamageIn, attackSpeedIn, builder); this.setRegistryName(name); } @Override public void fillItemGroup(ItemGroup group, NonNullList<ItemStack> items) { super.fillItemGroup(group, items); for (ItemStack stack : items) { if (stack.getItem().equals(this)) { stack.addEnchantment(Enchantments.SILK_TOUCH, 1); } } } public void onCreated(ItemStack stack, World worldIn, PlayerEntity playerIn) { addSilkTouch(stack); } public void inventoryTick(ItemStack stack, World worldIn, Entity entityIn, int itemSlot, boolean isSelected) { addSilkTouch(stack); super.inventoryTick(stack, worldIn, entityIn, itemSlot, isSelected); } @OnlyIn(Dist.CLIENT) public void addInformation(ItemStack stack, @Nullable World worldIn, List<ITextComponent> tooltip, ITooltipFlag flagIn) { addSilkTouch(stack); super.addInformation(stack, worldIn, tooltip, flagIn); ItemStack.addEnchantmentTooltips(tooltip, getEnchantments(stack)); } public static ListNBT getEnchantments(ItemStack stack) { CompoundNBT compoundnbt = stack.getTag(); return compoundnbt != null ? compoundnbt.getList("StoredEnchantments", 10) : new ListNBT(); } private void addSilkTouch(ItemStack stack) { if (!stack.isEnchanted()) { stack.addEnchantment(Enchantments.SILK_TOUCH, 1); } else { if (EnchantmentHelper.getEnchantments(stack).get(Enchantments.SILK_TOUCH) == null) { stack.addEnchantment(Enchantments.SILK_TOUCH, 1); } } } } Thanks in advance, regards. Edited April 27, 20205 yr by littlemonge solved
April 26, 20205 yr Author 9 minutes ago, diesieben07 said: Just add the stack directly instead of calling super and then looping through the entire list. ok thanks for the quick reply , something else?
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.