import { EquipmentSlot, GameMode, world } from "@minecraft/server";
const blockLootMap = {
    "minecraft:short_grass": "short_grass",
    "minecraft:sunflower": "sunflower",
    "minecraft:red_flower": "poppy",
    "minecraft:stone": "stone",
    "minecraft:cactus": "cactus",
    "minecraft:birch_leaves": "birch_leaves",
    "minecraft:cherry_leaves": "cherry_leaves",
    "minecraft:dark_oak_leaves": "dark_oak_leaves",
    "minecraft:fern": "fern",
    "minecraft:jungle_leaves": "jungle_leaves",
    "minecraft:large_fern": "large_fern",
    "minecraft:mangrove_leaves": "mangrove_leaves",
    "minecraft:oak_leaves": "oak_leaves",
    "minecraft:spruce_leaves": "spruce_leaves",
    "minecraft:tall_grass": "tall_grass",
    "minecraft:poppy": "poppy"
};

const breakCropSubscription = world.afterEvents.playerBreakBlock.subscribe((event) => {
    const { brokenBlockPermutation, block } = event;
    const blockType = brokenBlockPermutation.type.id;

    const x = block.location.x;
    const y = block.location.y;
    const z = block.location.z;


    const hasChance = (chance) => Math.random() < chance;



    if (blockLootMap[blockType] && hasChance(0.4)) {
        const dropCommand = `loot spawn ${x} ${y} ${z} loot ${blockLootMap[blockType]}`;
        world.getDimension("overworld").runCommandAsync(dropCommand).catch((error) => {
            console.error("Error executing command:", error);
        });
    }
});



const entityDieSubsription = world.afterEvents.entityDie.subscribe(({ damageSource, deadEntity }) => {
    const died = deadEntity;
    const killer = damageSource.damagingEntity;

    if (died.getComponent("minecraft:type_family")?.hasTypeFamily("goat")) {
        died.runCommandAsync("loot spawn ~ ~ ~ loot goat")
    }
});

