Jump to content
  • Home
  • Files
  • Docs
  • Merch
Topics
  • All Content

  • This Topic
  • This Forum

  • Advanced Search
  • Existing user? Sign In  

    Sign In



    • Not recommended on shared computers


    • Forgot your password?

  • Sign Up
  • All Activity
  • Home
  • Mod Developer Central
  • Modder Support
  • [1.9.4] Adding Enchantments
1.13 Update Notes for Mod Creators
Sign in to follow this  
Followers 1
JimiIT92

[1.9.4] Adding Enchantments

By JimiIT92, June 22, 2016 in Modder Support

  • Reply to this topic
  • Start new topic

Recommended Posts

JimiIT92    19

JimiIT92

JimiIT92    19

  • Dragon Slayer
  • JimiIT92
  • Members
  • 19
  • 723 posts
Posted June 22, 2016

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 :)

  • Quote

Share this post


Link to post
Share on other sites

diesieben07    6671

diesieben07

diesieben07    6671

  • Reality Controller
  • diesieben07
  • Forum Team
  • 6671
  • 45607 posts
Posted June 22, 2016

Ok, so.

To register an enchantment you make a new instance of it. Then you call

GameRegistry.register(<ench>, <id>)

, where

<id>

is a ResourceLocation like you would use for Blocks. This is how all new forge registry types work (blocks, items, potions, enchantments, biomes, etc.).

Do not use

Enchantment.REGISTRY

to register.

 

performAction

is defined in

MWEnchantment

. Nothing in Minecraft's code knows about it. You have to call this method, but personally I think it is a pretty bad approach to this.

  • Quote

Share this post


Link to post
Share on other sites

JimiIT92    19

JimiIT92

JimiIT92    19

  • Dragon Slayer
  • JimiIT92
  • Members
  • 19
  • 723 posts
Posted June 22, 2016

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

Share this post


Link to post
Share on other sites

diesieben07    6671

diesieben07

diesieben07    6671

  • Reality Controller
  • diesieben07
  • Forum Team
  • 6671
  • 45607 posts
Posted June 22, 2016

Well, you want your enchantment to do something, right?

At the point where this "something" needs to happen, you need to check if the enchantment is present and if so act accordingly.

  • Quote

Share this post


Link to post
Share on other sites

JimiIT92    19

JimiIT92

JimiIT92    19

  • Dragon Slayer
  • JimiIT92
  • Members
  • 19
  • 723 posts
Posted June 22, 2016

Mmm, so i could check the BlockBreakingEvent and there check if the tool i'm using is enchanted with this enchant right?

  • Quote

Share this post


Link to post
Share on other sites

diesieben07    6671

diesieben07

diesieben07    6671

  • Reality Controller
  • diesieben07
  • Forum Team
  • 6671
  • 45607 posts
Posted June 22, 2016

Yes.

  • Quote

Share this post


Link to post
Share on other sites

JimiIT92    19

JimiIT92

JimiIT92    19

  • Dragon Slayer
  • JimiIT92
  • Members
  • 19
  • 723 posts
Posted June 22, 2016

Oh, that was easier than i thought :D

  • Quote

Share this post


Link to post
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.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  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.

  • Insert image from URL
×
  • Desktop
  • Tablet
  • Phone
Sign in to follow this  
Followers 1
Go To Topic Listing



  • Recently Browsing

    No registered users viewing this page.

  • Posts

    • diesieben07
      [1.12.2] How do i make it so my sword renders in my mobs hand?.

      By diesieben07 · Posted 11 minutes ago

      Post updated code, your entire mob class.
    • J0WAY
      [1.12.2] How do i make it so my sword renders in my mobs hand?.

      By J0WAY · Posted 13 minutes ago

      Well to be honest at this point i really don't know what i'm doing and it's not working still when i call those methods so if you could show me an example of what to do that would be amazing, Also are mobs suppose to have inventories because if they are then my doesn't have one and i think the main problem in this is it can't find which hand to set the sword to so that's why it isn't spawning with the mob and it just keeps denying it, Again i don't know how to fix because like i said before i'm only good at blocks and items and i just got into making mobs like 3 days ago.
    • Sfgg
      NO SE ME ABRE

      By Sfgg · Posted 19 minutes ago

      No lo entiendo
    • saxon564
      [1.14.4] [UNSOLVED] Server Thread Freezes After Entity Explodes

      By saxon564 · Posted 28 minutes ago

      I feel like I've done that before with no positive results, but that may have been for a different issue, so I will give it a shot tonight and see if that works.
    • diesieben07
      [1-14-newer] how to make a button with a specified texture ?

      By diesieben07 · Posted 37 minutes ago

  • Topics

    • J0WAY
      20
      [1.12.2] How do i make it so my sword renders in my mobs hand?.

      By J0WAY
      Started Thursday at 09:10 PM

    • Sfgg
      2
      NO SE ME ABRE

      By Sfgg
      Started 2 hours ago

    • saxon564
      8
      [1.14.4] [UNSOLVED] Server Thread Freezes After Entity Explodes

      By saxon564
      Started Yesterday at 05:11 AM

    • matt1999rd
      10
      [1-14-newer] how to make a button with a specified texture ?

      By matt1999rd
      Started 4 hours ago

    • juls07
      1
      help please

      By juls07
      Started 1 hour ago

  • Who's Online (See full list)

    • diesieben07
    • DragonITA
    • AdhocWhistle193
    • DaemonUmbra
    • Vorquel
    • anothertime
    • zuckpad
    • TheKA3YC
    • juls07
    • Sfgg
  • All Activity
  • Home
  • Mod Developer Central
  • Modder Support
  • [1.9.4] Adding Enchantments
  • Theme
  • Contact Us
  • Discord

Copyright © 2019 ForgeDevelopment LLC · Ads by Curse Powered by Invision Community