[TUTORIAL] How to make Enchantments with Coding

Started by GamesofFreak on Sat, 10/01/2022 - 11:17

Topic category: User side tutorials

Last seen on 14:38, 3. Dec 2022
Joined Feb 2018
Points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • Tracker tickets:
  • MCreator plugins:
  • Comments:
[TUTORIAL] How to make Enchantments with Coding
Sat, 10/01/2022 - 17:04 (edited)

Hey guys, before I start the tutorial I want to say something:
I've been experimenting a bit with enchantments over the past few days and found some new methods to make much better enchantments (you'll also learn how to create your own enchantment categories.)

Here is the Sections you learn:

  1. Preparation
  2. Custom Enchantment Category
  3. Blacklisted Enchantments
  4. canEnchant Method
  5. Ranged Item Shooting Only Enchantment
  6. Pull or Repel Enchantments

Preparation

Before we start we need to do some preparations! 

This tutorial is based on 1.18, but it can work in 1.16.5 too! I also recommend you to check out MCP Reborn(Or other MC Mods Source Code), because if you have the Minecraft source code, you can look at certain codes yourself.

We need the More Item Triggers plugin from NerdyPuzzle! (Big thanks to the Creator!)

Custom Enchantment Category

We'll cover the enchantment categories first, as you can then create your own categories to enchant only specific items.

First you create your own Custom Element with a name of your choice.

Custom Enchantment Category Element

In this Tutorial my Custom Element is called AECategory.

After you created your Custom Element delete everything, but not this line:

package net.mcreator.advancedenchantments;

This line is needed to make it work. This is your Package Name.

Then we need to make an public class first. And make sure the Public Class Name is the same Name of the Custom Element.

public class YourCustomElementName {

}

After that we need to add the following  line for creating a Custom Enchantment Category:

public static final EnchantmentCategory YOURCATEGORYNAME = EnchantmentCategory.create("yourcategoryname", item -> item instanceof YourItem);

Example we want to make an Enchantment Category only for Hoes than we need to change the above like this:

public static final EnchantmentCategory HOE = EnchantmentCategory.create("hoe", item -> item instanceof HoeItem);

now the following the Code must be look like this:

public class AECategory {
	public static final EnchantmentCategory HOE = EnchantmentCategory.create("hoe", item -> item instanceof HoeItem);
}

Now you created yourself an Enchantment Category, but make sure to import the things:

import net.minecraft.world.item.enchantment.EnchantmentCategory;
import net.minecraft.world.item.Item;

And for each Item you want to add you need the specified import, for example if I want to use HoeItem, then we need to use this import:

import net.minecraft.world.item.HoeItem;

If you want to use multiple Items then you need a || to seperate like this example:

public static final EnchantmentCategory TOOLS = EnchantmentCategory.create("tools", item -> item instanceof AxeItem || item instanceof ShovelItem || item instanceof PickaxeItem);

To use your custom Categories then make an Enchantmetn like normal and lock the Element and this import:

import net.mcreator.advancedenchantments.AECategory;

net.mcreator.advancedenchantments change this to your Package Name and change the AECategory with the name of your Custom Element where your Categories are.

After that to replace the EnchantmentCategory you need to do it like this:

super(Enchantment.Rarity.COMMON, AECategory.HOE, slots);

Change AECategory.HOE with your own Category Name like AECategory is the Custom Element Name and .HOE is the Item Category like before.

Blacklisted Enchantments

To make a Blacklist make your Enchantment and add the Enchantments to "Can be combined with" you want to Blacklist directly.

After that we need to lock the Enchantment Element. Inside the Code you should be see this section:

@Override
protected boolean checkCompatibility(Enchantment ench) {
    return ench == Enchantments.THORNS;
}

Than turn:
 

return ench == Enchantments.THORNS;

to this:

if (ench == Enchantments.THORNS)
    return false;
return true;

And this looks if you add more Enchantments do it:

if (ench == AdvancedEnchantmentsModEnchantments.BONE_CRUSHER.get())
	return false;
if (ench == Enchantments.BANE_OF_ARTHROPODS)
	return false;
if (ench == Enchantments.SHARPNESS)
	return false;
if (ench == Enchantments.SMITE)
	return false;
return true;

If you want to add Custom Enchantments that you created before then make it like the first line above.

canEnchant Method

The canEnchant Method it great if you want to create example an Enchantment only for the Sword and the Axe. First of all create your Enchantment Element and lock it again.

Then add this line to the Enchantment Element:

@Override
public boolean canEnchant(ItemStack Axe) {
    return Axe.getItem() instanceof AxeItem ? true : super.canEnchant(Axe);
}

You need the ItemStack Import. and this line "(ItemStack Axe)" you can change the Name Axe to anything you want, change all Names with Axe with the ItemStack Name except for AxeItem parameter(this is your Item you want to add.)

And you can only add one Item with this Method.

Ranged Item Shooting Only Enchantment

To make those Enchantments you need to make a Global Trigger "Entity attacked", look at the Image:

Ranged Item Shooting only Enchantment

The Picture shows an Strike Enchantment that checks for the Ranged Item like Bow, Crossbow and Trident and Strike at the Position of Immediate Source Entity(Projectile) if you hit the Target Entity. I also added an Cooldown and a Chance to the Enchantment. And the Extra Tooltype Block is from the Plugin Extra Item Trigger before the Tutorial begins.

Pull or Repel Enchantments

First to make an Repel Enchantment we need to subtract each of the XYZ Position of Event/Target Entity(The Entity you want to repel) with the XYZ Position of Source Entity(From the Entity you want to repel with):

Repel Entity

If you want to make a Pull enchantment then you need the switch it.

If you don't understand something, just ask, I'm happy to help.

Edited by GamesofFreak on Sat, 10/01/2022 - 17:04