User Tools

Site Tools


tutorial:enchantments

Differences

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

Link to this comparison view

Next revision
Previous revision
tutorial:enchantments [2019/07/02 20:08] – initial kermit 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 7: Line 7:
   * add translations for your enchantment ((When you register enchantments, books are automatically added to the game for each level. The translated name of the enchantment (''enchantment.modid.enchantname'') is what appears as the book name.))   * add translations for your enchantment ((When you register enchantments, books are automatically added to the game for each level. The translated name of the enchantment (''enchantment.modid.enchantname'') is what appears as the book name.))
  
-Enchantments can either have custom functionality implemented separately (such as smelting ores mined) or can use already existing mechanics (such as ''DamageEnchantment''), which are applied when appropriate.+Enchantments can either have custom functionality implemented separately (such as smelting ores mined) or can use already existing mechanics (such as ''DamageEnchantment''), which are applied when appropriate. The base ''Enchantment'' class also has several methods to create functionality, such as an "on enemy hit" method.
  
 ==== Creating Enchantment Class ==== ==== Creating Enchantment Class ====
-We'll be creating an enchantment called //Wrath//, which adds an extra 1.5 points of damageper level, to a melee weaponThe easiest way to do this is by creating a class that extends ''DamageEnchantment'':+Our new enchantment is called //Frost// and slows mobs on hitThe slowness effectdurabilityand potency will grow relative to the level of the enchantmentIn 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 WrathEnchantment extends DamageEnchantment  +public class FrostEnchantment extends Enchantment 
-+    public FrostEnchantment() { 
-    public WrathEnchantment(Weight weight, int typeIndex, EquipmentSlot... slots) +        super(Enchantment.Rarity.UNCOMMONEnchantmentTarget.WEAPONnew EquipmentSlot[] {EquipmentSlot.MAINHAND});
-    +
-        super(weighttypeIndexslots)+
     }     }
 +}
 +</code>
  
-    @Override +We will now override a few basic methods for basic functionality: 
-    public float getAttackDamage(int level, EntityGroup group+ 
-    { +''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"]> 
 +@Override 
 +public int getMinPower(int level) { 
 +    return 1; 
 +
 +</code> 
 + 
 +''getMaxLevel'' is the number of levels the enchantment hasSharpness has a max level of 5((Enchantments with more than a single level will have roman numerals after the name to show the levelIf the enchantment only has a single level, nothing is added.)) 
 +<code java [enable_line_numbers="false"]> 
 +@Override 
 +public int getMaxLevel() { 
 +    return 3; 
 +
 +</code> 
 + 
 +Finally, we will implement our slowness effect in the ''onTargetDamage'' method, which is called when you whack an enemy with a tool that has your enchantment. 
 +<code java [enable_line_numbers="false"]> 
 +@Override 
 +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>
 +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     @Override
-    public int getMinimumPower(int int_1) +    public int getMinPower(int level) { 
-    +        return 1;
-        // ....+
     }     }
  
     @Override     @Override
-    public int getMaximumLevel() +    public int getMaxLevel() { 
-    +        return 3;
-        // ....+
     }     }
  
-    @Override +    public void onTargetDamaged(LivingEntity user, Entity target, int level) { 
-    public boolean differs(Enchantment enchantment) +        if(target instanceof LivingEntity) { 
-    +            ((LivingEntity) target).addStatusEffect(new StatusEffectInstance(StatusEffects.SLOWNESS, 20 * 2 * level, level - 1)); 
-        // ....+        } 
 + 
 +        super.onTargetDamaged(user, target, level);
     }     }
 } }
 </code> </code>
  
-''getAttackDamage'', as you would expect, is the additional damage provided by the enchantment. ''level'' is the level of the enchantment:+==== Registering Enchantment ==== 
 +Registering enchantments follows the same process as usual:
 <code java [enable_line_numbers="false"]> <code java [enable_line_numbers="false"]>
-public float getAttackDamage(int levelEntityGroup group)+public class EnchantingExample implements ModInitializer { 
 +    public static Enchantment FROST = new FrostEnchantment(); 
 + 
 +    @Override 
 +    public void onInitialize() { 
 +        Registry.register(Registries.ENCHANTMENTnew Identifier("tutorial", "frost"), FROST); 
 +    } 
 +
 +</code> 
 + 
 +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 ==== 
 +You'll need to add a translation to your enchantment as well. Head over to your [[tutorial:lang|mod lang file]] and add a new entry: 
 + 
 +<code json [enable_line_numbers="false"]>
 { {
-    return level * 1.5f;+    "enchantment.tutorial.frost": "Frost"
 } }
 </code> </code>
 +
 +If you go in-game, [[https://i.imgur.com/31nFl2H.png|you should be able to enchant main hand weapons with your new enchant.]]
 +
 +
 +
 +
tutorial/enchantments.1562098084.txt.gz · Last modified: 2019/07/02 20:08 by draylar