Skip to content

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.

  1. Make an item class that overrides use(…).
  2. On the server, give the player the effect with a duration and amplifier.
  3. Use up one item with stack.consume(1, player) (shrink(1) before 1.20.5).
  4. Register the item with your class.

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.

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:

  • MobEffectInstance
  • MobEffects
  • LivingEntity.addEffect
  • ItemStack.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.

Uses:

  • MobEffectInstance
  • MobEffects
  • LivingEntity.addEffect
  • ItemStack.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.

Uses:

  • MobEffectInstance
  • MobEffects
  • LivingEntity.addEffect
  • ItemStack.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.

Uses:

  • MobEffectInstance
  • MobEffects
  • LivingEntity.addEffect
  • ItemStack.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.

Uses:

  • EffectInstance
  • Effects
  • LivingEntity.addEffect
  • ItemStack.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.

Uses:

  • MobEffectInstance
  • MobEffects
  • LivingEntity.addEffect
  • ItemStack.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.

Uses:

  • MobEffectInstance
  • MobEffects
  • LivingEntity.addEffect
  • ItemStack.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.

Uses:

  • PotionEffect
  • MobEffects
  • EntityLivingBase.addEffect
  • ItemStack.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.

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.