Jump to content

[SOLVED] [1.15.2] Adding enchantment a tool by default.


littlemonge

Recommended Posts

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 by littlemonge
solved
Link to comment
Share on other sites

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.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Announcements



×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.