tutorial:enchantments
Differences
This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
| tutorial:enchantments [2020/07/03 18:33] – formatting changes + mapping updates 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 10: | Line 45: | ||
| ==== Creating Enchantment Class ==== | ==== Creating Enchantment Class ==== | ||
| - | Our new enchantment is called //Frost// and slows mobs on hit. The slowness effect, durability, and potency will grow relative to the level of the enchantment. | + | Our new enchantment is called //Frost// and slows mobs on hit. The slowness effect, durability, and potency will grow relative to the level of the enchantment. In our enchantment class, we pass up '' |
| <code java [enable_line_numbers=" | <code java [enable_line_numbers=" | ||
| public class FrostEnchantment extends Enchantment { | public class FrostEnchantment extends Enchantment { | ||
| - | + | | |
| - | | + | super(Enchantment.Rarity.UNCOMMON, EnchantmentTarget.WEAPON, new EquipmentSlot[] {EquipmentSlot.MAINHAND}); |
| - | super(rarity, | + | |
| } | } | ||
| } | } | ||
| Line 22: | Line 56: | ||
| We will now override a few basic methods for basic functionality: | We will now override a few basic methods for basic functionality: | ||
| - | '' | + | '' |
| <code java [enable_line_numbers=" | <code java [enable_line_numbers=" | ||
| @Override | @Override | ||
| Line 42: | Line 76: | ||
| @Override | @Override | ||
| public void onTargetDamaged(LivingEntity user, Entity target, int level) { | public void onTargetDamaged(LivingEntity user, Entity target, int level) { | ||
| - | if(target instanceof LivingEntity) { | + | if(target instanceof LivingEntity |
| - | | + | |
| } | } | ||
| Line 50: | Line 84: | ||
| </ | </ | ||
| If the entity we are hitting can have status effects ('' | 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 | ||
| + | public int getMinPower(int level) { | ||
| + | return 1; | ||
| + | } | ||
| + | |||
| + | @Override | ||
| + | public int getMaxLevel() { | ||
| + | return 3; | ||
| + | } | ||
| + | |||
| + | public void onTargetDamaged(LivingEntity user, Entity target, int level) { | ||
| + | if(target instanceof LivingEntity) { | ||
| + | ((LivingEntity) target).addStatusEffect(new StatusEffectInstance(StatusEffects.SLOWNESS, | ||
| + | } | ||
| + | |||
| + | super.onTargetDamaged(user, | ||
| + | } | ||
| + | } | ||
| + | </ | ||
| ==== Registering Enchantment ==== | ==== Registering Enchantment ==== | ||
| Registering enchantments follows the same process as usual: | Registering enchantments follows the same process as usual: | ||
| <code java [enable_line_numbers=" | <code java [enable_line_numbers=" | ||
| - | private | + | public class ExampleMod implements ModInitializer { |
| + | public | ||
| - | @Override | + | |
| - | public void onInitialize() { | + | public void onInitialize() { |
| - | FROST = Registry.register( | + | Registry.register(Registries.ENCHANTMENT, |
| - | Registry.ENCHANTMENT, | + | |
| - | new Identifier(" | + | |
| - | new FrostEnchantment( | + | |
| - | Enchantment.Weight.VERY_RARE, | + | |
| - | EnchantmentTarget.WEAPON, | + | |
| - | new EquipmentSlot[] { | + | |
| - | EquipmentSlot.MAINHAND | + | |
| - | } | + | |
| - | ) | + | |
| - | | + | |
| } | } | ||
| </ | </ | ||
| + | > **Note:** In practice, if you have many enchantments, | ||
| + | |||
| + | This registers our enchantment under the namespace '' | ||
| - | This registers our enchantment under the namespace '' | + | ===== Adding Translations ===== |
| + | =) This part works both versions since 1.21 and before 1.21. | ||
| - | ==== Adding Translations & Testing ==== | ||
| You'll need to add a translation to your enchantment as well. Head over to your [[tutorial: | You'll need to add a translation to your enchantment as well. Head over to your [[tutorial: | ||
| - | <code json [enable_line_numbers=" | + | < |
| { | { | ||
| " | " | ||
tutorial/enchantments.1593801181.txt.gz · Last modified: 2020/07/03 18:33 by draylar