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 [2020/05/10 11:37] – addPotionEffect -> addStatusEffect sirwindfieldtutorial:enchantments [2020/07/03 19:02] draylar
Line 10: Line 10:
  
 ==== Creating Enchantment Class ==== ==== Creating Enchantment Class ====
-We'll be creating an enchantment called //Frost//, which slows mobs. The slowness effect durability potency will grow relative to the level of the enchantment.+Our new enchantment is called //Frost// and slows mobs on hit. The slowness effectdurability, and potency will grow relative to the level of the enchantment. In 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 FrostEnchantment extends Enchantment  +public class FrostEnchantment extends Enchantment { 
-+ 
-    public FrostEnchantment(Weight weight, EnchantmentTarget target, EquipmentSlot[] slots) +    public FrostEnchantment() { 
-    +        super(Enchantment.Rarity.UNCOMMONEnchantmentTarget.WEAPONnew EquipmentSlot[] {EquipmentSlot.MAINHAND});
-        super(weighttargetslots)+
     }     }
 } }
 </code> </code>
  
-You'll have to override a few basic methods for basic functionality:+We will now override a few basic methods for basic functionality:
  
-''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:+''getMinPower'' is related to the minimum level needed to see the enchant in table, but it is not a 1:1 ratio. Most 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 int getMinimumPower(int int_1) +public int getMinPower(int level) {
-{+
     return 1;     return 1;
 } }
 </code> </code>
  
-''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.))+''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 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 getMaximumLevel() +public int getMaxLevel() {
-{+
     return 3;     return 3;
 } }
 </code> </code>
  
-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.+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 void onTargetDamaged(LivingEntity user, Entity target, int level) +public void onTargetDamaged(LivingEntity user, Entity target, int level) { 
-+    if(target instanceof LivingEntity) {
-    if(target instanceof LivingEntity) +
-    {+
         ((LivingEntity) target).addStatusEffect(new StatusEffectInstance(StatusEffects.SLOWNESS, 20 * 2 * level, level - 1));         ((LivingEntity) target).addStatusEffect(new StatusEffectInstance(StatusEffects.SLOWNESS, 20 * 2 * level, level - 1));
     }     }
Line 54: Line 49:
 } }
 </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.+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 FROST;+public class EnchantingExample implements ModInitializer {
  
-@Override +    private static Enchantment FROST = Registry.register( 
-public void onInitialize() +            Registry.ENCHANTMENT, 
-+            new Identifier("tutorial", "frost"), 
-    FROST = Registry.register( +            new FrostEnchantment()
-        Registry.ENCHANTMENT, +
- new Identifier("tutorial", "frost"), +
- new FrostEnchantment( +
-     Enchantment.Weight.VERY_RARE, +
-     EnchantmentTarget.WEAPON, +
-     new EquipmentSlot[] { +
- EquipmentSlot.MAINHAND +
-     } +
- )+
     );     );
 +
 +    @Override
 +    public void onInitialize() {
 +
 +    }
 } }
 </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.+This registers our enchantment under the namespace ''tutorial:frost''. All non-treasure enchantments are available in an enchanting tableincluding the ones you register.
  
 ==== Adding Translations & Testing ==== ==== Adding Translations & Testing ====
Line 90: Line 81:
  
 If you go in-game, [[https://i.imgur.com/31nFl2H.png|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/enchantments.txt · Last modified: 2023/01/04 13:52 by datsuns