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

const RIDE_CONFIGS = [
    {
        playerTag: "scream_rag_bag",
        mobType: "mebesm:rag_bag",
        mobTag: "is_screaming_rag_bag"
    },
    {
        playerTag: "scream_gutter_plush",
        mobType: "mebesm:gutter_plush",
        mobTag: "is_screaming_gutter_plush"
    },
    {
        playerTag: "scream_lily_lovebraids",
        mobType: "mebesm:lily_lovebraids",
        mobTag: "is_screaming_lily_lovebraids"
    },
    {
        playerTag: "scream_bunzo_bunnyon",
        mobType: "mebesm:bunzo_bunny",
        mobTag: "is_screaming_bunzo_bunnyon"
    },
    {
        playerTag: "scream_harley_soyer",
        mobType: "mebesm:harley_soyer",
        mobTag: "is_screaming_harley_soyer"
    },
    {
        playerTag: "scream_nightmare_critters",
        mobType: "mebesm:nightmare_critters",
        mobTag: "is_screaming_nightmare_critters"
    },
    {
        playerTag: "scream_smiling_critters",
        mobType: "mebesm:smiling_critters",
        mobTag: "is_screaming_smiling_critters"
    },
    {
        playerTag: "scream_monster_doey",
        mobType: "mebesm:monster_doey",
        mobTag: "is_screaming_monster_doey"
    },
    {
        playerTag: "scream_pj_pug_a_pillar",
        mobType: "mebesm:pj_pug_a_pillar",
        mobTag: "is_screaming_pj_pug_a_pillar"
    },
    {
        playerTag: "scream_bot_harley_soyer1",
        mobType: "mebesm:bot_harley_soyer1",
        mobTag: "is_screaming_bot_harley_soyer1"
    },
    {
        playerTag: "scream_prototype",
        mobType: "mebesm:prototype",
        mobTag: "is_screaming_prototype"
    },
    {
        playerTag: "scream_chum_chomlkins",
        mobType: "mebesm:chum_chomlkins",
        mobTag: "is_screaming_chum_chomlkins"
    },
    {
        playerTag: "scream_boxy_boo",
        mobType: "mebesm:boxy_boo",
        mobTag: "is_screaming_boxy_boo"
    },
    {
        playerTag: "scream_catnap",
        mobType: "mebesm:catnap",
        mobTag: "is_screaming_catnap"
    },
    {
        playerTag: "scream_mommy_long_legs",
        mobType: "mebesm:mommy_long_legs",
        mobTag: "is_screaming_mommy_long_legs"
    },
    {
        playerTag: "scream_catnap_hallucination",
        mobType: "mebesm:catnap_hallucination",
        mobTag: "is_screaming_catnap_hallucination"
    },
    {
        playerTag: "scream_mommy_long_legs_angry",
        mobType: "mebesm:mommy_long_legs_angry",
        mobTag: "is_screaming_mommy_long_legs_angry"
    },
    {
        playerTag: "scream_yarnabyfire",
        mobType: "mebesm:yarnabyfire",
        mobTag: "is_screaming_yarnabyfire"
    },
    {
        playerTag: "scream_yarnaby",
        mobType: "mebesm:yarnaby",
        mobTag: "is_screaming_yarnaby"
    },
    {
        playerTag: "scream_yarnabylisi",
        mobType: "mebesm:yarnabylisi",
        mobTag: "is_screaming_yarnabylisi"
    },
    {
        playerTag: "scream_dogday",
        mobType: "mebesm:dogday",
        mobTag: "is_screaming_dogday"
    },
    {
        playerTag: "scream_doey",
        mobType: "mebesm:doey",
        mobTag: "is_screaming_doey"
    },
    {
        playerTag: "scream_pianosaurus",
        mobType: "mebesm:pianosaurus",
        mobTag: "is_screaming_pianosaurus"
    },
    {
        playerTag: "scream_kissy_missy_vent",
        mobType: "mebesm:kissy_missy_vent",
        mobTag: "is_screaming_kissy_missy_vent"
    },
    {
        playerTag: "scream_kissy_missy_wounded",
        mobType: "mebesm:kissy_missy_wounded",
        mobTag: "is_screaming_kissy_missy_wounded"
    },
    {
        playerTag: "scream_kissy_missy_poppy",
        mobType: "mebesm:kissy_missy_poppy",
        mobTag: "is_screaming_kissy_missy_poppy"
    },
    {
        playerTag: "scream_huggy_wuggy_wounded",
        mobType: "mebesm:huggy_wuggy_wounded",
        mobTag: "is_screaming_huggy_wuggy_wounded"
    },
    {
        playerTag: "scream_huggy_wuggy_vent",
        mobType: "mebesm:huggy_wuggy_vent",
        mobTag: "is_screaming_huggy_wuggy_vent"
    },
    {
        playerTag: "scream_kissy_missy",
        mobType: "mebesm:kissy_missy",
        mobTag: "is_screaming_kissy_missy"
    },
    {
        playerTag: "scream_huggy_wuggy",
        mobType: "mebesm:huggy_wuggy",
        mobTag: "is_screaming_huggy_wuggy"
    },
    {
        playerTag: "scream_miss_delight",
        mobType: "mebesm:miss_delight",
        mobTag: "is_screaming_miss_delight"
    },
    {
        playerTag: "scream_nightmare_huggy",
        mobType: "mebesm:nightmare_huggy",
        mobTag: "is_screaming_nightmare_huggy"
    }

];

const MOB_TYPES = new Set(RIDE_CONFIGS.map(c => c.mobType));

world.afterEvents.entityHurt.subscribe((event) => {
    const victim = event.hurtEntity;
    const attacker = event.damageSource?.damagingEntity;

    if (
        victim?.typeId === "minecraft:player" &&
        victim.isValid() &&
        victim.getComponent("minecraft:health")?.currentValue > 0 &&
        attacker &&
        MOB_TYPES.has(attacker.typeId)
    ) {
        attacker.runCommandAsync("event entity @s mebesm:start_scream").catch(() => { });

        attacker.addTag("screaming");
        system.runTimeout(() => {
            try { attacker.removeTag("screaming"); } catch { }
        }, 1);
    }
});

system.runInterval(() => {
    for (const player of world.getAllPlayers()) {
        for (const config of RIDE_CONFIGS) {
            if (player.hasTag(config.playerTag)) {
                player.runCommandAsync(
                    `ride @s[m=!creative] start_riding @e[type=${config.mobType}, tag=${config.mobTag}, c=1, r=15] teleport_rider`
                ).catch(() => { });
            }
        }
    }
}, 1);