Jump to content

[1.9.4] Adding Enchantments


JimiIT92

Recommended Posts

So i was trying adding the smelting enchant on the pickaxe, using the CraftPlusPlus code, but is not working. This is the first time i try adding an enchant, so i don't know nothing about them. I've also tried looking into other enchantments classes, like silk touch, but there is no code of the tool behavior on that enchant.

At the moment i'm using this as a base class for all the custom enchantments

package com.mineworld.core;

import java.util.List;
import java.util.OptionalInt;
import java.util.stream.IntStream;

import com.google.common.collect.Lists;
import com.mineworld.MW;

import net.minecraft.client.resources.I18n;
import net.minecraft.enchantment.Enchantment;
import net.minecraft.enchantment.EnchantmentHelper;
import net.minecraft.enchantment.EnumEnchantmentType;
import net.minecraft.entity.Entity;
import net.minecraft.inventory.EntityEquipmentSlot;
import net.minecraft.item.ItemStack;
import net.minecraft.util.ResourceLocation;
import net.minecraftforge.fml.common.eventhandler.Event;
import net.minecraftforge.fml.common.registry.GameRegistry;

public abstract class MWEnchantment extends Enchantment {
/**
 * A list of all of Craft++'s enchantments
 */
public static List<MWEnchantment> Enchantments = Lists.newArrayList();

protected MWEnchantment(String name, Enchantment.Rarity rarityIn, EnumEnchantmentType typeIn, EntityEquipmentSlot... slots) {
	super(rarityIn, typeIn, slots);
	this.setName(name);
	Enchantment.REGISTRY.register(findFreeEnchantmentID(name), new ResourceLocation(MW.MODID + ":" + name), this);
	Enchantments.add(this);
}

/**
 * Finds the first free enchantment ID to register this enchantment
 *
 * @param enchantmentName the name of the enchantment
 * @return The enchantment ID for this enchantment to use
 */
private static int findFreeEnchantmentID(String enchantmentName) {
	OptionalInt freeEnchantmentID = IntStream.range(0, 256).filter(i -> Enchantment.getEnchantmentByID(i) == null).findFirst();
	if (!freeEnchantmentID.isPresent())
		throw new NoFreeEnchantmentIDException(enchantmentName);
	return freeEnchantmentID.getAsInt();
}

@Override
public boolean isAllowedOnBooks() {
	return true;
}

/**
 * Gets the enchantment level of this enchantment on the specified ItemStack
 *
 * @param itemstack The ItemStack to check
 * @return The enchantment level of this enchantment on the ItemStack
 */
protected int getEnchantmentLevel(ItemStack itemstack) {
	return EnchantmentHelper.getEnchantmentLevel(this, itemstack);
}

@Override
public int getMinEnchantability(int enchantmentLevel) {
	return getMinimumEnchantability(enchantmentLevel);
}

@Override
public int getMaxEnchantability(int enchantmentLevel) {
	return getMaximumEnchantability(enchantmentLevel);
}

/**
 * Performs the action this enchantment does
 *
 * @param entity    The entity to go along with the enchantment
 * @param baseEvent The event to go along with the enchantment
 */
public abstract void performAction(Entity entity, Event baseEvent);

public abstract int getMinimumEnchantability(int enchantmentLevel);

public abstract int getMaximumEnchantability(int enchantmentLevel);

private static class NoFreeEnchantmentIDException extends RuntimeException {
	private NoFreeEnchantmentIDException(String enchantmentName) {
		super("Could not find a free enchantment ID for " + I18n.format("enchantment." + enchantmentName));
	}
}
}

 

And this as the class of the smelting enchant

package com.mineworld.enchantments;

import java.lang.reflect.Constructor;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Random;

import com.mineworld.core.MWEnchantment;

import net.minecraft.enchantment.EnumEnchantmentType;
import net.minecraft.entity.Entity;
import net.minecraft.entity.EntityLivingBase;
import net.minecraft.inventory.EntityEquipmentSlot;
import net.minecraft.item.ItemBlock;
import net.minecraft.item.ItemStack;
import net.minecraft.item.crafting.FurnaceRecipes;
import net.minecraftforge.event.world.BlockEvent.HarvestDropsEvent;
import net.minecraftforge.fml.common.eventhandler.Event;

public class EnchantmentSmelting extends MWEnchantment {

public EnchantmentSmelting() {
	super("smelting", Rarity.UNCOMMON, EnumEnchantmentType.DIGGER,new EntityEquipmentSlot[]{EntityEquipmentSlot.MAINHAND, EntityEquipmentSlot.OFFHAND});
}


@Override
public int getMaxLevel() {
	return 1;
}

@Override
public String getName() {
	return "enchantment.smelting.name";
}

@Override
public boolean canApplyAtEnchantingTable(ItemStack stack) {
	return true;
}

@Override
public boolean isAllowedOnBooks() {
	return true;
}

@Override
public void performAction(Entity entity, Event baseEvent) {
	if (entity != null && this.getEnchantmentLevel(((EntityLivingBase) entity).getHeldItemMainhand()) > 0) {
		HarvestDropsEvent event = (HarvestDropsEvent) baseEvent;
		List<ItemStack> drops = event.getDrops();
		List<ItemStack> dropsCopy = this.copyList(drops);
		drops.clear();
		for (ItemStack drop : dropsCopy)
			if (drop != null) {
				ItemStack smeltingResult = FurnaceRecipes.instance().getSmeltingResult(drop);
				if (smeltingResult != null) {
					smeltingResult = smeltingResult.copy();
					smeltingResult.stackSize *= drop.stackSize;
					int fortuneLevel = event.getFortuneLevel();
					if (!(smeltingResult.getItem() instanceof ItemBlock))
						smeltingResult.stackSize *= new Random().nextInt(fortuneLevel + 1) + 1;
					drops.add(smeltingResult);
				} else
					drops.add(drop);
			}
	}
}

public static <T> List<T> copyList(List<T> list) {
	try {
		Constructor constructor = list.getClass().getConstructor(Collection.class);
		return (List<T>) constructor.newInstance(list);
	} catch (Exception exception) {
		return new ArrayList<>(list);
	}
}

@Override
public int getMinimumEnchantability(int enchantmentLevel) {
	return 1 + 10 * (enchantmentLevel - 1);
}

@Override
public int getMaximumEnchantability(int enchantmentLevel) {
	return super.getMinEnchantability(enchantmentLevel) + 50;
}

}

 

I'm also registring the enchant in the preInit method by simply calling the EnchantmentSmelting class

new EnchantmentSmelting();

 

As i said the performAction function is never called, so wich function i had to write to make this enchant actually work? :) Thanks in advance for your help :)

Don't blame me if i always ask for your help. I just want to learn to be better :)

Link to comment
Share on other sites

As i said i'm new to enchantments, so which will be a good approach to add one? :)

I've deleted that base class, so now i have only the enchantment class

package com.mineworld.enchantments;

import net.minecraft.enchantment.Enchantment;
import net.minecraft.enchantment.EnumEnchantmentType;
import net.minecraft.entity.Entity;
import net.minecraft.entity.EntityLivingBase;
import net.minecraft.init.Enchantments;
import net.minecraft.inventory.EntityEquipmentSlot;
import net.minecraft.item.ItemStack;

public class EnchantmentSmelting extends Enchantment {

public EnchantmentSmelting() {
	super(Rarity.UNCOMMON, EnumEnchantmentType.DIGGER,new EntityEquipmentSlot[]{EntityEquipmentSlot.MAINHAND, EntityEquipmentSlot.OFFHAND});
	this.setRegistryName("smelting");
}


@Override
public int getMaxLevel() {
	return 1;
}

@Override
public String getName() {
	return "enchantment.smelting.name";
}

@Override
public boolean canApplyAtEnchantingTable(ItemStack stack) {
	return true;
}

@Override
public boolean isAllowedOnBooks() {
	return true;
}

@Override
public boolean canApplyTogether(Enchantment ench) {
	return super.canApplyTogether(ench) && ench != Enchantments.SILK_TOUCH;
}

@Override
public int getMinEnchantability(int enchantmentLevel) {
	return 1 + 10 * (enchantmentLevel - 1);
}

@Override
public int getMaxEnchantability(int enchantmentLevel) {
	return super.getMinEnchantability(enchantmentLevel) + 50;
}

}

 

But looking at the function i can override in this class i don't find anything that could lead me to define what the performAction function do

  • Like 1

Don't blame me if i always ask for your help. I just want to learn to be better :)

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.



×
×
  • Create New...

Important Information

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