import { world, system, ItemStack, BlockPermutation, MolangVariableMap } from "@minecraft/server";
import { decrementstack } from "./bum_crops_custom_components.js";


// 			bonemeal foo

world.beforeEvents.worldInitialize.subscribe(initEvent => {
    initEvent.blockComponentRegistry.registerCustomComponent('bumble:bonemealable', {
        onPlayerInteract: e => {
            const block = e.block
            const player = e.player
            const item = e.player.getComponent('equippable').getEquipment('Mainhand')
            bonemeal(player, block, item, 5, false)
        },
    });
    initEvent.blockComponentRegistry.registerCustomComponent('bumble:bonemealable_sapling', {
        onPlayerInteract: e => {
            const block = e.block
            const player = e.player
            const item = e.player.getComponent('equippable').getEquipment('Mainhand')
            bonemeal(player, block, item, undefined, false)
        },
    });
    initEvent.blockComponentRegistry.registerCustomComponent('bumble:bonemealable_herb', {
        onPlayerInteract: e => {
            const block = e.block
            const player = e.player
            const item = e.player.getComponent('equippable').getEquipment('Mainhand')
            if (block.permutation.getState('bumble:has_leaves') >= 1) {
                bonemeal(player, block, item, 4, false)
            }
        },
    });
    initEvent.blockComponentRegistry.registerCustomComponent('bumble:bonemealable_special', {
        onPlayerInteract: e => {
            const block = e.block
            const player = e.player
            const item = e.player.getComponent('equippable').getEquipment('Mainhand')
            switch (block.typeId) {
                case "bumble:corn_crop":
                    bonemeal(player, block, item, 8, true, 1)
                    break;
                case "bumble:grape_crop":
                    bonemeal(player, block, item, 7, true, 1)
                    break;
                case "bumble:dragonfruit_crop":
                    bonemeal(player, block, item, 2, false)
                    break;
                case "bumble:strawberry_crop":
                    bonemeal(player, block, item, 4, false)
                    break;
                default:
                    //"bumble:pineapple_crop" || "bumble:shrooms_crop" || "bumble:rhubarb_crop" || "bumble:smokeleaf_crop"
                    bonemeal(player, block, item, 3, false)
                    break;
            }
        },
    });

//						Other plant/crop/herb stuff.


    initEvent.blockComponentRegistry.registerCustomComponent('bumble:add_seed', {
        onPlayerInteract: e => {
            const block = e.block
            const player = e.player
            const item = e.player.getComponent('equippable').getEquipment('Mainhand')
            if (item?.typeId == "bumble:corn_seeds" && block.permutation.getState('bumble:planted_seeds') < 3) {
                block.setPermutation(block.permutation.withState('bumble:planted_seeds', block.permutation.getState('bumble:planted_seeds') + 1))
                block.dimension.playSound('dig.grass', block.center())
                decrementstack(item, player)
            }
        },
    });
    initEvent.blockComponentRegistry.registerCustomComponent('bumble:pick_crop', {
        onPlayerInteract: e => {
            const block = e.block
            const player = e.player
            switch (block.typeId) {
                case "bumble:asparagus_crop":
                    pick_crop(player, block, '"plants/crops/asparagus_crop_no_seed"', "dig.grass")
                    break;
                case 'bumble:coffee_crop':
                    pick_crop(player, block, '"plants/crops/coffee_crop"', "dig.grass")
                    break;
                case 'bumble:corn_crop':
                    pick_crop(player, block, '"plants/crops/corn_crop_no_seed"', "dig.grass")
                    block.above(1).setPermutation(BlockPermutation.resolve('minecraft:air'))
                    break;
                case 'bumble:dragonfruit_crop':
                    pick_crop(player, block, '"plants/crops/dragonfruit_crop_harvest"', "dig.grass")
                    break;
                case 'bumble:grape_crop':
                    pick_crop(player, block, '"plants/crops/grape_crop_no_seed"', "dig.grass")
                    block.above(1).setPermutation(BlockPermutation.resolve('minecraft:air'))
                    break;
                case 'bumble:hops_crop':
                    pick_crop(player, block, '"plants/crops/hops_crop_no_seed"', "dig.grass")
                    break;
                case 'bumble:pepper_crop':
                    pick_crop(player, block, '"plants/crops/pepper_crop_no_seed"', "dig.grass")
                    break;
                case 'bumble:pineapple_crop':
                    pick_crop(player, block, '"plants/crops/pineapple_crop_no_seed"', "dig.grass")
                    break;
                case 'bumble:red_cabbage_crop':
                    pick_crop(player, block, '"plants/crops/red_cabbage_crop_no_seed"', "dig.grass")
                    break;
                case 'bumble:shrooms_crop':
                    pick_crop(player, block, '"plants/crops/shrooms_crop"', "dig.grass")
                    break;
                case 'bumble:smokeleaf_crop':
                    pick_crop(player, block, '"plants/crops/smokeleaf_crop_no_seed"', "dig.grass")
                    break;
                case 'bumble:tomato_crop':
                    pick_crop(player, block, '"plants/crops/tomato_crop_no_seed"', "dig.grass")
                    break;
                case 'bumble:rhubarb_crop':
                    pick_crop(player, block, '"plants/crops/rhubarb_crop_no_seed"', "dig.grass")
                    break;
                case 'bumble:turnip_crop':
                    pick_crop(player, block, '"plants/crops/turnip_crop_no_seed"', "dig.grass")
                    break;
                case 'bumble:strawberry_crop':
                    pick_crop(player, block, '"plants/crops/strawberry_crop_no_seed"', "dig.grass")
                    break;
                case 'bumble:zucchini_crop':
                    pick_crop(player, block, '"plants/crops/zucchini_crop_no_seed"', "dig.grass")
                    break;
            }
        },
    });
    initEvent.blockComponentRegistry.registerCustomComponent('bumble:pick_herb', {
        onPlayerInteract: e => {
            const block = e.block
            const player = e.player
			if( e.player.getComponent('equippable').getEquipment('Mainhand')?.typeId != 'minecraft:bone_meal' ) {
				if (block.permutation.getState('bumble:has_leaves') > 1) {
					switch (block.typeId) {
						case "bumble:pot_chamomile":
							pick_crop(player, block, '"plants/herbs/plant_chamomile_leaf"', "dig.grass")
							break;
						case "bumble:pot_lavender":
							pick_crop(player, block, '"plants/herbs/plant_lavender_leaf"', "dig.grass")
							break;
						case "bumble:pot_mint":
							pick_crop(player, block, '"plants/herbs/plant_mint_leaf"', "dig.grass")
							break;
					   case "bumble:pot_aloe":
							pick_crop(player, block, '"plants/plant_aloe_leaf"', "dig.grass")
							break;
						case "bumble:chamomile":
							pick_crop(player, block, '"plants/herbs/plant_chamomile_leaf"', "dig.grass")
							break;
						case "bumble:lavender":
							pick_crop(player, block, '"plants/herbs/plant_lavender_leaf"', "dig.grass")
							break;
						case "bumble:mint":
							pick_crop(player, block, '"plants/herbs/plant_mint_leaf"', "dig.grass")
							break;
						case "bumble:aloe":
							pick_crop(player, block, '"plants/plant_aloe_leaf"', "dig.grass")
							break;
					}
				}
				if (block.permutation.getState('bumble:has_leaves') == 1) {
					switch (block.typeId) {
						case "bumble:pot_chamomile":
							pick_crop(player, block, '"plants/herbs/plant_chamomile_death"', "dig.grass")
							block.setPermutation(BlockPermutation.resolve('minecraft:flower_pot'))
							break;
						case "bumble:pot_lavender":
							pick_crop(player, block, '"plants/herbs/plant_lavender_death"', "dig.grass")
							block.setPermutation(BlockPermutation.resolve('minecraft:flower_pot'))
							break;
						case "bumble:pot_mint":
							pick_crop(player, block, '"plants/herbs/plant_mint_death"', "dig.grass")
							block.setPermutation(BlockPermutation.resolve('minecraft:flower_pot'))
							break;
						case "bumble:pot_aloe":
							pick_crop(player, block, '"plants/plant_aloe_death"', "dig.grass")
							block.setPermutation(BlockPermutation.resolve('minecraft:flower_pot'))
							break;
						case "bumble:chamomile":
							pick_crop(player, block, '"plants/herbs/plant_chamomile_death"', "dig.grass")
							block.setPermutation(BlockPermutation.resolve('minecraft:air'))
							break;
						case "bumble:lavender":
							pick_crop(player, block, '"plants/herbs/plant_lavender_death"', "dig.grass")
							block.setPermutation(BlockPermutation.resolve('minecraft:air'))
							break;
						case "bumble:mint":
							pick_crop(player, block, '"plants/herbs/plant_mint_death"', "dig.grass")
							block.setPermutation(BlockPermutation.resolve('minecraft:air'))
							break;
						case "bumble:aloe":
							pick_crop(player, block, '"plants/plant_aloe_death"', "dig.grass")
							block.setPermutation(BlockPermutation.resolve('minecraft:air'))
							break;
					}
				}
			}
        },
    });
    initEvent.blockComponentRegistry.registerCustomComponent('bumble:pick_crop_top', {
        onPlayerInteract: e => {
            const block = e.block
            const player = e.player

            if (block.typeId == 'bumble:corn_crop_top_4') {
                pick_crop(player, block, '"plants/crops/corn_crop_no_seed"', "dig.grass")
                block.below(1).setPermutation(BlockPermutation.resolve('minecraft:air'))
            }
            if (block.typeId == 'bumble:grape_crop_top_7') {
                pick_crop(player, block, '"plants/crops/grape_crop"', "dig.grass")
                block.below(1).setPermutation(BlockPermutation.resolve('bumble:grape_crop_no_seed'))
            }
        },
    });
    initEvent.blockComponentRegistry.registerCustomComponent('bumble:cherry_leaves_drops', {
        onPlayerDestroy: e => {
            const block = e.block
            const item = e.player.getComponent('equippable').getEquipment('Mainhand')
            const player = e.player
            if (item?.typeId == "minecraft:shears") {
                player.runCommandAsync('loot spawn ' + JSON.stringify(block.location.x) + ' ' + JSON.stringify(block.location.y + 1) + ' ' + JSON.stringify(block.location.z) + ' loot "plants/trees/cherry/cherry_leaves_sheared"')
            }
            if (!(item?.typeId == "minecraft:shears")) {
                player.runCommandAsync('loot spawn ' + JSON.stringify(block.location.x) + ' ' + JSON.stringify(block.location.y + 1) + ' ' + JSON.stringify(block.location.z) + ' loot "plants/trees/cherry/cherry_leaves"')
            }

        },
    });
    initEvent.blockComponentRegistry.registerCustomComponent('bumble:lemon_leaves_drops', {
        onPlayerDestroy: e => {
            const block = e.block
            const item = e.player.getComponent('equippable').getEquipment('Mainhand')
            const player = e.player
            if (item?.typeId == "minecraft:shears") {
                player.runCommandAsync('loot spawn ' + JSON.stringify(block.location.x) + ' ' + JSON.stringify(block.location.y + 1) + ' ' + JSON.stringify(block.location.z) + ' loot "plants/trees/lemon/lemon_leaves_sheared"')
            }
            if (!(item?.typeId == "minecraft:shears")) {
                player.runCommandAsync('loot spawn ' + JSON.stringify(block.location.x) + ' ' + JSON.stringify(block.location.y + 1) + ' ' + JSON.stringify(block.location.z) + ' loot "plants/trees/lemon/lemon_leaves"')
            }

        },
    });
    initEvent.blockComponentRegistry.registerCustomComponent('bumble:maple_leaves_drops', {
        onPlayerDestroy: e => {
            const block = e.block
            const item = e.player.getComponent('equippable').getEquipment('Mainhand')
            const player = e.player
            if (item?.typeId == "minecraft:shears") {
                player.runCommandAsync('loot spawn ' + JSON.stringify(block.location.x) + ' ' + JSON.stringify(block.location.y + 1) + ' ' + JSON.stringify(block.location.z) + ' loot "plants/trees/maple/maple_leaves_sheared"')
            }
            if (!(item?.typeId == "minecraft:shears")) {
                player.runCommandAsync('loot spawn ' + JSON.stringify(block.location.x) + ' ' + JSON.stringify(block.location.y + 1) + ' ' + JSON.stringify(block.location.z) + ' loot "plants/trees/maple/maple_leaves"')
            }

        },
    });
    initEvent.blockComponentRegistry.registerCustomComponent('bumble:orange_leaves_drops', {
        onPlayerDestroy: e => {
            const block = e.block
            const item = e.player.getComponent('equippable').getEquipment('Mainhand')
            const player = e.player
            if (item?.typeId == "minecraft:shears") {
                player.runCommandAsync('loot spawn ' + JSON.stringify(block.location.x) + ' ' + JSON.stringify(block.location.y + 1) + ' ' + JSON.stringify(block.location.z) + ' loot "plants/trees/orange/orange_leaves_sheared"')
            }
            if (!(item?.typeId == "minecraft:shears")) {
                player.runCommandAsync('loot spawn ' + JSON.stringify(block.location.x) + ' ' + JSON.stringify(block.location.y + 1) + ' ' + JSON.stringify(block.location.z) + ' loot "plants/trees/orange/orange_leaves"')
            }

        },
    });
    initEvent.blockComponentRegistry.registerCustomComponent('bumble:peach_leaves_drops', {
        onPlayerDestroy: e => {
            const block = e.block
            const item = e.player.getComponent('equippable').getEquipment('Mainhand')
            const player = e.player
            if (item?.typeId == "minecraft:shears") {
                player.runCommandAsync('loot spawn ' + JSON.stringify(block.location.x) + ' ' + JSON.stringify(block.location.y + 1) + ' ' + JSON.stringify(block.location.z) + ' loot "plants/trees/peach/peach_leaves_sheared"')
            }
            if (!(item?.typeId == "minecraft:shears")) {
                player.runCommandAsync('loot spawn ' + JSON.stringify(block.location.x) + ' ' + JSON.stringify(block.location.y + 1) + ' ' + JSON.stringify(block.location.z) + ' loot "plants/trees/peach/peach_leaves"')
            }

        },
    });
    initEvent.blockComponentRegistry.registerCustomComponent('bumble:pear_leaves_drops', {
        onPlayerDestroy: e => {
            const block = e.block
            const item = e.player.getComponent('equippable').getEquipment('Mainhand')
            const player = e.player
            if (item?.typeId == "minecraft:shears") {
                player.runCommandAsync('loot spawn ' + JSON.stringify(block.location.x) + ' ' + JSON.stringify(block.location.y + 1) + ' ' + JSON.stringify(block.location.z) + ' loot "plants/trees/pear/pear_leaves_sheared"')
            }
            if (!(item?.typeId == "minecraft:shears")) {
                player.runCommandAsync('loot spawn ' + JSON.stringify(block.location.x) + ' ' + JSON.stringify(block.location.y + 1) + ' ' + JSON.stringify(block.location.z) + ' loot "plants/trees/pear/pear_leaves"')
            }

        },
    });
    initEvent.blockComponentRegistry.registerCustomComponent('bumble:grow', {
        onRandomTick: e => {
            const block = e.block
            let num = Math.round(Math.random() * 8)
            if (!(block.hasTag('bumble:herb'))) {
                if (num <= 1) {
                    if (block.permutation.getState('bumble:growth_stage') < 5) {
                        block.setPermutation(block.permutation.withState('bumble:growth_stage', block.permutation.getState('bumble:growth_stage') + 1))
                    }
                }
            }
            else {
                if (num <= 1) {
                    if (block.permutation.getState('bumble:has_leaves') < 4) {
                        block.setPermutation(block.permutation.withState('bumble:has_leaves', block.permutation.getState('bumble:has_leaves') + 1))
                    }
                }
            }
        },
    });
    initEvent.blockComponentRegistry.registerCustomComponent('bumble:grow_sapling', {
        onRandomTick: e => {
            const block = e.block
            grow_sapling(block)
        },
    });
    initEvent.blockComponentRegistry.registerCustomComponent('bumble:grow_special', {
        onRandomTick: e => {
            const block = e.block
            if (block.above(1).isAir || block.above(1).hasTag('bumble:replaceable')) {
                if (block.typeId == 'bumble:corn_crop') {
                    grow_special(block, 4, 8)
                }
                if (block.typeId == 'bumble:grape_crop') {
                    grow_special(block, 10, 6)
                }
                if (block.typeId == 'bumble:pineapple_crop') {
                    grow_special(block, 6, 3)
                }
                if (block.typeId == 'bumble:shrooms_crop') {
                    grow_special(block, 8, 3)
                }
                if (block.typeId == 'bumble:rhubarb_crop') {
                    grow_special(block, 8, 3)
                }
                if (block.typeId == 'bumble:smokeleaf_crop') {
                    grow_special(block, 10, 3)
                }
                if (block.typeId == 'bumble:strawberry_crop') {
                    grow_special(block, 8, 4)
                }
            }
        },
    });


});


//						functions


function bonemeal(player, block, item, max_growth, should_check_air, how_high) {
    if (how_high == undefined) {
        how_high = 0
    }
    switch (should_check_air) {
        case true:
            if (block.above(how_high).isAir || block.above(how_high).hasTag('bumble:replaceable')) {
            }
            else {
                break;
            }
        case false:
            if (item?.typeId == "minecraft:bone_meal" && block.permutation.getState('bumble:growth_stage') < max_growth) {
                if (block.permutation.getState('bumble:growth_stage') >= (max_growth - 3)) {
                    block.setPermutation(block.permutation.withState('bumble:growth_stage', block.permutation.getState('bumble:growth_stage') + 1))
                    if (block.typeId == 'bumble:corn_crop') {
                        corn_top(block)
                    }
                    if (block.typeId == 'bumble:grape_crop') {
                        grape_top(block)
                    }
                }
                else {
                    let num = Math.round(Math.random() * 2) + 1
                    block.setPermutation(block.permutation.withState('bumble:growth_stage', block.permutation.getState('bumble:growth_stage') + num))
                    if (block.typeId == 'bumble:corn_crop') {
                        corn_top(block)
                    }
                    if (block.typeId == 'bumble:grape_crop') {
                        grape_top(block)
                    }
                }
                block.dimension.spawnParticle('minecraft:crop_growth_emitter', block.center())
                block.dimension.playSound('item.bone_meal.use', block.center())
                decrementstack(item, player)
                break;
            }
            if (item?.typeId == "minecraft:bone_meal" && block.permutation.getState('bumble:has_leaves') < max_growth) {
                if (block.permutation.getState('bumble:has_leaves') >= (max_growth - 2)) {
                    block.setPermutation(block.permutation.withState('bumble:has_leaves', block.permutation.getState('bumble:has_leaves') + 1))
                }
                else {
                    let num = Math.round(Math.random() * 1) + 1
                    block.setPermutation(block.permutation.withState('bumble:has_leaves', block.permutation.getState('bumble:has_leaves') + num))
                }
                block.dimension.spawnParticle('minecraft:crop_growth_emitter', block.center())
                block.dimension.playSound('item.bone_meal.use', block.center())
                decrementstack(item, player)
                break;
            }
            if (item?.typeId == "minecraft:bone_meal" && block.hasTag('bumble:sapling')) {

                let num = Math.round(Math.random() * 10)
                if (num < 1) {
                    grow_sapling(block)
                }
                block.dimension.spawnParticle('minecraft:crop_growth_emitter', block.center())
                block.dimension.playSound('item.bone_meal.use', block.center())
                decrementstack(item, player)
                break;
            }
    }
}



function grow_special(block, random, max_growth) {
    let num = Math.round(Math.random() * random)
    if (num <= 1) {
        if (block.above(1).isAir || block.above(1).hasTag('bumble:replaceable')) {
            if (block.permutation.getState('bumble:growth_stage') < max_growth) {
                block.setPermutation(block.permutation.withState('bumble:growth_stage', block.permutation.getState('bumble:growth_stage') + 1))
                if (block.typeId == 'bumble:corn_crop') {
                    corn_top(block)
                }
                if (block.typeId == 'bumble:grape_crop') {
                    grape_top(block)
                }
            }
        }
    }
}

function grow_sapling(block) {
    //let num = Math.round(Math.random() * 2)
    let num = 0
    if (num < 1) {
        let num = Math.round(Math.random() * 4)
        if (block.typeId == "bumble:cherry_sapling") {
            tree_grow(block, num, "cherry", 3, 3)
        }
        if (block.typeId == "bumble:lemon_sapling") {
            tree_grow(block, num, "lemon", 2, 2)
        }
        if (block.typeId == "bumble:maple_sapling") {
            tree_grow(block, num, "maple", 6, 6)
        }
        if (block.typeId == "bumble:orange_sapling") {
            tree_grow(block, num, "orange", 2, 2)
        }
        if (block.typeId == "bumble:peach_sapling") {
            tree_grow(block, num, "peach", 2, 2)
        }
        if (block.typeId == "bumble:pear_sapling") {
            tree_grow(block, num, "pear", 2, 2)
        }
    }
}

function tree_grow(block, num, type, offsetx, offsetz) {
    if (num == 4) {
        block.dimension.runCommandAsync("structure load " + type + "_tree1 " + JSON.stringify(block.location.x - offsetx) + ' ' + JSON.stringify(block.location.y) + ' ' + JSON.stringify(block.location.z - offsetz) + " 0_degrees")
    }
    if (num == 3) {
        block.dimension.runCommandAsync("structure load " + type + "_tree1 " + JSON.stringify(block.location.x - offsetx) + ' ' + JSON.stringify(block.location.y) + ' ' + JSON.stringify(block.location.z - offsetz) + " 90_degrees")
    }
    if (num == 2) {
        block.dimension.runCommandAsync("structure load " + type + "_tree1 " + JSON.stringify(block.location.x - offsetx) + ' ' + JSON.stringify(block.location.y) + ' ' + JSON.stringify(block.location.z - offsetz) + " 180_degrees")
    }
    if (num <= 1) {
        block.dimension.runCommandAsync("structure load " + type + "_tree1 " + JSON.stringify(block.location.x - offsetx) + ' ' + JSON.stringify(block.location.y) + ' ' + JSON.stringify(block.location.z - offsetz) + " 270_degrees")
    }
}

function pick_crop(player, block, loot, sound) {
    player.runCommandAsync('loot spawn ' + JSON.stringify(block.location.x) + ' ' + JSON.stringify(block.location.y + 1) + ' ' + JSON.stringify(block.location.z) + ' loot ' + loot)
    player.runCommandAsync('playsound ' + sound + '@a ' + JSON.stringify(block.location.x) + ' ' + JSON.stringify(block.location.y) + ' ' + JSON.stringify(block.location.z) + ' 0.5')
    if (block.permutation.getState('bumble:growth_stage') != undefined) {
        block.setPermutation(block.permutation.withState('bumble:growth_stage', 0))
    }
    if (block.permutation.getState('bumble:has_leaves') != undefined) {
        block.setPermutation(block.permutation.withState('bumble:has_leaves', block.permutation.getState('bumble:has_leaves') - 1))
    }
}

function corn_top(block) {
    if (block.permutation.getState('bumble:growth_stage') == 4) {
        block.above(1).setPermutation(BlockPermutation.resolve('bumble:corn_crop_top_1'))
        block.above(1).setPermutation(block.above(1).permutation.withState('minecraft:cardinal_direction', block.permutation.getState('minecraft:cardinal_direction')))
        block.above(1).setPermutation(block.above(1).permutation.withState('bumble:planted_seeds', block.permutation.getState('bumble:planted_seeds')))
    }
    if (block.permutation.getState('bumble:growth_stage') == 5) {
        block.above(1).setPermutation(BlockPermutation.resolve('bumble:corn_crop_top_2'))
        block.above(1).setPermutation(block.above(1).permutation.withState('minecraft:cardinal_direction', block.permutation.getState('minecraft:cardinal_direction')))
        block.above(1).setPermutation(block.above(1).permutation.withState('bumble:planted_seeds', block.permutation.getState('bumble:planted_seeds')))
    }
    if (block.permutation.getState('bumble:growth_stage') == 6) {
        block.above(1).setPermutation(BlockPermutation.resolve('bumble:corn_crop_top_3'))
        block.above(1).setPermutation(block.above(1).permutation.withState('minecraft:cardinal_direction', block.permutation.getState('minecraft:cardinal_direction')))
        block.above(1).setPermutation(block.above(1).permutation.withState('bumble:planted_seeds', block.permutation.getState('bumble:planted_seeds')))
    }
    if (block.permutation.getState('bumble:growth_stage') == 7) {
        block.above(1).setPermutation(BlockPermutation.resolve('bumble:corn_crop_top_4'))
        block.above(1).setPermutation(block.above(1).permutation.withState('minecraft:cardinal_direction', block.permutation.getState('minecraft:cardinal_direction')))
        block.above(1).setPermutation(block.above(1).permutation.withState('bumble:planted_seeds', block.permutation.getState('bumble:planted_seeds')))
    }
}


function grape_top(block) {
    if (block.permutation.getState('bumble:growth_stage') == 1) {
        block.above(1).setPermutation(BlockPermutation.resolve('bumble:grape_crop_top_2'))
        block.above(1).setPermutation(block.above(1).permutation.withState('minecraft:cardinal_direction', block.permutation.getState('minecraft:cardinal_direction')))
    }
    if (block.permutation.getState('bumble:growth_stage') == 2) {
        block.above(1).setPermutation(BlockPermutation.resolve('bumble:grape_crop_top_3'))
        block.above(1).setPermutation(block.above(1).permutation.withState('minecraft:cardinal_direction', block.permutation.getState('minecraft:cardinal_direction')))
    }
    if (block.permutation.getState('bumble:growth_stage') == 3) {
        block.above(1).setPermutation(BlockPermutation.resolve('bumble:grape_crop_top_4'))
        block.above(1).setPermutation(block.above(1).permutation.withState('minecraft:cardinal_direction', block.permutation.getState('minecraft:cardinal_direction')))
    }
    if (block.permutation.getState('bumble:growth_stage') == 4) {
        block.above(1).setPermutation(BlockPermutation.resolve('bumble:grape_crop_top_5'))
        block.above(1).setPermutation(block.above(1).permutation.withState('minecraft:cardinal_direction', block.permutation.getState('minecraft:cardinal_direction')))
    }
    if (block.permutation.getState('bumble:growth_stage') == 5) {
        block.above(1).setPermutation(BlockPermutation.resolve('bumble:grape_crop_top_6'))
        block.above(1).setPermutation(block.above(1).permutation.withState('minecraft:cardinal_direction', block.permutation.getState('minecraft:cardinal_direction')))
    }
    if (block.permutation.getState('bumble:growth_stage') == 6) {
        block.above(1).setPermutation(BlockPermutation.resolve('bumble:grape_crop_top_7'))
        block.above(1).setPermutation(block.above(1).permutation.withState('minecraft:cardinal_direction', block.permutation.getState('minecraft:cardinal_direction')))
    }
}