TetraJSEvents.workbenchCraft(event => {
    let { player, level, currentSchematic, currentSlot, targetStack, upgradedStack, materials, workbenchTile } = event
    let materialItem = materials[0].id
    if (level.clientSide) return
    //移除原有幻化
    // upgradedStack.nbt.remove('transmog:transmogItem');
    // remove_transmog(event)
    // //根据材料幻化
    // materials_transmog(event)
    // //根据schematic幻化
    // schematic_transmog(event)

})
const transmog_slot = [
    'helmet',
    'chestplate',
    'belt',
    'feet'
]

const tetra_wear_transmog = [
    {
        material: 'minecraft:iron_ingot',
        helmet: 'minecraft:iron_helmet',
        chestplate: 'minecraft:iron_chestplate',
        leggings: 'minecraft:iron_leggings',
        boots: 'minecraft:iron_boots'
    },
    {
        material: 'minecraft:diamond',
        helmet: 'minecraft:diamond_helmet',
        chestplate: 'minecraft:diamond_chestplate',
        leggings: 'minecraft:diamond_leggings',
        boots: 'minecraft:diamond_boots'
    },
    {
        material: 'cataclysm:ignitium_ingot',
        helmet: 'cataclysm:ignitium_helmet',
        chestplate: 'cataclysm:ignitium_chestplate',
        leggings: 'cataclysm:ignitium_leggings',
        boots: 'cataclysm:ignitium_boots'
    },
    {
        material: 'create:precision_mechanism',
        helmet: 'protection_pixel:lanceras_helmet',
        chestplate: 'protection_pixel:magneticstormas_chestplate',
        leggings: 'protection_pixel:buoyancyas_leggings',
        boots: 'protection_pixel:socks_boots'
    },
]
//根据材料幻化
function materials_transmog(event) {
    let { player, level, currentSchematic, currentSlot, targetStack, upgradedStack, materials, workbenchTile } = event
    let materialItem = materials[0].id
    if (level.clientSide) return

    let boolean = false
    if (!targetStack.nbt.contains("transmog:transmogItem")) {
        boolean = true
    }
    if (hasModuleKey(currentSchematic) && transmog_slot.includes(currentSlot)) {
        boolean = true
    }
    if (boolean == false) return

    // 检测物品是否为头盔
    if (upgradedStack === "1 helmet") {
        let transmog_item = 'minecraft:netherite_helmet';
        for (let material of tetra_wear_transmog) {
            if (material.material === materialItem) {
                transmog_item = material.helmet;
                break;
            }
        }
        $TransmogUtils.transmogAppearanceOntoItemStack(transmog_item, upgradedStack);
    } else if (upgradedStack === "1 chestplate") {
        let transmog_item = 'minecraft:netherite_chestplate';
        for (let material of tetra_wear_transmog) {
            if (material.material === materialItem) {
                transmog_item = material.chestplate;
                break;
            }
        }
        $TransmogUtils.transmogAppearanceOntoItemStack(transmog_item, upgradedStack);
    } else if (upgradedStack === "1 legging") {
        let transmog_item = 'minecraft:netherite_leggings';
        for (let material of tetra_wear_transmog) {
            if (material.material === materialItem) {
                transmog_item = material.leggings;
                break;
            }
        }
        $TransmogUtils.transmogAppearanceOntoItemStack(transmog_item, upgradedStack);
    } else if (upgradedStack === "1 feet") {
        let transmog_item = 'minecraft:netherite_boots';
        for (let material of tetra_wear_transmog) {
            if (material.material === materialItem) {
                transmog_item = material.boots;
                break;
            }
        }
        $TransmogUtils.transmogAppearanceOntoItemStack(transmog_item, upgradedStack);
    }
}
//tetra盔甲幻化改造
let tetra_wear_schematic_transmog = {
    //动力盔甲
    // 'armor/helmet/power_helmet': Item.of('protection_pixel:lanceras_helmet'),
    // 'armor/chestplate/power_chestplate': Item.of('protection_pixel:falconnestas_chestplate'),
    // 'armor/legging/power_belt': Item.of('protection_pixel:anchorpointas_leggings'),
    // 'armor/feet/power_feet': Item.of('protection_pixel:socks_boots'),
    //活体盔甲
    // 'armor/helmet/living_helmet': Item.of('biomancy:acolyte_armor_helmet', '{"biomancy:nutrients":200}'),
    // 'armor/chestplate/living_chestplate': Item.of('biomancy:acolyte_armor_chestplate', '{"biomancy:nutrients":250}'),
    // 'armor/legging/living_belt': Item.of('biomancy:acolyte_armor_leggings', '{"biomancy:nutrients":250}'),
    // 'armor/feet/living_feet': Item.of('biomancy:acolyte_armor_boots', '{"biomancy:nutrients":200}'),
    //大气之靴
    'armor/feet/atmospheric_boots': 'legendary_monsters:atmospheric_boots',
}
let tetra_wear_schematic_transmog1 = [
    //动力盔甲
    'protection_pixel:lanceras_helmet',
    'protection_pixel:falconnestas_chestplate',
    'protection_pixel:anchorpointas_leggings',
    'protection_pixel:socks_boots',
    //活体盔甲
    'biomancy:acolyte_armor_helmet',
    'biomancy:acolyte_armor_chestplate',
    'biomancy:acolyte_armor_leggings',
    'biomancy:acolyte_armor_boots',
    //大气之靴
    'legendary_monsters:atmospheric_boots',
]
//根据schematic幻化
function schematic_transmog(event) {
    let { player, level, currentSchematic, currentSlot, targetStack, upgradedStack, materials, workbenchTile } = event
    if (level.clientSide) return
    let schematic = currentSchematic.key
    // player.tell(schematic)
    let transmog = tetra_wear_schematic_transmog[schematic]
    if (transmog) {
        upgradedStack.nbt.put('world_only_render', 1);
        $TransmogUtils.transmogAppearanceOntoItemStack(transmog, upgradedStack)
    }
}

//改造时移除原有幻化，只移除盔甲的，且改造前的护甲幻化的如果不是tetra_wear_schematic_transmog里的幻化物品，则不移除幻化
function remove_transmog(event) {
    let { player, level, currentSchematic, currentSlot, targetStack, upgradedStack, materials, workbenchTile } = event
    if (level.clientSide) return
    let schematicType = currentSchematic.type 
    let schematicRarity = currentSchematic.rarity

    if (schematicType == "major" && schematicRarity != "hone" && transmog_slot.includes(currentSlot) ) {
    // player.tell(schematicType)
        // 获取物品的NBT数据
        let nbt = targetStack.getNbt()
        let targetStacktransmog = nbt.getCompound("transmog:transmogItem")
        let transmogItem = targetStacktransmog.getString("id")
        // player.tell(transmogItem)
        if (tetra_wear_schematic_transmog1.includes(transmogItem)) {
            // if (!tetra_wear_schematic_transmog[schematic]) {
            upgradedStack.nbt.remove('transmog:transmogItem')
            upgradedStack.nbt.remove('world_only_render')
        }
    }
}