Jump to content
View in the app

A better way to browse. Learn more.

Forge Forums

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Featured Replies

Posted

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

  • 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.

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...

Important Information

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

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.