import { world, ItemStack, GameMode } from '@minecraft/server';

const toolTypeIds = [
    'tool:meteorite_pickaxe',
    'tool:meteorite_axe',
    'tool:meteorite_shovel',
    'tool:meteorite_hoe',
    'tool:starlite_pickaxe',
    'tool:starlite_axe',
    'tool:starlite_shovel',
    'tool:starlite_hoe',
    'tool:aquamarine_pickaxe',
    'tool:aquamarine_axe',
    'tool:aquamarine_shovel',
    'tool:aquamarine_hoe',
    'tool:moonstone_pickaxe',
    'tool:moonstone_axe',
    'tool:moonstone_shovel',
    'tool:moonstone_hoe',
    'tool:solar_pickaxe',
    'tool:solar_axe',
    'tool:solar_shovel',
    'tool:solar_hoe',
    'tool:luminite_pickaxe',
    'tool:luminite_axe',
    'tool:luminite_shovel',
    'tool:luminite_hoe',
    'tool:lunar_pickaxe',
    'tool:lunar_axe',
    'tool:lunar_shovel',
    'tool:lunar_hoe',
    'tool:comet_pickaxe',
    'tool:comet_axe',
    'tool:comet_shovel',
    'tool:comet_hoe',
    'tool:arkose_pickaxe',
    'tool:arkose_axe',
    'tool:arkose_shovel',
    'tool:arkose_hoe',
    'tool:sapphire_pickaxe',
    'tool:sapphire_axe',
    'tool:sapphire_shovel',
    'tool:sapphire_hoe',
    'tool:blushie_pickaxe',
    'tool:blushie_axe',
    'tool:blushie_shovel',
    'tool:blushie_hoe',
    'tool:amethyst_pickaxe',
    'tool:amethyst_axe',
    'tool:amethyst_shovel',
    'tool:amethyst_hoe',
    'tool:nether_quartz_pickaxe',
    'tool:nether_quartz_axe',
    'tool:nether_quartz_shovel',
    'tool:nether_quartz_hoe',
    'tool:enduranium_pickaxe',
    'tool:enduranium_axe',
    'tool:enduranium_shovel',
    'tool:enduranium_hoe',
    'tool:dragonite_pickaxe',
    'tool:dragonite_axe',
    'tool:dragonite_shovel',
    'tool:dragonite_hoe',
    'tool:molybdenum_pickaxe',
    'tool:molybdenum_axe',
    'tool:molybdenum_shovel',
    'tool:molybdenum_hoe',
    'tool:ascendant_pickaxe',
    'tool:ascendant_axe',
    'tool:ascendant_shovel',
    'tool:ascendant_hoe',
    'tool:ice_pickaxe',
    'tool:ice_axe',
    'tool:ice_shovel',
    'tool:ice_hoe',
    'tool:aurora_pickaxe',
    'tool:aurora_axe',
    'tool:aurora_shovel',
    'tool:aurora_hoe',
    'tool:lapis_pickaxe',
    'tool:lapis_axe',
    'tool:lapis_shovel',
    'tool:lapis_hoe',
    'tool:steel_pickaxe',
    'tool:steel_axe',
    'tool:steel_shovel',
    'tool:steel_hoe',
    'tool:flint_pickaxe',
    'tool:flint_axe',
    'tool:flint_shovel',
    'tool:flint_hoe',
    'tool:resin_dipped_pickaxe',
    'tool:resin_dipped_axe',
    'tool:resin_dipped_shovel',
    'tool:resin_dipped_hoe',
    'tool:obsidian_pickaxe',
    'tool:obsidian_axe',
    'tool:obsidian_shovel',
    'tool:obsidian_hoe',
    'tool:emerald_pickaxe',
    'tool:emerald_axe',
    'tool:emerald_shovel',
    'tool:emerald_hoe',
    'tool:ruby_pickaxe',
    'tool:ruby_axe',
    'tool:ruby_shovel',
    'tool:ruby_hoe',
    'tool:citrine_pickaxe',
    'tool:citrine_axe',
    'tool:citrine_shovel',
    'tool:citrine_hoe',
    'tool:brimstone_pickaxe',
    'tool:brimstone_axe',
    'tool:brimstone_shovel',
    'tool:brimstone_hoe',
    'tool:pyro_pickaxe',
    'tool:pyro_axe',
    'tool:pyro_shovel',
    'tool:pyro_hoe',
    'tool:blackstone_pickaxe',
    'tool:blackstone_axe',
    'tool:blackstone_shovel',
    'tool:blackstone_hoe',
    'tool:bone_pickaxe',
    'tool:bone_axe',
    'tool:bone_shovel',
    'tool:bone_hoe',
    'tool:souling_gold_pickaxe',
    'tool:souling_gold_axe',
    'tool:souling_gold_shovel',
    'tool:souling_gold_hoe',
    'tool:deepslate_pickaxe',
    'tool:deepslate_axe',
    'tool:deepslate_shovel',
    'tool:deepslate_hoe',
    'tool:ghost_crystal_pickaxe',
    'tool:ghost_crystal_axe',
    'tool:ghost_crystal_shovel',
    'tool:ghost_crtstal_hoe',
    'tool:echoed_copper_pickaxe',
    'tool:echoed_copper_axe',
    'tool:echoed_copper_shovel',
    'tool:echoed_copper_hoe',
    'tool:echo_shard_pickaxe',
    'tool:echo_shard_axe',
    'tool:echo_shard_shovel',
    'tool:echo_shard_hoe',
    'tool:scorpion_pickaxe',
    'tool:scorpion_axe',
    'tool:scorpion_shovel',
    'tool:scorpion_hoe',
    'tool:amber_pickaxe',
    'tool:amber_axe',
    'tool:amber_shovel',
    'tool:amber_hoe',
    'sword:titanium_pickaxe',
    'sword:titanium_axe',
    'sword:titanium_shovel',
    'sword:titanium_hoe',
    'tool:cobalt_pickaxe',
    'tool:cobalt_axe',
    'tool:cobalt_shovel',
    'tool:cobalt_hoe',
    'tool:palladium_pickaxe',
    'tool:palladium_axe',
    'tool:palladium_shovel',
    'tool:palladium_hoe',
    'tool:rose_gold_pickaxe',
    'tool:rose_gold_axe',
    'tool:rose_gold_shovel',
    'tool:rose_gold_hoe',
    'tool:aetherium_pickaxe',
    'tool:aetherium_axe',
    'tool:aetherium_shovel',
    'tool:aetherium_hoe',
    'tool:jasper_pickaxe',
    'tool:jasper_axe',
    'tool:jasper_shovel',
    'tool:jasper_hoe',
    'tool:kyanite_pickaxe',
    'tool:kyanite_axe',
    'tool:kyanite_shovel',
    'tool:kyanite_hoe',
    'tool:staballoy_pickaxe',
    'tool:staballoy_axe',
    'tool:staballoy_shovel',
    'tool:staballoy_hoe',
    'tool:magnet_pickaxe',
    'tool:magnet_axe',
    'tool:magnet_shovel',
    'tool:magnet_hoe',
    'tool:drill'
];
const weaponTypeIds = [
    'sword:obsidian_sword',
    'sword:emerald_sword',
    'sword:ruby_sword'
];

world.afterEvents.playerBreakBlock.subscribe(evd => {
    const { player, itemStackBeforeBreak: itemUsed } = evd;

    // This returns if itemUsed is undefined or if the player is in Creative.
    if (!itemUsed || player.matches({ gameMode: GameMode.creative })) return;

    if (toolTypeIds.includes(itemUsed.typeId) || weaponTypeIds.includes(itemUsed.typeId)) {
        // This retrieves the player's equippable component.
        const playerEquippableComp = player.getComponent("equippable");

        // This returns if playerEquippableComp is undefined.
        if (!playerEquippableComp) return;

        // This retrieves the enchantable component of the item.
        const itemEnchantmentComp = itemUsed.getComponent("minecraft:enchantable");
        const unbreakingLevel = itemEnchantmentComp?.getEnchantment("unbreaking")?.level ?? 0;

        // Calculates the chance of an item breaking based on its unbreaking level. This is the vanilla unbreaking formula.
        const breakChance = 100 / (unbreakingLevel + 1);
        // Generates a random chance value between 0 and 100.
        const randomizeChance = Math.random() * 100;

        // This returns if breakChance is less than randomizeChance.
        if (breakChance < randomizeChance) return;

        // This retrieves the durability component of the item.
        const itemUsedDurabilityComp = itemUsed.getComponent("durability");

        // This returns if itemUsedDurabilityComp is undefined.
        if (!itemUsedDurabilityComp) return;

        let durabilityModifier = 0;
        if (toolTypeIds.includes(itemUsed.typeId)) {
            // If the item is a tool, then it will set the durability modifier to 1.
            durabilityModifier = 1;
        } else {
            // If the item is a weapon, then it will set the durability modifier to 2.
            durabilityModifier = 2;
        }

        // This will set the new durability value.
        itemUsedDurabilityComp.damage += durabilityModifier;

        // Declares and checks if the item is out of durability
        const maxDurability = itemUsedDurabilityComp.maxDurability
        const currentDamage = itemUsedDurabilityComp.damage
        if (currentDamage >= maxDurability) {

            // If the item is out of durability, plays the item breaking sound and removes the item
            player.playSound('random.break', { pitch: 1, location: player.location, volume: 1 })
            playerEquippableComp.setEquipment("Mainhand", new ItemStack('minecraft:air', 1));
        }
        else if (currentDamage < maxDurability) {

            // This sets the item in the player's selected slot.
            playerEquippableComp.setEquipment("Mainhand", itemUsed);
        }
    }
})