Damage and Status Effects
Damage events
// Block any single hit over 100 damage
mod.events().register(EntityEvents.ALLOW_DAMAGE, (entity, damage) -> damage.amount() < 100);
// Halve magic damage
mod.events().register(EntityEvents.MODIFY_DAMAGE, (entity, damage) ->
damage.type().equals(ResourceId.minecraft("magic")) ? damage.amount() / 2 : damage.amount());
mod.events().register(EntityEvents.DIED, (entity, cause) ->
LOGGER.info("{} died ({})", entity.name(), cause.type()));
| Event | Listener | Meaning |
|---|---|---|
ALLOW_DAMAGE | (LivingEntityView, DamageInfo) -> boolean | false cancels the damage |
MODIFY_DAMAGE | (LivingEntityView, DamageInfo) -> float | Changes the damage amount |
DIED | (LivingEntityView, DamageInfo) -> void | After death |
DamageInfo has type() (the damage type ID), amount(), and attacker(). All of them are called on the server thread.
- Modifying the amount: For damage that
ALLOW_DAMAGEdid not cancel, listeners change the amount in registration order. If it drops to 0 or below, the damage is cancelled. - The game's damage handling is called again with the changed amount, so armor and resistance effects apply to the changed amount.
Dealing damage
living.hurt(5); // generic damage; false if cancelled
living.hurt(ResourceId.of("my_mod", "zap"), 3); // a data-defined damage type
Define damage types in the vanilla format in a data pack at data/<mod>/damage_type/<name>.json.
Status effects
mod.content().effect("glowing_heart", new EffectDefinition(EffectCategory.BENEFICIAL, 0xFFD54F, 20,
(entity, amplifier) -> entity.setHealth(entity.health() + 1)));
living.addEffect(ResourceId.of("my_mod", "glowing_heart"), 100, 0); // ticks, amplifier
living.hasEffect(ResourceId.minecraft("glowing"));
EffectDefinition(category, color, tickInterval, behavior): the category, the color, how many ticks between actions, and the action (EffectBehavior.onTick)- The action runs on the server thread.
- The effect icon is
assets/<mod>/textures/mob_effect/<name>.png.