import { world, system, EquipmentSlot } from "@minecraft/server";
import { afterEvents } from "./utils2";
import * as utils from "./utils";
import * as shroom_shield from "./shroom_shield";


let timesAttacked = 0;
let timesDefendedTNT = 0;
let timesDefendedMagma = 0;
let timesDefendedDragon = 0;
let timesDefendedWarden = 0;
let timesDefendedEnderman = 0;
let timesDefendedSporeBlossom = 0;
let timesDefendedSkeleton = 0;
let timesDefendedIce = 0;

export async function onEntityHurt(event)
{
    if (!event.hurtEntity.isPlayer() || !event.damageSource.damagingEntity)
        return;
    const equippable = event.hurtEntity.equippable;
    const mainhand = equippable.getEquipment(EquipmentSlot.Mainhand);
    const offhand = equippable.getEquipment(EquipmentSlot.Offhand);
    if (mainhand) {
        const trueShields = mainhand.getTags();
        trueShieldsF(event.damageSource.damagingEntity, event.hurtEntity, trueShields, mainhand);
        shieldDamage(mainhand, equippable, EquipmentSlot.Mainhand, event.hurtEntity);
        if (mainhand.typeId == 'pu_se:sci_fi_shield') {
            shieldFunction2(event.hurtEntity, event.damageSource.damagingEntity);
            event.hurtEntity.removeEffect('hunger');
            shieldDamageSciFi(mainhand, equippable, EquipmentSlot.Mainhand, event.hurtEntity);
        }
        if (mainhand.typeId == 'pu_se:netherite_shield') {
            event.hurtEntity.teleport(event.hurtEntity.location);
        }
    }
    if (offhand) {
        const trueShields = offhand.getTags();
        trueShieldsF(event.damageSource.damagingEntity, event.hurtEntity, trueShields, offhand);
        shieldDamage(offhand, equippable, EquipmentSlot.Offhand, event.hurtEntity);
        if (offhand.typeId == 'pu_se:sci_fi_shield') {
            shieldFunction2(event.hurtEntity, event.damageSource.damagingEntity);
            event.hurtEntity.removeEffect('hunger');
            shieldDamageSciFi(offhand, equippable, EquipmentSlot.Offhand, event.hurtEntity);
        }
        if (offhand.typeId == 'pu_se:netherite_shield') {
            event.hurtEntity.teleport(event.hurtEntity.location);
        }
    }
};
export async function onEntityHurtEntity(event)
{
    if (event.damagingEntity.isValid())
        event.damagingEntity.addTag('pu_se_hitting_entity');
    if(event.damagingEntity.isPlayer()){
        const equippable = event.damagingEntity.equippable;
        const mainhand = equippable.getEquipment(EquipmentSlot.Mainhand);
        if (mainhand) {
            if (mainhand.typeId == 'pu_se:weapon_shield') {
                let rider = world.getDimension('overworld').spawnEntity("pu_se:rider_utility", event.damagingEntity.location)
                event.damagingEntity.getComponent("minecraft:rideable").addRider(rider);
                system.runTimeout(() => { event.damagingEntity.getComponent("minecraft:rideable").ejectRider(rider); }, 5);
                system.runTimeout(() => { event.damagingEntity.getComponent("minecraft:rideable").ejectRider(rider); }, 10);
            }
        }
    }
};
export async function onTick()
{
    for (const player of world.getPlayers()) {
        if (player.hasTag('pu_se_hitting_entity')) {
            player.removeEffect('resistance');
            system.runTimeout(() => {
                player.removeTag('pu_se_hitting_entity');
            }, 20);
        }
        const equippable = player.equippable;
        const mainhand = equippable.getEquipment(EquipmentSlot.Mainhand);
        const offhand = equippable.getEquipment(EquipmentSlot.Offhand);
        const entity = player.dimension.getEntities({ location: player.location,maxDistance: 12});
        const inv = player.inventory.container;
        for (let i = 0; i < inv.size; i++) {
            const item = inv.getItem(i);
            if (!item || !item.typeId.includes('shield'))
                continue;
        }
        if (offhand) {
            if (offhand.typeId == 'pu_se:ice_shield') {
                utils.runPlayerCommand(player, "execute at @s if block ~-2 ~-1 ~-2 water run fill ~-2 ~-1 ~-2 ~-2 ~-1 ~-2 frosted_ice replace water");
                utils.runPlayerCommand(player, "execute at @s if block ~-2 ~-1 ~-1 water run fill ~-2 ~-1 ~-1 ~-2 ~-1 ~-1 frosted_ice replace water");
                utils.runPlayerCommand(player, "execute at @s if block ~-2 ~-1 ~ water run fill ~-2 ~-1 ~ ~-2 ~-1 ~ frosted_ice replace water");
                utils.runPlayerCommand(player, "execute at @s if block ~-2 ~-1 ~1 water run fill ~-2 ~-1 ~1 ~-2 ~-1 ~1 frosted_ice replace water");
                utils.runPlayerCommand(player, "execute at @s if block ~-2 ~-1 ~2 water run fill ~-2 ~-1 ~2 ~-2 ~-1 ~2 frosted_ice replace water");
                utils.runPlayerCommand(player, "execute at @s if block ~-1 ~-1 ~-2 water run fill ~-1 ~-1 ~-2 ~-1 ~-1 ~-2 frosted_ice replace water");
                utils.runPlayerCommand(player, "execute at @s if block ~-1 ~-1 ~-1 water run fill ~-1 ~-1 ~-1 ~-1 ~-1 ~-1 frosted_ice replace water");
                utils.runPlayerCommand(player, "execute at @s if block ~-1 ~-1 ~ water run fill ~-1 ~-1 ~ ~-1 ~-1 ~ frosted_ice replace water");
                utils.runPlayerCommand(player, "execute at @s if block ~-1 ~-1 ~1 water run fill ~-1 ~-1 ~1 ~-1 ~-1 ~1 frosted_ice replace water");
                utils.runPlayerCommand(player, "execute at @s if block ~-1 ~-1 ~2 water run fill ~-1 ~-1 ~2 ~-1 ~-1 ~2 frosted_ice replace water");
                utils.runPlayerCommand(player, "execute at @s if block ~ ~-1 ~-2 water run fill ~ ~-1 ~-2 ~ ~-1 ~-2 frosted_ice replace water");
                utils.runPlayerCommand(player, "execute at @s if block ~ ~-1 ~-1 water run fill ~ ~-1 ~-1 ~ ~-1 ~-1 frosted_ice replace water");
                utils.runPlayerCommand(player, "execute at @s if block ~ ~-1 ~ water run fill ~ ~-1 ~ ~ ~-1 ~ frosted_ice replace water");
                utils.runPlayerCommand(player, "execute at @s if block ~ ~-1 ~1 water run fill ~ ~-1 ~1 ~ ~-1 ~1 frosted_ice replace water");
                utils.runPlayerCommand(player, "execute at @s if block ~ ~-1 ~2 water run fill ~ ~-1 ~2 ~ ~-1 ~2 frosted_ice replace water");
                utils.runPlayerCommand(player, "execute at @s if block ~1 ~-1 ~-2 water run fill ~1 ~-1 ~-2 ~1 ~-1 ~-2 frosted_ice replace water");
                utils.runPlayerCommand(player, "execute at @s if block ~1 ~-1 ~-1 water run fill ~1 ~-1 ~-1 ~1 ~-1 ~-1 frosted_ice replace water");
                utils.runPlayerCommand(player, "execute at @s if block ~1 ~-1 ~ water run fill ~1 ~-1 ~ ~1 ~-1 ~ frosted_ice replace water");
                utils.runPlayerCommand(player, "execute at @s if block ~1 ~-1 ~1 water run fill ~1 ~-1 ~1 ~1 ~-1 ~1 frosted_ice replace water");
                utils.runPlayerCommand(player, "execute at @s if block ~1 ~-1 ~2 water run fill ~1 ~-1 ~2 ~1 ~-1 ~2 frosted_ice replace water");
                utils.runPlayerCommand(player, "execute at @s if block ~2 ~-1 ~-2 water run fill ~2 ~-1 ~-2 ~2 ~-1 ~-2 frosted_ice replace water");
                utils.runPlayerCommand(player, "execute at @s if block ~2 ~-1 ~-1 water run fill ~2 ~-1 ~-1 ~2 ~-1 ~-1 frosted_ice replace water");
                utils.runPlayerCommand(player, "execute at @s if block ~2 ~-1 ~ water run fill ~2 ~-1 ~ ~2 ~-1 ~ frosted_ice replace water");
                utils.runPlayerCommand(player, "execute at @s if block ~2 ~-1 ~1 water run fill ~2 ~-1 ~1 ~2 ~-1 ~1 frosted_ice replace water");
                utils.runPlayerCommand(player, "execute at @s if block ~2 ~-1 ~2 water run fill ~2 ~-1 ~2 ~2 ~-1 ~2 frosted_ice replace water");
                utils.runPlayerCommand(player, "execute at @s if block ~ ~-1 ~ frosted_ice run fill ~ ~-1 ~ ~ ~-1 ~ frosted_ice replace frosted_ice");
                utils.runPlayerCommand(player, "execute at @s if block ~1 ~-1 ~ frosted_ice run fill ~1 ~-1 ~ ~1 ~-1 ~ frosted_ice replace frosted_ice");
                utils.runPlayerCommand(player, "execute at @s if block ~ ~-1 ~1 frosted_ice run fill ~ ~-1 ~1 ~ ~-1 ~1 frosted_ice replace frosted_ice");
                utils.runPlayerCommand(player, "execute at @s if block ~-1 ~-1 ~ frosted_ice run fill ~-1 ~-1 ~ ~-1 ~-1 ~ frosted_ice replace frosted_ice");
                utils.runPlayerCommand(player, "execute at @s if block ~ ~-1 ~-1 frosted_ice run fill ~ ~-1 ~-1 ~ ~-1 ~-1 frosted_ice replace frosted_ice");
                utils.runPlayerCommand(player, "execute at @s if block ~1 ~-1 ~1 frosted_ice run fill ~1 ~-1 ~1 ~1 ~-1 ~1 frosted_ice replace frosted_ice");
                utils.runPlayerCommand(player, "execute at @s if block ~1 ~-1 ~-1 frosted_ice run fill ~1 ~-1 ~-1 ~1 ~-1 ~-1 frosted_ice replace frosted_ice");
                utils.runPlayerCommand(player, "execute at @s if block ~-1 ~-1 ~1 frosted_ice run fill ~-1 ~-1 ~1 ~-1 ~-1 ~1 frosted_ice replace frosted_ice");
                utils.runPlayerCommand(player, "execute at @s if block ~-1 ~-1 ~-1 frosted_ice run fill ~-1 ~-1 ~-1 ~-1 ~-1 ~-1 frosted_ice replace frosted_ice");
            }
            if (offhand.typeId == 'pu_se:turtle_shield') {
                player.addEffect("conduit_power", 220, { amplifier: 1, showParticles: false });
            }
            if (offhand.typeId == 'pu_se:magma_shield') {
                player.addEffect("fire_resistance", 220, { amplifier: 1, showParticles: false });
                if (offhand.getComponent('cooldown').getCooldownTicksRemaining(player) > 50){
                    if (timesDefendedMagma == 0){
                        utils.runPlayerCommand(player, "titleraw @s actionbar {\"rawtext\":[{\"text\":\" §0■     ■     ■§r \"}]}");
                    }
                    else if (timesDefendedMagma == 1){
                        utils.runPlayerCommand(player, "titleraw @s actionbar {\"rawtext\":[{\"text\":\" §f■     §0■     ■§r \"}]}");
                    }
                    else if (timesDefendedMagma == 2){
                        utils.runPlayerCommand(player, "titleraw @s actionbar {\"rawtext\":[{\"text\":\" §f■     ■     §0■§r \"}]}");
                    }
                    else {
                        utils.runPlayerCommand(player, "titleraw @s actionbar {\"rawtext\":[{\"text\":\" §a■     ■     ■§r \"}]}");
                        system.runTimeout(() => { utils.runPlayerCommand(player, "titleraw @s actionbar {\"rawtext\":[{\"translate\":\"ability_refreshed.message\"}]}"); }, 50);
                    }
                }
                else if (offhand.getComponent('cooldown').getCooldownTicksRemaining(player) > 10 && offhand.getComponent('cooldown').getCooldownTicksRemaining(player) < 20){
                    system.runTimeout(() => { utils.runPlayerCommand(player, "titleraw @s actionbar {\"rawtext\":[{\"translate\":\"ability_refreshed.message\"}]}"); }, 20);
                }
            }
            if (offhand.typeId == 'pu_se:sci_fi_shield') {
                player.addEffect("night_vision", 220, { amplifier: 1, showParticles: false });
                shieldProtection(player);
            }
            if (offhand.typeId == 'pu_se:netherite_shield') {
                player.addEffect("fire_resistance", 220, { amplifier: 5, showParticles: false });
            }
            if (offhand.getTags().some(e => e.includes('pu_se_true_shield')) && player.isSneaking && entity[0] && !player.hasTag('pu_se_hitting_entity')) {
                shieldProtection(player);
            }
            if (offhand.typeId == 'pu_se:tnt_shield') {
                if (offhand.getComponent('cooldown').getCooldownTicksRemaining(player) > 50){
                    if (timesDefendedTNT == 0){
                        utils.runPlayerCommand(player, "titleraw @s actionbar {\"rawtext\":[{\"text\":\" §0■     ■     ■§r \"}]}");
                    }
                    else if (timesDefendedTNT == 1){
                        utils.runPlayerCommand(player, "titleraw @s actionbar {\"rawtext\":[{\"text\":\" §f■     §0■     ■§r \"}]}");
                    }
                    else if (timesDefendedTNT == 2){
                        utils.runPlayerCommand(player, "titleraw @s actionbar {\"rawtext\":[{\"text\":\" §f■     ■     §0■§r \"}]}");
                    }
                    else {
                        utils.runPlayerCommand(player, "titleraw @s actionbar {\"rawtext\":[{\"text\":\" §a■     ■     ■§r \"}]}");
                        system.runTimeout(() => { utils.runPlayerCommand(player, "titleraw @s actionbar {\"rawtext\":[{\"translate\":\"ability_refreshed.message\"}]}"); }, 50);
                    }
                }
                else if (offhand.getComponent('cooldown').getCooldownTicksRemaining(player) > 10 && offhand.getComponent('cooldown').getCooldownTicksRemaining(player) < 20){
                    system.runTimeout(() => { utils.runPlayerCommand(player, "titleraw @s actionbar {\"rawtext\":[{\"translate\":\"ability_refreshed.message\"}]}"); }, 20);
                }
            }
            if (offhand.typeId == 'pu_se:dragon_shield') {
                if (offhand.getComponent('cooldown').getCooldownTicksRemaining(player) > 50){
                    if (timesDefendedDragon == 0){
                        utils.runPlayerCommand(player, "titleraw @s actionbar {\"rawtext\":[{\"text\":\" §0■     ■     ■§r \"}]}");
                    }
                    else if (timesDefendedDragon == 1){
                        utils.runPlayerCommand(player, "titleraw @s actionbar {\"rawtext\":[{\"text\":\" §f■     §0■     ■§r \"}]}");
                    }
                    else if (timesDefendedDragon == 2){
                        utils.runPlayerCommand(player, "titleraw @s actionbar {\"rawtext\":[{\"text\":\" §f■     ■     §0■§r \"}]}");
                    }
                    else {
                        utils.runPlayerCommand(player, "titleraw @s actionbar {\"rawtext\":[{\"text\":\" §a■     ■     ■§r \"}]}");
                        system.runTimeout(() => { utils.runPlayerCommand(player, "titleraw @s actionbar {\"rawtext\":[{\"translate\":\"ability_refreshed.message\"}]}"); }, 50);
                    }
                }
                else if (offhand.getComponent('cooldown').getCooldownTicksRemaining(player) > 10 && offhand.getComponent('cooldown').getCooldownTicksRemaining(player) < 20){
                    system.runTimeout(() => { utils.runPlayerCommand(player, "titleraw @s actionbar {\"rawtext\":[{\"translate\":\"ability_refreshed.message\"}]}"); }, 20);
                }
            }
            if (offhand.typeId == 'pu_se:warden_shield') {
                if (offhand.getComponent('cooldown').getCooldownTicksRemaining(player) > 50){
                    if (timesDefendedWarden == 0){
                        utils.runPlayerCommand(player, "titleraw @s actionbar {\"rawtext\":[{\"text\":\" §0■     ■     ■§r \"}]}");
                    }
                    else if (timesDefendedWarden == 1){
                        utils.runPlayerCommand(player, "titleraw @s actionbar {\"rawtext\":[{\"text\":\" §f■     §0■     ■§r \"}]}");
                    }
                    else if (timesDefendedWarden == 2){
                        utils.runPlayerCommand(player, "titleraw @s actionbar {\"rawtext\":[{\"text\":\" §f■     ■     §0■§r \"}]}");
                    }
                    else {
                        utils.runPlayerCommand(player, "titleraw @s actionbar {\"rawtext\":[{\"text\":\" §a■     ■     ■§r \"}]}");
                        system.runTimeout(() => { utils.runPlayerCommand(player, "titleraw @s actionbar {\"rawtext\":[{\"translate\":\"ability_refreshed.message\"}]}"); }, 50);
                    }
                }
                else if (offhand.getComponent('cooldown').getCooldownTicksRemaining(player) > 10 && offhand.getComponent('cooldown').getCooldownTicksRemaining(player) < 20){
                    system.runTimeout(() => { utils.runPlayerCommand(player, "titleraw @s actionbar {\"rawtext\":[{\"translate\":\"ability_refreshed.message\"}]}"); }, 20);
                }
            }
        }
        if (mainhand) {
            if (mainhand.typeId == 'pu_se:turtle_shield') {
                player.addEffect("conduit_power", 220, { amplifier: 1, showParticles: false });
            }
            if (mainhand.typeId == 'pu_se:sci_fi_shield') {
                player.addEffect("night_vision", 220, { amplifier: 1, showParticles: false });
                shieldProtection(player);
            }
            if (mainhand.typeId == 'pu_se:magma_shield') {
                player.addEffect("fire_resistance", 220, { amplifier: 1, showParticles: false });
                if (mainhand.getComponent('cooldown').getCooldownTicksRemaining(player) > 50){
                    if (timesDefendedMagma == 0){
                        utils.runPlayerCommand(player, "titleraw @s actionbar {\"rawtext\":[{\"text\":\" §0■     ■     ■§r \"}]}");
                    }
                    else if (timesDefendedMagma == 1){
                        utils.runPlayerCommand(player, "titleraw @s actionbar {\"rawtext\":[{\"text\":\" §f■     §0■     ■§r \"}]}");
                    }
                    else if (timesDefendedMagma == 2){
                        utils.runPlayerCommand(player, "titleraw @s actionbar {\"rawtext\":[{\"text\":\" §f■     ■     §0■§r \"}]}");
                    }
                    else {
                        utils.runPlayerCommand(player, "titleraw @s actionbar {\"rawtext\":[{\"text\":\" §a■     ■     ■§r \"}]}");
                        system.runTimeout(() => { utils.runPlayerCommand(player, "titleraw @s actionbar {\"rawtext\":[{\"translate\":\"ability_refreshed.message\"}]}"); }, 50);
                    }
                }
                else if (mainhand.getComponent('cooldown').getCooldownTicksRemaining(player) > 10 && mainhand.getComponent('cooldown').getCooldownTicksRemaining(player) < 20){
                    system.runTimeout(() => { utils.runPlayerCommand(player, "titleraw @s actionbar {\"rawtext\":[{\"translate\":\"ability_refreshed.message\"}]}"); }, 20);
                }
            }
            if (mainhand.typeId == 'pu_se:netherite_shield') {
                player.addEffect("fire_resistance", 220, { amplifier: 5, showParticles: false });
            }
            if (mainhand.typeId == 'pu_se:tnt_shield') {
                if (mainhand.getComponent('cooldown').getCooldownTicksRemaining(player) > 50){
                    if (timesDefendedTNT == 0){
                        utils.runPlayerCommand(player, "titleraw @s actionbar {\"rawtext\":[{\"text\":\" §0■     ■     ■§r \"}]}");
                    }
                    else if (timesDefendedTNT == 1){
                        utils.runPlayerCommand(player, "titleraw @s actionbar {\"rawtext\":[{\"text\":\" §f■     §0■     ■§r \"}]}");
                    }
                    else if (timesDefendedTNT == 2){
                        utils.runPlayerCommand(player, "titleraw @s actionbar {\"rawtext\":[{\"text\":\" §f■     ■     §0■§r \"}]}");
                    }
                    else {
                        utils.runPlayerCommand(player, "titleraw @s actionbar {\"rawtext\":[{\"text\":\" §a■     ■     ■§r \"}]}");
                        system.runTimeout(() => { utils.runPlayerCommand(player, "titleraw @s actionbar {\"rawtext\":[{\"translate\":\"ability_refreshed.message\"}]}"); }, 50);
                    }
                }
                else if (mainhand.getComponent('cooldown').getCooldownTicksRemaining(player) > 10 && mainhand.getComponent('cooldown').getCooldownTicksRemaining(player) < 20){
                    system.runTimeout(() => { utils.runPlayerCommand(player, "titleraw @s actionbar {\"rawtext\":[{\"translate\":\"ability_refreshed.message\"}]}"); }, 20);
                }
            }
            if (mainhand.typeId == 'pu_se:dragon_shield') {
                if (mainhand.getComponent('cooldown').getCooldownTicksRemaining(player) > 50){
                    if (timesDefendedDragon == 0){
                        utils.runPlayerCommand(player, "titleraw @s actionbar {\"rawtext\":[{\"text\":\" §0■     ■     ■§r \"}]}");
                    }
                    else if (timesDefendedDragon == 1){
                        utils.runPlayerCommand(player, "titleraw @s actionbar {\"rawtext\":[{\"text\":\" §f■     §0■     ■§r \"}]}");
                    }
                    else if (timesDefendedDragon == 2){
                        utils.runPlayerCommand(player, "titleraw @s actionbar {\"rawtext\":[{\"text\":\" §f■     ■     §0■§r \"}]}");
                    }
                    else {
                        utils.runPlayerCommand(player, "titleraw @s actionbar {\"rawtext\":[{\"text\":\" §a■     ■     ■§r \"}]}");
                        system.runTimeout(() => { utils.runPlayerCommand(player, "titleraw @s actionbar {\"rawtext\":[{\"translate\":\"ability_refreshed.message\"}]}"); }, 50);
                    }
                }
                else if (mainhand.getComponent('cooldown').getCooldownTicksRemaining(player) > 10 && mainhand.getComponent('cooldown').getCooldownTicksRemaining(player) < 20){
                    system.runTimeout(() => { utils.runPlayerCommand(player, "titleraw @s actionbar {\"rawtext\":[{\"translate\":\"ability_refreshed.message\"}]}"); }, 20);
                }
            }
            if (mainhand.typeId == 'pu_se:warden_shield') {
                if (mainhand.getComponent('cooldown').getCooldownTicksRemaining(player) > 50){
                    if (timesDefendedWarden == 0){
                        utils.runPlayerCommand(player, "titleraw @s actionbar {\"rawtext\":[{\"text\":\" §0■     ■     ■§r \"}]}");
                    }
                    else if (timesDefendedWarden == 1){
                        utils.runPlayerCommand(player, "titleraw @s actionbar {\"rawtext\":[{\"text\":\" §f■     §0■     ■§r \"}]}");
                    }
                    else if (timesDefendedWarden == 2){
                        utils.runPlayerCommand(player, "titleraw @s actionbar {\"rawtext\":[{\"text\":\" §f■     ■     §0■§r \"}]}");
                    }
                    else {
                        utils.runPlayerCommand(player, "titleraw @s actionbar {\"rawtext\":[{\"text\":\" §a■     ■     ■§r \"}]}");
                        system.runTimeout(() => { utils.runPlayerCommand(player, "titleraw @s actionbar {\"rawtext\":[{\"translate\":\"ability_refreshed.message\"}]}"); }, 50);
                    }
                }
                else if (mainhand.getComponent('cooldown').getCooldownTicksRemaining(player) > 10 && mainhand.getComponent('cooldown').getCooldownTicksRemaining(player) < 20){
                    system.runTimeout(() => { utils.runPlayerCommand(player, "titleraw @s actionbar {\"rawtext\":[{\"translate\":\"ability_refreshed.message\"}]}"); }, 20);
                }
            }
            if (mainhand.typeId == 'pu_se:ice_shield') {
                utils.runPlayerCommand(player, "execute at @s if block ~-2 ~-1 ~-2 water run fill ~-2 ~-1 ~-2 ~-2 ~-1 ~-2 frosted_ice replace water");
                utils.runPlayerCommand(player, "execute at @s if block ~-2 ~-1 ~-1 water run fill ~-2 ~-1 ~-1 ~-2 ~-1 ~-1 frosted_ice replace water");
                utils.runPlayerCommand(player, "execute at @s if block ~-2 ~-1 ~ water run fill ~-2 ~-1 ~ ~-2 ~-1 ~ frosted_ice replace water");
                utils.runPlayerCommand(player, "execute at @s if block ~-2 ~-1 ~1 water run fill ~-2 ~-1 ~1 ~-2 ~-1 ~1 frosted_ice replace water");
                utils.runPlayerCommand(player, "execute at @s if block ~-2 ~-1 ~2 water run fill ~-2 ~-1 ~2 ~-2 ~-1 ~2 frosted_ice replace water");
                utils.runPlayerCommand(player, "execute at @s if block ~-1 ~-1 ~-2 water run fill ~-1 ~-1 ~-2 ~-1 ~-1 ~-2 frosted_ice replace water");
                utils.runPlayerCommand(player, "execute at @s if block ~-1 ~-1 ~-1 water run fill ~-1 ~-1 ~-1 ~-1 ~-1 ~-1 frosted_ice replace water");
                utils.runPlayerCommand(player, "execute at @s if block ~-1 ~-1 ~ water run fill ~-1 ~-1 ~ ~-1 ~-1 ~ frosted_ice replace water");
                utils.runPlayerCommand(player, "execute at @s if block ~-1 ~-1 ~1 water run fill ~-1 ~-1 ~1 ~-1 ~-1 ~1 frosted_ice replace water");
                utils.runPlayerCommand(player, "execute at @s if block ~-1 ~-1 ~2 water run fill ~-1 ~-1 ~2 ~-1 ~-1 ~2 frosted_ice replace water");
                utils.runPlayerCommand(player, "execute at @s if block ~ ~-1 ~-2 water run fill ~ ~-1 ~-2 ~ ~-1 ~-2 frosted_ice replace water");
                utils.runPlayerCommand(player, "execute at @s if block ~ ~-1 ~-1 water run fill ~ ~-1 ~-1 ~ ~-1 ~-1 frosted_ice replace water");
                utils.runPlayerCommand(player, "execute at @s if block ~ ~-1 ~ water run fill ~ ~-1 ~ ~ ~-1 ~ frosted_ice replace water");
                utils.runPlayerCommand(player, "execute at @s if block ~ ~-1 ~1 water run fill ~ ~-1 ~1 ~ ~-1 ~1 frosted_ice replace water");
                utils.runPlayerCommand(player, "execute at @s if block ~ ~-1 ~2 water run fill ~ ~-1 ~2 ~ ~-1 ~2 frosted_ice replace water");
                utils.runPlayerCommand(player, "execute at @s if block ~1 ~-1 ~-2 water run fill ~1 ~-1 ~-2 ~1 ~-1 ~-2 frosted_ice replace water");
                utils.runPlayerCommand(player, "execute at @s if block ~1 ~-1 ~-1 water run fill ~1 ~-1 ~-1 ~1 ~-1 ~-1 frosted_ice replace water");
                utils.runPlayerCommand(player, "execute at @s if block ~1 ~-1 ~ water run fill ~1 ~-1 ~ ~1 ~-1 ~ frosted_ice replace water");
                utils.runPlayerCommand(player, "execute at @s if block ~1 ~-1 ~1 water run fill ~1 ~-1 ~1 ~1 ~-1 ~1 frosted_ice replace water");
                utils.runPlayerCommand(player, "execute at @s if block ~1 ~-1 ~2 water run fill ~1 ~-1 ~2 ~1 ~-1 ~2 frosted_ice replace water");
                utils.runPlayerCommand(player, "execute at @s if block ~2 ~-1 ~-2 water run fill ~2 ~-1 ~-2 ~2 ~-1 ~-2 frosted_ice replace water");
                utils.runPlayerCommand(player, "execute at @s if block ~2 ~-1 ~-1 water run fill ~2 ~-1 ~-1 ~2 ~-1 ~-1 frosted_ice replace water");
                utils.runPlayerCommand(player, "execute at @s if block ~2 ~-1 ~ water run fill ~2 ~-1 ~ ~2 ~-1 ~ frosted_ice replace water");
                utils.runPlayerCommand(player, "execute at @s if block ~2 ~-1 ~1 water run fill ~2 ~-1 ~1 ~2 ~-1 ~1 frosted_ice replace water");
                utils.runPlayerCommand(player, "execute at @s if block ~2 ~-1 ~2 water run fill ~2 ~-1 ~2 ~2 ~-1 ~2 frosted_ice replace water");
                utils.runPlayerCommand(player, "execute at @s if block ~ ~-1 ~ frosted_ice run fill ~ ~-1 ~ ~ ~-1 ~ frosted_ice replace frosted_ice");
                utils.runPlayerCommand(player, "execute at @s if block ~1 ~-1 ~ frosted_ice run fill ~1 ~-1 ~ ~1 ~-1 ~ frosted_ice replace frosted_ice");
                utils.runPlayerCommand(player, "execute at @s if block ~ ~-1 ~1 frosted_ice run fill ~ ~-1 ~1 ~ ~-1 ~1 frosted_ice replace frosted_ice");
                utils.runPlayerCommand(player, "execute at @s if block ~-1 ~-1 ~ frosted_ice run fill ~-1 ~-1 ~ ~-1 ~-1 ~ frosted_ice replace frosted_ice");
                utils.runPlayerCommand(player, "execute at @s if block ~ ~-1 ~-1 frosted_ice run fill ~ ~-1 ~-1 ~ ~-1 ~-1 frosted_ice replace frosted_ice");
                utils.runPlayerCommand(player, "execute at @s if block ~1 ~-1 ~1 frosted_ice run fill ~1 ~-1 ~1 ~1 ~-1 ~1 frosted_ice replace frosted_ice");
                utils.runPlayerCommand(player, "execute at @s if block ~1 ~-1 ~-1 frosted_ice run fill ~1 ~-1 ~-1 ~1 ~-1 ~-1 frosted_ice replace frosted_ice");
                utils.runPlayerCommand(player, "execute at @s if block ~-1 ~-1 ~1 frosted_ice run fill ~-1 ~-1 ~1 ~-1 ~-1 ~1 frosted_ice replace frosted_ice");
                utils.runPlayerCommand(player, "execute at @s if block ~-1 ~-1 ~-1 frosted_ice run fill ~-1 ~-1 ~-1 ~-1 ~-1 ~-1 frosted_ice replace frosted_ice");

            }
            if (mainhand.getTags().some(e => e.includes('pu_se_true_shield')) && player.isSneaking && entity[0] && !player.hasTag('pu_se_hitting_entity')) {
                shieldProtection(player);
            }
        }
    }
};
function shieldDamage(item, equipment, slot, player) {
    if (!player.isSneaking)
        return;
    if (!item.getTags().some(e => e.includes('pu_se_true_shield')))
        return;
    const durability = item.getComponent('durability');
    if (!durability)
        return;
    for (let i = 0; i <= 2; i++) {
        durability.damage++;
    }
    equipment.setEquipment(slot, item);
    if (durability.damage >= durability.maxDurability) {
        player.playSound('random.break');
        equipment.setEquipment(slot);
    }
}
function shieldDamageSciFi(item, equipment, slot, player) {
    const durability = item.getComponent('durability');
    if (!durability)
        return;
    for (let i = 0; i <= 2; i++) {
        durability.damage++;
    }
    equipment.setEquipment(slot, item);
    if (durability.damage >= durability.maxDurability) {
        player.playSound('random.break');
        equipment.setEquipment(slot);
    }
}
function shieldProtection(player) {
    if(!player.isFalling){
        player.addEffect('resistance', 10, { amplifier: 255, showParticles: false });
    }
}
function shieldFunction(player, source) {
    const entity = player.dimension.getEntities({ location: player.location,maxDistance: 12});
    if (player.hasTag('pu_se_hitting_entity'))
        return;
    if(source.isValid()){
        if(source.typeId!=="minecraft:splash_potion"){
            if (source.matches({families: ['monster'] })) {
                player.teleport(player.location);
                player.extinguishFire();
                if(typeof source.applyKnockback === 'function') source.applyKnockback(source.location.x, source.location.z, 1, 0.5);
                //player.playSound('item.shield.block');
            }
        }
    }
}
function shieldFunction2(player, source) {
    const entity = player.dimension.getEntities({ location: player.location,maxDistance: 12});
    if (player.hasTag('pu_se_hitting_entity'))
        return;
    if(source.isValid()){
        if(source.typeId!=="minecraft:splash_potion"){
            if (source.matches({families: ['monster'] })) {
                player.teleport(player.location);
                player.extinguishFire();
                if(typeof source.applyKnockback === 'function') source.applyKnockback(source.location.x, source.location.z, 2, 0.75);
                //player.playSound('item.shield.block');
                let rider = world.getDimension('overworld').spawnEntity("pu_se:rider_utility", player.location)
                player.getComponent("minecraft:rideable").addRider(rider);
                system.runTimeout(() => { player.getComponent("minecraft:rideable").ejectRider(rider); }, 5);
                system.runTimeout(() => { player.getComponent("minecraft:rideable").ejectRider(rider); }, 10);
            }
        }
    }
}
function shieldSlimeFunction(hurtEntity, source) {
    const entity = hurtEntity.dimension.getEntities({ location: hurtEntity.location,maxDistance: 12});
    if (!entity[0] || hurtEntity.hasTag('pu_se_hitting_entity'))
        return;
    hurtEntity.teleport(hurtEntity.location);
    const mobs = source.dimension.getEntities({ location: hurtEntity.location,maxDistance: 5,excludeTypes: ["minecraft:falling_block", "minecraft:player", "pu_se:dragon_projectile", "pu_se:ice_projectile", "pu_se:magma_projectile", "pu_se:tnt_projectile", "pu_se:warden_projectile", "minecraft:boat", "minecraft:chest_boat", "minecraft:minecart", "minecraft:chest_minecart", "minecraft:command_block_minecart", "minecraft:hopper_minecart", "minecraft:tnt_minecart", "minecraft:leash_knot", "minecraft:xp_orb", "minecraft:item", "minecraft:armor_stand", "minecraft:ender_crystal", "minecraft:minecart"]});
    for (let mob of mobs){
        if(typeof mob.applyKnockback === 'function') mob.applyKnockback(mob.location.x, mob.location.z, 3, 0.5);
    }
}
function AxolotlShieldEffect(player){
    timesAttacked++;
    if(timesAttacked % 3 === 0){
        utils.runPlayerCommand(player, "effect @a[r=7] regeneration 5 2 false");
        utils.runPlayerCommand(player, "effect @a[r=7] saturation 5 2 false");
    }
}
function MagmaShieldEffect(player, source){
    source.setOnFire(10);
    player.extinguishFire();
}

function trueShieldsF(source, player, trueShields, equipment) {
    if (!player.isSneaking || !trueShields.some(e => e.includes('pu_se_true_shield')))
        return;
    if(!source.isValid())
        return;
    player.removeEffect('hunger');
    shieldFunction(player, source);

    let rider = world.getDimension('overworld').spawnEntity("pu_se:rider_utility", player.location)
    player.getComponent("minecraft:rideable").addRider(rider);
    system.runTimeout(() => { player.getComponent("minecraft:rideable").ejectRider(rider); }, 5);
    system.runTimeout(() => { player.getComponent("minecraft:rideable").ejectRider(rider); }, 10);

    if (equipment.typeId == 'pu_se:axolotl_shield') {
        AxolotlShieldEffect(player);
    }
    else if (equipment.typeId == 'pu_se:magma_shield') {
        MagmaShieldEffect(player, source);
        timesDefendedMagma++;
        if (timesDefendedMagma % 3 == 0){
            system.runTimeout(() => { timesDefendedMagma=0; }, 5);
            system.runTimeout(() => {
                player.startItemCooldown("pu_se_magma_shield", 1);
            }, 5);
        }
    }
    else if (equipment.typeId == 'pu_se:slime_shield') {
        source.applyDamage(2);
        shieldSlimeFunction(player, source);
    }
    else if (equipment.typeId == 'pu_se:spike_shield') {
        source.applyDamage(3);
    }
    else if (equipment.typeId == 'pu_se:spore_blossom_shield') {
        const nearbyEntities = source.dimension.getEntities({ location: source.location,maxDistance: 5})
        const nearbyMonsters = [];
        for(const entity2 of nearbyEntities){
            if(entity2.isValid()){
                if(entity2.typeId!=="minecraft:splash_potion"){
                    if (entity2.matches({families: ['monster'] }))
                        nearbyMonsters.push(entity2);
                }
            }
        }
        for(const monster of nearbyMonsters){
            monster.addEffect("poison", 40, { amplifier: 1 });
        }
        timesDefendedSporeBlossom++;
        if (timesDefendedSporeBlossom % 4 == 0){
            system.runTimeout(() => {
                player.startItemCooldown("pu_se_spore_blossom_shield", 1);
            }, 5);
        }
    }
    else if (equipment.typeId == 'pu_se:shroom_shield') {
        shroom_shield.applyEffect(player, source);
    }
    else if (equipment.typeId == 'pu_se:tnt_shield') {
        timesDefendedTNT++;
        source.applyDamage(5);
        if (timesDefendedTNT % 3 == 0){
            system.runTimeout(() => { timesDefendedTNT=0; }, 5);
            system.runTimeout(() => {
                player.startItemCooldown("pu_se_tnt_shield", 1);
            }, 5);
        }
    }
    else if (equipment.typeId == 'pu_se:dragon_shield') {
        timesDefendedDragon++;
        source.applyDamage(4);
        if (timesDefendedDragon % 3 == 0){
            system.runTimeout(() => { timesDefendedDragon=0; }, 5);
            system.runTimeout(() => {
                player.startItemCooldown("pu_se_dragon_shield", 1);
            }, 5);
        }
    }
    else if (equipment.typeId == 'pu_se:warden_shield') {
        timesDefendedWarden++;
        source.applyDamage(4);
        if (timesDefendedWarden % 3 == 0){
            system.runTimeout(() => { timesDefendedWarden=0; }, 5);
            system.runTimeout(() => {
                player.startItemCooldown("pu_se_warden_shield", 1);
            }, 5);
        }
    }
    else if (equipment.typeId == 'pu_se:enderman_shield') {
        timesDefendedEnderman++;
        source.applyDamage(4);
        if (timesDefendedEnderman % 4 == 0){
            system.runTimeout(() => {
                player.startItemCooldown("pu_se_enderman_shield", 1);
            }, 5);
        }
    }
    else if (equipment.typeId == 'pu_se:skeleton_shield') {
        timesDefendedSkeleton++;
        source.applyDamage(3);
        if (timesDefendedSkeleton % 3 == 0){
            system.runTimeout(() => {
                player.startItemCooldown("pu_se_skeleton_shield", 1);
            }, 5);
        }
    }
    else if (equipment.typeId == 'pu_se:ice_shield') {
        timesDefendedIce++;
        if (timesDefendedIce % 3 == 0){
            system.runTimeout(() => {
                player.startItemCooldown("pu_se_ice_shield", 1);
            }, 5);
        }
    }
    else if (equipment.typeId == 'pu_se:weapon_shield') {
        const nearbyEntities = source.dimension.getEntities({ location: source.location,maxDistance: 5,excludeTypes: ["minecraft:falling_block", "minecraft:player", "pu_se:dragon_projectile", "pu_se:ice_projectile", "pu_se:magma_projectile", "pu_se:tnt_projectile", "pu_se:warden_projectile", "minecraft:boat", "minecraft:chest_boat", "minecraft:minecart", "minecraft:chest_minecart", "minecraft:command_block_minecart", "minecraft:hopper_minecart", "minecraft:tnt_minecart", "minecraft:leash_knot", "minecraft:xp_orb", "minecraft:item", "minecraft:armor_stand", "minecraft:ender_crystal", "minecraft:minecart"]})
        const nearbyMonsters = [];
        for(const entity2 of nearbyEntities){
            if(entity2.isValid()){
                if(entity2.typeId!=="minecraft:splash_potion"){
                    if (entity2.matches({families: ['monster'] }))
                        nearbyMonsters.push(entity2);
                }
            }
        }
        for(const monster of nearbyMonsters){
            monster.applyDamage(6);
            if(typeof monster.applyKnockback === 'function') monster.applyKnockback(monster.location.x, monster.location.z, 0.75, 0.25);
        }
    }
    // else if (trueShields.some(e => e.includes('type_slime'))) { //escudos con el tag "type_slime"
    //     shieldSlimeFunction(player, source);
    // }
    // else if (trueShields.some(e => e.includes('type_magic_damage'))) { //escudos con el tag "type_slime"
    //     if (player.getEffect('instant_damage')) {
    //         player.removeEffect('instant_damage');
    //     }
    //     shieldFunction(player, source);
    // // }
    // else {
    //     shieldFunction(player, source);
    // }
}
