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
Next revisionBoth sides next revision
tutorial:enchantments [2019/07/02 20:08] – initial kermit 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)
     }     }
 +}
 +</code>
  
-    @Override +You'll have to override a few basic methods for basic functionality:
-    public float getAttackDamage(int level, EntityGroup group) +
-    { +
-        // .... +
-    }+
  
-    @Override +''getMinimumPower'' is the minimum level required to get the enchant in an enchanting table. We'll set it to 1, so you can get it at any level: 
-    public int getMinimumPower(int int_1) +<code java [enable_line_numbers="false"]> 
-    +@Override 
-        // .... +public int getMinimumPower(int int_1) 
-    }+
 +    return 1; 
 +} 
 +</code>
  
-    @Override +''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.)) 
-    public int getMaximumLevel() +<code java [enable_line_numbers="false"]> 
-    { +@Override 
-        // .... +public int getMaximumLevel() 
-    }+
 +    return 3; 
 +} 
 +</code>
  
-    @Override +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. 
-    public boolean differs(Enchantment enchantment)+<code java [enable_line_numbers="false"]> 
 +@Override 
 +public void onTargetDamaged(LivingEntity user, Entity target, int level) 
 +
 +    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.
  
-''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 level, EntityGroup group)+private static Enchantment FROST; 
 + 
 +@Override 
 +public void onInitialize()
 { {
-    return level * 1.5f;+    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