import { system, Player, Entity, BlockPermutation, world, System } from "@minecraft/server";
import { rushAttack, convertRotYtoPosXZ, getEntitiesInFront, fillCommand, shiftLocation, shootProjectile, removeProjectileInBLock, getDistance, damageByEffectStrength, caret, caretZ, rangedAttackForward, shootProjectilePropH, fillCommandDestroy, getEntitiesInRange, applyVarious, getEntitiesDistance, applySelfKnockback, cancelKnockback, isPlayer, spawnExplosion, clearAllSkill } from "./function";
import { getScoreUtility, setScoreUtility } from "./scoreboard";

//Combat//
/**
 * @param {Player} player 
 */
export function punch(player){
    player.runCommand("function punch_side_count");
    player.dimension.spawnParticle("hxc:punch_impact",caret(player.getHeadLocation(),player.getRotation(),[0,0,2]));
    player.dimension.playSound("punch",caret(player.getHeadLocation(),player.getRotation(),[0,0,2]),{volume:2,pitch:1});
    if(player.typeId == "minecraft:player"){
        player.startItemCooldown("combat",50);
    }else{
        player.triggerEvent("hxc:cooldown");
    }
    player.addTag("can_parry");
    system.runTimeout(()=>{
        player.removeTag("can_parry");
    },3);
    system.runTimeout(()=>{
        rangedAttackForward({
            damaging:player,
            range:[3.75,3.75],
            distance:3.75,
            damage:{amount:damageByEffectStrength(player,11,1.3),cause:"entityAttack"},
            knockback:{type:"front",horizontalStrength:3.7,verticalStrength:0.5}
        });
    },2);
}
/**
 * @param {Player} player 
 */
export function punch_right(player){
    if(player.typeId == "minecraft:player"){
        player.playAnimation("animation.player.punch_right",{nextState:"third_person",blendOutTime:0.25,controller:"controller.animation.player.root",stopExpression:"q.any_animation_finished"});
    }else{
        player.playAnimation("animation.player.punch_right",{nextState:"default",blendOutTime:0.25,controller:"controller.animation.mob",stopExpression:"q.any_animation_finished"});
    }
}
/**
 * @param {Player} player 
 */
export function punch_left(player){
    if(player.typeId == "minecraft:player"){
        player.playAnimation("animation.player.punch_left",{nextState:"third_person",blendOutTime:0.25,controller:"controller.animation.player.root",stopExpression:"q.any_animation_finished"});
    }else{
        player.playAnimation("animation.player.punch_left",{nextState:"default",blendOutTime:0.25,controller:"controller.animation.mob",stopExpression:"q.any_animation_finished"});
    }
}
/**
 * @param {Player} player 
 */
export function kick(player){
    player.runCommand("function kick_side_count");
    player.dimension.spawnParticle("hxc:punch_impact",caret(player.getHeadLocation(),player.getRotation(),[0,0,2]));
    player.dimension.playSound("kick",caret(player.getHeadLocation(),player.getRotation(),[0,0,2]),{volume:2,pitch:1});
    if(player.typeId == "minecraft:player"){
        player.startItemCooldown("combat",56);
    }else{
        player.triggerEvent("hxc:cooldown");
    }
    player.addTag("can_parry");
    system.runTimeout(()=>{
        player.removeTag("can_parry");
    },3);
    system.runTimeout(()=>{
        rangedAttackForward({
            damaging:player,
            range:[3.85,3.85],
            distance:3.85,
            damage:{amount:damageByEffectStrength(player,13,1.3),cause:"entityAttack"},
            knockback:{type:"front",horizontalStrength:3.85,verticalStrength:0.6}
        });
    },2);
}
/**
 * @param {Player} player 
 */
export function kick_right(player){
    if(player.typeId == "minecraft:player"){
        player.playAnimation("animation.player.kick_right",{nextState:"third_person",blendOutTime:0.25,controller:"controller.animation.player.root",stopExpression:"q.any_animation_finished"});
    }else{
        player.playAnimation("animation.player.kick_right",{nextState:"default",blendOutTime:0.25,controller:"controller.animation.mob",stopExpression:"q.any_animation_finished"});
    }
}
/**
 * @param {Player} player 
 */
export function kick_left(player){
    if(player.typeId == "minecraft:player"){
        player.playAnimation("animation.player.kick_left",{nextState:"third_person",blendOutTime:0.25,controller:"controller.animation.player.root",stopExpression:"q.any_animation_finished"});
    }else{
        player.playAnimation("animation.player.kick_left",{nextState:"default",blendOutTime:0.25,controller:"controller.animation.mob",stopExpression:"q.any_animation_finished"});
    }
}
/**
 * @param {Player} player 
 */
export function punch2(player){
    player.runCommand("function punch_side_count");
    player.dimension.spawnParticle("hxc:punch_impact2",caret(player.getHeadLocation(),player.getRotation(),[0,0,2]));
    player.dimension.playSound("punch",caret(player.getHeadLocation(),player.getRotation(),[0,0,2]),{volume:1,pitch:1.1});
    player.addTag("can_parry");
    system.runTimeout(()=>{
        rangedAttackForward({
            damaging:player,
            range:[4,4],
            distance:4,
            damage:{amount:damageByEffectStrength(player,5,1.3),cause:"entityAttack"},
            knockback:{type:"front",horizontalStrength:0.05,verticalStrength:0}
        });
    },2);
}
/**
 * @param {Player} player 
 */
export function punch2_final(player){
    player.runCommand("function punch_side_count");
    player.dimension.spawnParticle("hxc:punch_impact",caret(player.getHeadLocation(),player.getRotation(),[0,0,3.25]));
    player.dimension.playSound("punch",caret(player.getHeadLocation(),player.getRotation(),[0,0,3.25]),{volume:2,pitch:1});
    player.addTag("can_parry");
    system.runTimeout(()=>{
        player.removeTag("can_parry");
    },3);
    system.runTimeout(()=>{
        rangedAttackForward({
            damaging:player,
            range:[4.15,4.15],
            distance:4.15,
            damage:{amount:damageByEffectStrength(player,6,1.3),cause:"entityAttack"},
            knockback:{type:"front",horizontalStrength:3.85,verticalStrength:0.58}
        });
    },2);
}
/**
 * @param {Player} player 
 */
export function punch2_end(player){
    player.runCommand("scoreboard players set @s score_test 0");
    player.removeTag("can_parry");
    if(player.typeId == "minecraft:player"){
        player.startItemCooldown("combat",82);
    }else{
        player.triggerEvent("hxc:cooldown");
    }
}
/**
 * @param {Player} player 
 */
export function slash(player){
    player.runCommand("function slash_side_count");
    player.dimension.playSound("sword.slash",caret(player.getHeadLocation(),player.getRotation(),[0,0,2]),{volume:2,pitch:1.5});
    player.dimension.playSound("sword.slash2",caret(player.getHeadLocation(),player.getRotation(),[0,0,2]),{volume:1,pitch:2});
    if(player.typeId == "minecraft:player"){
        player.startItemCooldown("sword",52);
    }else{
        player.triggerEvent("hxc:cooldown");
    }
    player.addTag("can_parry");
    system.runTimeout(()=>{
        player.removeTag("can_parry");
    },3);
    system.runTimeout(()=>{
        rangedAttackForward({
            damaging:player,
            range:[4,4],
            distance:4,
            damage:{amount:damageByEffectStrength(player,15,1.3),cause:"entityAttack",swordDamage:true},
            knockback:{type:"front",horizontalStrength:3.85,verticalStrength:0.5}
        });
        const knockbackTargets = getEntitiesInFront(player,[5,5],5);
        for(const targetEntity of knockbackTargets){
            if(targetEntity.hasTag("can_parry")){
                player.dimension.spawnParticle("hxc:parry_slash1",caret(player.getHeadLocation(),player.getRotation(),[0,0,1]));
                player.dimension.playSound("parry_slash",caret(player.getHeadLocation(),player.getRotation(),[0,0,1]),{volume:1.5,pitch:1.05});
            }
        }
    },2);
}
/**
 * @param {Player} player 
 */
export function slash_right(player){
    if(player.typeId == "minecraft:player"){
        player.playAnimation("animation.player.slash_right",{nextState:"third_person",blendOutTime:0.25,controller:"controller.animation.player.root",stopExpression:"q.any_animation_finished"});
    }else{
        player.playAnimation("animation.player.slash_right",{nextState:"default",blendOutTime:0.25,controller:"controller.animation.mob",stopExpression:"q.any_animation_finished"});
    }
}
/**
 * @param {Player} player 
 */
export function slash_left(player){
    if(player.typeId == "minecraft:player"){
        player.playAnimation("animation.player.slash_left",{nextState:"third_person",blendOutTime:0.25,controller:"controller.animation.player.root",stopExpression:"q.any_animation_finished"});
    }else{
        player.playAnimation("animation.player.slash_left",{nextState:"default",blendOutTime:0.25,controller:"controller.animation.mob",stopExpression:"q.any_animation_finished"});
    }
}
/**
 * @param {Player} player 
 */
export function slash_middle(player){
    if(player.typeId == "minecraft:player"){
        player.playAnimation("animation.player.slash_middle",{nextState:"third_person",blendOutTime:0.25,controller:"controller.animation.player.root",stopExpression:"q.any_animation_finished"});
    }else{
        player.playAnimation("animation.player.slash_middle",{nextState:"default",blendOutTime:0.25,controller:"controller.animation.mob",stopExpression:"q.any_animation_finished"});
    }
}
/**
 * @param {Player} player 
 */
export function slash2(player){
    player.runCommand("function slash_side_count");
    player.dimension.playSound("sword.slash",caret(player.getHeadLocation(),player.getRotation(),[0,0,2]),{volume:1.5,pitch:1.6});
    player.addTag("can_parry");
    system.runTimeout(()=>{
        rangedAttackForward({
            damaging:player,
            range:[4.15,4.15],
            distance:4.15,
            damage:{amount:damageByEffectStrength(player,7,1.3),cause:"entityAttack",swordDamage:true},
            knockback:{type:"front",horizontalStrength:0.05,verticalStrength:0}
        });
        const knockbackTargets = getEntitiesInFront(player,[5,5],5);
        for(const targetEntity of knockbackTargets){
            if(targetEntity.hasTag("can_parry")){
                player.dimension.spawnParticle("hxc:parry_slash1",caret(player.getHeadLocation(),player.getRotation(),[0,0,1]));
                player.dimension.playSound("parry_slash",caret(player.getHeadLocation(),player.getRotation(),[0,0,1]),{volume:1.5,pitch:1.05});
            }
        }
    },2);
}
/**
 * @param {Player} player 
 */
export function slash2_final(player){
    player.runCommand("function slash_side_count");
    player.dimension.playSound("sword.slash",caret(player.getHeadLocation(),player.getRotation(),[0,0,2.25]),{volume:2,pitch:1.5});
    player.dimension.playSound("sword.slash2",caret(player.getHeadLocation(),player.getRotation(),[0,0,2.25]),{volume:3,pitch:1.5});
    player.addTag("can_parry");
    system.runTimeout(()=>{
        player.removeTag("can_parry");
    },3);
    system.runTimeout(()=>{
        rangedAttackForward({
            damaging:player,
            range:[4.25,4.25],
            distance:4.25,
            damage:{amount:damageByEffectStrength(player,8,1.3),cause:"entityAttack",swordDamage:true},
            knockback:{type:"front",horizontalStrength:3.95,verticalStrength:0.6}
        });
        const knockbackTargets = getEntitiesInFront(player,[5.15,5.15],5.15);
        for(const targetEntity of knockbackTargets){
            if(targetEntity.hasTag("can_parry")){
                player.dimension.spawnParticle("hxc:parry_slash1",caret(player.getHeadLocation(),player.getRotation(),[0,0,1]));
                player.dimension.playSound("parry_slash",caret(player.getHeadLocation(),player.getRotation(),[0,0,1]),{volume:1.5,pitch:1.05});
            }
        }
    },2);
}
/**
 * @param {Player} player 
 */
export function slash2_end(player){
    player.runCommand("scoreboard players set @s score_test 0");
    player.removeTag("can_parry");
    if(player.typeId == "minecraft:player"){
        player.startItemCooldown("sword",88);
    }else{
        player.triggerEvent("hxc:cooldown");
    }
}
/**
 * @param {Player} player 
 */
export function slash3(player){
    if(player.typeId == "minecraft:player"){
        player.playAnimation("animation.player.slash_right",{nextState:"third_person",blendOutTime:0.25,controller:"controller.animation.player.root",stopExpression:"q.any_animation_finished"});
    }else{
        player.playAnimation("animation.player.slash_right",{nextState:"default",blendOutTime:0.25,controller:"controller.animation.mob",stopExpression:"q.any_animation_finished"});
    }
    player.addTag("can_parry");
    system.runTimeout(()=>{
        player.removeTag("can_parry");
    },3);
    const knockbackTargets = getEntitiesInFront(player,[4,4],4);
    for(const targetEntity of knockbackTargets){
        applyVarious({
            target:targetEntity,
            damaging:player,
            damage:{amount:damageByEffectStrength(player,12,1.3),cause:"entityAttack",swordDamage:true},
            knockback:{type:"front",horizontalStrength:5,verticalStrength:1}
        });
    }
    const parryTargets = getEntitiesInFront(player,[5,5],5);
    for(const targetEntity of parryTargets){
        if(targetEntity.hasTag("can_parry")){
            player.dimension.spawnParticle("hxc:parry_slash1",caret(player.getHeadLocation(),player.getRotation(),[0,0,1]));
            player.dimension.playSound("parry_slash",caret(player.getHeadLocation(),player.getRotation(),[0,0,1]),{volume:1.5,pitch:1.05});
        }
    }
    shootProjectile(player,"hxc:slash_p",1);
    player.dimension.playSound("sword.slash",caret(player.getHeadLocation(),player.getRotation(),[0,0,2]),{volume:2,pitch:1.5});
    player.dimension.playSound("sword.slash2",caret(player.getHeadLocation(),player.getRotation(),[0,0,2]),{volume:3,pitch:1});
    if(player.typeId == "minecraft:player"){
        player.startItemCooldown("sword",85);
    }else{
        player.triggerEvent("hxc:cooldown2");
    }
}
/**
 * @param {Entity} entity 
 */
export function slash_p(entity){
    if(removeProjectileInBLock(entity)) return;
    const projectileComp = entity.getComponent("projectile");
    if(projectileComp==undefined) return;
    const owner = projectileComp.owner;
    const targets = getEntitiesInRange(owner,shiftLocation(entity.location,-3,-1.5,-2.5),{x:6,y:3,z:5});
    for(const targetEntity of targets){
        if(owner.typeId == "hxc:hazama"){
            applyVarious({
                target:targetEntity,
                damaging:owner,
                damage:{amount:damageByEffectStrength(owner,14,1.3),cause:"entityAttack",swordDamage:true}
            });
        }else{
            applyVarious({
                target:targetEntity,
                damaging:owner,
                damage:{amount:damageByEffectStrength(owner,12,1.3),cause:"entityAttack",swordDamage:true}
            });
        }
    }
}
/**
 * @param {Player} player 
 */
export function slash4_charge(player){
    if(player.typeId == "minecraft:player"){
        player.playAnimation("animation.player.rush_slash_charge",{nextState:"third_person",blendOutTime:0.25,controller:"controller.animation.player.root",stopExpression:"q.any_animation_finished"});
    }else{
        player.playAnimation("animation.player.rush_slash_charge",{nextState:"default",blendOutTime:0.25,controller:"controller.animation.mob",stopExpression:"q.any_animation_finished"});
    }
}
/**
 * @param {Player} player 
 */
export function slash4(player){
    const startLocation = player.location;
    const cos0 = Math.cos(player.getRotation().x * (Math.PI/180));
    const sin0 = Math.sin(player.getRotation().x * (Math.PI/180));
    const horizontalStrength = player.getRotation().x < 0 ? 35 * cos0 : 35;
    const verticalStrength = Math.max(0,-3 * sin0);
    applySelfKnockback(player,horizontalStrength,verticalStrength);
    player.dimension.playSound("sword.rush",player.location);
    if(player.typeId == "minecraft:player"){
        player.startItemCooldown("sword",90);
    }else{
        player.triggerEvent("hxc:cooldown2");
    }
    if(player.typeId == "minecraft:player"){
        player.playAnimation("animation.player.rush_slash",{nextState:"third_person",blendOutTime:0.25,controller:"controller.animation.player.root",stopExpression:"q.any_animation_finished"});
    }else{
        player.playAnimation("animation.player.rush_slash",{nextState:"default",blendOutTime:0.25,controller:"controller.animation.mob",stopExpression:"q.any_animation_finished"});
    }
    system.runTimeout(()=>{
        cancelKnockback(player);
    },2);
    player.addTag("can_parry");
    system.runTimeout(()=>{
        player.removeTag("can_parry");
    },3);
    system.runTimeout(()=>{
        if(!player.isOnGround){
            cancelKnockback(player);
            cancelKnockback(player);
            cancelKnockback(player);
        }
        const stopLocation = player.location;
        rushAttack({
            damaging:player,
            startLocation,
            stopLocation,
            volume:{x:4,y:4,z:4},
            damage:{amount:damageByEffectStrength(player,20,1.3),cause:"entityAttack",swordDamage:true},
            particle:{trailParticleId:"hxc:rush_slash"}
        });
    },2);
}
/**
 * @param {Player} player 
 */
export function smash_charge(player){
    if(player.typeId == "minecraft:player"){
        player.playAnimation("animation.mob.smash_charge",{nextState:"third_person",blendOutTime:0.25,controller:"controller.animation.player.root",stopExpression:"q.any_animation_finished"});
    }else{
        player.playAnimation("animation.mob.smash_charge",{nextState:"default",blendOutTime:0.25,controller:"controller.animation.mob",stopExpression:"q.any_animation_finished"});
    }
}
/**
 * @param {Player} player 
 */
export function smash(player){
    if(player.typeId == "minecraft:player"){
        player.playAnimation("animation.mob.smash_attack",{nextState:"third_person",blendOutTime:0.25,controller:"controller.animation.player.root",stopExpression:"q.any_animation_finished"});
    }else{
        player.playAnimation("animation.mob.smash_attack",{nextState:"default",blendOutTime:0.25,controller:"controller.animation.mob",stopExpression:"q.any_animation_finished"});
    }
    player.dimension.spawnParticle("hxc:smash_attack1",caret(player.getHeadLocation(),player.getRotation(),[0,0.2,0]));
    player.dimension.spawnParticle("hxc:smash_attack2",caret(player.getHeadLocation(),player.getRotation(),[0,0.2,0]));
    player.dimension.spawnParticle("hxc:meruem_tail_attack1",caret(player.getHeadLocation(),player.getRotation(),[0,0.2,0]));
    const crack_loc = shiftLocation(player.location,0,-0.6,0);
    player.dimension.spawnParticle("hxc:crack",crack_loc);
    player.dimension.playSound("random.explode",caret(player.getHeadLocation(),player.getRotation(),[0,0.1,0]),{volume:3,pitch:1.5});
    player.dimension.playSound("punch",caret(player.getHeadLocation(),player.getRotation(),[0,0.1,0]),{volume:2,pitch:1.2});
    player.dimension.playSound("bigbang_impact",caret(player.getHeadLocation(),player.getRotation(),[0,0.1,0]),{volume:2,pitch:1.2});
    if(player.typeId == "minecraft:player"){
        player.startItemCooldown("combat",80);
    }else{
        player.triggerEvent("hxc:cooldown");
    }
    player.runCommand("camerashake add @a[r=12] 0.5 0.5");
    system.runTimeout(()=>{
        player.addTag("can_parry");
    },1);
    system.runTimeout(()=>{
        player.removeTag("can_parry");
    },4);
    system.runTimeout(()=>{
        const loc = player.location;
        for(const targetEntity of getEntitiesInRange(player,shiftLocation(loc,-8,-6,-8),{x:16,y:10,z:16})){
            applyVarious({
                target:targetEntity,
                damaging:player,
                damage:{amount:damageByEffectStrength(player,20,1.3),cause:"entityAttack"},
                knockback:{type:"sphere",horizontalStrength:4.5,verticalStrength:1}
            });
        }
        if(world.gameRules.mobGriefing==true){
            spawnExplosion(player,loc,1,false);
        }
    },2);
}
/**
 * @param {Player} player 
 */
export function dash(player){
    const canDash = player.getProperty("hxc:dash_ct")==false;
    const stunnedComp = player.getComponent("is_stunned");
    if(canDash && stunnedComp==undefined){
        if(player.typeId == "minecraft:player"){
            applySelfKnockback(player,2,0.25);
        }else{
            applySelfKnockback(player,3,0.5);
        }
        player.dimension.spawnParticle("hxc:dash_particle1",player.location);
        player.dimension.spawnParticle("hxc:dash_particle2",player.location);
        player.setProperty("hxc:dash_ct",true);
        if(player.typeId == "minecraft:player"){
            player.playAnimation("animation.player.dash",{nextState:"third_person",blendOutTime:0.25,controller:"controller.animation.player.root",stopExpression:"q.any_animation_finished"});
        }else{
            player.playAnimation("animation.player.dash",{nextState:"default",blendOutTime:0.25,controller:"controller.animation.mob",stopExpression:"q.any_animation_finished"});
        }
        system.runTimeout(()=>{
            player.setProperty("hxc:dash_ct",false);
        },30);
    }
}
/**
 * @param {Player} player 
 */
export function thrust_charge(player){
    if(player.typeId == "minecraft:player"){
        player.playAnimation("animation.player.thrust_charge",{nextState:"third_person",blendOutTime:0.25,controller:"controller.animation.player.root",stopExpression:"q.any_animation_finished"});
    }else{
        player.playAnimation("animation.player.thrust_charge",{nextState:"default",blendOutTime:0.25,controller:"controller.animation.mob",stopExpression:"q.any_animation_finished"});
    }
}
/**
 * @param {Player} player 
 */
export function thrust(player){
    if(player.typeId == "minecraft:player"){
        player.playAnimation("animation.player.thrust",{nextState:"third_person",blendOutTime:0.25,controller:"controller.animation.player.root",stopExpression:"q.any_animation_finished"});
    }else{
        player.playAnimation("animation.player.thrust",{nextState:"default",blendOutTime:0.25,controller:"controller.animation.mob",stopExpression:"q.any_animation_finished"});
    }
    player.dimension.spawnParticle("hxc:punch_impact",caret(player.getHeadLocation(),player.getRotation(),[0,0,3]));
    player.dimension.playSound("slash",caret(player.getHeadLocation(),player.getRotation(),[0,0,2]),{volume:0.85,pitch:1.25});
    player.dimension.playSound("sword.swing",caret(player.getHeadLocation(),player.getRotation(),[0,0,2]),{volume:0.8,pitch:1.25});
    if(player.typeId == "minecraft:player"){
        player.startItemCooldown("sword",54);
    }else{
        player.triggerEvent("hxc:cooldown");
    }
    player.addTag("can_parry");
    system.runTimeout(()=>{
        player.removeTag("can_parry");
    },3);
    system.runTimeout(()=>{
        rangedAttackForward({
            damaging:player,
            range:[4.25,4.25],
            distance:4.25,
            damage:{amount:damageByEffectStrength(player,18,1.3),cause:"entityAttack"},
            knockback:{type:"front",horizontalStrength:3.75,verticalStrength:0.5}
        });
        const knockbackTargets = getEntitiesInFront(player,[5.1,5.1],5.1);
        for(const targetEntity of knockbackTargets){
            if(targetEntity.hasTag("can_parry")){
                player.dimension.spawnParticle("hxc:parry_slash1",caret(player.getHeadLocation(),player.getRotation(),[0,0,1]));
                player.dimension.playSound("parry_slash",caret(player.getHeadLocation(),player.getRotation(),[0,0,1]),{volume:1.5,pitch:1.05});
            }
        }
    },2);
}
/**
 * @param {Player} player 
 */
export function thrust2(player){
    if(player.typeId == "minecraft:player"){
        player.playAnimation("animation.player.thrust",{nextState:"third_person",blendOutTime:0.25,controller:"controller.animation.player.root",stopExpression:"q.any_animation_finished"});
    }else{
        player.playAnimation("animation.player.thrust",{nextState:"default",blendOutTime:0.25,controller:"controller.animation.mob",stopExpression:"q.any_animation_finished"});
    }
    player.dimension.spawnParticle("hxc:punch_impact2",caret(player.getHeadLocation(),player.getRotation(),[0,0,3]));
    player.dimension.playSound("slash",caret(player.getHeadLocation(),player.getRotation(),[0,0,2]),{volume:0.85,pitch:1.25});
    player.dimension.playSound("sword.swing",caret(player.getHeadLocation(),player.getRotation(),[0,0,2]),{volume:0.8,pitch:1.25});
    player.addTag("can_parry");
    system.runTimeout(()=>{
        rangedAttackForward({
            damaging:player,
            range:[4.3,4.3],
            distance:4.3,
            damage:{amount:damageByEffectStrength(player,9,1.3),cause:"entityAttack"},
            knockback:{type:"front",horizontalStrength:0.05,verticalStrength:0}
        });
        const knockbackTargets = getEntitiesInFront(player,[5.15,5.15],5.15);
        for(const targetEntity of knockbackTargets){
            if(targetEntity.hasTag("can_parry")){
                player.dimension.spawnParticle("hxc:parry_slash1",caret(player.getHeadLocation(),player.getRotation(),[0,0,1]));
                player.dimension.playSound("parry_slash",caret(player.getHeadLocation(),player.getRotation(),[0,0,1]),{volume:1.5,pitch:1.05});
            }
        }
    },2);
}
/**
 * @param {Player} player 
 */
export function thrust2_final(player){
    if(player.typeId == "minecraft:player"){
        player.playAnimation("animation.player.thrust",{nextState:"third_person",blendOutTime:0.25,controller:"controller.animation.player.root",stopExpression:"q.any_animation_finished"});
    }else{
        player.playAnimation("animation.player.thrust",{nextState:"default",blendOutTime:0.25,controller:"controller.animation.mob",stopExpression:"q.any_animation_finished"});
    }
    player.dimension.spawnParticle("hxc:punch_impact",caret(player.getHeadLocation(),player.getRotation(),[0,0,3]));
    player.dimension.playSound("slash",caret(player.getHeadLocation(),player.getRotation(),[0,0,2]),{volume:0.85,pitch:1.25});
    player.dimension.playSound("sword.swing",caret(player.getHeadLocation(),player.getRotation(),[0,0,2]),{volume:0.8,pitch:1.25});
    player.addTag("can_parry");
    system.runTimeout(()=>{
        player.removeTag("can_parry");
    },3);
    system.runTimeout(()=>{
        rangedAttackForward({
            damaging:player,
            range:[4.35,4.35],
            distance:4.35,
            damage:{amount:damageByEffectStrength(player,10,1.3),cause:"entityAttack"},
            knockback:{type:"front",horizontalStrength:3.85,verticalStrength:0.6}
        });
        const knockbackTargets = getEntitiesInFront(player,[5.2,5.2],5.2);
        for(const targetEntity of knockbackTargets){
            if(targetEntity.hasTag("can_parry")){
                player.dimension.spawnParticle("hxc:parry_slash1",caret(player.getHeadLocation(),player.getRotation(),[0,0,1]));
                player.dimension.playSound("parry_slash",caret(player.getHeadLocation(),player.getRotation(),[0,0,1]),{volume:1.5,pitch:1.05});
            }
        }
    },2);
}
/**
 * @param {Player} player 
 */
export function thrust2_end(player){
    player.removeTag("can_parry");
    if(player.typeId == "minecraft:player"){
        player.startItemCooldown("sword",90);
    }else{
        player.triggerEvent("hxc:cooldown");
    }
}
/**
 * @param {Player} player 
 */
export function thrust3(player){
    const startLocation = player.location;
    const cos0 = Math.cos(player.getRotation().x * (Math.PI/180));
    const sin0 = Math.sin(player.getRotation().x * (Math.PI/180));
    const horizontalStrength = player.getRotation().x < 0 ? 35 * cos0 : 35;
    const verticalStrength = Math.max(0,-3 * sin0);
    applySelfKnockback(player,horizontalStrength,verticalStrength);
    player.dimension.playSound("sword.rush",player.location);
    player.dimension.playSound("sword.swing",player.location);
    player.dimension.playSound("sword slash",player.location);
    if(player.typeId == "minecraft:player"){
        player.startItemCooldown("sword",95);
    }else{
        player.triggerEvent("hxc:cooldown");
    }
    if(player.typeId == "minecraft:player"){
        player.playAnimation("animation.player.thrust",{nextState:"third_person",blendOutTime:0.25,controller:"controller.animation.player.root",stopExpression:"q.any_animation_finished"});
    }else{
        player.playAnimation("animation.player.thrust",{nextState:"default",blendOutTime:0.25,controller:"controller.animation.mob",stopExpression:"q.any_animation_finished"});
    }
    system.runTimeout(()=>{
        cancelKnockback(player);
    },2);
    player.addTag("can_parry");
    system.runTimeout(()=>{
        player.removeTag("can_parry");
    },3);
    system.runTimeout(()=>{
        if(!player.isOnGround){
            cancelKnockback(player);
            cancelKnockback(player);
            cancelKnockback(player);
        }
        const stopLocation = player.location;
        rushAttack({
            damaging:player,
            startLocation,
            stopLocation,
            volume:{x:5,y:5,z:5},
            damage:{amount:damageByEffectStrength(player,26,1.3),cause:"entityAttack"},
            particle:{trailParticleId:"hxc:rush_thrust"}
        });
    },2);
}
/**
 * @param {Player} player 
 */
export function trident_smash(player){
    if(player.typeId == "minecraft:player"){
        player.playAnimation("animation.player.trident_smash",{nextState:"third_person",blendOutTime:0.25,controller:"controller.animation.player.root",stopExpression:"q.any_animation_finished"});
    }else{
        player.playAnimation("animation.player.trident_smash",{nextState:"default",blendOutTime:0.25,controller:"controller.animation.mob",stopExpression:"q.any_animation_finished"});
    }
    if(player.typeId == "minecraft:player"){
        player.startItemCooldown("sword",100);
    }else{
        player.triggerEvent("hxc:cooldown");
    }
    system.runTimeout(()=>{
        player.runCommand("camerashake add @a[r=12] 0.5 0.6");
    },5);
    system.runTimeout(()=>{
        const location = shiftLocation(player.location,0,1,0);
        const location2 = shiftLocation(player.location,0,-0.6,0);
        player.dimension.playSound("sword.swing",caret(player.getHeadLocation(),player.getRotation(),[0,0,2]),{volume:1.5,pitch:1.25});
        player.dimension.playSound("sword slash",caret(player.getHeadLocation(),player.getRotation(),[0,0,2]),{volume:2,pitch:1.75});
        player.dimension.playSound("trident_spike",caret(player.getHeadLocation(),player.getRotation(),[0,0.5,0]),{volume:3,pitch:0.85});
        player.dimension.playSound("random.explode",caret(player.getHeadLocation(),player.getRotation(),[0,0.5,0]),{volume:3,pitch:1.1});
        player.dimension.spawnParticle("hxc:meruem_tail_attack1",location2);
        player.dimension.spawnParticle("hxc:trident_smash_spike",location);
        player.dimension.spawnParticle("hxc:crack",location2);
        for(const targetEntity of getEntitiesInRange(player,shiftLocation(player.location,-10,-5,-10),{x:20,y:10,z:20})){
            applyVarious({
                target:targetEntity,
                damaging:player,
                damage:{amount:damageByEffectStrength(player,38,1.3),cause:"entityAttack"},
                knockback:{type:"sphere",horizontalStrength:3.5,verticalStrength:1.5}
            });
        }
    },6);
}
/**
 * @param {Player} player 
 */
export function trident_throw_charge(player){
    if(player.typeId == "minecraft:player"){
        player.playAnimation("animation.player.trident_throw_charge",{nextState:"third_person",blendOutTime:0.25,controller:"controller.animation.player.root",stopExpression:"q.any_animation_finished"});
    }else{
        player.playAnimation("animation.player.trident_throw_charge",{nextState:"default",blendOutTime:0.25,controller:"controller.animation.mob",stopExpression:"q.any_animation_finished"});
    }
}
/**
 * @param {Player} player 
 */
export function trident_throw(player){
    if(player.typeId == "minecraft:player"){
        player.playAnimation("animation.player.trident_throw",{nextState:"third_person",blendOutTime:0.25,controller:"controller.animation.player.root",stopExpression:"q.any_animation_finished"});
    }else{
        player.playAnimation("animation.player.trident_throw",{nextState:"default",blendOutTime:0.25,controller:"controller.animation.mob",stopExpression:"q.any_animation_finished"});
    }
    player.addTag("can_parry");
    system.runTimeout(()=>{
        player.removeTag("can_parry");
    },3);
    const knockbackTargets = getEntitiesInFront(player,[4,4],4);
    for(const targetEntity of knockbackTargets){
        applyVarious({
            target:targetEntity,
            damaging:player,
            damage:{amount:damageByEffectStrength(player,25,1.3),cause:"entityAttack"},
            knockback:{type:"front",horizontalStrength:5,verticalStrength:1}
        });
    }
    const parryTargets = getEntitiesInFront(player,[5,5],5);
    for(const targetEntity of parryTargets){
        if(targetEntity.hasTag("can_parry")){
            player.dimension.spawnParticle("hxc:parry_slash1",caret(player.getHeadLocation(),player.getRotation(),[0,0,1]));
            player.dimension.playSound("parry_slash",caret(player.getHeadLocation(),player.getRotation(),[0,0,1]),{volume:1.5,pitch:1.05});
        }
    }
    shootProjectile(player,"hxc:god_trident_p",1);
    player.dimension.spawnParticle("hxc:god_trident_throw",caret(player.getHeadLocation(),player.getRotation(),[0,0,3]));
    player.dimension.playSound("slash",caret(player.getHeadLocation(),player.getRotation(),[0,0,2]),{volume:0.85,pitch:1.25});
    player.dimension.playSound("sword.swing",caret(player.getHeadLocation(),player.getRotation(),[0,0,2]),{volume:0.8,pitch:1.25});
    if(player.typeId == "minecraft:player"){
        player.startItemCooldown("sword",98);
    }else{
        player.triggerEvent("hxc:cooldown");
    }
}
/**
 * @param {Entity} entity 
 */
export function trident_throw_p(entity){
    if(removeProjectileInBLock(entity)) return;
    const projectileComp = entity.getComponent("projectile");
    if(projectileComp==undefined) return;
    const owner = projectileComp.owner;
    const targets = getEntitiesInRange(owner,shiftLocation(entity.location,-3,-3,-3),{x:6,y:6,z:6});
    for(const targetEntity of targets){
        if(owner.typeId == "hxc:god_warrior"){
            applyVarious({
                target:targetEntity,
                damaging:owner,
                damage:{amount:damageByEffectStrength(owner,32,1.3),cause:"entityAttack"}
            });
        }else{
            applyVarious({
                target:targetEntity,
                damaging:owner,
                damage:{amount:damageByEffectStrength(owner,25,1.3),cause:"entityAttack"}
            });
        }
    }
}

//Nen//
/**
 * @param {Player} player 
 */
export function ren(player){
    const lv = player.level;
    const range = lv<10 ? 16 : lv<20 ? 24 : lv<30 ? 32 : 48;
    new renEffect(player,range).particle().removeBadEffects().addEffects().addDamage();
}
export class renEffect {
    /**
     * @param {Player} player 
     * @param {Number} distance 
     */
    constructor(player,distance){
        this.player = player;
        this.location = player.location;
        this.level = player.getProperty("hxc:level");
        this.distance = distance;
    }
    particle(){
        this.player.dimension.spawnParticle("hxc:ren_e_particle1",this.location);
        if(this.level >= 30){
            this.player.dimension.spawnParticle("hxc:ren_e_particle2",this.location);
            this.player.dimension.playSound("ren",this.location,{volume:2});
        }
        return this;
    }
    removeBadEffects(){
        for(const badEffect of badEffects){
            this.player.removeEffect(badEffect);
        }
        return this;
    }
    addEffects(){
        for(const target of this.player.dimension.getEntities({location:this.location,maxDistance:this.distance,excludeFamilies:getExcludeFamilies(this.level)})){
            if(target.id != this.player.id){
                if(target.typeId=="minecraft:player"){
                    if(this.level > target.level){
                        target.addEffect("weakness",200,{amplifier:4});
                        target.addEffect("slowness",200,{amplifier:4});
                        target.addEffect("mining_fatigue",200,{amplifier:7});
                    }
                }
                else{
                    target.addEffect("weakness",200,{amplifier:4});
                    target.addEffect("slowness",200,{amplifier:4});
                    target.addEffect("mining_fatigue",200,{amplifier:7});
                }
            }
        }
        return this;
    }
    addDamage(){
        for(const damageTarget of this.player.dimension.getEntities({location:this.location,maxDistance:this.distance,excludeFamilies:getExcludeFamilies(this.level)})){
            damageTarget.applyDamage(20,{cause:"override",damagingEntity:this.player});
        }
    }
}
const badEffects = [
    "slowness",
    "mining_fatigue",
    "blindness",
    "darkness",
    "weakness",
    "bad_omen",
    "nausea"
];
/**
 * @param {Number} level 
 */
function getExcludeFamilies(level){
    if(level<10){
        return ["nen","damage"];
    }
    else if(true){
        return ["nen","damage"];
    }
};