User Tools

Site Tools


Sidebar

← Go back to the homepage

Fabric Tutorials

Setup

Basics

These pages are essential must-reads when modding with Fabric, and modding Minecraft in general, if you are new to modding, it is recommended you read the following.

Items

Blocks and Block Entities

Data Generation

World Generation

Commands

These pages will guide you through Mojang's Brigadier library which allows you to create commands with complex arguments and actions.

Events

These pages will guide you through using the many events included in Fabric API, and how to create your own events for you or other mods to use.

Entities

Fluids

Mixins & ASM

These pages will guide you through the usage of SpongePowered's Mixin library, which is a highly complex topic. We recommend you read these pages thoroughly.

Miscellaneous

Yarn

Contribute to Fabric

tutorial:status_effects

Adding Status Effects

To add status effects to your mod, you'll need to:

  • create a class that extends StatusEffect
  • add custom functionality
  • register your status effect
  • add translations and textures for your status effect

Creating Status Effect Class

We will add a new status effect that gives you EXP every tick.

public class ExpStatusEffect extends StatusEffect {
  public ExpStatusEffect() {
    super(
      StatusEffectCategory.BENEFICIAL, // whether beneficial or harmful for entities
      0x98D982); // color in RGB
  }
 
  // This method is called every tick to check whether it should apply the status effect or not
  @Override
  public boolean canApplyUpdateEffect(int duration, int amplifier) {
    // In our case, we just make it return true so that it applies the status effect every tick.
    return true;
  }
 
  // This method is called when it applies the status effect. We implement custom functionality here.
  @Override
  public void applyUpdateEffect(LivingEntity entity, int amplifier) {
    if (entity instanceof PlayerEntity) {
      ((PlayerEntity) entity).addExperience(1 << amplifier); // Higher amplifier gives you EXP faster
    }
  }
}

Registering Status Effect

This registers our status effect.

public class ExampleMod implements ModInitializer {
  public static final StatusEffect EXP = new ExpStatusEffect();
 
  @Override
  public void onInitialize() {
    Registry.register(Registry.STATUS_EFFECT, new Identifier("tutorial", "exp"), EXP);
  }
}

Adding Translations & Textures

You'll need to add a translation to your status effect. Head over to your mod lang file and add a new entry:

{
  "effect.tutorial.exp": "Experience"
}

You'll need to add a texture as well. The direct path is:

.../resources/assets/tutorial/textures/mob_effect/exp.png

Testing

You can run a command /effect give @p tutorial:exp in game to test your status effect.

tutorial/status_effects.txt · Last modified: 2022/02/17 08:59 by sattes