User Tools

Site Tools


tutorial:enchantments

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
tutorial:enchantments [2020/07/03 18:36] – more formatting fixes draylartutorial:enchantments [2023/01/04 13:52] (current) – [Registering Enchantment] In Sample Code: pass FROST variable to the 3rd param of Registry#register(). datsuns
Line 10: Line 10:
  
 ==== 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. In our enchantment class, we pass up ''VERY_RARE'' as the enchantment rarity, ''WEAPON'' as the enchantment target, and ''MAINHAND'' as the only valid tool type for our 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 ''UNCOMMON'' as the enchantment rarity, ''WEAPON'' as the enchantment target, and ''MAINHAND'' as the only valid tool type for our enchantment.
 <code java [enable_line_numbers="true"]> <code java [enable_line_numbers="true"]>
 public class FrostEnchantment extends Enchantment { public class FrostEnchantment extends Enchantment {
- 
     public FrostEnchantment() {     public FrostEnchantment() {
-        super(Enchantment.Rarity.VERY_RARE, EnchantmentTarget.WEAPON, new EquipmentSlot[] {EquipmentSlot.MAINHAND});+        super(Enchantment.Rarity.UNCOMMON, EnchantmentTarget.WEAPON, new EquipmentSlot[] {EquipmentSlot.MAINHAND});
     }     }
 } }
Line 22: Line 21:
 We will now override a few basic methods for basic functionality: We will now override a few basic methods for basic functionality:
  
-''getMinPower'' is related to the minimum level needed to see the enchant in a table, but it is not a 1:1 ratio. Most enchantments return something like ''10 * level'', with different scales depending on the max level and rarity of the enchantment. We will return 1 so it is always available.+''getMinPower'' is related to the minimum level needed to see the enchant in a table, but it is not a 1:1 ratio. Most enchantments return something like ''10 * level'', with different scales depending on the max level and rarity of the enchantment. We will return 1 so it is always available. Note that the max power of an enchantment is set to ''min(level) + 5'' by default, which means this enchantment will only appear at very low levels. You will have to tweak your enchantment properties on your own and look at similar enchantment values to find the sweet number spot.
 <code java [enable_line_numbers="false"]> <code java [enable_line_numbers="false"]>
 @Override @Override
Line 50: Line 49:
 </code> </code>
 If the entity we are hitting can have status effects (''LivingEntity''s can have status effects, but not ''Entity''), give it the slowness effect. The duration of the effect is 2 seconds per level, and the potency is equivalent to the level. If the entity we are hitting can have status effects (''LivingEntity''s can have status effects, but not ''Entity''), give it the slowness effect. The duration of the effect is 2 seconds per level, and the potency is equivalent to the level.
 +
 +The final enchantment file should look like this.
 +<code java [enable_line_numbers="true"]>
 +public class FrostEnchantment extends Enchantment {
 +    public FrostEnchantment() {
 +        super(Enchantment.Rarity.UNCOMMON, EnchantmentTarget.WEAPON, new EquipmentSlot[] {EquipmentSlot.MAINHAND});
 +    }
 +    
 +    @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, 20 * 2 * level, level - 1));
 +        }
 +
 +        super.onTargetDamaged(user, target, level);
 +    }
 +}
 +</code>
  
 ==== Registering Enchantment ==== ==== Registering Enchantment ====
Line 55: Line 81:
 <code java [enable_line_numbers="false"]> <code java [enable_line_numbers="false"]>
 public class EnchantingExample implements ModInitializer { public class EnchantingExample implements ModInitializer {
- +    public static Enchantment FROST = new FrostEnchantment();
-    private static Enchantment FROST = Registry.register( +
-            Registry.ENCHANTMENT, +
-            new Identifier("tutorial", "frost"), +
-            new FrostEnchantment(+
-    );+
  
     @Override     @Override
     public void onInitialize() {     public void onInitialize() {
 +        Registry.register(Registries.ENCHANTMENT, new Identifier("tutorial", "frost"), FROST);
     }     }
 } }
 </code> </code>
  
-This registers our enchantment under the namespace ''tutorial:frost''.+This registers our enchantment under the namespace ''tutorial:frost''. All non-treasure enchantments are available in an enchanting table, including the ones you register.
  
 ==== Adding Translations & Testing ==== ==== Adding Translations & Testing ====
tutorial/enchantments.1593801406.txt.gz · Last modified: 2020/07/03 18:36 by draylar