import { system, world, PlayerInteractWithEntityAfterEvent } from "@minecraft/server";
import './ui'
import { defaultEXP, petList, petsId } from "./ui";
{
let petEXP = world.scoreboard.getObjective(`tp_pw:pet_EXP`);
let petLVL = world.scoreboard.getObjective(`tp_pw:pet_LVL`);
let maxPet = 6
const aydiPet = [    `tp_pw:feral_lynx`,    `tp_pw:nebula_dragon`,    `tp_pw:flame_hawk`,    `tp_pw:frost_fox`,    `tp_pw:music_golem`,    `tp_pw:storm_griffin`]
system.runInterval(()=> {
    const entities = world.getDimension('overworld').getEntities()
    entities.forEach(entity=>{
        petsId.forEach(pet=>{
            if (entity.typeId===pet||entity.typeId===pet+`_untamed`) {
                try {
                    chooseAttack(entity)
                    //sleepStage(entity)
                } catch (error) {
                }
            }
        })
        if(entity.typeId==='tp_pw:nebula_dragon' || entity.typeId==='tp_pw:storm_griffin'){
            movementTargetBased(entity)
        }
        entity.runCommand('tag @s[scores={tp_pw:pet_LVL=5..}, tag=!tp_pw:ultiUnlock] add tp_pw:ultiUnlock')
    })
})
function movementTargetBased(entity) {
    if(entity.getProperty('tp_pw:has_target')===false && entity.getProperty('tp_pw:move_stage')==="walk" && !entity.hasTag('tp_pw:movement_initialized')) {
        system.runTimeout(()=>{
            try {
                if(entity.getProperty('tp_pw:has_target')===false) {
                    entity.triggerEvent('tf_flying')
                    entity.removeTag('tp_pw:movement_initialized')
                } else {
                    entity.removeTag('tp_pw:movement_initialized')
                }
            } catch (error) {
                
            }
        },3*20)
        entity.addTag('tp_pw:movement_initialized')
    }
}
function sleepStage(entity) {
    let instanced = false
    if(getTime()==='night'){
        if(!instanced){
            instanced = true
            const sleepTimer = system.runTimeout(()=>{
                entity.triggerEvent(`sleeping`)
            },5*20)
        }
    } else if (getTime()==='day'){
        entity.triggerEvent(`sleeping_off`)
    }
}
function getTime() {
    const worldTime = world.getTimeOfDay()
    const dayCycleLength = 24000;
    const currentDay = Math.floor(worldTime / dayCycleLength);
    
    const morningTime = 1000 + (currentDay * dayCycleLength);
    const nightTime = 13000 + (currentDay * dayCycleLength);
    
    if (worldTime >= morningTime && worldTime < nightTime) {
        return 'day';
    } else {
        return 'night';
    }
}
function getPVPmode() {
    return world.getDynamicProperty("tp_pw:pvp");
}
world.afterEvents.playerInteractWithEntity.subscribe((event)=>{
    const entity = event.target
    const player = event.player
    const itemStack = event.itemStack
    const perantaraLocation = entity.location
    petsId.forEach(pet=>{
        if (entity.typeId === pet+`_untamed` && itemStack.typeId === pet+`.food`) {
            const perantara = player.dimension.spawnEntity('tp_pw:perantara', perantaraLocation)
            const ownerName = player.name
            const ownerId = player.id
            const petAmount = player.getDynamicProperty(`tp_pw:petAmount`)
            if (player.getDynamicProperty('tp_pw:petAmount')===undefined) {player.setDynamicProperty('tp_pw:petAmount', 0)}
            const entityPetAmount = player.dimension.getEntities({tags:[`tp_pw:`+player.id]})
            const samePet = player.dimension.getEntities({type:pet,tags:[`tp_pw:`+player.id,`tp_pw:initialized`]})
            player.setDynamicProperty(`tp_pw:petAmount`, entityPetAmount.length)
            if (player.getDynamicProperty('tp_pw:petAmount')<maxPet) {
                if(samePet.length===0){
                    entity.setDynamicProperty('tp_pw:owner', `${ownerId}`)
                    entity.setDynamicProperty(`tp_pw:ownerName`, player.name)
                    entity.addTag(`tp_pw:${ownerId}`)
                    system.runTimeout(()=>{
                        perantara.dimension.getEntities({maxDistance:1,location:perantara.location,type:pet}).forEach(entity=>{
                            entity.setDynamicProperty('tp_pw:owner', `${ownerId}`)
                            entity.setDynamicProperty(`tp_pw:ownerName`, player.name)
                            entity.addTag(`tp_pw:${ownerId}`)
                            entity.runCommand('scoreboard players set @s tp_pw:pet_LVL 1')
                            entity.runCommand('scoreboard players set @s tp_pw:pet_EXP 0')
                            entity.runCommand('scoreboard players set @s tp_pw:pet_HGR 600')
                            entity.runCommand('scoreboard players set @s tp_pw:petBooster 0')
                            entity.addTag(`tp_pw:initialized`)
                        })
                    }, 20)
                } else {
                    entity.remove()
                    player.runCommand(`title @s title You can't tame the same pet.`)
                }
                // entity.runCommand('scoreboard players set @s tp_pw:pet_LVL 1')
                // entity.runCommand('scoreboard players set @s tp_pw:pet_EXP 0')
                // entity.runCommand('scoreboard players set @s tp_pw:pet_HGR 600')
                // entity.runCommand('scoreboard players set @s tp_pw:petBooster 0')
            } else {
                entity.remove()
                player.runCommand(`title @s title your pet count has\n reached the limit.`)
                player.runCommand(`title @s subtitle You can't have more pet.`)
            }
        }
    })
})
world.afterEvents.dataDrivenEntityTrigger.subscribe((arg)=>{
    // if (arg.eventId==='tp_pw:tamed' && petsId.includes(arg.entity.typeId)){
    //     arg.entity.dimension.getPlayers({closest: 1, location:arg.entity.location}).forEach(player=>{
    //         const ownerName = player.name
    //         const ownerId = player.id
    //         const petAmount = player.getDynamicProperty(`tp_pw:petAmount`)
    //         if (player.getDynamicProperty('tp_pw:petAmount')===undefined) {player.setDynamicProperty('tp_pw:petAmount', 0)}
    //         const entityPetAmount = player.dimension.getEntities({tags:[`tp_pw:`+player.id]})
    //         player.setDynamicProperty(`tp_pw:petAmount`, entityPetAmount.length)
    //         if (player.getDynamicProperty('tp_pw:petAmount')<maxPet) {
    //             arg.entity.setDynamicProperty('tp_pw:owner', `${ownerId}`)
    //             arg.entity.setDynamicProperty(`tp_pw:ownerName`, player.name)
    //             arg.entity.addTag(`tp_pw:${ownerId}`)
    //             arg.entity.runCommand('scoreboard players set @s tp_pw:pet_LVL 1')
    //             arg.entity.runCommand('scoreboard players set @s tp_pw:pet_EXP 0')
    //             arg.entity.runCommand('scoreboard players set @s tp_pw:pet_HGR 600')
    //             arg.entity.runCommand('scoreboard players set @s tp_pw:petBooster 0')
    //         } else {
    //             arg.entity.remove()
    //             player.runCommand(`title @s title your pet count has\n reached the limit.`)
    //             player.runCommand(`title @s subtitle You can't have more pet.`)
    //         }
    //         console.warn(`pet Amount: ${petAmount}`)
    //     })
    // }
    if (arg.eventId==='dying') {
        world.scoreboard.getObjective('tp_pw:despawn_counter').setScore(arg.entity, 10)
    }
})
world.afterEvents.entityHurt.subscribe((event)=>{
    const victim = event.hurtEntity
    const killer = event.damageSource
    const damage = event.damage

    if (victim.typeId==="tp_pw:nebula_dragon"){
        if (victim.hasTag('tp_pw:nebula_dragon_shield')) {
            if (killer.typeId==="tp_pw:nebula_dragon") {
            } else {
            killer.damagingEntity.applyDamage(damage)
            if (victim.isValid()===true){victim.addEffect('regeneration', 60, {showParticles: true, amplifier: damage})
            victim.runCommand('particle tp_pw:cosmic_shield_1')
            victim.runCommand('particle tp_pw:cosmic_shield_2')
            victim.runCommand('particle tp_pw:cosmic_shield_3')
            victim.runCommand('particle tp_pw:cosmic_shield_4')}
            }
        }
    }
})
world.afterEvents.entityDie.subscribe((event)=>{
    petEXP = world.scoreboard.getObjective(`tp_pw:pet_EXP`);
    petLVL = world.scoreboard.getObjective(`tp_pw:pet_LVL`);
    const target = event.deadEntity
    const source = event.damageSource.damagingEntity

    aydiPet.forEach(pet=>{
        if (source!==null) {
            try {
                if (source.typeId===pet) {
                    source.runCommand('scoreboard players add @s tp_pw:pet_EXP 1')
                    if (petEXP.getScore(source) >= defaultEXP * (petLVL.getScore(source))*0.8) { 
                        source.addEffect(`regeneration`, 3, {amplifier:3})
                        source.runCommand('scoreboard players set @s tp_pw:pet_EXP 0')
                        source.runCommand('scoreboard players add @s tp_pw:pet_LVL 1')
                        source.runCommand('scoreboard players add @s[scores={tp_pw:petBooster=1..}] tp_pw:pet_EXP 2')
                        source.runCommand('playsound random.levelup @a ~~~')
                    }
                }
            } catch (error) {
            }
        }
    })
})
function feralLynxAttack1(entity,player,petId) {
    if (entity.isValid()===true){const ownerId = entity.getDynamicProperty('tp_pw:owner'); let owner = null; entity.dimension.getPlayers().forEach(player=>{if(player.id===ownerId){owner=player.nameTag}})
    //entity.runCommand('say Attack1')
    entity.removeTag('tp_pw:attack1')
    system.runTimeout(()=>{
        if (entity.isValid()===true){
            entity.dimension.getEntities({maxDistance:4,location:entity.location,excludeTypes:['minecraft:xp_orb', 'minecraft:item'],excludeFamilies:['ip_pw']}).forEach(target=>{
                if(getPVPmode()===true){if(target===entity||target==player||petId.includes(target.id))return
                    target.applyDamage(4+petLVL.getScore(entity), {cause:'contact',damagingEntity:entity})
                    }else{if(target===entity||target==player||petId.includes(target.id)||aydiPet.includes(target.typeId))return
                        target.applyDamage(4+petLVL.getScore(entity), {cause:'contact',damagingEntity:entity})
                    }
            })
        }
    }, 0.46 * 20)
    system.runTimeout(()=>{
        if (entity.isValid()===true){entity.runCommand(`event entity @s attack0`)}
    }, 1.13 * 20)}return
}
function feralLynxAttack2(entity,player,petId) {
    if (entity.isValid()===true){const ownerId = entity.getDynamicProperty('tp_pw:owner'); let owner = null; entity.dimension.getPlayers().forEach(player=>{if(player.id===ownerId){owner=player.nameTag}})
    //entity.runCommand('say Attack2')
    entity.removeTag('tp_pw:attack2')
    system.runTimeout(()=>{
        if (entity.isValid()===true){
            entity.dimension.getEntities({maxDistance:4,location:entity.location,excludeTypes:['minecraft:xp_orb', 'minecraft:item'],excludeFamilies:['ip_pw']}).forEach(target=>{
                if(getPVPmode()===true){if(target===entity||target==player||petId.includes(target.id))return
                target.applyDamage(7+petLVL.getScore(entity), {cause:'contact',damagingEntity:entity})}else{if(target===entity||target==player||petId.includes(target.id)||aydiPet.includes(target.typeId))return
                    target.applyDamage(7+petLVL.getScore(entity), {cause:'contact',damagingEntity:entity})
                }
            })}
    }, 0.71 * 20)
    system.runTimeout(()=>{
        if (entity.isValid()===true){entity.runCommand(`event entity @s attack0`)}
    }, 1.38 * 20)}return
}
function feralLynxAttack3(entity,player,petId) {
    if (entity.isValid()===true){const ownerId = entity.getDynamicProperty('tp_pw:owner'); let owner = null; entity.dimension.getPlayers().forEach(player=>{if(player.id===ownerId){owner=player.nameTag}})
    //entity.runCommand('say Attack3')
    entity.removeTag('tp_pw:attack3')
    entity.runCommand(`effect @s slowness 2 255 true`)
    const target = {
        x: Math.floor(entity.getRotation().x),
        y: Math.floor(entity.getRotation().y)
    }
    system.runTimeout(()=>{
        const interval = system.runInterval(()=>{
            if (entity.isValid()===true){
                entity.runCommandAsync(`tp @s ^^^0.3 ${target.y} 0 true`)
                entity.dimension.getEntities({maxDistance:4,location:entity.location,excludeTypes:['minecraft:xp_orb', 'minecraft:item'],excludeFamilies:['ip_pw']}).forEach(target=>{
                    if(getPVPmode()===true){if(target===entity||target==player||petId.includes(target.id))return
                    target.applyDamage(7+petLVL.getScore(entity), {cause:'contact',damagingEntity:entity})}else{if(target===entity||target==player||petId.includes(target.id)||aydiPet.includes(target.typeId))return
                        target.applyDamage(7+petLVL.getScore(entity), {cause:'contact',damagingEntity:entity})
                    }
            })} else {
                system.clearRun(interval)
            }
        })
        system.runTimeout(()=>{
            system.clearRun(interval)
        }, 0.75 * 20)
    }, 0.23 * 20)
    system.runTimeout(()=>{
        if (entity.isValid()===true){entity.runCommand(`event entity @s attack0`)}
    }, 1.38 * 20)}return
}

function frostFoxAttack1(entity,player,petId) {
    if (entity.isValid()===true){const ownerId = entity.getDynamicProperty('tp_pw:owner'); let owner = null; entity.dimension.getPlayers().forEach(player=>{if(player.id===ownerId){owner=player.nameTag}})
    //entity.runCommand('say Attack1')
    entity.removeTag('tp_pw:attack1')
    system.runTimeout(()=>{
        if (entity.isValid()===true){
            entity.dimension.getEntities({maxDistance:4,location:entity.location,excludeTypes:['minecraft:xp_orb', 'minecraft:item'],excludeFamilies:['ip_pw']}).forEach(target=>{
                if(getPVPmode()===true){if(target===entity||target==player||petId.includes(target.id))return
                target.applyDamage(4+petLVL.getScore(entity), {cause:'contact',damagingEntity:entity})}else{if(target===entity||target==player||petId.includes(target.id)||aydiPet.includes(target.typeId))return
                    target.applyDamage(4+petLVL.getScore(entity), {cause:'contact',damagingEntity:entity})
                }
            })
        entity.runCommandAsync(`execute at @e[rm=1, r=4, name=!\"${owner}\", family=!tamed] as @e[rm=1, r=4, name=!\"${owner}\", family=!tamed] run particle tp_pw:frost_fox_normal_strike_1 ~~~`)
        entity.runCommandAsync(`execute at @e[rm=1, r=4, name=!\"${owner}\", family=!tamed] as @e[rm=1, r=4, name=!\"${owner}\", family=!tamed] run particle tp_pw:frost_fox_normal_strike_2 ~~~`)
        entity.runCommandAsync(`effect @e[rm=1, r=4, name=!\"${owner}\", family=!tamed] slowness 6 3`)}
    }, 0.46 * 20)
    system.runTimeout(()=>{
        if (entity.isValid()===true){entity.runCommand(`event entity @s attack0`)}
    }, 0.88 * 20)}return
}
function frostFoxAttack2(entity,player,petId) {
    if (entity.isValid()===true){const ownerId = entity.getDynamicProperty('tp_pw:owner'); let owner = null; entity.dimension.getPlayers().forEach(player=>{if(player.id===ownerId){owner=player.nameTag}})
    //entity.runCommand('say Attack1')
    entity.removeTag('tp_pw:attack2')
    entity.runCommand(`effect @s slowness 2 255 true`)
    system.runTimeout(()=>{
        if (entity.isValid()===true){const magic = entity.dimension.spawnEntity(`tp_pw:frost_fox_glacial_burst`, {
            x: entity.location.x,
            y: entity.location.y,
            z: entity.location.z
        });
        magic.runCommand('particle tp_pw:glacial_burst_1')
        magic.runCommand('particle tp_pw:glacial_burst_2')
        magic.runCommand('particle tp_pw:glacial_burst_3')
        const interval = system.runInterval(()=>{
            if (entity.isValid()===true){
                entity.dimension.getEntities({maxDistance:6,location:entity.location,excludeTypes:['minecraft:xp_orb', 'minecraft:item'],excludeFamilies:['ip_pw']}).forEach(target=>{
                    if(getPVPmode()===true){if(target===entity||target==player||petId.includes(target.id))return
                    target.applyDamage(3+petLVL.getScore(entity), {cause:'contact',damagingEntity:entity})}else{if(target===entity||target==player||petId.includes(target.id)||aydiPet.includes(target.typeId))return
                        target.applyDamage(3+petLVL.getScore(entity), {cause:'contact',damagingEntity:entity})
                    }
            })} else {
                system.clearRun(interval)
            }
        })
        system.runTimeout(()=>{
            system.clearRun(interval)
            if (entity.isValid()===true){magic.runCommand(`kill @s`)}
        }, 1 * 20)}
    }, 0.75 * 20)
    system.runTimeout(()=>{
        if (entity.isValid()===true){entity.runCommand(`event entity @s attack0`)}
    }, 1.75 * 20)}return;
}
function frostFoxAttack3(entity,player,petId) {
    if (entity.isValid()===true){const ownerId = entity.getDynamicProperty('tp_pw:owner'); let owner = null; entity.dimension.getPlayers().forEach(player=>{if(player.id===ownerId){owner=player.nameTag}})
    //entity.runCommand('say Attack3')
    entity.removeTag('tp_pw:attack3')
    entity.runCommand(`effect @s slowness 2 255 true`)
    const dir = {
        x: entity.location.x + entity.getViewDirection().x * 20,
        y: entity.location.y + 1,
        z: entity.location.z + entity.getViewDirection().z * 20
    }
    system.runTimeout(()=>{
        if (entity.isValid()===true){entity.runCommand('summon tp_pw:frost_fox_frostbite_dash ~~~ ~~')
        const interval = system.runInterval(()=>{
            if (entity.isValid()===true){
                entity.dimension.getEntities({maxDistance:4,location:entity.location,excludeTypes:['minecraft:xp_orb', 'minecraft:item'],excludeFamilies:['ip_pw']}).forEach(target=>{
                    if(getPVPmode()===true){if(target===entity||target==player||petId.includes(target.id))return
                    target.applyDamage(7+petLVL.getScore(entity), {cause:'contact',damagingEntity:entity})}else{if(target===entity||target==player||petId.includes(target.id)||aydiPet.includes(target.typeId))return
                        target.applyDamage(7+petLVL.getScore(entity), {cause:'contact',damagingEntity:entity})
                    }
                })
                entity.runCommandAsync(`setblock ~~-1~ ice`)}else{
                system.clearRun(interval)
            }
        })
        entity.applyKnockback(entity.getViewDirection().x * 1, entity.getViewDirection().z * 1, 3, 0)
        system.runTimeout(()=>{
            system.clearRun(interval)
        }, 1.13 * 20)}
    }, 0.38 * 20)
    system.runTimeout(()=>{
        if (entity.isValid()===true){entity.runCommand(`event entity @s attack0`)}
    }, 1.38 * 20)}
}
function frostFoxHowling(entity,player,petId) {
    if (entity.isValid()===true){const ownerId = entity.getDynamicProperty('tp_pw:owner'); let owner = null; entity.dimension.getPlayers().forEach(player=>{if(player.id===ownerId){owner=player.nameTag}})
    entity.runCommand('effect @s slowness 2 255 true')
    entity.removeTag('tp_pw:howling')
    system.runTimeout(()=>{
        if (entity.isValid()===true){
            entity.dimension.getEntities({maxDistance:6,location:entity.location,excludeTypes:['minecraft:xp_orb', 'minecraft:item'],excludeFamilies:['ip_pw']}).forEach(target=>{
                if(getPVPmode()===true){if(target===entity||target==player||petId.includes(target.id))return
                target.applyDamage(10+petLVL.getScore(entity), {cause:'contact',damagingEntity:entity})}else{if(target===entity||target==player||petId.includes(target.id)||aydiPet.includes(target.typeId))return
                    target.applyDamage(10+petLVL.getScore(entity), {cause:'contact',damagingEntity:entity})
                }
            })
        entity.runCommand(`effect @e[rm=1, r=6, name=!\"${owner}\", family=!tamed] slowness 10 10`)}
    }, 0.75 * 20)
    system.runTimeout(()=>{
        if (entity.isValid()===true){entity.runCommand(`event entity @s attack0`)}
    }, 1.25 * 20)}
}

function musicGolemAttack1(entity,player,petId) {
    if (entity.isValid()===true){const ownerId = entity.getDynamicProperty('tp_pw:owner'); let owner = null; entity.dimension.getPlayers().forEach(player=>{if(player.id===ownerId){owner=player.nameTag}})
    entity.removeTag('tp_pw:attack1')
    system.runTimeout(()=>{
        if (entity.isValid()===true){
            entity.dimension.getEntities({maxDistance:4,location:entity.location,excludeTypes:['minecraft:xp_orb', 'minecraft:item'],excludeFamilies:['ip_pw']}).forEach(target=>{
                if(getPVPmode()===true){if(target===entity||target==player||petId.includes(target.id))return
                target.applyDamage(4+petLVL.getScore(entity), {cause:'contact',damagingEntity:entity})}else{if(target===entity||target==player||petId.includes(target.id)||aydiPet.includes(target.typeId))return
                    target.applyDamage(4+petLVL.getScore(entity), {cause:'contact',damagingEntity:entity})
                }
            })}
    }, 0.29 * 20)
    system.runTimeout(()=>{
        if (entity.isValid()===true){entity.runCommand(`event entity @s attack0`)}
    }, 1.0 * 20)}return
}
function musicGolemAttack2(entity,player,petId) {
    if (entity.isValid()===true){const ownerId = entity.getDynamicProperty('tp_pw:owner'); let owner = null; entity.dimension.getPlayers().forEach(player=>{if(player.id===ownerId){owner=player.nameTag}})
    entity.removeTag('tp_pw:attack2')
    system.runTimeout(()=>{
        if (entity.isValid()===true){
            entity.dimension.getEntities({maxDistance:4,location:entity.location,excludeTypes:['minecraft:xp_orb', 'minecraft:item'],excludeFamilies:['ip_pw']}).forEach(target=>{
                if(getPVPmode()===true){if(target===entity||target==player||petId.includes(target.id))return
                target.applyDamage(4+petLVL.getScore(entity), {cause:'contact',damagingEntity:entity})}else{if(target===entity||target==player||petId.includes(target.id)||aydiPet.includes(target.typeId))return
                    target.applyDamage(4+petLVL.getScore(entity), {cause:'contact',damagingEntity:entity})
                }
            })}
    }, 0.56 * 20)
    system.runTimeout(()=>{
        if (entity.isValid()===true){entity.runCommand(`event entity @s attack0`)}
    }, 1.25 * 20)}
}
function musicGolemAttack3(entity,player,petId) {
    if (entity.isValid()===true){const ownerId = entity.getDynamicProperty('tp_pw:owner'); let owner = null; entity.dimension.getPlayers().forEach(player=>{if(player.id===ownerId){owner=player.nameTag}})
    entity.removeTag('tp_pw:attack3')
    system.runTimeout(()=>{
        const interval = system.runInterval(()=>{
            if (entity.isValid()===true){
                entity.dimension.getEntities({maxDistance:4,location:entity.location,excludeTypes:['minecraft:xp_orb', 'minecraft:item'],excludeFamilies:['ip_pw']}).forEach(target=>{
                    if(getPVPmode()===true){if(target===entity||target==player||petId.includes(target.id))return
                    target.applyDamage(4+petLVL.getScore(entity), {cause:'contact',damagingEntity:entity})}else{if(target===entity||target==player||petId.includes(target.id)||aydiPet.includes(target.typeId))return
                        target.applyDamage(4+petLVL.getScore(entity), {cause:'contact',damagingEntity:entity})
                    }
                })}else{system.clearRun(interval)}
        })
        system.runTimeout(()=>{
            system.clearRun(interval)
        }, 1.21 * 20)
    }, 0.38 * 20)
    system.runTimeout(()=>{
        if (entity.isValid()===true){entity.runCommand(`event entity @s attack0`)}
    }, 1.0 * 20)}
}
function musicGolemAttack4(entity,player,petId) {
    if (entity.isValid()===true){const ownerId = entity.getDynamicProperty('tp_pw:owner'); let owner = null; entity.dimension.getPlayers().forEach(player=>{if(player.id===ownerId){owner=player.nameTag}})
    entity.removeTag('tp_pw:attack4')
    const dir = {
        x: entity.location.x + entity.getViewDirection().x * 20,
        y: entity.location.y,
        z: entity.location.z + entity.getViewDirection().z * 20
    }
    system.runTimeout(()=>{
        const interval = system.runInterval(()=>{
            if (entity.isValid()===true){
                entity.dimension.getEntities({maxDistance:4,location:entity.location,excludeTypes:['minecraft:xp_orb', 'minecraft:item'],excludeFamilies:['ip_pw']}).forEach(target=>{
                    if(getPVPmode()===true){if(target===entity||target==player||petId.includes(target.id))return
                    target.applyDamage(4+petLVL.getScore(entity), {cause:'contact',damagingEntity:entity})}else{if(target===entity||target==player||petId.includes(target.id)||aydiPet.includes(target.typeId))return
                        target.applyDamage(4+petLVL.getScore(entity), {cause:'contact',damagingEntity:entity})
                    }
                })
            entity.runCommandAsync(`tp @s ^^^0.4 facing ${dir.x} ${dir.y} ${dir.z} true`)}else{system.clearRun(interval)}
        })
        system.runTimeout(()=>{
            system.clearRun(interval)
        }, 0.8 * 20)
    }, 0.1 * 20)
    system.runTimeout(()=>{
        if (entity.isValid()===true){entity.runCommand(`event entity @s attack0`)}
    }, 1.625 * 20)}
}

function flameHawkAttack1(entity,player,petId) {
    if (entity.isValid() === true) {
        const ownerId = entity.getDynamicProperty('tp_pw:owner'); let owner = null; entity.dimension.getPlayers().forEach(player=>{if(player.id===ownerId){owner=player.nameTag}})
    entity.removeTag('tp_pw:attack1')
    entity.runCommandAsync(`tp @s ^^1^-1`)
    entity.runCommand(`effect @s slowness 2 255 true`)
    const dir = {
        x: entity.location.x + entity.getViewDirection().x * 20,
        y: entity.location.y + entity.getViewDirection().y * 5,
        z: entity.location.z + entity.getViewDirection().z * 20
    }
    system.runTimeout(()=>{
        if (entity.isValid()===true){const interval = system.runInterval(()=>{
            if (entity.isValid()===true){entity.runCommandAsync(`tp @s ^^^0.3 facing ${dir.x} ${dir.y} ${dir.z} true`)}
            if (entity.isValid()===true){
                entity.dimension.getEntities({maxDistance:4,location:entity.location,excludeTypes:['minecraft:xp_orb', 'minecraft:item'],excludeFamilies:['ip_pw']}).forEach(target=>{
                    if(getPVPmode()===true){if(target===entity||target==player||petId.includes(target.id))return
                    target.applyDamage(4+petLVL.getScore(entity), {cause:'contact',damagingEntity:entity})}else{if(target===entity||target==player||petId.includes(target.id)||aydiPet.includes(target.typeId))return
                        target.applyDamage(4+petLVL.getScore(entity), {cause:'contact',damagingEntity:entity})
                    }
                })}else{system.clearRun(interval)}
        })
        system.runTimeout(()=>{
            system.clearRun(interval)
        }, 0.93 * 20)
    } return;
    }, 0.2 * 20)
    system.runTimeout(()=>{
        if (entity.isValid()===true){entity.runCommand(`event entity @s attack0`)}
    }, 1.5 * 20)
    } return;
}
function flameHawkAttack2(entity,player,petId) {
    if (entity.isValid()===true){
        const ownerId = entity.getDynamicProperty('tp_pw:owner'); let owner = null; entity.dimension.getPlayers().forEach(player=>{if(player.id===ownerId){owner=player.nameTag}})
    entity.removeTag('tp_pw:attack2')
    entity.runCommandAsync(`tp @s ^^2^-1`)
    entity.runCommand(`effect @s slowness 2 255 true`)
    const dir = {
        x: entity.location.x + entity.getViewDirection().x * 20,
        y: entity.location.y + entity.getViewDirection().y * 5,
        z: entity.location.z + entity.getViewDirection().z * 20
    }
    system.runTimeout(()=>{
        if (entity.isValid()===true){const interval = system.runInterval(()=>{
            entity.runCommandAsync(`tp @s ^^^0.6 facing ${dir.x} ${dir.y} ${dir.z} true`)
            entity.dimension.getEntities({maxDistance:4,location:entity.location,excludeTypes:['minecraft:xp_orb', 'minecraft:item'],excludeFamilies:['ip_pw']}).forEach(target=>{
                if(getPVPmode()===true){if(target===entity||target==player||petId.includes(target.id))return
                target.applyDamage(4+petLVL.getScore(entity), {cause:'contact',damagingEntity:entity})}else{if(target===entity||target==player||petId.includes(target.id)||aydiPet.includes(target.typeId))return
                    target.applyDamage(4+petLVL.getScore(entity), {cause:'contact',damagingEntity:entity})
                }
            })
        })
        system.runTimeout(()=>{
            system.clearRun(interval)
        }, 0.7 * 20)}return;
    }, 0.4 * 20)
    system.runTimeout(()=>{
        if (entity.isValid()===true){entity.runCommand(`event entity @s attack0`)}
    }, 1.63 * 20)} return;
}
function flameHawkAttack3(entity,player,petId) {
    if (entity.isValid()===true){
        const ownerId = entity.getDynamicProperty('tp_pw:owner'); let owner = null; entity.dimension.getPlayers().forEach(player=>{if(player.id===ownerId){owner=player.nameTag}})
    entity.removeTag('tp_pw:attack3')
    entity.runCommand(`effect @s slowness 2 255 true`)
    system.runTimeout(()=>{
        if (entity.isValid()===true){
            entity.dimension.getEntities({maxDistance:4,location:entity.location,excludeTypes:['minecraft:xp_orb', 'minecraft:item'],excludeFamilies:['ip_pw']}).forEach(target=>{
                if(getPVPmode()===true){if(target===entity||target==player||petId.includes(target.id))return
                target.applyDamage(4+petLVL.getScore(entity), {cause:'contact',damagingEntity:entity})}else{if(target===entity||target==player||petId.includes(target.id)||aydiPet.includes(target.typeId))return
                    target.applyDamage(4+petLVL.getScore(entity), {cause:'contact',damagingEntity:entity})
                }
            })}
    }, 1.0 * 20)
    system.runTimeout(()=>{
        if (entity.isValid()===true){
            entity.dimension.getEntities({maxDistance:4,location:entity.location,excludeTypes:['minecraft:xp_orb', 'minecraft:item'],excludeFamilies:['ip_pw']}).forEach(target=>{
                if(getPVPmode()===true){if(target===entity||target==player||petId.includes(target.id))return
                target.applyDamage(4+petLVL.getScore(entity), {cause:'contact',damagingEntity:entity})}else{if(target===entity||target==player||petId.includes(target.id)||aydiPet.includes(target.typeId))return
                    target.applyDamage(4+petLVL.getScore(entity), {cause:'contact',damagingEntity:entity})
                }
            })}
    }, 1.63 * 20)
    system.runTimeout(()=>{
        if (entity.isValid()===true){entity.runCommand(`event entity @s attack0`)}return;
    }, 2.0 * 20)} return;
}
function flameHawkAttack4(entity,player,petId) {
    if (entity.isValid()===true){const ownerId = entity.getDynamicProperty('tp_pw:owner'); let owner = null; entity.dimension.getPlayers().forEach(player=>{if(player.id===ownerId){owner=player.nameTag}})
    entity.removeTag('tp_pw:attack4')
    entity.runCommand(`effect @s slowness 2 255 true`)
    system.runTimeout(()=>{
        if (entity.isValid()===true){const interval = system.runInterval(()=>{
            entity.runCommandAsync(`tp @s ~~1~`)
            entity.dimension.getEntities({maxDistance:4,location:entity.location,excludeTypes:['minecraft:xp_orb', 'minecraft:item'],excludeFamilies:['ip_pw']}).forEach(target=>{
                if(getPVPmode()===true){if(target===entity||target==player||petId.includes(target.id))return
                target.applyDamage(4+petLVL.getScore(entity), {cause:'contact',damagingEntity:entity})}else{if(target===entity||target==player||petId.includes(target.id)||aydiPet.includes(target.typeId))return
                    target.applyDamage(4+petLVL.getScore(entity), {cause:'contact',damagingEntity:entity})
                }
            })
        })
        system.runTimeout(()=>{
            system.clearRun(interval)
        }, 0.46 * 20)}return;
    }, 0.3 * 20)
    system.runTimeout(()=>{
        if (entity.isValid()===true){entity.runCommand(`event entity @s attack0`)}
    }, 1.5 * 20)} return;
}

function nebulaDragonAttack1(entity,player,petId) {
    const ownerId = entity.getDynamicProperty('tp_pw:owner'); let owner = null; entity.dimension.getPlayers().forEach(player=>{if(player.id===ownerId){owner=player.nameTag}})
    entity.removeTag('tp_pw:attack1')
    system.runTimeout(()=>{
        entity.dimension.getEntities({maxDistance:4,location:entity.location,excludeTypes:['minecraft:xp_orb', 'minecraft:item'],excludeFamilies:['ip_pw']}).forEach(target=>{
            if(getPVPmode()===true){if(target===entity||target==player||petId.includes(target.id))return
            target.applyDamage(8+petLVL.getScore(entity), {cause:'contact',damagingEntity:entity})}else{if(target===entity||target==player||petId.includes(target.id)||aydiPet.includes(target.typeId))return
                target.applyDamage(8+petLVL.getScore(entity), {cause:'contact',damagingEntity:entity})
            }
        })
    }, 0.33 * 20)
    system.runTimeout(()=>{
        entity.runCommand(`event entity @s attack0`)
    }, 1.0 * 20)
}
function nebulaDragonAttack2(entity,player,petId) {
    const ownerId = entity.getDynamicProperty('tp_pw:owner'); let owner = null; entity.dimension.getPlayers().forEach(player=>{if(player.id===ownerId){owner=player.nameTag}})
    entity.removeTag('tp_pw:attack2')
    system.runTimeout(()=>{
        const magic = entity.dimension.spawnEntity(`tp_pw:nebula_dragon_stellar_pulse`, {
            x: entity.location.x + entity.getViewDirection().x * 2,
            y: entity.location.y + entity.getViewDirection().y * 2,
            z: entity.location.z + entity.getViewDirection().z * 2
        });
        const dir = entity.getRotation().y
        system.runTimeout(()=>{
            const interval = system.runInterval(()=>{
                magic.runCommandAsync(`tp @s ^^^1 ${dir} ~`)
                entity.dimension.getEntities({maxDistance:3,location:entity.location,excludeTypes:['minecraft:xp_orb', 'minecraft:item'],excludeFamilies:['ip_pw']}).forEach(target=>{
                    if(getPVPmode()===true){if(target===entity||target==player||petId.includes(target.id))return
                    target.applyDamage(30+petLVL.getScore(entity), {cause:'contact',damagingEntity:entity})}else{if(target===entity||target==player||petId.includes(target.id)||aydiPet.includes(target.typeId))return
                        target.applyDamage(30+petLVL.getScore(entity), {cause:'contact',damagingEntity:entity})
                    }
                })
            })
            system.runTimeout(()=>{
                system.clearRun(interval)
                magic.runCommand('event entity @s despawn')
            }, 1 * 20)
        })
    }, 0.67 * 20)
    system.runTimeout(()=>{
        entity.runCommand(`event entity @s attack0`)
    }, 1.25 * 20)
}
function nebulaDragonAttack3(entity,player,petId) {
    const ownerId = entity.getDynamicProperty('tp_pw:owner'); let owner = null; entity.dimension.getPlayers().forEach(player=>{if(player.id===ownerId){owner=player.nameTag}})
    entity.removeTag('tp_pw:attack3')
    entity.addTag('tp_pw:nebula_dragon_shield')
    system.runTimeout(()=>{
        entity.removeTag('tp_pw:nebula_dragon_shield')
    }, 10 * 20)
    system.runTimeout(()=>{
        entity.runCommand(`event entity @s attack0`)
    }, 1.25 * 20)
}
function nebulaDragonAttack4(entity,player,petId) {
    const ownerId = entity.getDynamicProperty('tp_pw:owner'); let owner = null; entity.dimension.getPlayers().forEach(player=>{if(player.id===ownerId){owner=player.nameTag}})
    entity.removeTag('tp_pw:attack4')
    system.runTimeout(()=>{
        const magic = entity.dimension.spawnEntity(('tp_pw:nebula_dragon_meteor_shower'), {
            x: entity.location.x + entity.getViewDirection().x * 6,
            y: entity.location.y,
            z: entity.location.z + entity.getViewDirection().z * 6
        })
        const interval = system.runInterval(()=>{
            entity.dimension.getEntities({maxDistance:4,location:entity.location,excludeTypes:['minecraft:xp_orb', 'minecraft:item'],excludeFamilies:['ip_pw']}).forEach(target=>{
                if(getPVPmode()===true){if(target===entity||target==player||petId.includes(target.id))return
                target.applyDamage(10+petLVL.getScore(entity), {cause:'contact',damagingEntity:entity})}else{if(target===entity||target==player||petId.includes(target.id)||aydiPet.includes(target.typeId))return
                    target.applyDamage(10+petLVL.getScore(entity), {cause:'contact',damagingEntity:entity})
                }
            })
        })
        system.runTimeout(()=>{
            system.clearRun(interval)
            magic.runCommand('event entity @s despawn')
        }, 1 * 20)
    }, 1.0 * 20)
    system.runTimeout(()=>{
        entity.runCommand(`event entity @s attack0`)
    }, 1.6 * 20)
}

function stormGriffinAttack1(entity,player,petId) {
    if (entity.isValid()===true){const ownerId = entity.getDynamicProperty('tp_pw:owner'); let owner = null; entity.dimension.getPlayers().forEach(player=>{if(player.id===ownerId){owner=player.nameTag}})
    entity.removeTag('tp_pw:attack1')
    system.runTimeout(()=>{
        if (entity.isValid()===true){
            entity.dimension.getEntities({maxDistance:4,location:entity.location,excludeTypes:['minecraft:xp_orb', 'minecraft:item'],excludeFamilies:['ip_pw']}).forEach(target=>{
                if(getPVPmode()===true){if(target===entity||target==player||petId.includes(target.id))return
                target.applyDamage(10+petLVL.getScore(entity), {cause:'contact',damagingEntity:entity})}else{if(target===entity||target==player||petId.includes(target.id)||aydiPet.includes(target.typeId))return
                    target.applyDamage(10+petLVL.getScore(entity), {cause:'contact',damagingEntity:entity})
                }
            })}
    }, 0.33 * 20)
    system.runTimeout(()=>{
        if (entity.isValid()===true){entity.runCommand(`event entity @s attack0`)}
    }, 1.0 * 20)}
}
function stormGriffinAttack2(entity,player,petId) {
    if (entity.isValid()===true){const ownerId = entity.getDynamicProperty('tp_pw:owner'); let owner = null; entity.dimension.getPlayers().forEach(player=>{if(player.id===ownerId){owner=player.nameTag}})
    entity.removeTag('tp_pw:attack2')
    system.runTimeout(()=>{if (entity.isValid()===true){
        const magic = entity.dimension.spawnEntity(`tp_pw:storm_griffin_thunder_clap`, {
            x: entity.location.x + entity.getViewDirection().x * 2,
            y: entity.location.y + entity.getViewDirection().y * 2,
            z: entity.location.z + entity.getViewDirection().z * 2
        });
        const dir = entity.getRotation().y
        system.runTimeout(()=>{
            const interval = system.runInterval(()=>{
                if (entity.isValid()===true){magic.runCommandAsync(`tp @s ^^^1 ${dir} ~`)
                    entity.dimension.getEntities({maxDistance:3,location:entity.location,excludeTypes:['minecraft:xp_orb', 'minecraft:item'],excludeFamilies:['ip_pw']}).forEach(target=>{
                        if(getPVPmode()===true){if(target===entity||target==player||petId.includes(target.id))return
                        target.applyDamage(30+petLVL.getScore(entity), {cause:'contact',damagingEntity:entity})}else{if(target===entity||target==player||petId.includes(target.id)||aydiPet.includes(target.typeId))return
                            target.applyDamage(30+petLVL.getScore(entity), {cause:'contact',damagingEntity:entity})
                        }
                    })}
            })
            system.runTimeout(()=>{
                system.clearRun(interval)
                if (entity.isValid()===true){magic.runCommand('event entity @s despawn')}
            }, 1 * 20)
        })}
    }, 0.67 * 20)
    system.runTimeout(()=>{
        if (entity.isValid()===true){entity.runCommand(`event entity @s attack0`)}
    }, 1.5 * 20)}
}
function stormGriffinAttack3(entity,player,petId) {
    if (entity.isValid()===true){const ownerId = entity.getDynamicProperty('tp_pw:owner'); let owner = null; entity.dimension.getPlayers().forEach(player=>{if(player.id===ownerId){owner=player.nameTag}})
    entity.removeTag('tp_pw:attack3')
    system.runTimeout(()=>{
        if (entity.isValid()===true){
        entity.dimension.getEntities({maxDistance:7,location:entity.location,excludeTypes:['minecraft:xp_orb', 'minecraft:item'],excludeFamilies:['ip_pw']}).forEach(target=>{
            if(getPVPmode()===true){if(target===entity||target==player||petId.includes(target.id))return
            target.dimension.spawnParticle(`tp_pw:lightning_strike_1`, target.location)
            target.dimension.spawnParticle(`tp_pw:lightning_strike_2`, target.location)
            target.dimension.spawnParticle(`tp_pw:lightning_strike_3`, target.location)
            target.dimension.spawnParticle(`tp_pw:lightning_strike_4`, target.location)
            target.dimension.spawnParticle(`tp_pw:lightning_strike_5`, target.location)
            target.applyDamage(20+petLVL.getScore(entity), {cause:'contact',damagingEntity:entity})}else{if(target===entity||target==player||petId.includes(target.id)||aydiPet.includes(target.typeId))return
                target.dimension.spawnParticle(`tp_pw:lightning_strike_1`, target.location)
                target.dimension.spawnParticle(`tp_pw:lightning_strike_2`, target.location)
                target.dimension.spawnParticle(`tp_pw:lightning_strike_3`, target.location)
                target.dimension.spawnParticle(`tp_pw:lightning_strike_4`, target.location)
                target.dimension.spawnParticle(`tp_pw:lightning_strike_5`, target.location)
                target.applyDamage(20+petLVL.getScore(entity), {cause:'contact',damagingEntity:entity})
            }
        })}
    }, 0.5 * 20)
    system.runTimeout(()=>{
        if (entity.isValid()===true){entity.runCommand(`event entity @s attack0`)}
    }, 1.25 * 20)}
}
function stormGriffinAttack4(entity,player,petId) {
    if (entity.isValid()===true){const ownerId = entity.getDynamicProperty('tp_pw:owner'); let owner = null; entity.dimension.getPlayers().forEach(player=>{if(player.id===ownerId){owner=player.nameTag}})
    entity.removeTag('tp_pw:attack4')
    system.runTimeout(()=>{
        if (entity.isValid()===true){
            entity.dimension.getEntities({maxDistance:4,location:entity.location,excludeTypes:['minecraft:xp_orb', 'minecraft:item'],excludeFamilies:['ip_pw']}).forEach(target=>{
                if(getPVPmode()===true){if(target===entity||target==player||petId.includes(target.id))return
                target.applyDamage(4+petLVL.getScore(entity), {cause:'contact',damagingEntity:entity})}else{if(target===entity||target==player||petId.includes(target.id)||aydiPet.includes(target.typeId))return
                    target.applyDamage(4+petLVL.getScore(entity), {cause:'contact',damagingEntity:entity})
                }
            })}
    }, 0.54 * 20)
    system.runTimeout(()=>{
        if (entity.isValid()===true){entity.runCommand(`event entity @s attack0`)}
    }, 1.25 * 20)}
}

function untamedFeralLynxAttack1(entity) {if(entity.isValid()===true){
    //entity.runCommand('say Attack1')
    entity.removeTag('tp_pw:attack1')
    system.runTimeout(()=>{
        if (entity.isValid()===true){entity.runCommandAsync(`damage @e[rm=1, r=4, type=!${entity.typeId}] 4 contact`)}
    }, 0.46 * 20)
    system.runTimeout(()=>{
        if (entity.isValid()===true){entity.runCommand(`event entity @s attack0`)}
    }, 1.13 * 20)}
}
function untamedFeralLynxAttack2(entity) {if(entity.isValid()===true){
    //entity.runCommand('say Attack2')
    entity.removeTag('tp_pw:attack2')
    system.runTimeout(()=>{
        if (entity.isValid()===true){entity.runCommandAsync(`damage @e[rm=1, r=4, type=!${entity.typeId}] 7 contact`)}
    }, 0.71 * 20)
    system.runTimeout(()=>{
        if (entity.isValid()===true){entity.runCommand(`event entity @s attack0`)}
    }, 1.38 * 20)}return
}
function untamedFeralLynxAttack3(entity) {if(entity.isValid()===true){
    //entity.runCommand('say Attack3')
    entity.removeTag('tp_pw:attack3')
    entity.runCommand(`effect @s slowness 2 255 true`)
    const target = {
        x: Math.floor(entity.getRotation().x),
        y: Math.floor(entity.getRotation().y)
    }
    system.runTimeout(()=>{
        const interval = system.runInterval(()=>{
            if (entity.isValid()===true){entity.runCommandAsync(`tp @s ^^^0.3 ${target.y} 0 true`)}
            if (entity.isValid()===true){entity.runCommand(`damage @e[rm=1, r=4, type=!${entity.typeId}] 7 contact`)}
        })
        system.runTimeout(()=>{
            system.clearRun(interval)
        }, 0.75 * 20)
    }, 0.23 * 20)
    system.runTimeout(()=>{
        if (entity.isValid()===true){entity.runCommand(`event entity @s attack0`)}
    }, 1.38 * 20)}
}

function untamedFrostFoxAttack1(entity) {if(entity.isValid()===true){
    //entity.runCommand('say Attack1')
    entity.removeTag('tp_pw:attack1')
    system.runTimeout(()=>{
        if (entity.isValid()===true){entity.runCommandAsync(`damage @e[rm=1, r=4, type=!${entity.typeId}] 4 contact`)
        entity.runCommandAsync(`execute at @e[rm=1, r=4, type=!${entity.typeId}] as @e[rm=1, r=4, type=!${entity.typeId}] run particle tp_pw:frost_fox_normal_strike_1 ~~~`)
        entity.runCommandAsync(`execute at @e[rm=1, r=4, type=!${entity.typeId}] as @e[rm=1, r=4, type=!${entity.typeId}] run particle tp_pw:frost_fox_normal_strike_2 ~~~`)
        entity.runCommandAsync(`effect @e[rm=1, r=4, type=!${entity.typeId}] slowness 6 3`)}
    }, 0.46 * 20)
    system.runTimeout(()=>{
        if (entity.isValid()===true){entity.runCommand(`event entity @s attack0`)}
    }, 0.88 * 20)}return
}
function untamedFrostFoxAttack2(entity) {if(entity.isValid()===true){
    //entity.runCommand('say Attack1')
    entity.removeTag('tp_pw:attack2')
    entity.runCommand(`effect @s slowness 2 255 true`)
    system.runTimeout(()=>{
        if (entity.isValid()===true){const magic = entity.dimension.spawnEntity(`tp_pw:frost_fox_glacial_burst`, {
            x: entity.location.x,
            y: entity.location.y,
            z: entity.location.z
        });
        magic.runCommand('particle tp_pw:glacial_burst_1')
        magic.runCommand('particle tp_pw:glacial_burst_2')
        magic.runCommand('particle tp_pw:glacial_burst_3')
        const interval = system.runInterval(()=>{
            if (entity.isValid()===true){magic.runCommand(`damage @e[r=6, type=!${entity.typeId}, type=!${entity.typeId}] 3 contact`)}
        })
        system.runTimeout(()=>{
            system.clearRun(interval)
            if (entity.isValid()===true){magic.runCommand(`kill @s`)}
        }, 1 * 20)}
    }, 0.75 * 20)
    system.runTimeout(()=>{
        if (entity.isValid()===true){entity.runCommand(`event entity @s attack0`)}
    }, 1.75 * 20)}
}
function untamedFrostFoxAttack3(entity) {if(entity.isValid()===true){
    //entity.runCommand('say Attack3')
    entity.removeTag('tp_pw:attack3')
    entity.runCommand(`effect @s slowness 2 255 true`)
    const dir = {
        x: entity.location.x + entity.getViewDirection().x * 20,
        y: entity.location.y + 1,
        z: entity.location.z + entity.getViewDirection().z * 20
    }
    system.runTimeout(()=>{
        if (entity.isValid()===true){entity.runCommand('summon tp_pw:frost_fox_frostbite_dash ~~~ ~~')
        const interval = system.runInterval(()=>{
            if (entity.isValid()===true){entity.runCommand(`damage @e[rm=1, r=4, type=!${entity.typeId}] 7 contact`)
            entity.runCommandAsync(`setblock ~~-1~ ice`)}
        })
        entity.applyKnockback(entity.getViewDirection().x * 1, entity.getViewDirection().z * 1, 3, 0)
        system.runTimeout(()=>{
            system.clearRun(interval)
        }, 1.13 * 20)}
    }, 0.38 * 20)
    system.runTimeout(()=>{
        if (entity.isValid()===true){entity.runCommand(`event entity @s attack0`)}
    }, 1.38 * 20)}
}
function untamedFrostFoxHowling(entity) {if(entity.isValid()===true){
    entity.runCommand('effect @s slowness 2 255 true')
    entity.removeTag('tp_pw:howling')
    system.runTimeout(()=>{
        if (entity.isValid()===true){entity.runCommand(`damage @e[rm=1, r=6, type=!${entity.typeId}] 10 contact`)
        entity.runCommand(`effect @e[rm=1, r=6, type=!${entity.typeId}] slowness 10 10`)}
    }, 0.75 * 20)
    system.runTimeout(()=>{
        if (entity.isValid()===true){entity.runCommand(`event entity @s attack0`)}
    }, 1.25 * 20)}
}

function untamedMusicGolemAttack1(entity) {if(entity.isValid()===true){
    entity.removeTag('tp_pw:attack1')
    system.runTimeout(()=>{
        if (entity.isValid()===true){entity.runCommandAsync(`damage @e[rm=1, r=4, type=!${entity.typeId}] 4 contact`)}
    }, 0.29 * 20)
    system.runTimeout(()=>{
        if (entity.isValid()===true){entity.runCommand(`event entity @s attack0`)}
    }, 1.0 * 20)}return
}
function untamedMusicGolemAttack2(entity) {if(entity.isValid()===true){
    entity.removeTag('tp_pw:attack2')
    system.runTimeout(()=>{
        if (entity.isValid()===true){entity.runCommandAsync(`damage @e[rm=1, r=4, type=!${entity.typeId}] 4 contact`)}
    }, 0.56 * 20)
    system.runTimeout(()=>{
        if (entity.isValid()===true){entity.runCommand(`event entity @s attack0`)}
    }, 1.25 * 20)}
}
function untamedMusicGolemAttack3(entity) {if(entity.isValid()===true){
    entity.removeTag('tp_pw:attack3')
    system.runTimeout(()=>{
        const interval = system.runInterval(()=>{
            if (entity.isValid()===true){entity.runCommandAsync(`damage @e[rm=1, r=4, type=!${entity.typeId}] 4 contact`)}
        })
        system.runTimeout(()=>{
            system.clearRun(interval)
        }, 1.21 * 20)
    }, 0.38 * 20)
    system.runTimeout(()=>{
        if (entity.isValid()===true){entity.runCommand(`event entity @s attack0`)}
    }, 1.0 * 20)}
}
function untamedMusicGolemAttack4(entity) {if(entity.isValid()===true){
    entity.removeTag('tp_pw:attack4')
    const dir = {
        x: entity.location.x + entity.getViewDirection().x * 20,
        y: entity.location.y,
        z: entity.location.z + entity.getViewDirection().z * 20
    }
    system.runTimeout(()=>{
        const interval = system.runInterval(()=>{
            if (entity.isValid()===true){entity.runCommandAsync(`damage @e[rm=1, r=4, type=!${entity.typeId}] 4 contact`)
            entity.runCommandAsync(`tp @s ^^^0.4 facing ${dir.x} ${dir.y} ${dir.z} true`)}
        })
        system.runTimeout(()=>{
            system.clearRun(interval)
        }, 0.8 * 20)
    }, 0.1 * 20)
    system.runTimeout(()=>{
        if (entity.isValid()===true){entity.runCommand(`event entity @s attack0`)}
    }, 1.625 * 20)}
}

function untamedFlameHawkAttack1(entity) {if(entity.isValid()===true){
        
    entity.removeTag('tp_pw:attack1')
    entity.runCommandAsync(`tp @s ^^1^-1`)
    entity.runCommand(`effect @s slowness 2 255 true`)
    const dir = {
        x: entity.location.x + entity.getViewDirection().x * 20,
        y: entity.location.y + entity.getViewDirection().y * 5,
        z: entity.location.z + entity.getViewDirection().z * 20
    }
    system.runTimeout(()=>{
        if (entity.isValid()===true){const interval = system.runInterval(()=>{
            if (entity.isValid()===true){entity.runCommandAsync(`tp @s ^^^0.3 facing ${dir.x} ${dir.y} ${dir.z} true`)}
            if (entity.isValid()===true){entity.runCommandAsync(`damage @e[rm=1, r=4, type=!${entity.typeId}] 4 contact`)}
        })
        system.runTimeout(()=>{
            system.clearRun(interval)
        }, 0.93 * 20)
    } return;
    }, 0.2 * 20)
    system.runTimeout(()=>{
        if (entity.isValid()===true){entity.runCommand(`event entity @s attack0`)}
    }, 1.5 * 20)
    } return;
}
function untamedFlameHawkAttack2(entity) {if(entity.isValid()===true){
        
    entity.removeTag('tp_pw:attack2')
    entity.runCommandAsync(`tp @s ^^2^-1`)
    entity.runCommand(`effect @s slowness 2 255 true`)
    const dir = {
        x: entity.location.x + entity.getViewDirection().x * 20,
        y: entity.location.y + entity.getViewDirection().y * 5,
        z: entity.location.z + entity.getViewDirection().z * 20
    }
    system.runTimeout(()=>{
        if (entity.isValid()===true){const interval = system.runInterval(()=>{
            entity.runCommandAsync(`tp @s ^^^0.6 facing ${dir.x} ${dir.y} ${dir.z} true`)
            entity.runCommandAsync(`damage @e[rm=1, r=4, type=!${entity.typeId}] 4 contact`)
        })
        system.runTimeout(()=>{
            system.clearRun(interval)
        }, 0.7 * 20)}return;
    }, 0.4 * 20)
    system.runTimeout(()=>{
        if (entity.isValid()===true){entity.runCommand(`event entity @s attack0`)}
    }, 1.63 * 20)} return;
}
function untamedFlameHawkAttack3(entity) {if(entity.isValid()===true){
        
    entity.removeTag('tp_pw:attack3')
    entity.runCommand(`effect @s slowness 2 255 true`)
    system.runTimeout(()=>{
        if (entity.isValid()===true){entity.runCommandAsync(`damage @e[rm=1, r=4, type=!${entity.typeId}] 4 contact`)}
    }, 1.0 * 20)
    system.runTimeout(()=>{
        if (entity.isValid()===true){entity.runCommandAsync(`damage @e[rm=1, r=4, type=!${entity.typeId}] 4 contact`)}
    }, 1.63 * 20)
    system.runTimeout(()=>{
        if (entity.isValid()===true){entity.runCommand(`event entity @s attack0`)}return;
    }, 2.0 * 20)} return;
}
function untamedFlameHawkAttack4(entity) {if(entity.isValid()===true){
    entity.removeTag('tp_pw:attack4')
    entity.runCommand(`effect @s slowness 2 255 true`)
    system.runTimeout(()=>{
        if (entity.isValid()===true){const interval = system.runInterval(()=>{
            entity.runCommandAsync(`tp @s ~~1~`)
            entity.runCommandAsync(`damage @e[rm=1, r=4, type=!${entity.typeId}] 4 contact`)
        })
        system.runTimeout(()=>{
            system.clearRun(interval)
        }, 0.46 * 20)}return;
    }, 0.3 * 20)
    system.runTimeout(()=>{
        if (entity.isValid()===true){entity.runCommand(`event entity @s attack0`)}
    }, 1.5 * 20)} return;
}

function untamedNebulaDragonAttack1(entity) {if(entity.isValid()===true){
    entity.removeTag('tp_pw:attack1')
    system.runTimeout(()=>{
        entity.runCommandAsync(`damage @e[rm=1, r=4, type=!${entity.typeId}] 4 contact`)
    }, 0.33 * 20)
    system.runTimeout(()=>{
        entity.runCommand(`event entity @s attack0`)
    }, 1.0 * 20)}
}
function untamedNebulaDragonAttack2(entity) {if(entity.isValid()===true){
    entity.removeTag('tp_pw:attack2')
    system.runTimeout(()=>{
        const magic = entity.dimension.spawnEntity(`tp_pw:nebula_dragon_stellar_pulse`, {
            x: entity.location.x + entity.getViewDirection().x * 2,
            y: entity.location.y + entity.getViewDirection().y * 2,
            z: entity.location.z + entity.getViewDirection().z * 2
        });
        const dir = entity.getRotation().y
        system.runTimeout(()=>{
            const interval = system.runInterval(()=>{
                magic.runCommandAsync(`tp @s ^^^1 ${dir} ~`)
                magic.runCommand(`damage @e[r=3, type=!${entity.typeId}, type=!${magic.typeId}, type=!${entity.typeId}] 30 contact`)
            })
            system.runTimeout(()=>{
                system.clearRun(interval)
                magic.runCommand('event entity @s despawn')
            }, 1 * 20)
        })
    }, 0.67 * 20)
    system.runTimeout(()=>{
        entity.runCommand(`event entity @s attack0`)
    }, 1.25 * 20)}
}
function untamedNebulaDragonAttack3(entity) {if(entity.isValid()===true){
    entity.removeTag('tp_pw:attack3')
    entity.addTag('tp_pw:nebula_dragon_shield')
    system.runTimeout(()=>{
        entity.removeTag('tp_pw:nebula_dragon_shield')
    }, 10 * 20)
    system.runTimeout(()=>{
        entity.runCommand(`event entity @s attack0`)
    }, 1.25 * 20)}
}
function untamedNebulaDragonAttack4(entity) {if(entity.isValid()===true){
    entity.removeTag('tp_pw:attack4')
    system.runTimeout(()=>{
        const magic = entity.dimension.spawnEntity(('tp_pw:nebula_dragon_meteor_shower'), {
            x: entity.location.x + entity.getViewDirection().x * 6,
            y: entity.location.y,
            z: entity.location.z + entity.getViewDirection().z * 6
        })
        const interval = system.runInterval(()=>{
            magic.runCommand(`damage @e[r=4, type=!${entity.typeId}, type=!${magic.typeId}, type=!${entity.typeId}] 10 contact`)
        })
        system.runTimeout(()=>{
            system.clearRun(interval)
            magic.runCommand('event entity @s despawn')
        }, 1 * 20)
    }, 1.0 * 20)
    system.runTimeout(()=>{
        entity.runCommand(`event entity @s attack0`)
    }, 1.6 * 20)}
}

function untamedStormGriffinAttack1(entity) {if(entity.isValid()===true){
    entity.removeTag('tp_pw:attack1')
    system.runTimeout(()=>{
        if (entity.isValid()===true){entity.runCommandAsync(`damage @e[rm=1, r=4, type=!${entity.typeId}] 4 contact`)}
    }, 0.33 * 20)
    system.runTimeout(()=>{
        if (entity.isValid()===true){entity.runCommand(`event entity @s attack0`)}
    }, 1.0 * 20)}
}
function untamedStormGriffinAttack2(entity) {if(entity.isValid()===true){
    entity.removeTag('tp_pw:attack2')
    system.runTimeout(()=>{if (entity.isValid()===true){
        const magic = entity.dimension.spawnEntity(`tp_pw:storm_griffin_thunder_clap`, {
            x: entity.location.x + entity.getViewDirection().x * 2,
            y: entity.location.y + entity.getViewDirection().y * 2,
            z: entity.location.z + entity.getViewDirection().z * 2
        });
        const dir = entity.getRotation().y
        system.runTimeout(()=>{
            const interval = system.runInterval(()=>{
                if (entity.isValid()===true){magic.runCommandAsync(`tp @s ^^^1 ${dir} ~`)
                magic.runCommand(`damage @e[r=3, type=!${entity.typeId}, type=!${magic.typeId}, type=!${entity.typeId}] 30 contact`)}
            })
            system.runTimeout(()=>{
                system.clearRun(interval)
                if (entity.isValid()===true){magic.runCommand('event entity @s despawn')}
            }, 1 * 20)
        })}
    }, 0.67 * 20)
    system.runTimeout(()=>{
        if (entity.isValid()===true){entity.runCommand(`event entity @s attack0`)}
    }, 1.5 * 20)}
}
function untamedStormGriffinAttack3(entity) {if(entity.isValid()===true){
    entity.removeTag('tp_pw:attack3')
    system.runTimeout(()=>{
        if (entity.isValid()===true){entity.runCommandAsync(`execute at @e[r=7, type=!tp_pw:storm_griffin, type=!${entity.typeId}] as @e[r=7, type=!tp_pw:storm_griffin, type=!${entity.typeId}] run particle tp_pw:lightning_strike_1`)
        entity.runCommandAsync(`execute at @e[r=7, type=!tp_pw:storm_griffin, type=!${entity.typeId}] as @e[r=7, type=!tp_pw:storm_griffin, type=!${entity.typeId}] run particle tp_pw:lightning_strike_2`)
        entity.runCommandAsync(`execute at @e[r=7, type=!tp_pw:storm_griffin, type=!${entity.typeId}] as @e[r=7, type=!tp_pw:storm_griffin, type=!${entity.typeId}] run particle tp_pw:lightning_strike_3`)
        entity.runCommandAsync(`execute at @e[r=7, type=!tp_pw:storm_griffin, type=!${entity.typeId}] as @e[r=7, type=!tp_pw:storm_griffin, type=!${entity.typeId}] run particle tp_pw:lightning_strike_4`)
        entity.runCommandAsync(`execute at @e[r=7, type=!tp_pw:storm_griffin, type=!${entity.typeId}] as @e[r=7, type=!tp_pw:storm_griffin, type=!${entity.typeId}] run particle tp_pw:lightning_strike_5`)
        entity.runCommandAsync(`damage @e[r=7, type=!tp_pw:storm_griffin, type=!${entity.typeId}] 20 contact`)}
    }, 0.5 * 20)
    system.runTimeout(()=>{
        if (entity.isValid()===true){entity.runCommand(`event entity @s attack0`)}
    }, 1.25 * 20)}
}
function untamedStormGriffinAttack4(entity) {if(entity.isValid()===true){
    entity.removeTag('tp_pw:attack4')
    system.runTimeout(()=>{
        if (entity.isValid()===true){entity.runCommandAsync(`damage @e[r=4, type=!${entity.typeId}, type=!${magic.typeId}, type=!${entity.typeId}] 4 contact`)}
    }, 0.54 * 20)
    system.runTimeout(()=>{
        if (entity.isValid()===true){entity.runCommand(`event entity @s attack0`)}
    }, 1.25 * 20)}
}


function chooseAttack(entity) {
    const hunger = world.scoreboard.getObjective(`tp_pw:pet_HGR`)
    let executedFunction = null;
    let player = null
    let petId = []
    try {
        const ownerId = entity.getDynamicProperty('tp_pw:owner')
        entity.dimension.getPlayers().forEach(plr=>{
            if(plr.id===ownerId){
                player = plr
            }
            const tamedEntities = plr.dimension.getEntities({tags:[`tp_pw:`+plr.id]})
            tamedEntities.forEach(tamedEntity=>{
                petId.push(tamedEntity.id)
            })
        })
    } catch (error) {
    }
    // Feral Lynx (normal dan untamed)
    if(entity.typeId === 'tp_pw:feral_lynx') {
        if(hunger.getScore(entity) > 50) {
            if(entity.hasTag('tp_pw:attack1')) {
                hunger.setScore(entity, hunger.getScore(entity)-3)
                executedFunction = feralLynxAttack1;
            } else if(entity.hasTag('tp_pw:attack2')) {
                hunger.setScore(entity, hunger.getScore(entity)-4)
                executedFunction = feralLynxAttack2;
            } else if(entity.hasTag('tp_pw:attack3')) {
                hunger.setScore(entity, hunger.getScore(entity)-5)
                executedFunction = feralLynxAttack3;
            } else if(entity.hasTag('tp_pw:howling')) {
                hunger.setScore(entity, hunger.getScore(entity)-6)
                executedFunction = null;
                entity.removeTag('tp_pw:howling')
            }
        } else {
            entity.triggerEvent('tp_pw:passive_mode')
        }
    } else if(entity.typeId === 'tp_pw:feral_lynx_untamed') {
        if(entity.hasTag('tp_pw:attack1')) {
            executedFunction = untamedFeralLynxAttack1;
        } else if(entity.hasTag('tp_pw:attack2')) {
            executedFunction = untamedFeralLynxAttack2;
        } else if(entity.hasTag('tp_pw:attack3')) {
            executedFunction = untamedFeralLynxAttack3;
        } else if(entity.hasTag('tp_pw:howling')) {
            executedFunction = null;
            entity.removeTag('tp_pw:howling')
        }
    }
    // Frost Fox (normal dan untamed)
    else if(entity.typeId === 'tp_pw:frost_fox') {
        if(hunger.getScore(entity) > 50) {
            if(entity.hasTag('tp_pw:attack1')) {
                hunger.setScore(entity, hunger.getScore(entity)-3)
                executedFunction = frostFoxAttack1;
            } else if(entity.hasTag('tp_pw:attack2')) {
                hunger.setScore(entity, hunger.getScore(entity)-4)
                executedFunction = frostFoxAttack2;
            } else if(entity.hasTag('tp_pw:attack3')) {
                hunger.setScore(entity, hunger.getScore(entity)-5)
                executedFunction = frostFoxAttack3;
            } else if(entity.hasTag('tp_pw:howling')) {
                hunger.setScore(entity, hunger.getScore(entity)-6)
                executedFunction = frostFoxHowling;
            }
        } else {
            entity.triggerEvent('tp_pw:passive_mode')
        }
    } else if(entity.typeId === 'tp_pw:frost_fox_untamed') {
        if(entity.hasTag('tp_pw:attack1')) {
            executedFunction = untamedFrostFoxAttack1;
        } else if(entity.hasTag('tp_pw:attack2')) {
            executedFunction = untamedFrostFoxAttack2;
        } else if(entity.hasTag('tp_pw:attack3')) {
            executedFunction = untamedFrostFoxAttack3;
        } else if(entity.hasTag('tp_pw:howling')) {
            executedFunction = untamedFrostFoxHowling;
        }
    }
    // Flame Hawk (normal dan untamed)
    else if(entity.typeId === 'tp_pw:flame_hawk') {
        if(hunger.getScore(entity) > 50) {
            if(entity.hasTag('tp_pw:attack1')) {
                hunger.setScore(entity, hunger.getScore(entity)-3)
                executedFunction = flameHawkAttack1;
            } else if(entity.hasTag('tp_pw:attack2')) {
                hunger.setScore(entity, hunger.getScore(entity)-4)
                executedFunction = flameHawkAttack2;
            } else if(entity.hasTag('tp_pw:attack3')) {
                hunger.setScore(entity, hunger.getScore(entity)-5)
                executedFunction = flameHawkAttack3;
            } else if(entity.hasTag('tp_pw:attack4')) {
                hunger.setScore(entity, hunger.getScore(entity)-6)
                executedFunction = flameHawkAttack4;
            }
        } else {
            entity.triggerEvent('tp_pw:passive_mode')
        }
    } else if(entity.typeId === 'tp_pw:flame_hawk_untamed') {
        if(entity.hasTag('tp_pw:attack1')) {
            executedFunction = untamedFlameHawkAttack1;
        } else if(entity.hasTag('tp_pw:attack2')) {
            executedFunction = untamedFlameHawkAttack2;
        } else if(entity.hasTag('tp_pw:attack3')) {
            executedFunction = untamedFlameHawkAttack3;
        } else if(entity.hasTag('tp_pw:attack4')) {
            executedFunction = untamedFlameHawkAttack4;
        }
    }
    // Music Golem (normal dan untamed)
    else if(entity.typeId === 'tp_pw:music_golem') {
        if(hunger.getScore(entity) > 50) {
            if(entity.hasTag('tp_pw:attack1')) {
                hunger.setScore(entity, hunger.getScore(entity)-3)
                executedFunction = musicGolemAttack1;
            } else if(entity.hasTag('tp_pw:attack2')) {
                hunger.setScore(entity, hunger.getScore(entity)-4)
                executedFunction = musicGolemAttack2;
            } else if(entity.hasTag('tp_pw:attack3')) {
                hunger.setScore(entity, hunger.getScore(entity)-5)
                executedFunction = musicGolemAttack3;
            } else if(entity.hasTag('tp_pw:attack4')) {
                hunger.setScore(entity, hunger.getScore(entity)-6)
                executedFunction = musicGolemAttack4;
            }
        } else {
            entity.triggerEvent('tp_pw:passive_mode')
        }
    } else if(entity.typeId === 'tp_pw:music_golem_untamed') {
        if(entity.hasTag('tp_pw:attack1')) {
            executedFunction = untamedMusicGolemAttack1;
        } else if(entity.hasTag('tp_pw:attack2')) {
            executedFunction = untamedMusicGolemAttack2;
        } else if(entity.hasTag('tp_pw:attack3')) {
            executedFunction = untamedMusicGolemAttack3;
        } else if(entity.hasTag('tp_pw:attack4')) {
            executedFunction = untamedMusicGolemAttack4;
        }
    }
    // Nebula Dragon (normal dan untamed)
    else if(entity.typeId === 'tp_pw:nebula_dragon') {
        if(hunger.getScore(entity) > 50) {
            if(entity.isOnGround){
                if(entity.hasTag('tp_pw:attack1')) {
                    hunger.setScore(entity, hunger.getScore(entity)-3)
                    executedFunction = nebulaDragonAttack1;
                } else if(entity.hasTag('tp_pw:attack2')) {
                    hunger.setScore(entity, hunger.getScore(entity)-4)
                    executedFunction = nebulaDragonAttack2;
                } else if(entity.hasTag('tp_pw:attack3')) {
                    hunger.setScore(entity, hunger.getScore(entity)-5)
                    executedFunction = nebulaDragonAttack3;
                } else if(entity.hasTag('tp_pw:attack4')) {
                    hunger.setScore(entity, hunger.getScore(entity)-6)
                    executedFunction = nebulaDragonAttack4;
                }
            }
        } else {
            entity.triggerEvent('tp_pw:passive_mode')
        }
    } else if(entity.typeId === 'tp_pw:nebula_dragon_untamed') {
        if(entity.isOnGround) {
            if(entity.hasTag('tp_pw:attack1')) {
                executedFunction = untamedNebulaDragonAttack1;
            } else if(entity.hasTag('tp_pw:attack2')) {
                executedFunction = untamedNebulaDragonAttack2;
            } else if(entity.hasTag('tp_pw:attack3')) {
                executedFunction = untamedNebulaDragonAttack3;
            } else if(entity.hasTag('tp_pw:attack4')) {
                executedFunction = untamedNebulaDragonAttack4;
            }
        }
    }
    // Storm Griffin (normal dan untamed)
    else if(entity.typeId === 'tp_pw:storm_griffin') {
        if(hunger.getScore(entity) > 50) {
            if(entity.isOnGround){
            if(entity.hasTag('tp_pw:attack1')) {
                hunger.setScore(entity, hunger.getScore(entity)-3)
                executedFunction = stormGriffinAttack1;
            } else if(entity.hasTag('tp_pw:attack2')) {
                hunger.setScore(entity, hunger.getScore(entity)-4)
                executedFunction = stormGriffinAttack2;
            } else if(entity.hasTag('tp_pw:attack3')) {
                hunger.setScore(entity, hunger.getScore(entity)-5)
                executedFunction = stormGriffinAttack3;
            } else if(entity.hasTag('tp_pw:attack4')) {
                hunger.setScore(entity, hunger.getScore(entity)-6)
                executedFunction = stormGriffinAttack4;
            }
            }
        } else {
            entity.triggerEvent('tp_pw:passive_mode')
        }
    } else if(entity.typeId === 'tp_pw:storm_griffin_untamed') {
        if(entity.isOnGround) {
            if(entity.hasTag('tp_pw:attack1')) {
                executedFunction = untamedStormGriffinAttack1;
            } else if(entity.hasTag('tp_pw:attack2')) {
                executedFunction = untamedStormGriffinAttack2;
            } else if(entity.hasTag('tp_pw:attack3')) {
                executedFunction = untamedStormGriffinAttack3;
            } else if(entity.hasTag('tp_pw:attack4')) {
                executedFunction = untamedStormGriffinAttack4;
            }
        }
    }

    if (executedFunction) {
        executedFunction(entity, player, petId);
    }
}
}