How to make an item that gives a potion effect (Minecraft mod)
Effects are instances of a mob effect (MobEffectInstance; EffectInstance on 1.16.5, PotionEffect on 1.12.2) with a duration in ticks and an amplifier (0 is level I). entity.addEffect(instance) gives it (addPotionEffect on 1.12.2). This item gives one when used and is used up, except in creative.
Works on Minecraft 1.20.1 to 26.3 on NeoForge; Minecraft 1.16.5 to 26.3 on Fabric; Minecraft 1.16.5 to 1.20.1 on Forge; Minecraft 1.12.2 on Forge. Each example was compiled against its loader and version before it shipped in Linen.
- Make an item class that overrides use(…).
- On the server, give the player the effect with a duration and amplifier.
- Use up one item with stack.consume(1, player) (shrink(1) before 1.20.5).
- Register the item with your class.
What you can change
Section titled “What you can change”The example uses these values. Change them in the code.
| Setting | In the example | What it does |
|---|---|---|
| effect | MOVEMENT_SPEED |
The effect it gives. |
| seconds | 30 |
How long it lasts. |
| amplifier | 0 |
0 is level I, 1 is level II. |
For each loader and version
Section titled “For each loader and version”The example mod is examplemod in the package com.example.examplemod. Use your own mod id and package.
Fabric and NeoForge 1.20.1 to 1.20.4, Forge 1.17.1 to 1.20.1
Section titled “Fabric and NeoForge 1.20.1 to 1.20.4, Forge 1.17.1 to 1.20.1”Uses:
MobEffectInstanceMobEffectsLivingEntity.addEffectItemStack.shrink
src/main/java/com/example/examplemod/item/SwiftTonicItem.java
package com.example.examplemod.item;
import net.minecraft.world.InteractionHand;import net.minecraft.world.InteractionResultHolder;import net.minecraft.world.effect.MobEffectInstance;import net.minecraft.world.effect.MobEffects;import net.minecraft.world.entity.player.Player;import net.minecraft.world.item.Item;import net.minecraft.world.item.ItemStack;import net.minecraft.world.level.Level;
// Swift Tonic: using it gives MOVEMENT_SPEED for 30 seconds and uses it up (not in creative).public class SwiftTonicItem extends Item { public SwiftTonicItem(Item.Properties properties) { super(properties); }
@Override public InteractionResultHolder<ItemStack> use(Level level, Player player, InteractionHand hand) { ItemStack stack = player.getItemInHand(hand); if (!level.isClientSide()) { player.addEffect(new MobEffectInstance(MobEffects.MOVEMENT_SPEED, 30 * 20, 0)); if (!player.getAbilities().instabuild) { stack.shrink(1); } } return InteractionResultHolder.sidedSuccess(stack, level.isClientSide()); }}It’s registered like any other item, as SwiftTonicItem: see a new item.
Fabric and NeoForge 1.20.5 to 1.21.1
Section titled “Fabric and NeoForge 1.20.5 to 1.21.1”Uses:
MobEffectInstanceMobEffectsLivingEntity.addEffectItemStack.consume
src/main/java/com/example/examplemod/item/SwiftTonicItem.java
package com.example.examplemod.item;
import net.minecraft.world.InteractionHand;import net.minecraft.world.InteractionResultHolder;import net.minecraft.world.effect.MobEffectInstance;import net.minecraft.world.effect.MobEffects;import net.minecraft.world.entity.player.Player;import net.minecraft.world.item.Item;import net.minecraft.world.item.ItemStack;import net.minecraft.world.level.Level;
// Swift Tonic: using it gives MOVEMENT_SPEED for 30 seconds and uses it up (not in creative).public class SwiftTonicItem extends Item { public SwiftTonicItem(Item.Properties properties) { super(properties); }
@Override public InteractionResultHolder<ItemStack> use(Level level, Player player, InteractionHand hand) { ItemStack stack = player.getItemInHand(hand); if (!level.isClientSide()) { player.addEffect(new MobEffectInstance(MobEffects.MOVEMENT_SPEED, 30 * 20, 0)); stack.consume(1, player); } return InteractionResultHolder.sidedSuccess(stack, level.isClientSide()); }}It’s registered like any other item, as SwiftTonicItem: see a new item.
Fabric and NeoForge 1.21.2 to 1.21.4
Section titled “Fabric and NeoForge 1.21.2 to 1.21.4”Uses:
MobEffectInstanceMobEffectsLivingEntity.addEffectItemStack.consume
src/main/java/com/example/examplemod/item/SwiftTonicItem.java
package com.example.examplemod.item;
import net.minecraft.world.InteractionHand;import net.minecraft.world.InteractionResult;import net.minecraft.world.effect.MobEffectInstance;import net.minecraft.world.effect.MobEffects;import net.minecraft.world.entity.player.Player;import net.minecraft.world.item.Item;import net.minecraft.world.item.ItemStack;import net.minecraft.world.level.Level;
// Swift Tonic: using it gives MOVEMENT_SPEED for 30 seconds and uses it up (not in creative).public class SwiftTonicItem extends Item { public SwiftTonicItem(Item.Properties properties) { super(properties); }
@Override public InteractionResult use(Level level, Player player, InteractionHand hand) { ItemStack stack = player.getItemInHand(hand); if (!level.isClientSide()) { player.addEffect(new MobEffectInstance(MobEffects.MOVEMENT_SPEED, 30 * 20, 0)); stack.consume(1, player); } return InteractionResult.SUCCESS; }}It’s registered like any other item, as SwiftTonicItem: see a new item.
Fabric and NeoForge 1.21.5 to 26.3
Section titled “Fabric and NeoForge 1.21.5 to 26.3”Uses:
MobEffectInstanceMobEffectsLivingEntity.addEffectItemStack.consume
Since 1.21.5 several MobEffects constants have the in-game names (SPEED, HASTE, STRENGTH, JUMP_BOOST, RESISTANCE, SLOWNESS, NAUSEA); older names like MOVEMENT_SPEED no longer exist, so this version names the constant directly instead of the effect choice.
src/main/java/com/example/examplemod/item/SwiftTonicItem.java
package com.example.examplemod.item;
import net.minecraft.world.InteractionHand;import net.minecraft.world.InteractionResult;import net.minecraft.world.effect.MobEffectInstance;import net.minecraft.world.effect.MobEffects;import net.minecraft.world.entity.player.Player;import net.minecraft.world.item.Item;import net.minecraft.world.item.ItemStack;import net.minecraft.world.level.Level;
// Swift Tonic: using it gives Speed (swap in any MobEffects constant) for 30 seconds and uses it up (not in creative).public class SwiftTonicItem extends Item { public SwiftTonicItem(Item.Properties properties) { super(properties); }
@Override public InteractionResult use(Level level, Player player, InteractionHand hand) { ItemStack stack = player.getItemInHand(hand); if (!level.isClientSide()) { player.addEffect(new MobEffectInstance(MobEffects.SPEED, 30 * 20, 0)); stack.consume(1, player); } return InteractionResult.SUCCESS; }}It’s registered like any other item, as SwiftTonicItem: see a new item.
Forge 1.16.5
Section titled “Forge 1.16.5”Uses:
EffectInstanceEffectsLivingEntity.addEffectItemStack.shrink
src/main/java/com/example/examplemod/item/SwiftTonicItem.java
package com.example.examplemod.item;
import net.minecraft.util.Hand;import net.minecraft.util.ActionResult;import net.minecraft.potion.EffectInstance;import net.minecraft.potion.Effects;import net.minecraft.entity.player.PlayerEntity;import net.minecraft.item.Item;import net.minecraft.item.ItemStack;import net.minecraft.world.World;
// Swift Tonic: using it gives MOVEMENT_SPEED for 30 seconds and uses it up (not in creative).public class SwiftTonicItem extends Item { public SwiftTonicItem(Item.Properties properties) { super(properties); }
@Override public ActionResult<ItemStack> use(World world, PlayerEntity player, Hand hand) { ItemStack stack = player.getItemInHand(hand); if (!world.isClientSide()) { player.addEffect(new EffectInstance(Effects.MOVEMENT_SPEED, 30 * 20, 0)); if (!player.abilities.instabuild) { stack.shrink(1); } } return ActionResult.sidedSuccess(stack, world.isClientSide()); }}It’s registered like any other item, as SwiftTonicItem: see a new item.
Fabric 1.16.5
Section titled “Fabric 1.16.5”Uses:
MobEffectInstanceMobEffectsLivingEntity.addEffectItemStack.shrink
src/main/java/com/example/examplemod/item/SwiftTonicItem.java
package com.example.examplemod.item;
import net.minecraft.world.InteractionHand;import net.minecraft.world.InteractionResultHolder;import net.minecraft.world.effect.MobEffectInstance;import net.minecraft.world.effect.MobEffects;import net.minecraft.world.entity.player.Player;import net.minecraft.world.item.Item;import net.minecraft.world.item.ItemStack;import net.minecraft.world.level.Level;
// Swift Tonic: using it gives MOVEMENT_SPEED for 30 seconds and uses it up (not in creative).public class SwiftTonicItem extends Item { public SwiftTonicItem(Item.Properties properties) { super(properties); }
@Override public InteractionResultHolder<ItemStack> use(Level level, Player player, InteractionHand hand) { ItemStack stack = player.getItemInHand(hand); if (!level.isClientSide()) { player.addEffect(new MobEffectInstance(MobEffects.MOVEMENT_SPEED, 30 * 20, 0)); if (!player.abilities.instabuild) { stack.shrink(1); } } return InteractionResultHolder.sidedSuccess(stack, level.isClientSide()); }}It’s registered like any other item, as SwiftTonicItem: see a new item.
Fabric 1.17 to 1.20
Section titled “Fabric 1.17 to 1.20”Uses:
MobEffectInstanceMobEffectsLivingEntity.addEffectItemStack.shrink
src/main/java/com/example/examplemod/item/SwiftTonicItem.java
package com.example.examplemod.item;
import net.minecraft.world.InteractionHand;import net.minecraft.world.InteractionResultHolder;import net.minecraft.world.effect.MobEffectInstance;import net.minecraft.world.effect.MobEffects;import net.minecraft.world.entity.player.Player;import net.minecraft.world.item.Item;import net.minecraft.world.item.ItemStack;import net.minecraft.world.level.Level;
// Swift Tonic: using it gives MOVEMENT_SPEED for 30 seconds and uses it up (not in creative).public class SwiftTonicItem extends Item { public SwiftTonicItem(Item.Properties properties) { super(properties); }
@Override public InteractionResultHolder<ItemStack> use(Level level, Player player, InteractionHand hand) { ItemStack stack = player.getItemInHand(hand); if (!level.isClientSide()) { player.addEffect(new MobEffectInstance(MobEffects.MOVEMENT_SPEED, 30 * 20, 0)); if (!player.getAbilities().instabuild) { stack.shrink(1); } } return InteractionResultHolder.sidedSuccess(stack, level.isClientSide()); }}It’s registered like any other item, as SwiftTonicItem: see a new item.
Forge 1.12.2
Section titled “Forge 1.12.2”Uses:
PotionEffectMobEffectsEntityLivingBase.addEffectItemStack.shrink
In 1.12.2 the call is player.addPotionEffect(new PotionEffect(potion, ticks, amplifier)) and the item overrides onItemRightClick.
src/main/java/com/example/examplemod/item/SwiftTonicItem.java
package com.example.examplemod.item;
import net.minecraft.entity.player.EntityPlayer;import net.minecraft.init.MobEffects;import net.minecraft.item.Item;import net.minecraft.item.ItemStack;import net.minecraft.potion.Potion;import net.minecraft.potion.PotionEffect;import net.minecraft.util.ActionResult;import net.minecraft.util.EnumActionResult;import net.minecraft.util.EnumHand;import net.minecraft.world.World;
// Swift Tonic: using it gives MOVEMENT_SPEED for 30 seconds and uses it up (not in creative).public class SwiftTonicItem extends Item { @Override public ActionResult<ItemStack> onItemRightClick(World world, EntityPlayer player, EnumHand hand) { ItemStack stack = player.getHeldItem(hand); if (!world.isRemote) { player.addPotionEffect(new PotionEffect(effect("MOVEMENT_SPEED"), 30 * 20, 0)); if (!player.capabilities.isCreativeMode) { stack.shrink(1); } } return new ActionResult<>(EnumActionResult.SUCCESS, stack); }
// 1.12.2 names its effects differently from newer versions (MOVEMENT_SPEED is SPEED, DIG_SPEED is HASTE, DAMAGE_BOOST is STRENGTH, JUMP is JUMP_BOOST, DAMAGE_RESISTANCE is RESISTANCE). private static Potion effect(String name) { switch (name) { case "DIG_SPEED": return MobEffects.HASTE; case "DAMAGE_BOOST": return MobEffects.STRENGTH; case "JUMP": return MobEffects.JUMP_BOOST; case "REGENERATION": return MobEffects.REGENERATION; case "DAMAGE_RESISTANCE": return MobEffects.RESISTANCE; case "FIRE_RESISTANCE": return MobEffects.FIRE_RESISTANCE; case "WATER_BREATHING": return MobEffects.WATER_BREATHING; default: return MobEffects.SPEED; } }}It’s registered like any other item, as SwiftTonicItem: see a new item.
Sources
Section titled “Sources”- https://docs.fabricmc.net/1.21.1/develop/entities/effects
- https://docs.neoforged.net/docs/1.21.1/items/mobeffects
Related how-tos
Section titled “Related how-tos”- An item that does something when used
- A block that does something when clicked
- A mob with attributes and a spawn egg
- A sword that sets targets on fire
- An item that changes the block you click
In Linen
Section titled “In Linen”Linen’s How-tos library has this page too, with the example for your own mod’s loader and Minecraft version. Hover a name in your code and Linen points to the how-tos that explain it. Both are free. Download Linen.