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
Next revisionBoth sides next revision
tutorial:enchantments [2019/07/02 20:13] – finished class creation section draylartutorial:enchantments [2019/07/02 22:39] draylar
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 damage, per level, to a melee weaponThe easiest way to do this is by creating a class that extends ''DamageEnchantment'':+We'll be creating an enchantment called //Frost//, which slows mobsThe slowness effect durability & potency will grow relative to the level of the enchantment.
 <code java [enable_line_numbers="true"]> <code java [enable_line_numbers="true"]>
-public class WrathEnchantment extends DamageEnchantment +public class FrostEnchantment extends Enchantment 
 { {
-    public WrathEnchantment(Weight weight, int typeIndex, EquipmentSlot... slots)+    public WrathEnchantment(Weight weight, EnchantmentTarget target, EquipmentSlot[] slots)
     {     {
-        super(weight, typeIndex, slots) +        super(weight, target, 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:+You'll have to override a few basic methods for basic functionality: 
 + 
 +''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:
 <code java [enable_line_numbers="false"]> <code java [enable_line_numbers="false"]>
 @Override @Override
-public float getAttackDamage(int level, EntityGroup group)+public int getMinimumPower(int int_1)
 { {
-    return level * 1.5f;+    return 1;
 } }
 </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:+''getMaximumLevel'' is the number of tiers the enchantment has((Enchantments with more than a single tier will have roman numerals after the name to show the level. If 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 getMaximumLevel()
 { {
-    return 1;;+    return 3;
 } }
 </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'll 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)
 { {
-    return 5;+    if(target instanceof LivingEntity) 
 +    { 
 +        ((LivingEntity) target).addPotionEffect(new StatusEffectInstance(StatusEffects.SLOWNESS, 20 * 2 * level, level - 1)); 
 +    } 
 + 
 +    super.onTargetDamaged(user, target, level);
 } }
 </code> </code>
 +Pretty simple logic: if the entity we're hitting can have status effects, give it slowness. The time 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 tool. As an example, sharpness can't be used with smite. We'll prevent our enchant from being combined with sharpness.+==== Registering Enchantment ==== 
 +Registering enchantments follows the same process as usual:
 <code java [enable_line_numbers="false"]> <code java [enable_line_numbers="false"]>
 +private static Enchantment FROST;
 +
 @Override @Override
-public boolean differs(Enchantment enchantment)+public void onInitialize()
 { {
-    return super.differs(enchantment&& enchantment != Enchantments.SHARPNESS;+    FROST = Registry.register( 
 +        Registry.ENCHANTMENT, 
 + new Identifier("tutorial", "frost")
 + new FrostEnchantment( 
 +     Enchantment.Weight.VERY_RARE, 
 +     EnchantmentTarget.WEAPON, 
 +     new EquipmentSlot[] { 
 + EquipmentSlot.MAINHAND 
 +     } 
 +
 +    );
 } }
 </code> </code>
 +
 +This registers our enchantment under the namespace ''tutorial:frost'', sets it as a very rare enchantment, and only allows it on main hand tools.
 +
 +==== 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"]>
 +{
 +    "enchantment.tutorial.frost": "Frost"
 +}
 +</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.txt · Last modified: 2023/01/04 13:52 by datsuns