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 [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:
-<code java [enable_line_numbers="false"]> +
-@Override +
-public float getAttackDamage(int level, EntityGroup group) +
-+
-    return level * 1.5f; +
-+
-</code>+
  
 ''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: ''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:
Line 59: Line 28:
 public int getMinimumPower(int int_1) public int getMinimumPower(int int_1)
 { {
-    return 1;;+    return 1;
 } }
 </code> </code>
Line 68: Line 37:
 public int getMaximumLevel() public int getMaximumLevel()
 { {
-    return 5;+    return 3;
 } }
 </code> </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.+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 boolean differs(Enchantment enchantment)+public void onTargetDamaged(LivingEntity user, Entity target, int level)
 { {
-    return super.differs(enchantment&& enchantment != Enchantments.SHARPNESS;+    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.
  
 ==== 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;+private static Enchantment FROST;
  
 @Override @Override
 public void onInitialize() public void onInitialize()
 { {
-    WRATH = Registry.register(+    FROST = Registry.register(
         Registry.ENCHANTMENT,         Registry.ENCHANTMENT,
- new Identifier("tutorial", "wrath"), + new Identifier("tutorial", "frost"), 
- new WrathEnchantment(+ new FrostEnchantment(
      Enchantment.Weight.VERY_RARE,      Enchantment.Weight.VERY_RARE,
-     0+     EnchantmentTarget.WEAPON
-     EquipmentSlot.MAINHAND+     new EquipmentSlot[] { 
 + EquipmentSlot.MAINHAND 
 +     }
  )  )
     );     );
Line 101: Line 78:
 </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'', sets it as a very rare enchantment, and only allows it on main hand tools.
  
 ==== Adding Translations & Testing ==== ==== Adding Translations & Testing ====
Line 108: Line 85:
 <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