tutorial:enchantments
Differences
This shows you the differences between two versions of the page.
| Next revision | Previous revision | ||
| tutorial:enchantments [2019/07/02 20:08] – initial kermit draylar | tutorial:enchantments [2024/08/23 13:52] (current) – [Adding Translations] solidblock | ||
|---|---|---|---|
| Line 1: | Line 1: | ||
| - | ===== Adding Enchantments ===== | + | ====== Adding Enchantments ====== |
| + | |||
| + | ===== Adding enchantments since 1.21 ===== | ||
| + | Since 1.21, enchantments has been data-driven, | ||
| + | |||
| + | To use the enchantments in code, you should just store '' | ||
| + | <code java TutorialEnchantments> | ||
| + | public final class TutorialEnchantments { | ||
| + | public static final RegistryKey< | ||
| + | |||
| + | private static RegistryKey< | ||
| + | return RegistryKey.of(RegistryKeys.ENCHANTMENT, | ||
| + | } | ||
| + | |||
| + | public static void initialize() { | ||
| + | } | ||
| + | } | ||
| + | </ | ||
| + | |||
| + | Remember to refer to the '' | ||
| + | <code java ExampleMod> | ||
| + | public class ExampleMod implements ModInitializer { | ||
| + | // [...] | ||
| + | |||
| + | @Override | ||
| + | public void onInitialize() { | ||
| + | // [...] | ||
| + | TutorialEnchantments.initialize(); | ||
| + | } | ||
| + | } | ||
| + | </ | ||
| + | |||
| + | To add a translated name for your enchantment, | ||
| + | |||
| + | ===== Adding enchantments before 1.21 ===== | ||
| + | :!: This part of the tutorial only applies to older Minecraft versions. | ||
| To add enchantments to your mod, you'll need to: | To add enchantments to your mod, you'll need to: | ||
| Line 7: | Line 42: | ||
| * add translations for your enchantment ((When you register enchantments, | * add translations for your enchantment ((When you register enchantments, | ||
| - | Enchantments can either have custom functionality implemented separately (such as smelting ores mined) or can use already existing mechanics (such as '' | + | Enchantments can either have custom functionality implemented separately (such as smelting ores mined) or can use already existing mechanics (such as '' |
| ==== Creating Enchantment Class ==== | ==== Creating Enchantment Class ==== | ||
| - | We'll be creating an enchantment called //Wrath//, which adds an extra 1.5 points of damage, per level, to a melee weapon. The easiest way to do this is by creating a class that extends | + | Our new enchantment |
| <code java [enable_line_numbers=" | <code java [enable_line_numbers=" | ||
| - | public class WrathEnchantment | + | public class FrostEnchantment |
| - | { | + | public |
| - | public | + | super(Enchantment.Rarity.UNCOMMON, EnchantmentTarget.WEAPON, new EquipmentSlot[] {EquipmentSlot.MAINHAND}); |
| - | | + | |
| - | super(weight, typeIndex, slots) | + | |
| } | } | ||
| + | } | ||
| + | </ | ||
| - | | + | We will now override a few basic methods for basic functionality: |
| - | public | + | |
| - | | + | '' |
| - | // .... | + | <code java [enable_line_numbers=" |
| + | @Override | ||
| + | public | ||
| + | | ||
| + | } | ||
| + | </code> | ||
| + | |||
| + | '' | ||
| + | <code java [enable_line_numbers=" | ||
| + | @Override | ||
| + | public int getMaxLevel() { | ||
| + | return 3; | ||
| + | } | ||
| + | </ | ||
| + | |||
| + | Finally, we will implement our slowness effect in the '' | ||
| + | <code java [enable_line_numbers=" | ||
| + | @Override | ||
| + | public void onTargetDamaged(LivingEntity user, Entity target, int level) { | ||
| + | if(target instanceof LivingEntity livingEntity) { | ||
| + | livingEntity.addStatusEffect(new StatusEffectInstance(StatusEffects.SLOWNESS, | ||
| } | } | ||
| + | super.onTargetDamaged(user, | ||
| + | } | ||
| + | </ | ||
| + | If the entity we are hitting can have status effects ('' | ||
| + | |||
| + | The final enchantment file should look like this. | ||
| + | <code java [enable_line_numbers=" | ||
| + | public class FrostEnchantment extends Enchantment { | ||
| + | public FrostEnchantment() { | ||
| + | super(Enchantment.Rarity.UNCOMMON, | ||
| + | } | ||
| + | | ||
| @Override | @Override | ||
| - | public int getMinimumPower(int int_1) | + | public int getMinPower(int level) { |
| - | | + | |
| - | | + | |
| } | } | ||
| @Override | @Override | ||
| - | public int getMaximumLevel() | + | public int getMaxLevel() { |
| - | | + | |
| - | | + | |
| } | } | ||
| - | | + | public |
| - | | + | |
| - | | + | ((LivingEntity) target).addStatusEffect(new StatusEffectInstance(StatusEffects.SLOWNESS, 20 * 2 * level, level - 1)); |
| - | | + | } |
| + | |||
| + | super.onTargetDamaged(user, | ||
| } | } | ||
| } | } | ||
| </ | </ | ||
| - | '' | + | ==== Registering Enchantment ==== |
| + | Registering enchantments follows the same process | ||
| <code java [enable_line_numbers=" | <code java [enable_line_numbers=" | ||
| - | public | + | public |
| + | public static Enchantment FROST = new FrostEnchantment(); | ||
| + | |||
| + | @Override | ||
| + | public void onInitialize() { | ||
| + | Registry.register(Registries.ENCHANTMENT, | ||
| + | } | ||
| + | } | ||
| + | </ | ||
| + | > **Note:** In practice, if you have many enchantments, | ||
| + | |||
| + | This registers our enchantment under the namespace '' | ||
| + | |||
| + | ===== Adding Translations ===== | ||
| + | =) This part works both versions since 1.21 and before 1.21. | ||
| + | |||
| + | You'll need to add a translation to your enchantment as well. Head over to your [[tutorial: | ||
| + | |||
| + | <code javascript resources/ | ||
| { | { | ||
| - | | + | |
| } | } | ||
| </ | </ | ||
| + | |||
| + | If you go in-game, [[https:// | ||
| + | |||
| + | |||
| + | |||
| + | |||
tutorial/enchantments.1562098084.txt.gz · Last modified: 2019/07/02 20:08 by draylar