// ItemEvents.rightClicked(event => {
//     let LEVEL_NAMES = ["", "§7新手", "§f学徒", "§e老手", "§a专家", "§b大师"];
//     let $VillagersHelper = Java.loadClass("jeresources.util.VillagersHelper");
//     let RandomSource = Java.loadClass('net.minecraft.util.RandomSource');
//     let random = RandomSource.create();
//     let player = event.entity;

//     // 最终映射：itemId -> { item_id, groups: ... }
//     let tradeMap = {};

//     VillagerUtils.getProfessions().forEach(profession => {
//         let profId = profession.toString();
//         if (profId === "nitwit") return;

//         let profName = $I18n.get("entity.minecraft.villager." + profId);

//         // 获取工作方块
//         let blockStates = $VillagersHelper.getPoiBlocks(profession);
//         let workBlocks = [];
//         if (blockStates && !blockStates.isEmpty()) {
//             let seen = {};
//             let iter = blockStates.iterator();
//             while (iter.hasNext()) {
//                 let id = iter.next().block.id;
//                 if (!seen[id]) {
//                     seen[id] = true;
//                     workBlocks.push(id);
//                 }
//             }
//         }

//         let blockStr = workBlocks.length > 0 ? workBlocks[0] : "无";
//         if (profId === "leatherworker") {
//             blockStr = "minecraft:cauldron";
//         }
//         let workBlockDisplay = $I18n.get("block." + blockStr.replace(":", "."));

//         for (let levelNum = 1; levelNum <= 5; levelNum++) {
//             let trades = VillagerUtils.getVillagerTrades(profession, levelNum);
//             if (!trades || trades.length === 0) continue;

//             trades.forEach(trade => {
//                 let offer = trade.getOffer(player, random);
//                 if (!offer || offer.getResult().isEmpty()) return;

//                 let itemId = offer.getResult().item.id;

//                 if (!tradeMap[itemId]) {
//                     tradeMap[itemId] = {
//                         "item_id": itemId,        // ← 英文键
//                         "groups": {}
//                     };
//                 }

//                 let key = profName + "||" + workBlockDisplay;

//                 if (!tradeMap[itemId].groups[key]) {
//                     tradeMap[itemId].groups[key] = {
//                         baseName: profName,
//                         workBlock: workBlockDisplay,
//                         levels: {}
//                     };
//                 }

//                 tradeMap[itemId].groups[key].levels[levelNum] = true;
//             });
//         }
//     });

//     // 构建最终结果数组
//     let resultArray = [];
//     for (let itemId in tradeMap) {
//         if (!tradeMap.hasOwnProperty(itemId)) continue;

//         let entry = tradeMap[itemId];
//         let careerEntries = [];

//         for (let key in entry.groups) {
//             if (!entry.groups.hasOwnProperty(key)) continue;

//             let group = entry.groups[key];
//             let levelNums = [];
//             for (let num in group.levels) {
//                 levelNums.push(parseInt(num));
//             }
//             levelNums.sort((a, b) => a - b);

//             let levelParts = [];
//             for (let i = 0; i < levelNums.length; i++) {
//                 let lv = levelNums[i];
//                 if (lv >= 1 && lv <= 5) {
//                     levelParts.push(LEVEL_NAMES[lv] + "(" + lv + ")");
//                 }
//             }

//             let careerStr = group.baseName + " " + levelParts.join(" ");
//             careerEntries.push({
//                 "career": careerStr,           // ← 英文键
//                 "workstation": group.workBlock // ← 英文键
//             });
//         }

//         resultArray.push({
//             "item_id": entry["item_id"],               // ← 英文键
//             "tradable_villager_careers": careerEntries // ← 英文键
//         });
//     }

//     console.log(JSON.stringify(resultArray, null, 2));
//     JsonIO.jsjs$write("kubejs/client_scripts/yi/json/villager_trade.json", resultArray);
// });














//  ↑ 生成json用 
//  ↓ 加tooltip用，不要注释
let Villager = JsonIO.readJson("kubejs/client_scripts/yi/json/villager_trade.json");
let itemsSchematic = JsonIO.readJson("kubejs/client_scripts/yi/json/material_items_schematic.json");

ItemEvents.tooltip(event => {
    event.addAdvancedToAll((item, advanced, text) => {
        let id = item.id;
        let tooltipBuffer = Utils.newList();
        if (id == "tetra:scroll_rolled") return
        if (id == "minecraft:enchanted_book") return
        if (id == "quark:ancient_tome") return

        // 查找匹配的村民交易数据
        let matchedCareers = null;
        for (let i = 0; i < Villager.size(); i++) {
            let entry = Villager.get(i);
            if (entry.get("item_id").getAsString() === id) {
                matchedCareers = entry.get("tradable_villager_careers");
                break;
            }
        }
        if (!matchedCareers) return;
        if (event.isAlt()) {
            tooltipBuffer.add(Component.literal("§8[ §f按下Alt§8 ]§6 可与以下村民交易获得："));
            for (let i = 0; i < matchedCareers.size(); i++) {
                let careerEntry = matchedCareers.get(i);
                let careerName = careerEntry.get("career");      // e.g. "盔甲匠 新手(1)..."
                let workstation = careerEntry.get("workstation"); // e.g. "高炉"

                // 第一行：职业与等级
                tooltipBuffer.add(Component.literal("§f职业与等级：§7" + careerName));
                // 第二行：工作方块
                tooltipBuffer.add(Component.literal("§8└ 工作方块：§7" + workstation));
            }
        } else {
            tooltipBuffer.add(Component.literal("§7[ §6按下Alt§7 ]§8 + §7[§8查看获取方法§7]§8§r"));
        }

        text.addAll(tooltipBuffer);
    });
    event.addAdvancedToAll((item, advanced, text) => {
        global.replacementItems.forEach(m => {
            let tooltipBuffer = Utils.newList()
            let outputItemI18 = $I18n.get( "tetra.holo.craft." + m.output.split(":")[1].replace(":",".") )
           // console.log("tetra.holo.craft." + m.output.split(":")[1].replace(":","."))
            for (let j = 0; j < m.input.length; j++) {
                let id = m.input[j].id;
                if (item.id != id) return
                tooltipBuffer.add("可于加工台改造为: §6" + outputItemI18 )
                text.addAll(tooltipBuffer)
                
            }
        })
    });
    event.addAdvanced("minecraft:enchanted_book", (item, advanced, text) => {
        let Enchantment = item?.nbt?.StoredEnchantments
        if (!Enchantment) return
        let Enchantment$1 = Enchantment[0]
        if (!Enchantment$1) return
        let extraTooltip = Enchantment$1.get("id").toString().replace("\"", "")
        let extraTooltip$1 = $I18n.get("enchantment." + extraTooltip.replace(":", "."))
        let extraTooltiplevel = Enchantment$1.get("lvl").toString().replace("s", "")
        if (!extraTooltip) return
        let tooltipBuffer = Utils.newList()
        tooltipBuffer.add("" + extraTooltip$1 + " " + extraTooltiplevel)
        text.addAll(tooltipBuffer)
    })
})