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 [2019/07/02 20:13] – finished class creation section 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.UNCOMMON, EnchantmentTarget.WEAPON, new EquipmentSlot[] {EquipmentSlot.MAINHAND});
-    +
-        super(weight, typeIndex, slots) +
-    } +
- +
-    @Override +
-    public float getAttackDamage(int level, EntityGroup group) +
-    { +
-        // ...+
-    } +
- +
-    @Override +
-    public int getMinimumPower(int int_1) +
-    { +
-        // .... +
-    } +
- +
-    @Override +
-    public int getMaximumLevel() +
-    { +
-        // .... +
-    } +
- +
-    @Override +
-    public boolean differs(Enchantment enchantment) +
-    { +
-        // ....+
     }     }
 } }
 </code> </code>
  
-''getAttackDamage'', as you would expect, is the additional damage provided by the enchantment. ''level'' is the level of the enchantment:+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 ratioMost 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
-public float getAttackDamage(int level, EntityGroup group) +public int getMinPower(int level) { 
-+    return 1;
-    return level * 1.5f;+
 } }
 </code> </code>
  
-''getMinimumPower'' is the minimum level required to get the enchant in an enchanting tableWe'll set it to 1, so you can get it at any level:+''getMaxLevel'' is the number of levels the enchantment has. Sharpness 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"]> <code java [enable_line_numbers="false"]>
 @Override @Override
-public int getMinimumPower(int int_1) +public int getMaxLevel() { 
-+    return 3;
-    return 1;;+
 } }
 </code> </code>
  
-''getMaximumLevel'' is the number of tiers the enchantment has. ((Enchantments with more than single tier will have roman numerals after the name to show the level. If the enchantment only has a single level, nothing is added.))+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"]> <code java [enable_line_numbers="false"]>
 @Override @Override
-public int getMaximumLevel() +public void onTargetDamaged(LivingEntity user, Entity target, int level{ 
-+    if(target instanceof LivingEntity) { 
-    return 5;+        ((LivingEntity) target).addStatusEffect(new StatusEffectInstance(StatusEffects.SLOWNESS, 20 * 2 * level, level - 1)); 
 +    
 + 
 +    super.onTargetDamaged(user, target, level);
 } }
 </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.
  
-Finally''differs'' is how you isolate enchantments and prevent them from being on the same toolAs an examplesharpness can't be used with smiteWe'll prevent our enchant from being combined with sharpness.+The final enchantment file should look like this. 
 +<code java [enable_line_numbers="true"]> 
 +public class FrostEnchantment extends Enchantment { 
 +    public FrostEnchantment() { 
 +        super(Enchantment.Rarity.UNCOMMONEnchantmentTarget.WEAPONnew 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 enchantments follows the same process as usual:
 <code java [enable_line_numbers="false"]> <code java [enable_line_numbers="false"]>
-@Override +public class EnchantingExample implements ModInitializer { 
-public boolean differs(Enchantment enchantment)+    public static Enchantment FROST = new FrostEnchantment(); 
 + 
 +    @Override 
 +    public void onInitialize() 
 +        Registry.register(Registries.ENCHANTMENT, new 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 super.differs(enchantment) && enchantment != Enchantments.SHARPNESS;+    "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.1562098398.txt.gz · Last modified: 2019/07/02 20:13 by draylar