[TUTORIAL] How to make Enchantments with Coding

Started by SparkleArts on

Topic category: User side tutorials

Joined Feb 2018
Points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • 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 SparkleArts on Sat, 10/01/2022 - 17:04
Joined Oct 2022
Points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • MCreator plugins:
  • Comments:
This was so helpful!! Thank…
Tue, 03/21/2023 - 21:29

This was so helpful!! Thank you so much dude. How do I do it for 1.19 though?

Joined Feb 2018
Points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • MCreator plugins:
  • Comments:
1.19 should be the same…
Fri, 03/24/2023 - 11:43

1.19 should be the same.

Also I need to mention that some informations are not right in the Tutorial and needs to be fixed.

Joined May 2023
Points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • MCreator plugins:
  • Comments:
Nice tutorialIm trying to…
Sun, 08/13/2023 - 15:18

Nice tutorial
Im trying to make my custom enchantment compatible with swords and axes but whevener i try to modify the code itself using the import itemstack and adding the line as you stated
I keep getting crash, minecraft version is 1.19.4 and mccreator is 2023.2
 

package net.mcreator.jbme.enchantment;


import net.minecraft.world.item.enchantment.EnchantmentCategory;
import net.minecraft.world.item.enchantment.Enchantment;
import net.minecraft.world.entity.EquipmentSlot;
import net.minecraft.world.item.ItemStack;

public class AshDestroyerEnchantment extends Enchantment {
public AshDestroyerEnchantment(EquipmentSlot... slots) {
super(Enchantment.Rarity.RARE, EnchantmentCategory.WEAPON, slots);
}

@Override
public int getMaxLevel() {
return 5;
}
@Override
public boolean canEnchant(ItemStack Axe) {
return Axe.getItem() instanceof AxeItem ? true : super.canEnchant(Axe);
}
}
Joined Feb 2018
Points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • MCreator plugins:
  • Comments:
Make sure to import all…
Mon, 08/14/2023 - 21:38

Make sure to import all Imports.

You can do that if you go to the Code and press at the Menu Button at the top where files etc. are located called "Code", then there is a "reformat Coe and Imports" Button. Press that.

 

The Tutorial is only for 1.18.2, but likely not working with 1.19.2 and 1.19.4. I can check how to add it properly and I will reply later.

Joined May 2023
Points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • MCreator plugins:
  • Comments:
I would really appreciate…
Mon, 08/14/2023 - 21:57

I would really appreciate it
thanks

Joined Feb 2018
Points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • MCreator plugins:
  • Comments:
Can you tell if you get the…
Tue, 08/15/2023 - 09:18

Can you tell if you get the Crash on Start Up or on applying the Enchantment?

Joined Feb 2018
Points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • MCreator plugins:
  • Comments:
Because I don't get a Crash…
Tue, 08/15/2023 - 09:21

Because I don't get a Crash or anything.

Joined Feb 2018
Points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • MCreator plugins:
  • Comments:
Can you send me the Log of…
Tue, 08/15/2023 - 09:22

Can you send me the Log of the Crash?

Joined Feb 2018
Points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • MCreator plugins:
  • Comments:
Wait I saw it now!You didn't…
Tue, 08/15/2023 - 09:33

Wait I saw it now!

You didn't import the AxeItem!

For Example All Items Are indicated in the Source Code as "ItemNameItem" so for example if you want to use Axe then you to replace ItemName with Axe so it is called "AxeItem".

Here is a Java Documentation to see all the Items.

IMPORTANT: If you on the Page go to the Section called "All known Subclasses" at the top of the page. There you see all the Items you can use with. But if you want to use the Vanilla Tools then here is the list:
AxeItem = Axe
HoeItem = Hoe
SwordItem = Sword
PickaxeItem = Pickaxe
ShovelItem = Shovel

Here is the List for Armor:
ArmorItem = Armor

It has only ArmorItem as Item you need to check seperately for which ArmorPart but you can just use your custom Enchantment Catgory for that.

Joined Sep 2022
Points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • MCreator plugins:
  • Comments:
How would I add item…
Thu, 08/15/2024 - 20:55

How would I add item subclasses for custom items?