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


// 稿子额外挖出物品的几率
const pickaxeExtraChance = 0.5;

// 稿子额外掉落，支持的方块
const pickaxeExtraBlocks = [
    'pm_mock:ancient_ore_nodes',
    'minecraft:coal_ore',
    'minecraft:deepslate_coal_ore',
    'minecraft:iron_ore',
    'minecraft:deepslate_iron_ore',
    'minecraft:copper_ore',
    'minecraft:deepslate_copper_ore',
    'minecraft:gold_ore',
    'minecraft:deepslate_gold_ore',
    'minecraft:diamond_ore',
    'minecraft:deepslate_diamond_ore',
    'minecraft:emerald_ore',
    'minecraft:deepslate_emerald_ore',
    'minecraft:redstone_ore',
    'minecraft:deepslate_redstone_ore',
    'minecraft:lapis_ore',
    'minecraft:deepslate_lapis_ore',
    'minecraft:lit_redstone_ore',
    'minecraft:lit_deepslate_redstone_ore',
];

// 处理物品耐久度的函数
export function handleItemDurability(player) {
    const playerMainhandItem = player.getComponent('minecraft:equippable').getEquipment('Mainhand');
    if (playerMainhandItem) {
        const durabilityComponent = playerMainhandItem.getComponent('minecraft:durability');

        if (player.getGameMode() !== "creative") {
            durabilityComponent.damage++;
            if (durabilityComponent.damage >= durabilityComponent.maxDurability) {
                player.getComponent('minecraft:equippable').setEquipment('Mainhand', undefined);
                player.playSound('random.break', player.location);
            } else {
                player.getComponent('minecraft:equippable').setEquipment('Mainhand', playerMainhandItem);
            }
        }

        return {
            itemDurability: durabilityComponent.damage,
            itemMaxDurability: durabilityComponent.maxDurability
        };
    }

    return {
        itemDurability: -1,
        itemMaxDurability: -1
    };
}

// 监听玩家破坏方块
world.afterEvents.playerBreakBlock.subscribe((event) => {
    const { block, player, itemStackBeforeBreak, brokenBlockPermutation, dimension } = event;

    const blockTypeId = brokenBlockPermutation.type.id;
    const itemTypeId = itemStackBeforeBreak?.typeId;

    // 判定玩家手持工具和破坏方块
    if (itemStackBeforeBreak && itemStackBeforeBreak.typeId === 'pm_mock:cavernite_pickaxe') {
        const { itemDurability, itemMaxDurability } = handleItemDurability(player);

        // 随机几率，50%几率触发
        if (Math.random() < pickaxeExtraChance && pickaxeExtraBlocks.includes(blockTypeId) && player.getGameMode() !== "creative") {
            dimension.runCommand(`setblock ${block.x} ${block.y} ${block.z} ${blockTypeId}`);
            dimension.runCommand(`loot spawn ${block.x} ${block.y} ${block.z} mine ${block.x} ${block.y} ${block.z} pm_mock:cavernite_pickaxe`);
            dimension.runCommand(`setblock ${block.x} ${block.y} ${block.z} air`);
            dimension.spawnParticle('pm_mock:cavernite_pickaxe_loot_1', { x: block.x + 0.5, y: block.y + 0.5, z: block.z + 0.5 });
            dimension.playSound('block.enchanting_table.use', block.location);
            // console.warn(`触发额外掉落!`);
        };
        

        // console.warn(`玩家 ${player.nameTag} 使用 ${itemTypeId} 破坏了方块 ${blockTypeId} , 耐久度值为 ${itemDurability} / ${itemMaxDurability}`);
    }
});

