123123111-web321

Create a Fabric mod where the player takes damage equal to the damage they deal to any mob.

Logic & Requirements:

Listen for entity damage events (ServerLivingEntityEvents.AFTER_DAMAGE or equivalent damage handler).

Check if the attacker is a player (PlayerEntity).

When the player deals damage to any living entity (excluding other players), retrieve the exact amount of damage dealt.

Instantly apply damage of that exact same amount back to the attacking player using player.damage(...).

Prevent infinite loops by checking that self-inflicted player damage does not re-trigger the event.

Display a subtle particle or sound effect on the player when the recoil damage is taken.

Kodari AI
KodariOct 14 at 4:09 am 2
DamageReflect
A Fabric mod that implements a damage reflection mechanic. Listens to ServerLivingEntityEvents.AFTER_DAMAGE to detect when a player deals damage to any non-player living entity. Retrieves the exact damage amount dealt and applies identical recoil damage back to the attacking player via player.damage(). Implements loop prevention by checking that self-inflicted damage does not re-trigger the event handler. Plays a subtle sound effect and spawns particles on the player when recoil damage is applied, providing clear visual and audio feedback for the reflected damage mechanic.

I’ll look up the Fabric and vanilla signatures first, then write the files.

The Damage Recoil mod is written. Every hit a player lands on a mob also hurts the player by the same amount. Nothing needs crafting: the effect is always on, so there is nothing to obtain in survival.

  • Trigger: ServerLivingEntityEvents.AFTER_DAMAGE in DamageRecoil.java. The recoil uses damageTaken, the damage that actually reached the mob after armor, so it matches what the mob lost.

  • Attacker check: recoil only applies when source.getEntity() is a ServerPlayer. Players hit by other players are skipped, so PvP does not recoil.

  • Damage call: in 26.2 the method is hurtServer(ServerLevel, DamageSource, float), not damage(...). The recoil uses player.damageSources().generic(), a source with no attacker, so the recoil hit cannot trigger the handler again. A static guard also stops any re-entrant chain.

  • Feedback: a DAMAGE_INDICATOR particle burst at the player’s chest and a PLAYER_HURT sound.

  • Scope: the recoil also applies to projectiles the player fires, because Minecraft credits the shooter as the damage source. Damage that is fully absorbed reports zero damage and produces no recoil.

Files: settings.gradle, gradle.properties, build.gradle, src/main/resources/fabric.mod.json, src/main/java/com/example/recoil/RecoilMod.java, src/main/java/com/example/recoil/DamageRecoil.java.

To get the jar, click Compile in the top bar, then Compile in the window, then Download when it finishes.

Kodari can make mistakes. Review important code before using.
Files
src
build.gradle
gradle.properties
settings.gradle
Code
6 125
src/main/java/com/example/recoil/RecoilMod.java
package com.example.recoil;

import net.fabricmc.api.ModInitializer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class RecoilMod implements ModInitializer {
    public static final String MOD_ID = "recoil";
    public static final Logger LOGGER = LoggerFactory.getLogger(MOD_ID);

    @Override
    public void onInitialize() {
        DamageRecoil.register();
        LOGGER.info("Damage recoil loaded");
    }
}