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 19:02] 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 13: Line 13:
 <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.UNCOMMON, EnchantmentTarget.WEAPON, new EquipmentSlot[] {EquipmentSlot.MAINHAND});         super(Enchantment.Rarity.UNCOMMON, EnchantmentTarget.WEAPON, new EquipmentSlot[] {EquipmentSlot.MAINHAND});
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);
     }     }
 } }
tutorial/enchantments.1593802922.txt.gz · Last modified: 2020/07/03 19:02 by draylar