//模块化锻造锤一系列效果实现
const hammer_ritenere_improvement_libro_nero = [

]
TetraJSEvents.workbenchCraft(event => {
    let { player, level, currentSchematic, currentSlot, targetStack, upgradedStack, materials, workbenchTile } = event
    //防止单人的时候client侧出问题
    if (level.clientSide || !targetStack || !upgradedStack) return
    let workbenchTilePos = workbenchTile.getBlockPos()
    //获取锻造锤的位置
    let pos = new BlockPos(workbenchTilePos.x, workbenchTilePos.y + 2, workbenchTilePos.z)
    //获取锻造锤方块实体
    let block_entity = level.getBlockEntity(pos)
    if (block_entity) {
        if (block_entity.persistentData.get('modular_hammer_nbt') != null) {
            //获取存储的nbt
            let nbt = block_entity.persistentData.get('modular_hammer_nbt')
            //创建物品
            let item = Item.of('tetra:hammer_modular')
            //塞入nbt
            item.getOrCreateTag().merge(nbt)
            //更新物品状态
            updateItemEffects(item)

            //原初气息
            if (simpleGetTetraEffectLevel(item, 'primordial_breath') > 0) {
                //获取改造的module
                let module = upgradedStack.getItem().getModuleFromSlot(upgradedStack, currentSlot)
                let module_key = module.key
                if (module_key == 'sword/huo_ti_jian_ren' && !module.getImprovement(targetStack, 'huo_ti_jian_ren/primordial_breath')){
                    let count = player.persistentData.getInt('YingYangCount')
                    if (count> 400){
                        //给改造的槽位增加改进
                        module.addImprovement(upgradedStack, 'huo_ti_jian_ren/primordial_breath', 0)
                        updateYingYangCount(player, count - 400)
                    }else{
                        player.tell('营养值不足')
                    }
                }
            }
            //高质量
            if (simpleGetTetraEffectLevel(item, 'quality_kubejs') > 0) {
                //获取改造的module
                let module = upgradedStack.getItem().getModuleFromSlot(upgradedStack, currentSlot)
                //给改造的槽位增加高质量
                module.addImprovement(upgradedStack, 'quality', simpleGetTetraEffectLevel(item, 'quality_kubejs'))
            }
            //稳固
            if (simpleGetTetraEffectLevel(item, 'shockwave_chamber') > 0) {
                //获取改造的module
                let module = upgradedStack.getItem().getModuleFromSlot(upgradedStack, currentSlot)
                //给改造的槽位增加高质量
                module.addImprovement(upgradedStack, 'settled', simpleGetTetraEffectLevel(item, 'shockwave_chamber'))
            }
            //精密锻锤
            if (simpleGetTetraEffectLevel(item, 'precision_hammer') > 0) {
                if (!hasModuleKey(currentSchematic) || hammer_ritenere_improvement_libro_nero.includes(upgradedStack.getId())) return
                if (detectModuleChanges(targetStack, upgradedStack).removed == null) return

                // 改造前的module
                // 遍历 outcomes 检查 moduleKey
                let remodel_module = ''
                for (let outcome of currentSchematic.definition.outcomes) {
                    if (outcome.moduleKey != null) {
                        //outcome.moduleKey有一大串东西，只取一个就行
                        remodel_module = outcome.moduleKey
                        break
                    }
                }
                if (remodel_module != '') {
                    let all_improvement = [];

                    // 获取目标装备的模块改进项及其等级
                    let modules_target = targetStack.getItem().getMajorModules(targetStack)
                    for (let module of modules_target) {
                        //检测是否为改造前的module
                        if (module && module.key == remodel_module) {
                            //获取全部的improvement，以及对应的等级
                            all_improvement = module.getImprovements(targetStack).map(improvement => {
                                return {
                                    name: improvement.key,
                                    level: module.getImprovementLevel(targetStack, improvement.key)
                                };
                            });
                            break;
                        }
                    }
                    // 将改进项及其等级应用到升级装备
                    let modules_upgraded = upgradedStack.getItem().getMajorModules(upgradedStack);
                    for (let module of modules_upgraded) {
                        if (module.key == remodel_module) {
                            for (let improvement of all_improvement) {
                                //加回improvement
                                module.addImprovement(upgradedStack, improvement.name, improvement.level);
                            }
                            break;
                        }
                    }
                }
            }
            
        }
    }
})