//bridge-file-version: #1
import { world, system, ItemStack, EquipmentSlot, EntityEquippableComponent, GameMode, ItemEnchantableComponent } from '@minecraft/server';
//function to spawn an item stack anywhere
function spawnItemAnywhere(item, location, dimension) {
    const itemEntity = dimension.spawnItem(item, {
        x: location.x,
        y: 100,
        z: location.z
    });
    itemEntity.teleport(location);
    return itemEntity;
}
//function to drop the patch items
function dropPatchItems(itemID, block, patchData, state) {
    if (state == 0) return;
    const item = new ItemStack(itemID, state);
    const loc = block.center();
    spawnItemAnywhere(item, loc, block.dimension);
}
//block components
const blockComponents = [
    {
        id: "test:patch_block",
        code: {
            onPlayerInteract: (data)=>{
                const { block , dimension , player  } = data;
                //get the patch data
                const patchData = patchBlocks.find((f)=>f.blockID == data.block.typeId);
                //return if the patch data doesn't exist
                if (!patchData) return;
                //get the state number
                const state = block.permutation.getState(patchData.stateID);
                //return if the state is at max or higher
                if (state >= patchData.maxState) return;
                //get the mainhand
                const mainhand = player.getComponent(EntityEquippableComponent.componentId).getEquipmentSlot(EquipmentSlot.Mainhand);
                //get the held item
                const item = mainhand.getItem();
                //if there is no item, return
                if (!item) return;
                if (item.typeId == patchData.itemID) {
                    //run this if the item identifier matches the patch item identifier
                    //set the block state
                    block.setPermutation(block.permutation.withState(patchData.stateID, state + 1));
                    //play the sound if it exists
                    if (patchData.placeSound) {
                        const sound = patchData.placeSound;
                        dimension.playSound(sound.id, block.center(), {
                            volume: sound.volume,
                            pitch: sound.pitch
                        });
                    }
                    //return if the player's game mode is creative
                    if (player.getGameMode() == GameMode.creative) return;
                    //set the amount to the item amount - 1
                    let amount = item.amount - 1;
                    if (amount > 0) {
                        //if the reduced amount is more than 0, set the item amount to the reduced amount
                        item.amount = amount;
                        mainhand.setItem(item);
                    } else {
                        //if the reduced amount isn't higher than 0, set the item to nothing
                        mainhand.setItem();
                    }
                } else if (item.typeId == "minecraft:bone_meal" && patchData.bonemealable) {
                    //run this if the item identifier is bone meal
                    //set the block state
                    block.setPermutation(block.permutation.withState(patchData.stateID, state + 1));
                    //try to spawn the particle
                    try {
                        dimension.spawnParticle("minecraft:crop_growth_emitter", block.center());
                    } catch  {}
                    //play the sound
                    dimension.playSound("item.bone_meal.use", block.center());
                    //return if the player's game mode is creative
                    if (player.getGameMode() == GameMode.creative) return;
                    //set the amount to the item amount - 1
                    let amount1 = item.amount - 1;
                    if (amount1 > 0) {
                        //if the reduced amount is more than 0, set the item amount to the reduced amount
                        item.amount = amount1;
                        mainhand.setItem(item);
                    } else {
                        //if the reduced amount isn't higher than 0, set the item to nothing
                        mainhand.setItem();
                    }
                }
            },
            onPlayerDestroy: (data)=>{
                const { block , dimension , player  } = data;
                const perm = data.destroyedBlockPermutation;
                //get the patch data
                const patchData = patchBlocks.find((f)=>f.blockID == perm.type.id);
                //if the patch data doesn't exist; return
                if (patchData == undefined) return;
                //if the player's game mode is creative; return
                if (player.getGameMode() == GameMode.creative) return;
                //drop the patch items
                dropPatchItems(patchData.itemID, block, patchData, perm.getState(patchData.stateID));
            }
        }
    }
];
const patchBlocks = [
    {
        blockID: "block:star_litter",
        itemID: "block:star_litter_placer",
        maxState: 3,
        stateID: "block:growth",
        bonemealable: false,
        placeSound: {
            id: 'dig.grass',
            volume: 0.80,
            pitch: 0.90
	   }
        },
        {
        blockID: "block:andesite_rock",
        itemID: "block:andesite_rock_placer",
        maxState: 3,
        stateID: "block:growth",
        bonemealable: false,
        placeSound: {
            id: 'dig.stone',
            volume: 0.80,
            pitch: 0.90
        }
    },
    {
        blockID: "block:tuff_rock",
        itemID: "block:tuff_rock_placer",
        maxState: 3,
        stateID: "block:growth",
        bonemealable: false,
        placeSound: {
            id: 'dig.tuff',
            volume: 0.80,
            pitch: 0.90
        }
    },
    {
        blockID: "block:end_stone_rock",
        itemID: "block:end_stone_rock_placer",
        maxState: 3,
        stateID: "block:growth",
        bonemealable: false,
        placeSound: {
            id: 'dig.stone',
            volume: 0.80,
            pitch: 0.90
        }
    },
    {
        blockID: "block:netherrack_rock",
        itemID: "block:netherrack_rock_placer",
        maxState: 3,
        stateID: "block:growth",
        bonemealable: false,
        placeSound: {
            id: 'dig.netherrack',
            volume: 0.80,
            pitch: 0.90
        }
    },
    {
        blockID: "block:calcite_rock",
        itemID: "block:calcite_rock_placer",
        maxState: 3,
        stateID: "block:growth",
        bonemealable: false,
        placeSound: {
            id: 'dig.calcite',
            volume: 0.80,
            pitch: 0.90
        }
    },
    {
        blockID: "block:deepslate_rock",
        itemID: "block:deepslate_rock_placer",
        maxState: 3,
        stateID: "block:growth",
        bonemealable: false,
        placeSound: {
            id: 'dig.deepslate',
            volume: 0.80,
            pitch: 0.90
        }
    },
    {
        blockID: "block:diorite_rock",
        itemID: "block:diorite_rock_placer",
        maxState: 3,
        stateID: "block:growth",
        bonemealable: false,
        placeSound: {
            id: 'dig.stone',
            volume: 0.80,
            pitch: 0.90
        }
    },
    {
        blockID: "block:granite_rock",
        itemID: "block:granite_rock_placer",
        maxState: 3,
        stateID: "block:growth",
        bonemealable: false,
        placeSound: {
            id: 'dig.stone',
            volume: 0.80,
            pitch: 0.90
        }
    },
    {
        blockID: "block:sodalite_rock",
        itemID: "block:sodalite_rock_placer",
        maxState: 3,
        stateID: "block:growth",
        bonemealable: false,
        placeSound: {
            id: 'dig.stone',
            volume: 0.80,
            pitch: 0.90
        }
    },
    {
        blockID: "block:rock",
        itemID: "block:rock_placer",
        maxState: 3,
        stateID: "block:growth",
        bonemealable: false,
        placeSound: {
            id: 'dig.stone',
            volume: 0.80,
            pitch: 0.90
        }
    },
    {
        blockID: "block:snow_litter",
        itemID: "block:snow_litter_placer",
        maxState: 3,
        stateID: "block:growth",
        bonemealable: false,
        placeSound: {
            id: 'dig.grass',
            volume: 0.80,
            pitch: 0.90
        }
    },
    {
        blockID: "block:red_petels",
        itemID: "block:red_petels_placer",
        maxState: 3,
        stateID: "block:growth",
        bonemealable: false,
        placeSound: {
            id: 'dig.grass',
            volume: 0.80,
            pitch: 0.90
        }
    },
    {
        blockID: "block:orange_petels",
        itemID: "block:orange_petels_placer",
        maxState: 3,
        stateID: "block:growth",
        bonemealable: false,
        placeSound: {
            id: 'dig.grass',
            volume: 0.80,
            pitch: 0.90
        }
    },
    {
        blockID: "block:blue_shell",
        itemID: "block:blue_shell_placer",
        maxState: 3,
        stateID: "block:growth",
        bonemealable: false,
        placeSound: {
            id: 'dig.grass',
            volume: 0.80,
            pitch: 0.90
        }
    },
    {
        blockID: "block:brown_shell",
        itemID: "block:brown_shell_placer",
        maxState: 3,
        stateID: "block:growth",
        bonemealable: false,
        placeSound: {
            id: 'dig.grass',
            volume: 0.80,
            pitch: 0.90
        }
    },
    {
        blockID: "block:crape_myrtle_litter",
        itemID: "block:crape_myrtle_litter_placer",
        maxState: 3,
        stateID: "block:growth",
        bonemealable: false,
        placeSound: {
            id: 'dig.grass',
            volume: 0.80,
            pitch: 0.90
        }
    },
    {
        blockID: "block:cassia_litter",
        itemID: "block:cassia_litter_placer",
        maxState: 3,
        stateID: "block:growth",
        bonemealable: false,
        placeSound: {
            id: 'dig.grass',
            volume: 0.80,
            pitch: 0.90
        }
    },
    {
        blockID: "block:pink_shell",
        itemID: "block:pink_shell_placer",
        maxState: 3,
        stateID: "block:growth",
        bonemealable: false,
        placeSound: {
            id: 'dig.grass',
            volume: 0.80,
            pitch: 0.90
        }
    },
    {
        blockID: "block:orange_starfish",
        itemID: "block:orange_starfish_placer",
        maxState: 3,
        stateID: "block:growth",
        bonemealable: false,
        placeSound: {
            id: 'dig.grass',
            volume: 0.80,
            pitch: 0.90
        }
    },
    {
        blockID: "block:pink_starfish",
        itemID: "block:pink_starfish_placer",
        maxState: 3,
        stateID: "block:growth",
        bonemealable: false,
        placeSound: {
            id: 'dig.grass',
            volume: 0.80,
            pitch: 0.90
        }
    },
    {
        blockID: "block:yellow_petels",
        itemID: "block:yellow_petels_placer",
        maxState: 3,
        stateID: "block:growth",
        bonemealable: false,
        placeSound: {
            id: 'dig.grass',
            volume: 0.80,
            pitch: 0.90
        }
    },
    {
        blockID: "block:blue_petels",
        itemID: "block:blue_petels_placer",
        maxState: 3,
        stateID: "block:growth",
        bonemealable: false,
        placeSound: {
            id: 'dig.grass',
            volume: 0.80,
            pitch: 0.90
        }
    },
    {
        blockID: "block:pale_leaf_litter",
        itemID: "block:pale_leaf_litter_placer",
        maxState: 3,
        stateID: "block:growth",
        bonemealable: false,
        placeSound: {
            id: 'dig.grass',
            volume: 0.80,
            pitch: 0.90
        }
    },
    {
        blockID: "block:acacia_leaf_litter",
        itemID: "block:acacia_leaf_litter_placer",
        maxState: 3,
        stateID: "block:growth",
        bonemealable: false,
        placeSound: {
            id: 'dig.grass',
            volume: 0.80,
            pitch: 0.90
        }
    },
    {
        blockID: "block:dark_oak_leaf_litter",
        itemID: "block:dark_oak_leaf_litter_placer",
        maxState: 3,
        stateID: "block:growth",
        bonemealable: false,
        placeSound: {
            id: 'dig.grass',
            volume: 0.80,
            pitch: 0.90
        }
    },
    {
        blockID: "block:mangrove_leaf_litter",
        itemID: "block:mangrove_leaf_litter_placer",
        maxState: 3,
        stateID: "block:growth",
        bonemealable: false,
        placeSound: {
            id: 'dig.grass',
            volume: 0.80,
            pitch: 0.90
        }
    },
    {
        blockID: "block:spruce_leaf_litter",
        itemID: "block:spruce_leaf_litter_placer",
        maxState: 3,
        stateID: "block:growth",
        bonemealable: false,
        placeSound: {
            id: 'dig.grass',
            volume: 0.80,
            pitch: 0.90
        }
    },
    {
        blockID: "block:birch_leaf_litter",
        itemID: "block:birch_leaf_litter_placer",
        maxState: 3,
        stateID: "block:growth",
        bonemealable: false,
        placeSound: {
            id: 'dig.grass',
            volume: 0.80,
            pitch: 0.90
        }
    },
    {
        blockID: "block:jungle_leaf_litter",
        itemID: "block:jungle_leaf_litter_placer",
        maxState: 3,
        stateID: "block:growth",
        bonemealable: false,
        placeSound: {
            id: 'dig.grass',
            volume: 0.80,
            pitch: 0.90
        }
    },
    {
        blockID: "block:small_lillypad",
        itemID: "block:small_lillypad_placer",
        maxState: 3,
        stateID: "block:growth",
        bonemealable: false,
        placeSound: {
            id: 'dig.grass',
            volume: 0.80,
            pitch: 0.90
        }
    },
    {
        blockID: "block:rock",
        itemID: "block:rock_placer",
        maxState: 3,
        stateID: "block:growth",
        bonemealable: false,
        placeSound: {
            id: 'dig.grass',
            volume: 0.80,
            pitch: 0.90
        }
    },
    {
        blockID: "block:orange_pansies",
        itemID: "block:orange_pansies_placer",
        maxState: 3,
        stateID: "block:growth",
        bonemealable: true,
        placeSound: {
            id: 'dig.grass',
            volume: 0.80,
            pitch: 0.90
        }
    },
    {
        blockID: "block:pink_pansies",
        itemID: "block:pink_pansies_placer",
        maxState: 3,
        stateID: "block:growth",
        bonemealable: true,
        placeSound: {
            id: 'dig.grass',
            volume: 0.80,
            pitch: 0.90
        }
    },
    {
        blockID: "block:white_pansies",
        itemID: "block:white_pansies_placer",
        maxState: 3,
        stateID: "block:growth",
        bonemealable: true,
        placeSound: {
            id: 'dig.grass',
            volume: 0.80,
            pitch: 0.90
        }
    },
    {
        blockID: "block:white_petels",
        itemID: "block:white_petels_placer",
        maxState: 3,
        stateID: "block:growth",
        bonemealable: true,
        placeSound: {
            id: 'dig.grass',
            volume: 0.80,
            pitch: 0.90
        }
    },
    {
        blockID: "block:white_sakura_leaf_litter",
        itemID: "block:white_sakura_leaf_litter_placer",
        maxState: 3,
        stateID: "block:growth",
        bonemealable: true,
        placeSound: {
            id: 'dig.grass',
            volume: 0.80,
            pitch: 0.90
        }
    },
    {
        blockID: "block:pink_sakura_leaf_litter",
        itemID: "block:pink_sakura_leaf_litter_placer",
        maxState: 3,
        stateID: "block:growth",
        bonemealable: true,
        placeSound: {
            id: 'dig.grass',
            volume: 0.80,
            pitch: 0.90
        }
    },
    {
        blockID: "block:blue_flax_flowers",
        itemID: "block:blue_flax_flowers_placer",
        maxState: 3,
        stateID: "block:growth",
        bonemealable: false,
        placeSound: {
            id: 'dig.grass',
            volume: 0.80,
            pitch: 0.90
        }
    },
    {
        blockID: "block:clovers",
        itemID: "block:clovers_placer",
        maxState: 3,
        stateID: "block:growth",
        bonemealable: false,
        placeSound: {
            id: 'dig.grass',
            volume: 0.80,
            pitch: 0.90
        }
    },
    {
        blockID: "block:pink_lotus_flowers",
        itemID: "block:pink_lotus_flowers_placer",
        maxState: 3,
        stateID: "block:growth",
        bonemealable: false,
        placeSound: {
            id: 'dig.grass',
            volume: 0.80,
            pitch: 0.90
        }
    },
    {
        blockID: "block:small_zinnia_flowers",
        itemID: "block:small_zinnia_flowers_placer",
        maxState: 3,
        stateID: "block:growth",
        bonemealable: false,
        placeSound: {
            id: 'dig.grass',
            volume: 0.80,
            pitch: 0.90
        }
    },
    {
        blockID: "block:variety_wild_flowers",
        itemID: "block:variety_wild_flowers_placer",
        maxState: 3,
        stateID: "block:growth",
        bonemealable: false,
        placeSound: {
            id: 'dig.grass',
            volume: 0.80,
            pitch: 0.90
        }
    },
    {
        blockID: "block:zinnia_flowers",
        itemID: "block:zinnia_flowers_placer",
        maxState: 3,
        stateID: "block:growth",
        bonemealable: false,
        placeSound: {
            id: 'dig.grass',
            volume: 0.80,
            pitch: 0.90
        }
    },
    {
        blockID: "block:purple_petels",
        itemID: "block:purple_petels_placer",
        maxState: 3,
        stateID: "block:growth",
        bonemealable: false,
        placeSound: {
            id: 'dig.grass',
            volume: 0.80,
            pitch: 0.90
        }
    },
    {
        blockID: "block:blushie_petels",
        itemID: "block:blushie_petels_placer",
        maxState: 3,
        stateID: "block:growth",
        bonemealable: false,
        placeSound: {
            id: 'dig.grass',
            volume: 0.80,
            pitch: 0.90
        }
    },
    {
        blockID: "block:void_leaf_litter",
        itemID: "block:void_leaf_litter_placer",
        maxState: 3,
        stateID: "block:growth",
        bonemealable: true,
        placeSound: {
            id: 'dig.grass',
            volume: 0.80,
            pitch: 0.90
        }
    },
    {
        blockID: "block:chorus_petels",
        itemID: "block:chorus_petels_placer",
        maxState: 3,
        stateID: "block:growth",
        bonemealable: false,
        placeSound: {
            id: 'dig.grass',
            volume: 0.80,
            pitch: 0.90
        }
    },
    {
        blockID: "block:chorus_leaf_litter",
        itemID: "block:chorus_leaf_litter_placer",
        maxState: 3,
        stateID: "block:growth",
        bonemealable: false,
        placeSound: {
            id: 'dig.grass',
            volume: 0.80,
            pitch: 0.90
        }
    },
    {
        blockID: "block:ender_petels",
        itemID: "block:ender_petels_placer",
        maxState: 3,
        stateID: "block:growth",
        bonemealable: false,
        placeSound: {
            id: 'dig.grass',
            volume: 0.80,
            pitch: 0.90
        }
    },
    {
        blockID: "block:blushie_petels",
        itemID: "block:blushie_petels_placer",
        maxState: 3,
        stateID: "block:growth",
        bonemealable: false,
        placeSound: {
            id: 'dig.grass',
            volume: 0.80,
            pitch: 0.90
        }
    },
    {
        blockID: "block:void_leaf_litter",
        itemID: "block:void_leaf_litter_placer",
        maxState: 3,
        stateID: "block:growth",
        bonemealable: true,
        placeSound: {
            id: 'dig.grass',
            volume: 0.80,
            pitch: 0.90
        }
    },
    {
        blockID: "block:moonrock",
        itemID: "block:moonrock_placer",
        maxState: 3,
        stateID: "block:growth",
        bonemealable: false,
        placeSound: {
            id: 'dig.grass',
            volume: 0.80,
            pitch: 0.90
        }
    },
];
world.beforeEvents.playerBreakBlock.subscribe((data)=>{
    //cancel the block destruction if the block is a patch block
    const { block , dimension , player , itemStack  } = data;
    try {
        const patchData = patchBlocks.find((f)=>f.blockID == block.typeId);
        if (patchData != undefined) {
            if (player.getGameMode() == GameMode.creative) return;
            if (itemStack != undefined) {
                const comp = itemStack.getComponent(ItemEnchantableComponent.componentId);
                if (comp != undefined) {
                    if (!comp.hasEnchantment("silk_touch")) return;
                    //cancel the block break if they use silk touch
                    data.cancel = true;
                    system.runTimeout(()=>{
                        if (!(block.isValid() && block.typeId == patchData.blockID)) return;
                        const loc = block.location;
                        //break the block
                        dropPatchItems(patchData.itemID, block, patchData, block.permutation.getState(patchData.stateID));
                        block.dimension.runCommand("fill " + loc.x + " " + loc.y + " " + loc.z + " " + loc.x + " " + loc.y + " " + loc.z + " air [] destroy");
                    });
                }
            }
        }
    } catch  {}
});
let reload = 0;
world.beforeEvents.worldInitialize.subscribe((data)=>{
    //reload needed to stop crashes
    reload = reload + 1;
    if (reload > 1) return;
    //register each block state
    for (const comp of blockComponents){
        data.blockComponentRegistry.registerCustomComponent(comp.id, comp.code);
    }
});
