import { world, BlockPermutation, Dimension, Block, system, ItemStack } from "@minecraft/server";
import { bush_Manager, branch_Manager, cactus_Manager } from './fence_Manager_rns'
import { decrementstack, sifting, adjustItemDamage } from './nature'
import { itemMine } from './itemDurability.js'

world.beforeEvents.worldInitialize.subscribe(initEvent => {
    initEvent.blockComponentRegistry.registerCustomComponent('rns:break_leaf', {
        onStepOn: e => {
            const player = e.entity
            const block = e.block
            if (block?.typeId == 'rns:bush') {
                if (block.permutation.getState('rns:leaf') == 0) {
                    if (Math.floor(Math.random() * 8) < 1) {
                        block.setPermutation(block.permutation.withState('rns:leaf', 1))
                        player.runCommandAsync('playsound block.sweet_berry_bush.break @p ' + JSON.stringify(block.location.x) + ' ' + JSON.stringify(block.location.y) + ' ' + JSON.stringify(block.location.z))
                        player.runCommandAsync('loot spawn ' + JSON.stringify(block.location.x) + ' ' + JSON.stringify(block.location.y + 1) + ' ' + JSON.stringify(block.location.z) + ' loot leaf')
                    }
                }
            }
            if (block?.typeId == 'rns:driedbush') {
                if (block.permutation.getState('rns:leaf') == 0) {
                    block.setPermutation(block.permutation.withState('rns:leaf', 1))
                    player.runCommandAsync('playsound block.sweet_berry_bush.break @p ' + JSON.stringify(block.location.x) + ' ' + JSON.stringify(block.location.y) + ' ' + JSON.stringify(block.location.z))
                    player.runCommandAsync('loot spawn ' + JSON.stringify(block.location.x) + ' ' + JSON.stringify(block.location.y + 1) + ' ' + JSON.stringify(block.location.z) + ' loot driedleaf')
                }
            }
            if (block?.typeId == 'rns:nimberrybush') {
                if (block.permutation.getState('rns:leaf') == 1) {
                    block.setPermutation(block.permutation.withState('rns:leaf', 0))
                    player.runCommandAsync('playsound block.sweet_berry_bush.break @p ' + JSON.stringify(block.location.x) + ' ' + JSON.stringify(block.location.y) + ' ' + JSON.stringify(block.location.z))
                    player.runCommandAsync('loot spawn ' + JSON.stringify(block.location.x) + ' ' + JSON.stringify(block.location.y + 1) + ' ' + JSON.stringify(block.location.z) + ' loot Nimberry')
                }
            }
        },

    });
    initEvent.blockComponentRegistry.registerCustomComponent('rns:pickberry', {
        onPlayerInteract: e => {
            const player = e.player
            const block = e.block

            if (block?.typeId == 'rns:nimberrybush') {
                if (block.permutation.getState('rns:leaf') == 1) {
                    block.setPermutation(block.permutation.withState('rns:leaf', 0))
                    player.runCommandAsync('playsound block.sweet_berry_bush.pick @p ' + JSON.stringify(block.location.x) + ' ' + JSON.stringify(block.location.y) + ' ' + JSON.stringify(block.location.z))
                    player.runCommandAsync('loot spawn ' + JSON.stringify(block.location.x) + ' ' + JSON.stringify(block.location.y + 1) + ' ' + JSON.stringify(block.location.z) + ' loot Nimberry')
                }
            }
        },
    });
    initEvent.blockComponentRegistry.registerCustomComponent('rns:pickwheatfrombeachgrass', {
        onPlayerInteract: e => {
            const player = e.player
            const block = e.block

            if (block.permutation.getState('rns:wheatless') == 0) {
                block.setPermutation(block.permutation.withState('rns:wheatless', 1))
                block.dimension.spawnItem(new ItemStack("minecraft:wheat"), { x: block.location.x + 0.5, y: block.location.y + 0.3, z: block.location.z + 0.5 })
                player.runCommandAsync('playsound block.sweet_berry_bush.pick @p ' + JSON.stringify(block.location.x) + ' ' + JSON.stringify(block.location.y) + ' ' + JSON.stringify(block.location.z))
            }

        },
    });



    initEvent.blockComponentRegistry.registerCustomComponent('rns:regrow', {
        onRandomTick: e => {
            const block = e.block
            if (block.permutation.getState('rns:berrygrowth') < 4) {
                block.setPermutation(block.permutation.withState('rns:berrygrowth', block.permutation.getState('rns:berrygrowth') + 1))
            }
            if (block.permutation.getState('rns:leaf') == 0 && block.permutation.getState('rns:berrygrowth') == 4) {
                block.setPermutation(block.permutation.withState('rns:leaf', 1))
            }
        },
    });
    initEvent.blockComponentRegistry.registerCustomComponent('rns:berryify', {
        onPlace: e => {
            const block = e.block
            if (block.permutation.getState('rns:firstberry') == 0) {
                block.setPermutation(block.permutation.withState('rns:leaf', 1))
                block.setPermutation(block.permutation.withState('rns:firstberry', 1))
            }
        },
    });
    initEvent.blockComponentRegistry.registerCustomComponent('rns:snowify', {
        onTick: e => {
            const block = e.block
            if (block.above(1).matches("minecraft:snow_layer")) {
                block.dimension.runCommand("fill " + JSON.stringify(block.location.x) + ' ' + JSON.stringify(block.location.y + 1) + ' ' + JSON.stringify(block.location.z) + ' ' + JSON.stringify(block.location.x) + ' ' + JSON.stringify(block.location.y + 1) + ' ' + JSON.stringify(block.location.z) + " air")
                if (block.permutation.getState('rns:snow') == 0) {
                    block.setPermutation(block.permutation.withState('rns:snow', 1))
                }
            }

        },
    });
    initEvent.blockComponentRegistry.registerCustomComponent('rns:randomdirection', {
        onPlace: e => {
            let num = Math.round(Math.random() * 4)
            const block = e.block
            if (num == 4) {
                block.setPermutation(block.permutation.withState('minecraft:cardinal_direction', "north"))
            }
            if (num == 3) {
                block.setPermutation(block.permutation.withState('minecraft:cardinal_direction', "south"))
            }
            if (num == 2) {
                block.setPermutation(block.permutation.withState('minecraft:cardinal_direction', "east"))
            }
            if (num <= 1) {
                block.setPermutation(block.permutation.withState('minecraft:cardinal_direction', "west"))
            }
        },
    });
    initEvent.blockComponentRegistry.registerCustomComponent('rns:dryingtime', {
        onTick: e => {
            const block = e.block
            block.setPermutation(block.permutation.withState('rns:dried', block.permutation.getState('rns:input')))
            block.setPermutation(block.permutation.withState('rns:input', 0))
            block.dimension.runCommand('playsound random.fizz @p ' + JSON.stringify(block.location.x) + ' ' + JSON.stringify(block.location.y) + ' ' + JSON.stringify(block.location.z) + ' 0.5')
            if (block.permutation.getState('rns:salt') > 0) {
                block.setPermutation(block.permutation.withState('rns:salt', block.permutation.getState('rns:salt') - 1))
            }
        },
    });
    initEvent.blockComponentRegistry.registerCustomComponent('rns:sizzle', {
        onRandomTick: e => {
            const block = e.block
            block.dimension.spawnParticle('minecraft:water_evaporation_bucket_emitter', block.center())
        },
    });
    initEvent.blockComponentRegistry.registerCustomComponent('rns:defrost', {
        onPlayerDestroy: e => {
            const block = e.block
            block.setPermutation(BlockPermutation.resolve('minecraft:mud'))
        },
    });
    initEvent.blockComponentRegistry.registerCustomComponent('rns:hollycandleflame', {
        onTick: e => {
            const block = e.block
            var x = block.location.x
            var y = block.location.y
            var z = block.location.z
            if (block.permutation.getState('minecraft:cardinal_direction') == 'south') {
                block.dimension.spawnParticle('minecraft:candle_flame_particle', { x: x + 0.25, y: y + 0.5, z: z + 0.175 })
                if (block.permutation.getState('rns:addbone') > 0) {
                    block.dimension.spawnParticle('minecraft:candle_flame_particle', { x: x + 0.575, y: y + 0.425, z: z + 0.375 })
                }
                if (block.permutation.getState('rns:addbone') > 1) {
                    block.dimension.spawnParticle('minecraft:candle_flame_particle', { x: x + 0.45, y: y + 0.375, z: z + 0.625 })
                }
                if (block.permutation.getState('rns:addbone') > 2) {
                    block.dimension.spawnParticle('minecraft:candle_flame_particle', { x: x + 0.75, y: y + 0.25, z: z + 0.8 })
                }
            }
            if (block.permutation.getState('minecraft:cardinal_direction') == 'north') {
                block.dimension.spawnParticle('minecraft:candle_flame_particle', { x: x + 0.75, y: y + 0.5, z: z + 0.825 })
                if (block.permutation.getState('rns:addbone') > 0) {
                    block.dimension.spawnParticle('minecraft:candle_flame_particle', { x: x + 0.45, y: y + 0.425, z: z + 0.625 })
                }
                if (block.permutation.getState('rns:addbone') > 1) {
                    block.dimension.spawnParticle('minecraft:candle_flame_particle', { x: x + 0.575, y: y + 0.375, z: z + 0.375 })
                }
                if (block.permutation.getState('rns:addbone') > 2) {
                    block.dimension.spawnParticle('minecraft:candle_flame_particle', { x: x + 0.25, y: y + 0.25, z: z + 0.2 })
                }
            }
            if (block.permutation.getState('minecraft:cardinal_direction') == 'west') {
                block.dimension.spawnParticle('minecraft:candle_flame_particle', { x: x + 0.2, y: y + 0.5, z: z + 0.75 })
                if (block.permutation.getState('rns:addbone') > 0) {
                    block.dimension.spawnParticle('minecraft:candle_flame_particle', { x: x + 0.425, y: y + 0.425, z: z + 0.4375 })
                }
                if (block.permutation.getState('rns:addbone') > 1) {
                    block.dimension.spawnParticle('minecraft:candle_flame_particle', { x: x + 0.625, y: y + 0.375, z: z + 0.5625 })
                }
                if (block.permutation.getState('rns:addbone') > 2) {
                    block.dimension.spawnParticle('minecraft:candle_flame_particle', { x: x + 0.825, y: y + 0.25, z: z + 0.2625 })
                }
            }
            if (block.permutation.getState('minecraft:cardinal_direction') == 'east') {
                block.dimension.spawnParticle('minecraft:candle_flame_particle', { x: x + 0.825, y: y + 0.5, z: z + 0.25 })
                if (block.permutation.getState('rns:addbone') > 0) {
                    block.dimension.spawnParticle('minecraft:candle_flame_particle', { x: x + 0.625, y: y + 0.425, z: z + 0.5625 })
                }
                if (block.permutation.getState('rns:addbone') > 1) {
                    block.dimension.spawnParticle('minecraft:candle_flame_particle', { x: x + 0.4, y: y + 0.375, z: z + 0.4375 })
                }
                if (block.permutation.getState('rns:addbone') > 2) {
                    block.dimension.spawnParticle('minecraft:candle_flame_particle', { x: x + 0.2, y: y + 0.25, z: z + 0.75 })
                }
            }
        },
    });
    initEvent.blockComponentRegistry.registerCustomComponent('rns:beegrootstem', {
        onPlace: e => {
            const block = e.block
            if (block.above(1).isAir) {
                block.dimension.setBlockType({ x: block.location.x, y: block.location.y + 1, z: block.location.z }, 'rns:beetrootstem')
            }
        },
    });
    initEvent.blockComponentRegistry.registerCustomComponent('rns:beachtallgrass', {
        onPlace: e => {
            const block = e.block
            if (block.above(1).isAir && block.typeId == 'rns:tallbeachgrassbottom') {
                block.dimension.setBlockType({ x: block.location.x, y: block.location.y + 1, z: block.location.z }, 'rns:tallbeachgrasstop')
                block.above(1).setPermutation(block.above(1).permutation.withState('minecraft:cardinal_direction', block.permutation.getState('minecraft:cardinal_direction')))
            }
            if (block.above(1).isAir && block.typeId == 'rns:tallbeachgrasswheatbottom') {
                block.dimension.setBlockType({ x: block.location.x, y: block.location.y + 1, z: block.location.z }, 'rns:tallbeachgrasstop_wheattipped')
                block.above(1).setPermutation(block.above(1).permutation.withState('minecraft:cardinal_direction', block.permutation.getState('minecraft:cardinal_direction')))
            }
        },
    });
    initEvent.blockComponentRegistry.registerCustomComponent('rns:primrose', {
        onPlace: e => {
            const block = e.block
            if (block.above(1).isAir && block.typeId == 'rns:redprimrosebottom') {
                block.dimension.setBlockType({ x: block.location.x, y: block.location.y + 1, z: block.location.z }, 'rns:redprimrosetop')
                block.above(1).setPermutation(block.above(1).permutation.withState('minecraft:cardinal_direction', block.permutation.getState('minecraft:cardinal_direction')))
            }
            if (block.above(1).isAir && block.typeId == 'rns:whiteprimrosebottom') {
                block.dimension.setBlockType({ x: block.location.x, y: block.location.y + 1, z: block.location.z }, 'rns:whiteprimrosetop')
                block.above(1).setPermutation(block.above(1).permutation.withState('minecraft:cardinal_direction', block.permutation.getState('minecraft:cardinal_direction')))
            }
        },
    });
    initEvent.blockComponentRegistry.registerCustomComponent('rns:on', {
        onPlayerInteract: e => {
            const block = e.block
            const item = e.player.getComponent('equippable').getEquipment('Mainhand')

            if (block.typeId == 'rns:paperblinds') {
                block.dimension.playSound('item.book.put', { x: block.location.x, y: block.location.y + 1, z: block.location.z })
                block.setPermutation(block.permutation.withState('rns:on', 1))
            }
            if (block.typeId == 'rns:paperlantern' && (item?.typeId == 'minecraft:torch' || item?.typeId == 'minecraft:fire_charge' || item?.typeId == 'minecraft:flint_and_steel')) {
                block.dimension.playSound('fire.ignite', { x: block.location.x, y: block.location.y + 1, z: block.location.z })
                block.setPermutation(BlockPermutation.resolve('rns:paperlanternlit'))
            }
            if (block.typeId == 'rns:rocksaltlamp') {
                block.dimension.playSound('step.basalt', { x: block.location.x, y: block.location.y + 1, z: block.location.z })
                block.setPermutation(block.permutation.withState('rns:on', 1))
            }
        },
    });
    initEvent.blockComponentRegistry.registerCustomComponent('rns:off', {
        onPlayerInteract: e => {
            const block = e.block
            block.setPermutation(block.permutation.withState('rns:on', 0))
            if (block.typeId == 'rns:paperblinds') {
                block.dimension.playSound('item.book.put', { x: block.location.x, y: block.location.y + 1, z: block.location.z })
            }
            if (block.typeId == 'rns:paperlanternlit') {
                block.dimension.playSound('random.fizz', { x: block.location.x, y: block.location.y + 1, z: block.location.z })
                block.setPermutation(BlockPermutation.resolve('rns:paperlantern'))
            }
            if (block.typeId == 'rns:rocksaltlamp') {
                block.dimension.playSound('step.basalt', { x: block.location.x, y: block.location.y + 1, z: block.location.z })
                block.setPermutation(block.permutation.withState('rns:on', 0))
            }
        },
    });
    initEvent.blockComponentRegistry.registerCustomComponent('rns:strip', {
        onPlayerInteract: e => {
            const block = e.block
            const player = e.player
            const item = e.player.getComponent('equippable').getEquipment('Mainhand')

            const inv = player.getComponent("minecraft:inventory").container
            const slot = player.selectedSlotIndex

            if (item.hasTag("minecraft:is_axe")) {

                if (block.hasTag('rns:branch') && !block.typeId.match("_stripped")) {

                    let direction = block.permutation.getState('minecraft:cardinal_direction')
                    let north = block.permutation.getState('rns:north')
                    let south = block.permutation.getState('rns:south')
                    let west = block.permutation.getState('rns:west')
                    let east = block.permutation.getState('rns:east')

                    block.setPermutation(BlockPermutation.resolve(block.typeId + "_stripped"))

                    if (!block.typeId.match('up')) {
                        block.setPermutation(block.permutation.withState('minecraft:cardinal_direction', direction))
                    }
                    else {
                        block.setPermutation(block.permutation.withState('rns:north', north))
                        block.setPermutation(block.permutation.withState('rns:south', south))
                        block.setPermutation(block.permutation.withState('rns:west', west))
                        block.setPermutation(block.permutation.withState('rns:east', east))
                    }

                    player.playSound("fall.wood")
                    adjustItemDamage(inv, item, 1, slot)
                }
            }
        },
    });
    //Coarse Sifter
    initEvent.blockComponentRegistry.registerCustomComponent('rns:coarsesifter', {
        onPlayerInteract: e => {
            const block = e.block
            const player = e.player
            const item = e.player.getComponent('equippable').getEquipment('Mainhand')

            if (item?.typeId == 'minecraft:dirt') {
                sifting(block, player, 'siftdirt', item, item.typeId)

            }
            if (item?.typeId == 'minecraft:sand') {
                sifting(block, player, 'sandsift', item, item.typeId)

            }
            if (item?.typeId == 'minecraft:snow') {
                sifting(block, player, 'snowsift', item, item.typeId)

            }
        },
    });

    initEvent.blockComponentRegistry.registerCustomComponent('rns:andesitemortar', {
        onPlayerInteract: e => {
            const block = e.block
            const player = e.player
            const item = e.player.getComponent('equippable').getEquipment('Mainhand')

            if (block.typeId == 'rns:andesitemortar' && block?.permutation.getState('rns:input') < 1 && block?.permutation.getState('rns:output') < 1) {
                if (item?.typeId == 'rns:pinkrocksalt') {

                    mortarplop(item, player, block, 'rns:pinkrocksalt', 1)

                }
                if (item?.hasTag('rns:red') || item?.typeId == 'minecraft:beetroot' || item?.typeId == 'minecraft:melon_slice' || item?.typeId == 'minecraft:nether_wart' || item?.typeId == 'minecraft:crimson_roots' || item?.typeId == 'minecraft:sweet_berries') {

                    mortarplop(item, player, block, item.typeId, 2)

                }
                if (item?.typeId == 'minecraft:bone' || item?.typeId == 'minecraft:nautilus_shell' || item?.typeId.match('seashell')) {

                    mortarplop(item, player, block, item.typeId, 3)

                }
                if (item?.typeId == 'minecraft:bone_meal' || item?.hasTag('rns:white')) {

                    mortarplop(item, player, block, item.typeId, 4)

                }
                if (item?.hasTag('rns:lightgray')) {

                    mortarplop(item, player, block, item.typeId, 5)

                }
                if (item?.hasTag('rns:yellow')) {

                    mortarplop(item, player, block, item.typeId, 6)

                }
                if (item?.hasTag('rns:orange')) {

                    mortarplop(item, player, block, item.typeId, 7)

                }
                if (item?.hasTag('rns:lightblue') || item?.typeId == 'minecraft:warped_roots') {

                    mortarplop(item, player, block, item.typeId, 8)

                }
                if (item?.hasTag('rns:blue') || item?.typeId == 'minecraft:lapis_lazuli') {

                    mortarplop(item, player, block, item.typeId, 9)

                }
                if (item?.hasTag('rns:magenta')) {

                    mortarplop(item, player, block, item.typeId, 10)

                }
                if (item?.hasTag('rns:pink')) {

                    mortarplop(item, player, block, item.typeId, 11)

                }
                if (item?.hasTag('rns:black') || item?.typeId == 'minecraft:ink_sac') {

                    mortarplop(item, player, block, item.typeId, 12)

                }
                if (item?.typeId == 'minecraft:sugar_cane') {

                    mortarplop(item, player, block, item.typeId, 13)

                }

            }
            if (block.typeId == 'rns:andesitemortar' && block?.permutation.getState('rns:input') > 0 && block?.permutation.getState('rns:output') < 1) {
                if (item?.hasTag('rns:pestle')) {
                    if (block?.permutation.getState('rns:input') == 1) {

                        mortarcrush(player, block, 1)

                    }
                    if (block?.permutation.getState('rns:input') == 2) {

                        mortarcrush(player, block, 2)

                    }
                    if (block?.permutation.getState('rns:input') == 3) {

                        mortarcrush(player, block, 3)

                    }
                    if (block?.permutation.getState('rns:input') == 4) {

                        mortarcrush(player, block, 5)

                    }
                    if (block?.permutation.getState('rns:input') == 5) {

                        mortarcrush(player, block, 6)

                    }
                    if (block?.permutation.getState('rns:input') == 6) {

                        mortarcrush(player, block, 7)

                    }
                    if (block?.permutation.getState('rns:input') == 7) {

                        mortarcrush(player, block, 8)

                    }
                    if (block?.permutation.getState('rns:input') == 8) {

                        mortarcrush(player, block, 9)

                    }
                    if (block?.permutation.getState('rns:input') == 9) {

                        mortarcrush(player, block, 10)

                    }
                    if (block?.permutation.getState('rns:input') == 10) {

                        mortarcrush(player, block, 11)

                    }
                    if (block?.permutation.getState('rns:input') == 11) {

                        mortarcrush(player, block, 12)

                    }
                    if (block?.permutation.getState('rns:input') == 12) {

                        mortarcrush(player, block, 13)

                    }
                    if (block?.permutation.getState('rns:input') == 13) {

                        mortarcrush(player, block, 14)

                    }

                }
            }
            if (block.typeId == 'rns:andesitemortar' && block?.permutation.getState('rns:input') < 1 && block?.permutation.getState('rns:output') > 0) {
                if (block?.permutation.getState('rns:output') == 1) {


                    mortartake(player, block, 'powdersalt')

                }
                if (block?.permutation.getState('rns:output') == 2) {


                    mortartake(player, block, 'reddye')

                }
                if (block?.permutation.getState('rns:output') == 3) {


                    if (item?.hasTag('rns:pestle')) {
                        mortarcrush(player, block, 4)
                    }
                    else {
                        mortartake(player, block, 'bone_meal')
                    }

                }
                if (block?.permutation.getState('rns:output') == 4) {


                    mortartake(player, block, 'whitedye3x')

                }
                if (block?.permutation.getState('rns:output') == 5) {


                    mortartake(player, block, 'whitedye')

                }
                if (block?.permutation.getState('rns:output') == 6) {


                    mortartake(player, block, 'lightgraydye')

                }
                if (block?.permutation.getState('rns:output') == 7) {


                    mortartake(player, block, 'yellowdye')

                }
                if (block?.permutation.getState('rns:output') == 8) {


                    mortartake(player, block, 'orangedye')

                }
                if (block?.permutation.getState('rns:output') == 9) {


                    mortartake(player, block, 'lightbluedye')

                }
                if (block?.permutation.getState('rns:output') == 10) {


                    mortartake(player, block, 'bluedye')

                }
                if (block?.permutation.getState('rns:output') == 11) {


                    mortartake(player, block, 'magentadye')

                }
                if (block?.permutation.getState('rns:output') == 12) {


                    mortartake(player, block, 'pinkdye')

                }
                if (block?.permutation.getState('rns:output') == 13) {


                    mortartake(player, block, 'blackdye')

                }
                if (block?.permutation.getState('rns:output') == 14) {


                    mortartake(player, block, 'sugar')

                }
            }
        },
    });

    initEvent.blockComponentRegistry.registerCustomComponent('rns:sequentialblocks', {
        onPlayerInteract: e => {
            const block = e.block
            const player = e.player
            const item = e.player.getComponent('equippable').getEquipment('Mainhand')
            const inv = player.getComponent("minecraft:inventory").container
            const slot = player.selectedSlotIndex

            if (block.typeId == 'rns:pineneedles') {
                if (item?.typeId == 'rns:pineneedlesitem' && block?.permutation.getState('rns:addbone') < 3) {


                    block.setPermutation(block.permutation.withState('rns:addbone', block.permutation.getState('rns:addbone') + 1))
                    decrementstack(item, player, "rns:pineneedlesitem")
                    player.runCommandAsync('playsound block.sweet_berry_bush.place @p ' + JSON.stringify(block.location.x) + ' ' + JSON.stringify(block.location.y) + ' ' + JSON.stringify(block.location.z) + ' 0.5')

                }
            }

            if (block.typeId == 'rns:greensucculents') {
                if (item?.typeId == 'rns:beach_greensucculentitem' && block?.permutation.getState('rns:addbone') < 3) {


                    block.setPermutation(block.permutation.withState('rns:addbone', block.permutation.getState('rns:addbone') + 1))
                    decrementstack(item, player, item.typeId)
                    player.runCommandAsync('playsound block.cloth.place @p ' + JSON.stringify(block.location.x) + ' ' + JSON.stringify(block.location.y) + ' ' + JSON.stringify(block.location.z) + ' 0.5')

                }
            }

            if (block.typeId == 'rns:whitesucculents') {
                if (item?.typeId == 'rns:beach_whitesucculentitem' && block?.permutation.getState('rns:addbone') < 3) {


                    block.setPermutation(block.permutation.withState('rns:addbone', block.permutation.getState('rns:addbone') + 1))
                    decrementstack(item, player, item.typeId)
                    player.runCommandAsync('playsound block.cloth.place @p ' + JSON.stringify(block.location.x) + ' ' + JSON.stringify(block.location.y) + ' ' + JSON.stringify(block.location.z) + ' 0.5')

                }
            }

            if (block.typeId == 'rns:flametippedwhitesucculents') {
                if (item?.typeId == 'rns:beach_flametippedwhitesucculentitem' && block?.permutation.getState('rns:addbone') < 3) {


                    block.setPermutation(block.permutation.withState('rns:addbone', block.permutation.getState('rns:addbone') + 1))
                    decrementstack(item, player, item.typeId)
                    player.runCommandAsync('playsound block.cloth.place @p ' + JSON.stringify(block.location.x) + ' ' + JSON.stringify(block.location.y) + ' ' + JSON.stringify(block.location.z) + ' 0.5')

                }
            }

            if (block.typeId == 'rns:flametippedgreensucculents') {
                if (item?.typeId == 'rns:beach_flametippedgreensucculentitem' && block?.permutation.getState('rns:addbone') < 3) {


                    block.setPermutation(block.permutation.withState('rns:addbone', block.permutation.getState('rns:addbone') + 1))
                    decrementstack(item, player, item.typeId)
                    player.runCommandAsync('playsound block.cloth.place @p ' + JSON.stringify(block.location.x) + ' ' + JSON.stringify(block.location.y) + ' ' + JSON.stringify(block.location.z) + ' 0.5')

                }
            }

            if (block.typeId == 'rns:bones') {
                if (item?.typeId == 'minecraft:bone' && block?.permutation.getState('rns:addbone') < 2) {


                    block.setPermutation(block.permutation.withState('rns:addbone', block.permutation.getState('rns:addbone') + 1))
                    decrementstack(item, player, item.typeId)
                    player.runCommandAsync('playsound dig.bone_block @p ' + JSON.stringify(block.location.x) + ' ' + JSON.stringify(block.location.y) + ' ' + JSON.stringify(block.location.z) + ' 0.5')

                }
            }

            if (block.typeId == 'rns:hollycandle') {
                if (item?.typeId == 'minecraft:candle' && block?.permutation.getState('rns:addbone') < 3) {


                    block.setPermutation(block.permutation.withState('rns:addbone', block.permutation.getState('rns:addbone') + 1))
                    decrementstack(item, player, item.typeId)
                    player.runCommandAsync('playsound use.candle @p ' + JSON.stringify(block.location.x) + ' ' + JSON.stringify(block.location.y) + ' ' + JSON.stringify(block.location.z) + ' 0.5')

                }

                if (block?.permutation.getState('rns:lit') == 0 && item?.typeId == 'minecraft:flint_and_steel') {


                    block.setPermutation(block.permutation.withState('rns:lit', 1))
                    adjustItemDamage(inv, item, 1, slot)
                    player.runCommandAsync('playsound fire.ignite @p ' + JSON.stringify(block.location.x) + ' ' + JSON.stringify(block.location.y) + ' ' + JSON.stringify(block.location.z) + ' 0.5')

                }
                if (block?.permutation.getState('rns:lit') == 1 && item?.hasTag('minecraft:is_shovel')) {


                    block.setPermutation(block.permutation.withState('rns:lit', 0))
                    player.runCommandAsync('playsound extinguish.candle @p ' + JSON.stringify(block.location.x) + ' ' + JSON.stringify(block.location.y) + ' ' + JSON.stringify(block.location.z) + ' 0.5')

                }
            }

            if (block.typeId == 'rns:sticks') {
                if (item?.typeId == 'minecraft:stick' && block?.permutation.getState('rns:addbone') < 3) {

                    block.setPermutation(block.permutation.withState('rns:addbone', block.permutation.getState('rns:addbone') + 1))
                    decrementstack(item, player, item.typeId)
                    player.runCommandAsync('playsound use.wood @p ' + JSON.stringify(block.location.x) + ' ' + JSON.stringify(block.location.y) + ' ' + JSON.stringify(block.location.z) + ' 0.5')

                }
            }
        },
    });

    initEvent.blockComponentRegistry.registerCustomComponent('rns:lithollycandle', {
        onPlayerInteract: e => {
            const block = e.block
            const player = e.player
            const item = e.player.getComponent('equippable').getEquipment('Mainhand')
            const inv = player.getComponent("minecraft:inventory").container
            const slot = player.selectedSlotIndex


            if (block?.permutation.getState('rns:lit') == 0 && item?.typeId == 'minecraft:flint_and_steel') {


                block.setPermutation(block.permutation.withState('rns:lit', 1))
                adjustItemDamage(inv, item, 1, slot)
                player.runCommandAsync('playsound fire.ignite @p ' + JSON.stringify(block.location.x) + ' ' + JSON.stringify(block.location.y) + ' ' + JSON.stringify(block.location.z) + ' 0.5')

            }
            if (block?.permutation.getState('rns:lit') == 1 && item?.hasTag('minecraft:is_shovel')) {


                block.setPermutation(block.permutation.withState('rns:lit', 0))
                player.runCommandAsync('playsound extinguish.candle @p ' + JSON.stringify(block.location.x) + ' ' + JSON.stringify(block.location.y) + ' ' + JSON.stringify(block.location.z) + ' 0.5')

            }

        },
    });

    initEvent.blockComponentRegistry.registerCustomComponent('rns:dryingrackplace', {
        onPlayerInteract: e => {
            const block = e.block
            const player = e.player
            const item = e.player.getComponent('equippable').getEquipment('Mainhand')

            if (item?.typeId == 'rns:dryingrackupgrade' && block?.permutation.getState('rns:upgrade') != 1) {

                block.setPermutation(block.permutation.withState('rns:upgrade', 1))
                decrementstack(item, player, item.typeId)
                player.runCommandAsync('playsound block.scaffolding.place @p ' + JSON.stringify(block.location.x) + ' ' + JSON.stringify(block.location.y) + ' ' + JSON.stringify(block.location.z) + ' 0.5')

            }
            if (item?.hasTag('rns:salt') && block?.permutation.getState('rns:salt') < 3 && block?.permutation.getState('rns:upgrade') == 1) {

                block.setPermutation(block.permutation.withState('rns:salt', block.permutation.getState('rns:salt') + 1))
                decrementstack(item, player, item.typeId)
                player.runCommandAsync('playsound hit.sand @p ' + JSON.stringify(block.location.x) + ' ' + JSON.stringify(block.location.y) + ' ' + JSON.stringify(block.location.z) + ' 0.5')

            }
            if (block?.permutation.getState('rns:input') == 0 && block?.permutation.getState('rns:dried') == 0) {
                if (item?.typeId == 'minecraft:kelp') {

                    dryingrackplace(block, player, item, 'random.pop', 1)

                }
                if (item?.typeId == 'rns:leaf') {

                    dryingrackplace(block, player, item, 'random.pop', 2)

                }
                if (item?.typeId == 'rns:paperpulp') {

                    dryingrackplace(block, player, item, 'random.pop', 3)

                }
                if (item?.typeId == 'minecraft:apple') {

                    dryingrackplace(block, player, item, 'random.pop', 4)

                }
                if (item?.typeId == 'sev:saltedhide') {

                    dryingrackplace(block, player, item, 'random.pop', 5)

                }
                if (item?.typeId == 'rns:meatchunk') {

                    dryingrackplace(block, player, item, 'random.pop', 6)
                }
                if (item?.typeId == 'sev:cattailreeds') {

                    dryingrackplace(block, player, item, 'random.pop', 7)
                }
            }
            if (block?.permutation.getState('rns:dried') == 1) {
                dryingracktake(block, player, 'dried_kelp')

            }
            if (block?.permutation.getState('rns:dried') == 2) {

                dryingracktake(block, player, 'rns:driedleaf')

            }
            if (block?.permutation.getState('rns:dried') == 3) {

                dryingracktake(block, player, 'paper')

            }
            if (block?.permutation.getState('rns:dried') == 4) {

                dryingracktake(block, player, 'rns:applechip', 3)

            }
            if (block?.permutation.getState('rns:dried') == 5) {

                dryingracktake(block, player, 'leather')

            }
            if (block?.permutation.getState('rns:dried') == 6) {

                dryingracktake(block, player, 'rns:meatjerkey')

            }
            if (block?.permutation.getState('rns:dried') == 7) {

                dryingracktake(block, player, 'sev:driedcattailreeds')

            }
        },
    });

    initEvent.blockComponentRegistry.registerCustomComponent('rns:ated', {
        onPlayerInteract: e => {
            const block = e.block
            const player = e.player
            const item = e.player.getComponent('equippable').getEquipment('Mainhand')
            const loot = 'roastedbeetrootbowl'
            if (block.permutation.getState('rns:ated') < 16 && item?.typeId == 'minecraft:bowl') {
                block.setPermutation(block.permutation.withState('rns:ated', block.permutation.getState('rns:ated') + 1))
                block.dimension.playSound('mob.panda.eat', { x: block.location.x, y: block.location.y + 1, z: block.location.z })
                decrementstack(item, player, 'minecraft:bowl')
                player.runCommandAsync('loot spawn ' + JSON.stringify(block.location.x) + ' ' + JSON.stringify(block.location.y + 0.5) + ' ' + JSON.stringify(block.location.z) + ' loot ' + loot)
            }
            if (block.permutation.getState('rns:ated') == 15 && item?.typeId == 'minecraft:bowl') {
                block.setPermutation(block.permutation.withState('rns:ated', block.permutation.getState('rns:ated') + 1))
                block.dimension.playSound('mob.panda.eat', { x: block.location.x, y: block.location.y + 1, z: block.location.z })
                decrementstack(item, player, 'minecraft:bowl')
                player.runCommandAsync('loot spawn ' + JSON.stringify(block.location.x) + ' ' + JSON.stringify(block.location.y + 0.5) + ' ' + JSON.stringify(block.location.z) + ' loot ' + loot)
                block.setPermutation(BlockPermutation.resolve('minecraft:air'))
            }
        },
    });
    initEvent.blockComponentRegistry.registerCustomComponent('rns:randomvariant', {
        onPlace: e => {
            let num = Math.round(Math.random() * 2)
            const block = e.block
            if (num == 2) {
                block.setPermutation(block.permutation.withState('rns:type', 2))
            }
            if (num == 1) {
                block.setPermutation(block.permutation.withState('rns:type', 1))
            }
            if (num < 1) {
                block.setPermutation(block.permutation.withState('rns:type', 0))
            }
        },
    });
    initEvent.blockComponentRegistry.registerCustomComponent('rns:unsnow', {
        onStepOn: e => {
            const player = e.entity
            const block = e.block
            if (Math.floor(Math.random() * 4) < 1) {
                block.setPermutation(BlockPermutation.resolve('minecraft:azalea'))
                player.dimension.spawnParticle('minecraft:snowflake_particle', block.center())
                player.runCommandAsync('loot spawn ' + JSON.stringify(block.location.x) + ' ' + JSON.stringify(block.location.y + 1) + ' ' + JSON.stringify(block.location.z) + ' loot snowball')
            }


        },
    });
    initEvent.blockComponentRegistry.registerCustomComponent('rns:icebreak', {
        onStepOn: e => {
            const player = e.entity
            const block = e.block
            block.setPermutation(BlockPermutation.resolve('minecraft:air'))
            player.dimension.spawnParticle('minecraft:snowflake_particle', block.center())
            block.dimension.playSound('random.glass', { x: block.location.x, y: block.location.y + 1, z: block.location.z })
            player.runCommandAsync('loot spawn ' + JSON.stringify(block.location.x) + ' ' + JSON.stringify(block.location.y + 1) + ' ' + JSON.stringify(block.location.z) + ' loot thiniceloot')
        },
    });
});

world.afterEvents.playerBreakBlock.subscribe((data) => {
    bush_Manager.updateBushesAround(data.block)
    branch_Manager.updateBranchesAround(data.block)
    cactus_Manager.updateCactusAround(data.block)
})


world.afterEvents.playerPlaceBlock.subscribe((data) => {
    branch_Manager.updateBranchesAround(data.block)
    bush_Manager.updateBushesAround(data.block)
    cactus_Manager.updateCactusAround(data.block)
})

//Mortar
export function mortarplop(item, player, block, req, num) {
    block.setPermutation(block.permutation.withState('rns:input', num))
    decrementstack(item, player, req)
    player.runCommandAsync('playsound block.composter.fill @p ' + JSON.stringify(block.location.x) + ' ' + JSON.stringify(block.location.y) + ' ' + JSON.stringify(block.location.z) + ' 0.5')
}

export function mortarcrush(player, block, num) {
    block.setPermutation(block.permutation.withState('rns:output', num))
    block.setPermutation(block.permutation.withState('rns:input', 0))
    itemMine(player)
    player.runCommandAsync('playsound block.composter.fill @p ' + JSON.stringify(block.location.x) + ' ' + JSON.stringify(block.location.y) + ' ' + JSON.stringify(block.location.z) + ' 0.5')
}

export function mortartake(player, block, loot) {
    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 block.composter.fill @p ' + JSON.stringify(block.location.x) + ' ' + JSON.stringify(block.location.y) + ' ' + JSON.stringify(block.location.z) + ' 0.5')
    block.setPermutation(block.permutation.withState('rns:output', 0))
    block.setPermutation(block.permutation.withState('rns:input', 0))
}

//Drying Rack
export function dryingrackplace(block, player, item, sound, input) {
    block.setPermutation(block.permutation.withState('rns:input', input))
    decrementstack(item, player, item.typeId)
    player.runCommandAsync('playsound ' + sound + ' @p ' + JSON.stringify(block.location.x) + ' ' + JSON.stringify(block.location.y) + ' ' + JSON.stringify(block.location.z) + ' 0.5')
}

export function dryingracktake(block, player, loot, amount) {
    if (amount == undefined) {
        amount = 1
    }
    player.runCommandAsync('playsound random.pop @p ' + JSON.stringify(block.location.x) + ' ' + JSON.stringify(block.location.y) + ' ' + JSON.stringify(block.location.z) + ' 0.5')
    block.dimension.spawnItem(new ItemStack(loot, amount), { x: block.location.x + 0.5, y: block.location.y + 1, z: block.location.z + 0.5 })
    block.setPermutation(block.permutation.withState('rns:dried', 0))
}