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

world.afterEvents.entityHitEntity.subscribe((event) => {
    const attacker = event.damagingEntity;
    const target = event.hitEntity;

    if (attacker.typeId === "dragons:nether_dragon") {
        target.setOnFire(10, true)
    }

});




//lethal AOE
world.afterEvents.entityHitEntity.subscribe((event) => {
    const hurtEntity = event.hitEntity;
    const damageSource = event.damagingEntity;
    if (damageSource.typeId === "dragons:lethal_dragon") {
        const nearbyMobs = hurtEntity.dimension.getEntities({
            location: hurtEntity.location,
            maxDistance: 3,
            excludeFamilies: ['monster', 'ignore']
        });
        for (const mob of nearbyMobs) {
            const xDif = mob.location.x - damageSource.location.x;
            const zDif = mob.location.z - damageSource.location.z;
            if (mob !== hurtEntity) {
                mob.applyDamage(10, {
                    cause: EntityDamageCause.entityAttack,
                    damagingEntity: damageSource
                });
            }
            if (mob.typeId === 'minecraft:player') {
                mob.runCommandAsync('camerashake add @s 0.6 1 positional');
            }
            mob.applyKnockback(xDif, zDif, 4, 0.4);
        }
    }
});

//mining fatgiue
system.afterEvents.scriptEventReceive.subscribe((event) => {
    const id = event.id;
    const entity = event.sourceEntity;
    const message = event.message;
    if (id === 'dragons:mining_fatigue_delayed') {
        const value = parseFloat(message);
        if (!value) {
            console.warn('No value specified');
            return;
        }
        system.runTimeout(() => {
            entity.runCommand(`effect @e[r=64,family=!lethal] mining_fatigue ${value} 1 true`)
        }, 40)
    }
});

//stun shock
system.afterEvents.scriptEventReceive.subscribe((event) => {
    const id = event.id;
    const entity = event.sourceEntity;
    const message = event.message;
    if (id === 'dragons:stun') {
        const value = parseFloat(message);
        if (!value) {
            console.warn('No value specified');
            return;
        }

        entity.applyDamage(5, { cause: EntityDamageCause.lightning })

        if (entity.typeId !== "minecraft:player") {
            entity.addEffect("slowness", value * 20, { amplifier: 10, showParticles: false })
            return;
        }

        if (world.scoreboard.getObjective("lethalStun").getScore(entity) == undefined) {
            world.scoreboard.getObjective("lethalStun").addScore(entity, 0)

        }
        if (world.scoreboard.getObjective("lethalStun").getScore(entity) > 1) return;

        world.scoreboard.getObjective("lethalStun").addScore(entity, Math.ceil(value * 20))
        if (entity.typeId !== "minecraft:player") entity.addEffect("slowness", value * 20, { amplifier: 10, showParticles: false })


    }
    if (id === 'dragons:stun_add') {
        const value = parseFloat(message);
        if (!value) {
            console.warn('No value specified');
            return;
        }
        entity.applyDamage(5, { cause: EntityDamageCause.lightning })

        if (entity.typeId !== "minecraft:player") {
            entity.addEffect("slowness", value * 20, { amplifier: 10, showParticles: false })
            return;
        }

        if (world.scoreboard.getObjective("lethalStun").getScore(entity) == undefined) {
            world.scoreboard.getObjective("lethalStun").addScore(entity, 0)

        }
        world.scoreboard.getObjective("lethalStun").addScore(entity, Math.ceil(value * 20))

    }
});

system.runInterval(() => {
    for (const player of world.getPlayers({
        scoreOptions: [{
            objective: "lethalStun",
            minScore: 1
        }]
    })) {
        var lethalStun = world.scoreboard.getObjective("lethalStun")
        if (!lethalStun) {
            world.scoreboard.addObjective("lethalStun");
            lethalStun = world.scoreboard.getObjective("lethalStun")
        }
        let stunT = lethalStun.getScore(player);
        if (stunT == 1) {
            player.inputPermissions.setPermissionCategory(1, true)
            player.inputPermissions.setPermissionCategory(2, true)
        } else {
            player.inputPermissions.setPermissionCategory(1, false)
            player.inputPermissions.setPermissionCategory(2, false)
        }
        player.dimension.spawnParticle("dragons:electric_aura", player.location)
        player.addEffect("weakness", 5, { amplifier: 2, showParticles: false })

        if (stunT % 10 == 0) {
            player.runCommandAsync('camerashake add @s 0.6 0.3 positional');
            player.playAnimation("animation.player.stun")
        }
        lethalStun.addScore(player, -1)
    }
});










//not an attack but i rlly cba to make a new file so deal with it
world.afterEvents.dataDrivenEntityTrigger.subscribe((event) => {
    const entity = event.entity;
    const id = event.eventId;

    if (id === "on_tame") {
        if (entity.typeId === "dragons:void_dragon" && entity.nameTag === "") {
            entity.nameTag = "§v§o§i§d§r";
            return;
        } else if (entity.typeId === "dragons:forest_dragon" && entity.nameTag === "") {
            entity.nameTag = "§f§o§r§e§r";
            return;
        } else if (entity.typeId === "dragons:fire_dragon" && entity.nameTag === "") {
            entity.nameTag = "§f§i§r§e§r";
            return;
        } else if (entity.typeId === "dragons:tide_dragon" && entity.nameTag === "") {
            entity.nameTag = "§t§i§d§e§r";
            return;
        } else if (entity.typeId === "dragons:snow_dragon" && entity.nameTag === "") {
            entity.nameTag = "§s§n§o§w§r";
            return;
        } else if (entity.typeId === "dragons:sky_dragon" && entity.nameTag === "") {
            entity.nameTag = "§s§k§y§y§r";
            return;
        } else if (entity.typeId === "dragons:echo_dragon" && entity.nameTag === "") {
            entity.nameTag = "§e§c§h§o§r";
            return;
        } else if (entity.typeId === "dragons:mycelium_dragon" && entity.nameTag === "") {
            entity.nameTag = "§m§y§c§e§r";
            return;
        } else if (entity.typeId === "dragons:nether_dragon" && entity.nameTag === "") {
            entity.nameTag = "§n§e§t§h§r";
            return;
        } else if (entity.typeId === "dragons:ghost_dragon" && entity.nameTag === "") {
            entity.nameTag = "§g§h§o§s§r";
            return;
        } else if (entity.typeId === "dragons:bamboo_dragon" && entity.nameTag === "") {
            entity.nameTag = "§b§a§m§b§r";
            return;
        } else if (entity.typeId === "dragons:common_dragon" && entity.nameTag === "") {
            entity.nameTag = "§c§o§m§m§r";
            return;
        } else if (entity.typeId === "dragons:night_dragon" && entity.nameTag === "") {
            entity.nameTag = "§n§i§g§h§r";
            return;
        } else if (entity.typeId === "dragons:enchanted_dragon" && entity.nameTag === "") {
            entity.nameTag = "§e§n§c§h§r";
            return;
        } else if (entity.typeId === "dragons:quake_dragon" && entity.nameTag === "") {
            entity.nameTag = "§q§u§a§k§r";
            return;
        } else if (entity.typeId === "dragons:deadly_dragon" && entity.nameTag === "") {
            entity.nameTag = "§d§e§a§d§r";
            return;
        }
    }

});