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

// 8号的一些技能



function attackCallback(event){
    const hurtEntity = event.hitEntity;
    const sourceEntity = event.damagingEntity;

    if (sourceEntity.hasTag('pm_m8:monster_8')) {
        if (sourceEntity.hasTag('pm_m8:knockback_enhancement')) {
            const directionX = hurtEntity.location.x - sourceEntity.location.x;
            const directionZ = hurtEntity.location.z - sourceEntity.location.z;
            hurtEntity.applyKnockback(directionX, directionZ, 4, 1);
            if(sourceEntity.hasTag('justfly')) {
                const view = sourceEntity.getViewDirection()
                sourceEntity.applyKnockback(view.x, view.z, 9, 1);
            }
        }
    }
}

const filterOption = {
    entityTypes : [
        'minecraft:player'
    ],
};

world.afterEvents.entityHitEntity.subscribe(attackCallback, filterOption);



system.afterEvents.scriptEventReceive.subscribe((event) => {
    const {
        id, // returns string (wiki:test)
        initiator, // returns Entity (or undefined if an NPC did not fire the command)
        message, // returns string (Hello World)
        sourceBlock, // returns Block (or undefined if a block did not fire the command)
        sourceEntity, // returns Entity (or undefined if an entity did not fire the command)
        sourceType, // returns MessageSourceType (can be 'Block', 'Entity', 'NPCDialogue', or 'Server')
    } = event;

    if (id === 'pm_m8:m8_jump_boost') {
        sourceEntity.runCommand('event entity @s start_long_jumping');
        const view = sourceEntity.getViewDirection()
        sourceEntity.applyKnockback(view.x, view.z, 9, 1);
        
        system.runTimeout(() => {
            sourceEntity.runCommand('event entity @s stop_long_jumping');
        }, 1.1 * 20);
    }
});
