#I need help with this custom enchant im getting error

1 messages · Page 1 of 1 (latest)

tepid pollen
#
import { registerEnchant, slots } from "../../enchantment";

// Register the Rage enchantment
const rageEnchant = registerEnchant("rage", "Rage", 1, slots.chest);

mc.world.afterEvents.entityHitEntity.subscribe((event) => {
    try {
        const damagingEntity = event.damagingEntity;

        // Ensure the damaging entity is a player
        if (!damagingEntity || damagingEntity.typeId !== "minecraft:player") {
            return;
        }

        // Get the player's chestplate
        const equipment = damagingEntity.getComponent("minecraft:equippable");
        if (!equipment) {
            console.log("[Rage] Player does not have equippable components.");
            return;
        }

        const chestplate = equipment.getEquipment("chest");
        if (!chestplate) {
            console.log("[Rage] No chestplate equipped.");
            return;
        }

        // Check if the chestplate has the Rage enchantment
        const { valid } = rageEnchant.getItem(chestplate);
        if (!valid) {
            console.log("[Rage] No Rage enchantment on chestplate.");
            return;
        }

        // Get the player's health component
        const healthComponent = damagingEntity.getComponent("minecraft:health");
        if (!healthComponent) {
            console.log("[Rage] Player does not have a health component.");
            return;
        }

        const currentHealth = healthComponent.currentValue;
        const maxHealth = healthComponent.defaultValue;

        // Calculate health percentage
        const healthPercentage = (currentHealth / maxHealth) * 100;

        // Determine damage multiplier
        let damageMultiplier = 1;
        if (healthPercentage <= 50) {
            damageMultiplier = 1.5; // +50% at <= 50% health
        }
        if (healthPercentage <= 20) {
            damageMultiplier = 1.75; // +75% at <= 20% health
        }

        // Apply bonus damage
        if (event.hitEntity && typeof event.damage === "number") {
            const bonusDamage = event.damage * (damageMultiplier - 1);
            event.hitEntity.applyDamage(event.damage + bonusDamage, damagingEntity);
            console.log(
                `[Rage] Bonus damage: ${bonusDamage}, Total damage: ${
                    event.damage + bonusDamage
                }.`
            );
        }

        // Add visual feedback
        damagingEntity.dimension.spawnParticle("minecraft:angry_villager", {
            x: damagingEntity.location.x,
            y: damagingEntity.location.y + 1.5,
            z: damagingEntity.location.z,
        });

        damagingEntity.dimension.playSound("entity.blaze.shoot", {
            x: damagingEntity.location.x,
            y: damagingEntity.location.y,
            z: damagingEntity.location.z,
        });
    } catch (error) {
        console.error(`[Rage enchantment error]: ${error}`);
    }
});
#

thats the code

half path
#
import * as mc from "@minecraft/server";
import { registerEnchant, slots } from "../../enchantment";

// Register the Rage enchantment
const rageEnchant = registerEnchant("rage", "Rage", 1, slots.chest);

mc.world.afterEvents.entityHurt.subscribe((event) => {
    try {
        const damagingEntity = event.damageSource.damagingEntity;

        // Ensure the damaging entity is a player
        if (!damagingEntity || damagingEntity.typeId !== "minecraft:player") {
            return;
        }

        // Get the player's chestplate
        const equipment = damagingEntity.getComponent("minecraft:equippable");
        if (!equipment) {
            console.log("[Rage] Player does not have equippable components.");
            return;
        }

        const chestplate = equipment.getEquipment("Chest");
        if (!chestplate) {
            console.log("[Rage] No chestplate equipped.");
            return;
        }

        // Check if the chestplate has the Rage enchantment
        const { valid } = rageEnchant.getItem(chestplate);
        if (!valid) {
            console.log("[Rage] No Rage enchantment on chestplate.");
            return;
        }

        // Get the player's health component
        const healthComponent = damagingEntity.getComponent("minecraft:health");
        if (!healthComponent) {
            console.log("[Rage] Player does not have a health component.");
            return;
        }

        const currentHealth = healthComponent.currentValue;
        const maxHealth = healthComponent.defaultValue;

        // Calculate health percentage
        const healthPercentage = (currentHealth / maxHealth) * 100;

        // Determine damage multiplier
        let damageMultiplier = 1;
        if (healthPercentage <= 50) {
            damageMultiplier = 1.5; // +50% at <= 50% health
        }
        if (healthPercentage <= 20) {
            damageMultiplier = 1.75; // +75% at <= 20% health
        }

        // Apply bonus damage
        if (event.hurtEntity && typeof event.damage === "number") {
            const bonusDamage = event.damage * (damageMultiplier - 1);
            event.hurtEntity.applyDamage(event.damage + bonusDamage, damagingEntity);
            console.log(
                `[Rage] Bonus damage: ${bonusDamage}, Total damage: ${
                    event.damage + bonusDamage
                }.`
            );
        }

        // Add visual feedback
        damagingEntity.dimension.spawnParticle("minecraft:angry_villager", {
            x: damagingEntity.location.x,
            y: damagingEntity.location.y + 1.5,
            z: damagingEntity.location.z,
        });

        damagingEntity.dimension.playSound("entity.blaze.shoot", damagingEntity.location);
    } catch (error) {
        console.error(`[Rage enchantment error]: ${error}`);
    }
});
#
//Changes:
entityHurt.subscribe
event.damageSource.damagingEntity
equipment.getEquipment("Chest")
event.hurtEntity
rough matrix
#

Crazy how close ai is to getting to using the api properly

tepid pollen
#

Nah that’s insane

rough matrix
fallow dove
rough matrix
fallow dove
#

a little bit outdated events.beforeChat

rough matrix
fallow dove
#

it is becouse it learns what you give him

rough matrix
fallow dove
rough matrix
#

Maybe 😭

fallow dove
rough matrix
#

😭

#

Probably used the web search feature right?

fallow dove
#

yes

rough matrix
#

Ericdoa mentioned 💔

fallow dove
#

now stop that's getting off topic