#Help

1 messages · Page 1 of 1 (latest)

runic latch
#

I want the entity that causes damage to receive the status effect, not the entity that is hit. After replacing  hitEntity  with  damagingEntity , it doesn't work. What should I do?

    
    if (Math.random() < 0.3) {
        data.hitEntity.runCommandAsync("execute as @s at @s run summon minecraft:lightning_bolt ~ ~ ~");
    }
    if (Math.random() < 0.25) {
        data.damagingEntity.addEffect('resistance',20, 4);
    }
        data.hitEntity.applyKnockback(data.damagingEntity.getViewDirection().x, data.damagingEntity.getViewDirection().z, 0.3, 0.3,)
        data.damagingEntity.addEffect('regeneration',20, 3)
        }```
nimble cedar
# runic latch I want the entity that causes damage to receive the status effect, not the entit...

import { world } from "@minecraft/server";

world.afterEvents.entityHitEntity.subscribe(({ damagingEntity: attacker, hitEntity: victim }) => {
   const items = attacker.getComponent("equippable")?.getEquipment("Mainhand");
    if (attacker.typeId === 'minecraft:player' && items?.typeId === 'jd:knight_sword') {
        applyEffectsWithChance(attacker, victim);
    victim.applyKnockback(attacker.getViewDirection().x, attacker.getViewDirection().z, 0.3, 0.3,)
    }
});

function applyEffectsWithChance(killer, victim) {
    const effects = [
        { command: "effect @s resistance 1 4", target: killer, chance: 25 },
        { command: "summon lightning_bolt ~ ~ ~", target: victim, chance: 30 },
        { command: "effect @s regeneration 1 3", target: killer, chance: 100 }
    ];

    effects.forEach(effect => {
        const randomChance = Math.random() * 100;
        if (randomChance <= effect.chance) {
            effect.target.runCommandAsync(effect.command);
        };
    });
};