import { world } from "@minecraft/server"
import { SwordMechaniExecutioner } from "./Swordbasic";
import { SwordMechanifreak } from "./Swordbasic";
import { SwordMechanicWand } from "./Wandbasic";
import { SwordMechanicWandL } from "./Wandbasic";
import { SwordMechanicWind } from "./Wandbasic";
import { SwordMechanicWandR } from "./Wandbasic";
import { HeadsetM } from "./Swordbasic";



SwordMechaniExecutioner(true);
SwordMechanifreak(true);
SwordMechanicWand(true);
SwordMechanicWandR(true);
SwordMechanicWandL(true);
SwordMechanicWind(true);
HeadsetM(true);


world.afterEvents.itemUse.subscribe((data) => {
    const player = data.source
    const item = data.itemStack

    if (item.typeId === "morellager:materializer_cc") {
        player.runCommandAsync('function materializer/materializer_cc')
    }
})

world.afterEvents.itemUse.subscribe((data) => {
    const player = data.source
    const item = data.itemStack

    if (item.typeId === "morellager:materializer_maze") {
        player.runCommandAsync('function materializer/materializer_maze')
    }
})

world.afterEvents.itemUse.subscribe((data) => {
    const player = data.source
    const item = data.itemStack

    if (item.typeId === "morellager:obsidian_keep_new_materializer") {
        player.runCommandAsync('function materializer/materializer_keep')
    }
})

const Knockback = {
    Horizontal: 2, // The Longitude From The Player (The Distance Away Straight From The Player)
    Vertical: 0.25 // The Height From The Ground
}

world.afterEvents.itemUse.subscribe((event) => {
    const item = event.itemStack
    const player = event.source
    const looking = player.getViewDirection()
    if (item.typeId == "morellager:whirlwind_axe") {
        player.applyKnockback(looking.x, looking.z, Knockback.Horizontal, Knockback.Vertical)
    } else {
        console.warn(item.typeId)
    }
})


world.afterEvents.itemCompleteUse.subscribe(data => {
    const item = data.itemStack
    const player = data.source
    if (item?.typeId === 'morellager:cc_potion') {
        player.runCommandAsync('function Other/cc_potion')
    }
})

world.afterEvents.entityDie.subscribe((data) => {
    const source = data.damageSource.damagingEntity
    const deadEntity = data.deadEntity
    const item = source.getComponent("equippable").getEquipment("Mainhand")
    const health = source.getComponent("health")
    const currentHealth = health.currentValue
    if (source.typeId === "minecraft:player" && item.typeId === "morellager:soul_scythe") {
        health.setCurrentValue(currentHealth + 4)
    }
})