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:23] – finish up end of tut draylartutorial:enchantments [2020/07/03 18:36] – more formatting fixes 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 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 ''VERY_RARE'' 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 WrathEnchantment(Weight weight, int typeIndex, EquipmentSlot... slots) +
-    { +
-        super(weight, typeIndex, slots) +
-    }+
  
-    @Override +    public FrostEnchantment() { 
-    public float getAttackDamage(int level, EntityGroup group) +        super(Enchantment.Rarity.VERY_RARE, EnchantmentTarget.WEAPON, new EquipmentSlot[] {EquipmentSlot.MAINHAND});
-    +
-        // .... +
-    } +
- +
-    @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.
 <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))
-} +    }
-</code>+
  
-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. +    super.onTargetDamaged(user, target, level);
-<code java [enable_line_numbers="false"]> +
-@Override +
-public boolean differs(Enchantment enchantment) +
-+
-    return super.differs(enchantment&& enchantment != Enchantments.SHARPNESS;+
 } }
 </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.
  
 ==== Registering Enchantment ==== ==== Registering Enchantment ====
 Registering enchantments follows the same process as usual: Registering enchantments follows the same process as usual:
 <code java [enable_line_numbers="false"]> <code java [enable_line_numbers="false"]>
-private static Enchantment WRATH;+public class EnchantingExample implements ModInitializer {
  
-@Override +    private static Enchantment FROST = Registry.register( 
-public void onInitialize() +            Registry.ENCHANTMENT, 
-+            new Identifier("tutorial", "frost"), 
-    WRATH = Registry.register( +            new FrostEnchantment()
-        Registry.ENCHANTMENT, +
- new Identifier("tutorial", "wrath"), +
- new WrathEnchantment( +
-     Enchantment.Weight.VERY_RARE, +
-     0, +
-     EquipmentSlot.MAINHAND +
- )+
     );     );
 +
 +    @Override
 +    public void onInitialize() {
 +
 +    }
 } }
 </code> </code>
  
-This registers our enchantment under the namespace ''tutorial:wrath'', sets it as a very rare enchantment, and only allows it on main hand tools.+This registers our enchantment under the namespace ''tutorial:frost''.
  
 ==== Adding Translations & Testing ==== ==== Adding Translations & Testing ====
Line 108: Line 76:
 <code json [enable_line_numbers="false"]> <code json [enable_line_numbers="false"]>
 { {
-    "enchantment.tutorial.wrath": "Wrath"+    "enchantment.tutorial.frost": "Frost"
 } }
 </code> </code>
  
-If you go in-game, you should be able to enchant main hand weapons with your new enchant:+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:enchantment_wrath.png?400|}} 
  
tutorial/enchantments.txt · Last modified: 2023/01/04 13:52 by datsuns