import { world, BlockPermutation, Dimension, Block, system, ItemStack, MolangVariableMap, Player } from "@minecraft/server";
import { decrementstack, adjustItemDamage } from './nature'
import { add_effect } from './bum_crops_custom_components'
import { buildmushroom } from './misc'

let growth = 0

world.beforeEvents.worldInitialize.subscribe(initEvent => {
    initEvent.blockComponentRegistry.registerCustomComponent('pcr:choppingblock', {
        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

            //Setting Log
            if (block.permutation.getState('sev:woodtype') < 1 && block.permutation.getState('sev:woodtype2') < 1) {
                switch (item?.typeId) {
                    case 'minecraft:oak_log':
                        set_log(block, item, player, "sev:woodtype", 1)
                        break
                    case 'rns:oakbranch':
                        set_log(block, item, player, "sev:woodtype", 1)
                        break
                    case 'minecraft:birch_log':
                        set_log(block, item, player, "sev:woodtype", 2)
                        break
                    case 'minecraft:spruce_log':
                        set_log(block, item, player, "sev:woodtype", 3)
                        break
                    case 'minecraft:jungle_log':
                        set_log(block, item, player, "sev:woodtype", 4)
                        break
                    case 'minecraft:acacia_log':
                        set_log(block, item, player, "sev:woodtype", 5)
                        break
                    case 'minecraft:dark_oak_log':
                        set_log(block, item, player, "sev:woodtype", 6)
                        break
                    case 'minecraft:mangrove_log':
                        set_log(block, item, player, "sev:woodtype", 7)
                        break
                    case 'minecraft:cherry_log':
                        set_log(block, item, player, "sev:woodtype", 8)
                        break
                    case 'minecraft:bamboo_block':
                        set_log(block, item, player, "sev:woodtype", 9)
                        break
                    case 'bumble:maple_log':
                        set_log(block, item, player, "sev:woodtype", 12)
                        break
                    case 'bumble:lemon_log':
                        set_log(block, item, player, "sev:woodtype", 13)
                        break
                    case 'bumble:orange_log':
                        set_log(block, item, player, "sev:woodtype", 14)
                        break
                    case 'bumble:peach_log':
                        set_log(block, item, player, "sev:woodtype", 15)
                        break
                    case 'bumble:pear_log':
                        set_log(block, item, player, "sev:woodtype", 11)
                        break
                    case 'bumble:cherry_log':
                        set_log(block, item, player, "sev:woodtype", 10)
                        break
                    case 'minecraft:pale_oak_log':
                        set_log(block, item, player, "sev:woodtype2", 1)
                        break
                }
            }
            //Chop
            if ((block.permutation.getState('sev:woodtype') > 0 || block.permutation.getState('sev:woodtype2') > 0) && block.permutation.getState('sev:chop') < 4 && (item?.hasTag('minecraft:is_axe') && !(item?.hasTag('pcr:scraper')))) {
                block.setPermutation(block.permutation.withState('sev:chop', block.permutation.getState('sev:chop') + 1))
                block.dimension.playSound('dig.wood', { x: block.location.x, y: block.location.y, z: block.location.z })
            }

            //Final Chop
            if (block.permutation.getState('sev:chop') == 4 && (item?.hasTag('minecraft:is_axe') && !(item?.hasTag('pcr:scraper')))) {
                adjustItemDamage(inv, item, 1, slot)
                block.dimension.playSound('dig.wood', { x: block.location.x, y: block.location.y, z: block.location.z })
                switch (block.permutation.getState('sev:woodtype')) {
                    case 1:
                        block.dimension.runCommandAsync('loot spawn ' + JSON.stringify(block.location.x) + ' ' + JSON.stringify(block.location.y + 1) + ' ' + JSON.stringify(block.location.z) + ' loot "choppingblockplanks/oakplank"')
                        break
                    case 2:
                        block.dimension.runCommandAsync('loot spawn ' + JSON.stringify(block.location.x) + ' ' + JSON.stringify(block.location.y + 1) + ' ' + JSON.stringify(block.location.z) + ' loot "choppingblockplanks/birchplank"')
                        break
                    case 3:
                        block.dimension.runCommandAsync('loot spawn ' + JSON.stringify(block.location.x) + ' ' + JSON.stringify(block.location.y + 1) + ' ' + JSON.stringify(block.location.z) + ' loot "choppingblockplanks/spruceplank"')
                        break
                    case 4:
                        block.dimension.runCommandAsync('loot spawn ' + JSON.stringify(block.location.x) + ' ' + JSON.stringify(block.location.y + 1) + ' ' + JSON.stringify(block.location.z) + ' loot "choppingblockplanks/jungleplank"')
                        break
                    case 5:
                        block.dimension.runCommandAsync('loot spawn ' + JSON.stringify(block.location.x) + ' ' + JSON.stringify(block.location.y + 1) + ' ' + JSON.stringify(block.location.z) + ' loot "choppingblockplanks/acaciaplank"')
                        break
                    case 6:
                        block.dimension.runCommandAsync('loot spawn ' + JSON.stringify(block.location.x) + ' ' + JSON.stringify(block.location.y + 1) + ' ' + JSON.stringify(block.location.z) + ' loot "choppingblockplanks/darkoakplank"')
                        break
                    case 7:
                        block.dimension.runCommandAsync('loot spawn ' + JSON.stringify(block.location.x) + ' ' + JSON.stringify(block.location.y + 1) + ' ' + JSON.stringify(block.location.z) + ' loot "choppingblockplanks/mangroveplanks"')
                        break
                    case 8:
                        block.dimension.runCommandAsync('loot spawn ' + JSON.stringify(block.location.x) + ' ' + JSON.stringify(block.location.y + 1) + ' ' + JSON.stringify(block.location.z) + ' loot "choppingblockplanks/minecraftcherryplank"')
                        break
                    case 9:
                        block.dimension.runCommandAsync('loot spawn ' + JSON.stringify(block.location.x) + ' ' + JSON.stringify(block.location.y + 1) + ' ' + JSON.stringify(block.location.z) + ' loot "choppingblockplanks/bambooplanks"')
                        break
                    case 10:
                        block.dimension.runCommandAsync('loot spawn ' + JSON.stringify(block.location.x) + ' ' + JSON.stringify(block.location.y + 1) + ' ' + JSON.stringify(block.location.z) + ' loot "choppingblockplanks/cherryplank"')
                        break
                    case 11:
                        block.dimension.runCommandAsync('loot spawn ' + JSON.stringify(block.location.x) + ' ' + JSON.stringify(block.location.y + 1) + ' ' + JSON.stringify(block.location.z) + ' loot "choppingblockplanks/pearplanks"')
                        break
                    case 12:
                        block.dimension.runCommandAsync('loot spawn ' + JSON.stringify(block.location.x) + ' ' + JSON.stringify(block.location.y + 1) + ' ' + JSON.stringify(block.location.z) + ' loot "choppingblockplanks/maplebumplanks"')
                        break
                    case 13:
                        block.dimension.runCommandAsync('loot spawn ' + JSON.stringify(block.location.x) + ' ' + JSON.stringify(block.location.y + 1) + ' ' + JSON.stringify(block.location.z) + ' loot "choppingblockplanks/lemonplanks"')
                        break
                    case 14:
                        block.dimension.runCommandAsync('loot spawn ' + JSON.stringify(block.location.x) + ' ' + JSON.stringify(block.location.y + 1) + ' ' + JSON.stringify(block.location.z) + ' loot "choppingblockplanks/orangeplanks"')
                        break
                    case 15:
                        block.dimension.runCommandAsync('loot spawn ' + JSON.stringify(block.location.x) + ' ' + JSON.stringify(block.location.y + 1) + ' ' + JSON.stringify(block.location.z) + ' loot "choppingblockplanks/peachplanks"')
                        break
                }
                switch (block.permutation.getState('sev:woodtype2')) {
                    case 1:
                        block.dimension.runCommandAsync('loot spawn ' + JSON.stringify(block.location.x) + ' ' + JSON.stringify(block.location.y + 1) + ' ' + JSON.stringify(block.location.z) + ' loot "choppingblockplanks/paleoakplank"')
                        break
                }
                block.setPermutation(block.permutation.withState('sev:woodtype', 0))
                block.setPermutation(block.permutation.withState('sev:woodtype2', 0))
                block.setPermutation(block.permutation.withState('sev:chop', 0))
            }
        },
    });
    initEvent.blockComponentRegistry.registerCustomComponent('pcr:campfire', {
        onPlayerInteract: e => {
            const block = e.block
            const player = e.player
            const offhanditem = player.getComponent("minecraft:equippable").getEquipment('Offhand')
            const item = e.player.getComponent('equippable').getEquipment('Mainhand')
            const inv = player.getComponent("minecraft:inventory").container
            const slot = player.selectedSlotIndex


            //Campfire
            if (block.permutation.getState('pcr:built') < 3 && item?.hasTag('minecraft:logs')) {
                block.setPermutation(block.permutation.withState('pcr:built', block.permutation.getState('pcr:built') + 1))
                block.dimension.playSound('use.wood', { x: block.location.x, y: block.location.y, z: block.location.z })
                decrementstack(item, player, item.typeId)
            }

            //Kindling Step
            if (block.permutation.getState('pcr:built') == 3 && item?.typeId == 'mms:kindling') {
                block.setPermutation(block.permutation.withState('pcr:built', block.permutation.getState('pcr:built') + 1))
                block.dimension.playSound('dig.grass', { x: block.location.x, y: block.location.y, z: block.location.z })
                decrementstack(item, player, item.typeId)
            }

            //Igniting
            if (block.permutation.getState('pcr:built') == 4 && (item?.typeId == 'minecraft:torch' || item?.typeId == 'minecraft:flint_and_steel' || item?.hasTag("pcr:firestarter") || item.typeId == 'sev:firestick' && offhanditem?.typeId == 'sev:fireflint')) {
                block.setPermutation(BlockPermutation.resolve('minecraft:campfire'))
                block.dimension.playSound('fire.ignite', { x: block.location.x, y: block.location.y, z: block.location.z })
                if (item?.typeId == 'minecraft:torch' || item?.hasTag("pcr:firestarter")) {
                    decrementstack(item, player, item.typeId)
                }
                if (item?.typeId == 'minecraft:flint_and_steel') {
                    adjustItemDamage(inv, item, 1, slot)
                }
                if (item.typeId == 'sev:firestick' && offhanditem?.typeId == 'sev:fireflint') {
                    decrementstack(item, player, item.typeId)
                    offhanddurability(offhanditem, player)
                }

            }
        },
    });

    initEvent.blockComponentRegistry.registerCustomComponent('pcr:charcoaldestroy', {
        onPlayerDestroy: e => {
            const block = e.block
            const item = e.player.getComponent('equippable').getEquipment('Mainhand')

            //Charcoal
            if (item?.hasTag('minecraft:is_pickaxe')) {
                block.dimension.runCommandAsync('loot spawn ' + JSON.stringify(block.location.x) + ' ' + JSON.stringify(block.location.y + 1) + ' ' + JSON.stringify(block.location.z) + ' loot charcoalblockitself')
            }
            else {
                block.dimension.runCommandAsync('loot spawn ' + JSON.stringify(block.location.x) + ' ' + JSON.stringify(block.location.y + 1) + ' ' + JSON.stringify(block.location.z) + ' loot charcoalresult')
            }
        },
    });

    initEvent.blockComponentRegistry.registerCustomComponent('pcr:woodencrucible', {
        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

            //Filling Crucible
            if (block.permutation.getState('pcr:waterlevel') < 12 && (item?.hasTag('pcr:water_filler') || item?.typeId == 'minecraft:water_bucket')) {
                block.setPermutation(block.permutation.withState('pcr:waterlevel', 12))
                block.setPermutation(block.permutation.withState('pcr:tan', 0))
                block.dimension.playSound('bucket.empty_water', { x: block.location.x, y: block.location.y, z: block.location.z })
                decrementstack(item, player, item.typeId)
                if (item?.typeId == 'sev:fullfluidbladder') {
                    let newitem = new ItemStack('sev:fluidbladder', 1)
                    block.dimension.spawnItem(newitem, { x: block.location.x + 0.5, y: block.location.y + 1, z: block.location.z + 0.5 })
                }
                if (item?.typeId == 'minecraft:water_bucket') {
                    let newitem = new ItemStack('minecraft:bucket', 1)
                    block.dimension.spawnItem(newitem, { x: block.location.x + 0.5, y: block.location.y + 1, z: block.location.z + 0.5 })
                }
            }

            //Adding Tanner or Washing Hide
            if (block.permutation.getState('pcr:waterlevel') > 0 && block.permutation.getState('pcr:tan') < 1) {
                if (item?.typeId == 'add:tannis') {
                    block.setPermutation(block.permutation.withState('pcr:tan', 1))
                    block.dimension.playSound('bucket.empty_water', { x: block.location.x, y: block.location.y, z: block.location.z })
                    decrementstack(item, player, item.typeId)
                }
                if (item?.typeId == 'add:scraped_hide') {
                    block.setPermutation(block.permutation.withState('pcr:waterlevel', block.permutation.getState('pcr:waterlevel') - 1))
                    block.dimension.playSound('bucket.empty_water', { x: block.location.x, y: block.location.y, z: block.location.z })

                    let newitem = new ItemStack('add:washed_hide', 1)
                    block.dimension.spawnItem(newitem, { x: block.location.x + 0.5, y: block.location.y + 1, z: block.location.z + 0.5 })

                    decrementstack(item, player, item.typeId)
                }

            }

            //Tanning Hide
            if (block.permutation.getState('pcr:waterlevel') > 0 && block.permutation.getState('pcr:tan') == 1 && item?.typeId == 'add:washed_hide') {
                block.setPermutation(block.permutation.withState('pcr:waterlevel', block.permutation.getState('pcr:waterlevel') - 1))
                block.dimension.playSound('bucket.empty_water', { x: block.location.x, y: block.location.y, z: block.location.z })

                let newitem = new ItemStack('add:tanned_hide', 1)
                block.dimension.spawnItem(newitem, { x: block.location.x + 0.5, y: block.location.y + 1, z: block.location.z + 0.5 })

                decrementstack(item, player, item.typeId)
            }
        },
    });

    //Clay Cooker (Hot Rocks)
    initEvent.blockComponentRegistry.registerCustomComponent('pcr:hotrocks', {
        onTick: e => {
            const block = e.block

            //Random Fire
            block.dimension.runCommandAsync('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) + ' fire replace air')
            //Terrocotta
            block.dimension.runCommandAsync('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) + ' hardened_clay replace clay')

        },
    });

    initEvent.blockComponentRegistry.registerCustomComponent('pcr:packedmudframe', {
        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

            //Filling Clay Slot 2
            if (block.permutation.getState('pcr:first') == 1 && block.permutation.getState('pcr:second') != 1 && item?.typeId == 'minecraft:clay_ball') {
                block.setPermutation(block.permutation.withState('pcr:second', 1))
                block.dimension.playSound('block.packed_mud.place', { x: block.location.x, y: block.location.y, z: block.location.z })
                decrementstack(item, player, item.typeId)
            }

            //Filling Clay Slot 1
            if (block.permutation.getState('pcr:first') == 0 && block.permutation.getState('pcr:second') != 1 && item?.typeId == 'minecraft:clay_ball') {
                block.setPermutation(block.permutation.withState('pcr:first', 1))
                block.dimension.playSound('block.packed_mud.place', { x: block.location.x, y: block.location.y, z: block.location.z })
                decrementstack(item, player, item.typeId)
            }


        },
    });
    //Packed Mud Frame (Brick 1 Slot)
    initEvent.blockComponentRegistry.registerCustomComponent('pcr:dryingbrick1', {
        onTick: e => {
            const block = e.block

            //Drying
            if (block.below(1).hasTag('fire')) {
                block.setPermutation(block.permutation.withState('pcr:first', 0))
                block.dimension.playSound('random.fizz', { x: block.location.x, y: block.location.y, z: block.location.z })
                let newitem = new ItemStack('minecraft:brick', 1)
                block.dimension.spawnItem(newitem, { x: block.location.x + 0.5, y: block.location.y + 0.3, z: block.location.z + 0.5 })
            }
        },
    });
    //Packed Mud Frame (Brick 2 Slot)
    initEvent.blockComponentRegistry.registerCustomComponent('pcr:dryingbrick2', {
        onTick: e => {
            const block = e.block

            //Drying
            if (block.below(1).hasTag('fire')) {
                block.setPermutation(block.permutation.withState('pcr:second', 0))
                block.dimension.playSound('random.fizz', { x: block.location.x, y: block.location.y, z: block.location.z })
                let newitem = new ItemStack('minecraft:brick', 1)
                block.dimension.spawnItem(newitem, { x: block.location.x + 0.5, y: block.location.y, z: block.location.z + 0.5 })
            }
        },
    });

    //Mechanical Crank 
    initEvent.blockComponentRegistry.registerCustomComponent('pcr:crankturn', {
        onPlayerInteract: e => {
            const block = e.block
            const player = e.player

            player.addEffect("hunger", 20, { amplifier: 29 })
            block.setPermutation(block.permutation.withState('vatonage:signal', 1))

        },
    });
    //Instantly turns back crank after turn (How its originally designed in the block)
    initEvent.blockComponentRegistry.registerCustomComponent('pcr:crankturnback', {
        onTick: e => {
            const block = e.block

            block.setPermutation(block.permutation.withState('vatonage:signal', 0))


        },
    });

    //Turn Table
    //Turn Table recieving
    initEvent.blockComponentRegistry.registerCustomComponent('pcr:turntableaccept', {
        onTick: e => {
            const block = e.block
            if (block.north(1).hasTag('cogsignal') || block.south(1).hasTag('cogsignal') || block.east(1).hasTag('cogsignal') || block.west(1).hasTag('cogsignal')) {
                block.setPermutation(block.permutation.withState('rns:turn', 1))
            }
        },
    });
    //Turn Table shaping clay (Later stages of clay turning should be first)
    initEvent.blockComponentRegistry.registerCustomComponent('pcr:turntableshape', {
        onTick: e => {
            const block = e.block

            if (block.above(1).typeId == "minecraft:clay" || block.above(1).hasTag('pcr:unfiredclay')) {
                for (let i = 0; i < 50; i++) {
                    const newLocation = {
                        x: block.above(1).location.x + Math.floor(Math.random()) - Math.random() + 1,
                        y: block.above(1).location.y + Math.floor(Math.random()) - Math.random() + 1,
                        z: block.above(1).location.z + Math.floor(Math.random()) - Math.random() + 1,
                    }
                    block.above(1).dimension.spawnParticle("minecraft:falling_dust_gravel_particle", newLocation)
                }

            }
            block.dimension.runCommandAsync('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 replace pcr:unfiredmug')
            block.dimension.runCommandAsync('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) + ' pcr:unfiredmug replace pcr:unfiredflowerpot')
            block.dimension.runCommandAsync('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) + ' pcr:unfiredflowerpot replace pcr:unfiredjabena')
            block.dimension.runCommandAsync('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) + ' pcr:unfiredjabena replace clay')
            block.dimension.playSound('block.grindstone.use', { x: block.location.x, y: block.location.y, z: block.location.z })
            block.setPermutation(block.permutation.withState('rns:turn', 0))
        },
    });

    //Clay Hardening
    initEvent.blockComponentRegistry.registerCustomComponent('pcr:dryingunfired', {
        onTick: e => {
            const block = e.block

            //Unfired Cooking
            if (block.below(1).hasTag('fire')) {
                if (block.typeId == 'pcr:unfiredflowerpot') {
                    block.setPermutation(BlockPermutation.resolve('minecraft:flower_pot'))
                }
                if (block.typeId == 'pcr:unfiredjabena') {
                    block.setPermutation(BlockPermutation.resolve('bumble:jabena_block'))
                }
                if (block.typeId == 'pcr:unfiredmug') {
                    block.setPermutation(BlockPermutation.resolve('bumble:mug_block'))
                }
                block.dimension.playSound('random.fizz', { x: block.location.x, y: block.location.y, z: block.location.z })
            }
        },
    });

    //Filling Clay Basin
    initEvent.blockComponentRegistry.registerCustomComponent('pcr:fillbasin', {
        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

            //Filling Basin
            if (block.permutation.getState('pcr:water') < 7 && (item?.hasTag('pcr:water_filler') || item?.typeId == 'minecraft:water_bucket')) {
                block.setPermutation(block.permutation.withState('pcr:water', 7))
                block.setPermutation(block.permutation.withState('pcr:steamy', 0))
                block.dimension.playSound('bucket.empty_water', { x: block.location.x, y: block.location.y, z: block.location.z })
                decrementstack(item, player, item.typeId)
                if (item?.typeId == 'sev:fullfluidbladder') {
                    let newitem = new ItemStack('sev:fluidbladder', 1)
                    block.dimension.spawnItem(newitem, { x: block.location.x + 0.5, y: block.location.y + 1, z: block.location.z + 0.5 })
                }
                if (item?.typeId == 'minecraft:water_bucket') {
                    let newitem = new ItemStack('minecraft:bucket', 1)
                    block.dimension.spawnItem(newitem, { x: block.location.x + 0.5, y: block.location.y + 1, z: block.location.z + 0.5 })
                }
            }
        },
    });

    //Insert Steamy Item
    initEvent.blockComponentRegistry.registerCustomComponent('pcr:steamingwhat', {
        onPlayerInteract: e => {
            const block = e.block
            const player = e.player
            const item = e.player.getComponent('equippable').getEquipment('Mainhand')
            //Steaming Bricks
            if (item?.typeId == 'minecraft:clay_ball' && block.permutation.getState('pcr:steaming') == 0) {
                block.setPermutation(block.permutation.withState('pcr:steaming', 1))
                block.dimension.playSound('block.packed_mud.place', { x: block.location.x, y: block.location.y, z: block.location.z })
                decrementstack(item, player, item.typeId)

            }
            //Steaming Porcelain Bricks
            if (item?.typeId == 'pcr:porcelainclay' && block.permutation.getState('pcr:steaming') == 0) {
                block.setPermutation(block.permutation.withState('pcr:steaming', 2))
                block.dimension.playSound('block.packed_mud.place', { x: block.location.x, y: block.location.y, z: block.location.z })
                decrementstack(item, player, item.typeId)
            }
            //Steaming Fish
            if (item?.typeId == 'minecraft:cod' && block.permutation.getState('pcr:steaming') == 0) {
                block.setPermutation(block.permutation.withState('pcr:steaming', 3))
                block.dimension.playSound('block.packed_mud.place', { x: block.location.x, y: block.location.y, z: block.location.z })
                decrementstack(item, player, item.typeId)
            }
        },
    });

    //Steam evaporating from basin
    initEvent.blockComponentRegistry.registerCustomComponent('pcr:steamy', {
        onTick: e => {
            const block = e.block

            //Steam on hot
            if (block.below(1).hasTag('fire')) {
                if (block.permutation.getState('pcr:water') == 1) {
                    block.setPermutation(block.permutation.withState('pcr:steamy', 0))
                    block.dimension.spawnParticle("minecraft:campfire_smoke_particle", block.center())
                    block.setPermutation(block.permutation.withState('pcr:water', block.permutation.getState('pcr:water') - 1))
                }
                else {
                    block.setPermutation(block.permutation.withState('pcr:water', block.permutation.getState('pcr:water') - 1))
                    block.dimension.playSound('random.fizz', { x: block.location.x, y: block.location.y, z: block.location.z })
                    block.dimension.spawnParticle("minecraft:campfire_smoke_particle", block.center())
                    block.setPermutation(block.permutation.withState('pcr:steamy', 1))
                }
            }
        },
    });

    //Finished Steaming
    initEvent.blockComponentRegistry.registerCustomComponent('pcr:steamingitem', {
        onTick: e => {
            const block = e.block

            if (block.below(1).hasTag('pcr:steam') || block.below(2).hasTag('pcr:steam')) {
                //Steaming Bricks
                if (block.permutation.getState('pcr:steaming') == 1) {
                    block.setPermutation(block.permutation.withState('pcr:steaming', 0))
                    block.dimension.playSound('mob.sniffer.plop', { x: block.location.x, y: block.location.y, z: block.location.z })
                    let newitem = new ItemStack('minecraft:brick', 1)
                    block.dimension.spawnItem(newitem, { x: block.location.x + 0.5, y: block.location.y + 1.1, z: block.location.z + 0.5 })
                }
                //Steaming Porcelain Bricks
                if (block.permutation.getState('pcr:steaming') == 2) {
                    block.setPermutation(block.permutation.withState('pcr:steaming', 0))
                    block.dimension.playSound('mob.sniffer.plop', { x: block.location.x, y: block.location.y, z: block.location.z })
                    let newitem = new ItemStack('pcr:porcelain', 1)
                    block.dimension.spawnItem(newitem, { x: block.location.x + 0.5, y: block.location.y + 1.1, z: block.location.z + 0.5 })
                }
                //Steaming Fish
                if (block.permutation.getState('pcr:steaming') == 3) {
                    block.setPermutation(block.permutation.withState('pcr:steaming', 0))
                    block.dimension.playSound('mob.sniffer.plop', { x: block.location.x, y: block.location.y, z: block.location.z })
                    let newitem = new ItemStack('minecraft:cooked_cod', 1)
                    block.dimension.spawnItem(newitem, { x: block.location.x + 0.5, y: block.location.y + 1.1, z: block.location.z + 0.5 })
                }
            }
        },
    });

    //Age 2

    //Quartz Pressure Cooker
    initEvent.blockComponentRegistry.registerCustomComponent('pcr:isheated', {
        onTick: e => {
            const block = e.block
            if (block.below(1).hasTag('fire')) {
                block.setPermutation(block.permutation.withState('pcr:active', 1))
            }
        },
    });
    //not active anymore
    initEvent.blockComponentRegistry.registerCustomComponent('pcr:isnotheated', {
        onTick: e => {
            const block = e.block
            if (block.below(1).hasTag('fire')) {
                block.setPermutation(block.permutation.withState('pcr:active', 1))
            }

        },
    });
    //Detects a mechanical crank input
    initEvent.blockComponentRegistry.registerCustomComponent('pcr:shouldcrush', {
        onTick: e => {
            const block = e.block
            if ((block.north(1).hasTag('cogsignal') || block.south(1).hasTag('cogsignal') || block.east(1).hasTag('cogsignal') || block.west(1).hasTag('cogsignal')) && block.below(1).hasTag('fire') && block.permutation.getState('pcr:hasitem') >= 1) {
                if (block.permutation.getState('pcr:hasitem') == 1) {
                    block.setPermutation(block.permutation.withState('pcr:hasitem', 0))
                    block.dimension.playSound('dig.netherrack', { x: block.location.x, y: block.location.y, z: block.location.z })
                    let newitem = new ItemStack('minecraft:quartz', 1)
                    block.dimension.spawnItem(newitem, { x: block.location.x + 0.5, y: block.location.y + 1.1, z: block.location.z + 0.5 })
                }
            }
        },
    });

    initEvent.blockComponentRegistry.registerCustomComponent('pcr:acceptcrush', {
        onPlayerInteract: e => {
            const block = e.block
            const player = e.player
            const item = e.player.getComponent('equippable').getEquipment('Mainhand')
            //Calcite to Quartz
            if (item?.typeId == 'minecraft:calcite' && block.permutation.getState('pcr:hasitem') == 0) {
                block.setPermutation(block.permutation.withState('pcr:hasitem', 1))
                block.dimension.playSound('place.calcite', { x: block.location.x, y: block.location.y, z: block.location.z })
                decrementstack(item, player, item.typeId)
            }
        },
    });
    //Ghost Pedestals
    initEvent.blockComponentRegistry.registerCustomComponent('pcr:replaceghostpedestal', {
        onPlayerInteract: e => {
            const block = e.block
            const player = e.player
            const item = e.player.getComponent('equippable').getEquipment('Mainhand')
            //Ghost Pedestal to actual ones
            if (item?.typeId == 'pcr:pedestal') {
                block.dimension.runCommandAsync('fill ' + JSON.stringify(block.location.x) + ' ' + JSON.stringify(block.location.y) + ' ' + JSON.stringify(block.location.z) + " " + JSON.stringify(block.location.x) + ' ' + JSON.stringify(block.location.y) + ' ' + JSON.stringify(block.location.z) + ' pcr:pedestal replace pcr:ghostpedestals')
                block.dimension.playSound('use.bone_block', { x: block.location.x, y: block.location.y, z: block.location.z })
                decrementstack(item, player, item.typeId)
            }
        },
    });

    //Altar Center Stuff
    initEvent.blockComponentRegistry.registerCustomComponent('pcr:summonghostpedestals', {
        onPlace: e => {
            const block = e.block
            //Ghost Pedestal Replace Air
            block.dimension.runCommandAsync('fill ' + JSON.stringify(block.location.x + 2) + ' ' + JSON.stringify(block.location.y) + ' ' + JSON.stringify(block.location.z) + " " + JSON.stringify(block.location.x + 2) + ' ' + JSON.stringify(block.location.y) + ' ' + JSON.stringify(block.location.z) + ' pcr:ghostpedestals replace air')
            block.dimension.runCommandAsync('fill ' + JSON.stringify(block.location.x) + ' ' + JSON.stringify(block.location.y) + ' ' + JSON.stringify(block.location.z + 2) + " " + JSON.stringify(block.location.x) + ' ' + JSON.stringify(block.location.y) + ' ' + JSON.stringify(block.location.z + 2) + ' pcr:ghostpedestals replace air')
            block.dimension.runCommandAsync('fill ' + JSON.stringify(block.location.x) + ' ' + JSON.stringify(block.location.y) + ' ' + JSON.stringify(block.location.z - 2) + " " + JSON.stringify(block.location.x) + ' ' + JSON.stringify(block.location.y) + ' ' + JSON.stringify(block.location.z - 2) + ' pcr:ghostpedestals replace air')
            block.dimension.runCommandAsync('fill ' + JSON.stringify(block.location.x - 2) + ' ' + JSON.stringify(block.location.y) + ' ' + JSON.stringify(block.location.z + 1) + " " + JSON.stringify(block.location.x - 2) + ' ' + JSON.stringify(block.location.y) + ' ' + JSON.stringify(block.location.z + 1) + ' pcr:ghostpedestals replace air')
            block.dimension.runCommandAsync('fill ' + JSON.stringify(block.location.x - 2) + ' ' + JSON.stringify(block.location.y) + ' ' + JSON.stringify(block.location.z - 1) + " " + JSON.stringify(block.location.x - 2) + ' ' + JSON.stringify(block.location.y) + ' ' + JSON.stringify(block.location.z - 1) + ' pcr:ghostpedestals replace air')
            //Ghost Pedestal Replace short Grass
            block.dimension.runCommandAsync('fill ' + JSON.stringify(block.location.x + 2) + ' ' + JSON.stringify(block.location.y) + ' ' + JSON.stringify(block.location.z) + " " + JSON.stringify(block.location.x + 2) + ' ' + JSON.stringify(block.location.y) + ' ' + JSON.stringify(block.location.z) + ' pcr:ghostpedestals replace short_grass')
            block.dimension.runCommandAsync('fill ' + JSON.stringify(block.location.x) + ' ' + JSON.stringify(block.location.y) + ' ' + JSON.stringify(block.location.z + 2) + " " + JSON.stringify(block.location.x) + ' ' + JSON.stringify(block.location.y) + ' ' + JSON.stringify(block.location.z + 2) + ' pcr:ghostpedestals replace short_grass')
            block.dimension.runCommandAsync('fill ' + JSON.stringify(block.location.x) + ' ' + JSON.stringify(block.location.y) + ' ' + JSON.stringify(block.location.z - 2) + " " + JSON.stringify(block.location.x) + ' ' + JSON.stringify(block.location.y) + ' ' + JSON.stringify(block.location.z - 2) + ' pcr:ghostpedestals replace short_grass')
            block.dimension.runCommandAsync('fill ' + JSON.stringify(block.location.x - 2) + ' ' + JSON.stringify(block.location.y) + ' ' + JSON.stringify(block.location.z + 1) + " " + JSON.stringify(block.location.x - 2) + ' ' + JSON.stringify(block.location.y) + ' ' + JSON.stringify(block.location.z + 1) + ' pcr:ghostpedestals replace short_grass')
            block.dimension.runCommandAsync('fill ' + JSON.stringify(block.location.x - 2) + ' ' + JSON.stringify(block.location.y) + ' ' + JSON.stringify(block.location.z - 1) + " " + JSON.stringify(block.location.x - 2) + ' ' + JSON.stringify(block.location.y) + ' ' + JSON.stringify(block.location.z - 1) + ' pcr:ghostpedestals replace short_grass')
        },
    });

    initEvent.blockComponentRegistry.registerCustomComponent('pcr:deleteghostpedestals', {
        onPlayerDestroy: e => {
            const block = e.block
            block.dimension.runCommandAsync('fill ' + JSON.stringify(block.location.x + 2) + ' ' + JSON.stringify(block.location.y) + ' ' + JSON.stringify(block.location.z) + " " + JSON.stringify(block.location.x + 2) + ' ' + JSON.stringify(block.location.y) + ' ' + JSON.stringify(block.location.z) + ' air replace pcr:ghostpedestals')
            block.dimension.runCommandAsync('fill ' + JSON.stringify(block.location.x) + ' ' + JSON.stringify(block.location.y) + ' ' + JSON.stringify(block.location.z + 2) + " " + JSON.stringify(block.location.x) + ' ' + JSON.stringify(block.location.y) + ' ' + JSON.stringify(block.location.z + 2) + ' air replace pcr:ghostpedestals')
            block.dimension.runCommandAsync('fill ' + JSON.stringify(block.location.x) + ' ' + JSON.stringify(block.location.y) + ' ' + JSON.stringify(block.location.z - 2) + " " + JSON.stringify(block.location.x) + ' ' + JSON.stringify(block.location.y) + ' ' + JSON.stringify(block.location.z - 2) + ' air replace pcr:ghostpedestals')
            block.dimension.runCommandAsync('fill ' + JSON.stringify(block.location.x - 2) + ' ' + JSON.stringify(block.location.y) + ' ' + JSON.stringify(block.location.z + 1) + " " + JSON.stringify(block.location.x - 2) + ' ' + JSON.stringify(block.location.y) + ' ' + JSON.stringify(block.location.z + 1) + ' air replace pcr:ghostpedestals')
            block.dimension.runCommandAsync('fill ' + JSON.stringify(block.location.x - 2) + ' ' + JSON.stringify(block.location.y) + ' ' + JSON.stringify(block.location.z - 1) + " " + JSON.stringify(block.location.x - 2) + ' ' + JSON.stringify(block.location.y) + ' ' + JSON.stringify(block.location.z - 1) + ' air replace pcr:ghostpedestals')
        },
    });

    initEvent.blockComponentRegistry.registerCustomComponent('pcr:detectmosspower', {
        onTick: e => {
            const block = e.block
            if (block.east(2).hasTag("mosspowered") && block.south(2).hasTag("mosspowered") && block.north(2).hasTag("mosspowered") && block.west(2).north(1).hasTag("mosspowered") && block.west(2).south(1).hasTag("mosspowered") && block.hasTag("mosspowered")) {
                block.setPermutation(block.permutation.withState('pcr:ismossy', 0))
                //Resets Other Pedestals
                block.east(2).setPermutation(BlockPermutation.resolve('pcr:pedestal'))
                block.south(2).setPermutation(BlockPermutation.resolve('pcr:pedestal'))
                block.north(2).setPermutation(BlockPermutation.resolve('pcr:pedestal'))
                block.west(2).north(1).setPermutation(BlockPermutation.resolve('pcr:pedestal'))
                block.west(2).south(1).setPermutation(BlockPermutation.resolve('pcr:pedestal'))

                block.setPermutation(block.permutation.withState('pcr:active', 1))
                block.dimension.playSound('respawn_anchor.charge', { x: block.location.x, y: block.location.y, z: block.location.z })

                //Particles
                for (let i = 0; i < 10; i++) {

                    const newLocation = {
                        x: block.location.x + Math.floor(Math.random()) + Math.random(),
                        y: block.location.y + Math.floor(Math.random() * 3),
                        z: block.location.z + Math.floor(Math.random()) + Math.random(),
                    };
                    block.dimension.spawnParticle('minecraft:crop_growth_area_emitter', newLocation);
                }
            }
        },
    });
    //Generic Pickaxe Drops for blocks
    initEvent.blockComponentRegistry.registerCustomComponent('pcr:pickaxedrops', {
        onPlayerDestroy: e => {
            const block = e.block
            const player = e.player
            const item = e.player.getComponent('equippable').getEquipment('Mainhand')

            if (item?.hasTag("minecraft:is_pickaxe")) {
                switch (block.typeId) {
                    case "mb:gloomy_stone":
                        block.dimension.runCommandAsync('loot spawn ' + JSON.stringify(block.location.x) + ' ' + JSON.stringify(block.location.y + 1) + ' ' + JSON.stringify(block.location.z) + ' loot "blocks/mb_gloomy_stone"')
                        break
                    case "hax:tin_ore":
                        block.dimension.runCommandAsync('loot spawn ' + JSON.stringify(block.location.x) + ' ' + JSON.stringify(block.location.y + 1) + ' ' + JSON.stringify(block.location.z) + ' loot "blocks/tin_ore"')
                }
            }
        },
    });
    //Gloomy shroom growth
    initEvent.blockComponentRegistry.registerCustomComponent('pcr:gloomyshroom', {
        onPlayerInteract: e => {
            const block = e.block
            const player = e.player
            const item = e.player.getComponent('equippable').getEquipment('Mainhand')


            if (item?.typeId.includes("bone_meal")) {
                if (block.typeId == "pcr:mb_gloomyshroom") {
                    decrementstack(item, player, item.typeId)
                    growth = growth + 1
                    if (growth == 4) {
                        buildmushroom(block, player)
                        growth = 0
                    }
                }
            }
        },
    });

    //Cogwheel
    initEvent.blockComponentRegistry.registerCustomComponent('pcr:cogwheelaccept', {
        onTick: e => {
            const block = e.block
            if (block.north(1).hasTag('cogsignal') || block.south(1).hasTag('cogsignal') || block.east(1).hasTag('cogsignal') || block.west(1).hasTag('cogsignal') || block.above(1).hasTag('cogsignal') || block.below(1).hasTag('cogsignal')) {
                block.setPermutation(block.permutation.withState('vatonage:signal', 1))
                block.setPermutation(block.permutation.withState('vatonage:rotation', block.permutation.getState('vatonage:rotation') + 1))
            }
        },
    });

    //Crushing Wheel
    initEvent.blockComponentRegistry.registerCustomComponent('pcr:crushingwheelaccept', {
        onTick: e => {
            const block = e.block
            if (block.north(1).hasTag('cogsignal') || block.south(1).hasTag('cogsignal') || block.east(1).hasTag('cogsignal') || block.west(1).hasTag('cogsignal')) {
                block.dimension.playSound('block.grindstone.use', { x: block.location.x, y: block.location.y, z: block.location.z })
                block.setPermutation(block.permutation.withState('vatonage:signal', 1))
                //Cobblestone --> Gravel --> Sand
                crusher(block, "minecraft:cobblestone", "minecraft:gravel", undefined, "minecraft:falling_dust_gravel_particle")
                crusher(block, "minecraft:gravel", "minecraft:sand", undefined, "minecraft:falling_dust_gravel_particle")
                crusher(block, "minecraft:sand", "pcr:dust", undefined, "minecraft:falling_dust_sand_particle")

                //Item loots pool crushings
                crusher(block, "sev:charcoalblock", undefined, 'charcoalresultcrushing', "minecraft:falling_dust_dragon_egg_particle")
                crusher(block, "pcr:crushablevegetation", undefined, 'shreddedvegetation', "minecraft:falling_dust_concrete_powder_particle")
                crusher(block, "minecraft:hay_block", undefined, 'shreddedvegetation', "minecraft:falling_dust_concrete_powder_particle")
                crusher(block, "_leaves", undefined, 'shreddedvegetation', "minecraft:falling_dust_concrete_powder_particle")

            }
        },
    });
    //Dust Moister
    initEvent.blockComponentRegistry.registerCustomComponent('pcr:hydratedust', {
        onPlayerInteract: e => {
            const block = e.block
            const player = e.player
            const item = e.player.getComponent('equippable').getEquipment('Mainhand')

            //Dust to Clay
            block.above(1).setPermutation(BlockPermutation.resolve('minecraft:clay'))

            block.dimension.playSound('bucket.empty_water', { x: block.location.x, y: block.location.y, z: block.location.z })
            decrementstack(item, player, item.typeId)
            if (item?.typeId == 'sev:fullfluidbladder') {
                let newitem = new ItemStack('sev:fluidbladder', 1)
                block.dimension.spawnItem(newitem, { x: block.location.x + 0.5, y: block.location.y + 1, z: block.location.z + 0.5 })
            }
            if (item?.typeId == 'minecraft:water_bucket') {
                let newitem = new ItemStack('minecraft:bucket', 1)
                block.dimension.spawnItem(newitem, { x: block.location.x + 0.5, y: block.location.y + 1, z: block.location.z + 0.5 })
            }

        },
    });
    //Is Ashen Heater Below
    initEvent.blockComponentRegistry.registerCustomComponent('pcr:hotterlit', {
        onTick: e => {
            const block = e.block

            //Ashen Forge
            if (block.below(1).hasTag('hotfire')) {
                block.setPermutation(block.permutation.withState('pcr:active', 1))
            }
        },
    });
    //No is below
    initEvent.blockComponentRegistry.registerCustomComponent('pcr:nothotterlit', {
        onTick: e => {
            const block = e.block

            //Ashen Forge
            if (!(block.below(1).hasTag('hotfire'))) {
                block.setPermutation(block.permutation.withState('pcr:active', 0))
            }
        },
    });
    //Cog Lamp
    initEvent.blockComponentRegistry.registerCustomComponent('pcr:coglampon', {
        onTick: e => {
            const block = e.block
            if (block.north(1).hasTag('cogsignal') || block.south(1).hasTag('cogsignal') || block.east(1).hasTag('cogsignal') || block.west(1).hasTag('cogsignal')) {
                block.setPermutation(block.permutation.withState('vatonage:signal', 1))
            }
        },
    });
    //Creative Motor
    initEvent.blockComponentRegistry.registerCustomComponent('pcr:creativemotor', {
        onTick: e => {
            const block = e.block
            block.setPermutation(block.permutation.withState('vatonage:signal', 1))
        },
    });
    //Cobblestone Generater Primitive Create
    initEvent.blockComponentRegistry.registerCustomComponent('pcr:cobblestonegen', {
        onTick: e => {
            const block = e.block
            if (block.north(1).hasTag('cogsignal') || block.south(1).hasTag('cogsignal') || block.east(1).hasTag('cogsignal') || block.west(1).hasTag('cogsignal')) {
                block.setPermutation(block.permutation.withState('vatonage:signal', 1))
                block.below(1).setPermutation(BlockPermutation.resolve('minecraft:cobblestone'))
            }
        },
    });

    //Saw (Only Destroys Straight up trees)
    initEvent.blockComponentRegistry.registerCustomComponent('pcr:primitivesaw', {
        onTick: e => {
            const block = e.block
            if (block.north(1).hasTag('cogsignal') || block.south(1).hasTag('cogsignal') || block.east(1).hasTag('cogsignal') || block.west(1).hasTag('cogsignal')) {
                block.setPermutation(block.permutation.withState('vatonage:signal', 1))
                if (block?.dimension.getBlockFromRay({ x: block?.location.x, y: block?.location.y + 2, z: block?.location.z }, { x: 0, y: 1, z: 0 },).block?.typeId != undefined) {
                    while (block?.dimension.getBlockFromRay({ x: block?.location.x, y: block?.location.y + 2, z: block?.location.z }, { x: 0, y: 1, z: 0 },).block?.typeId.includes("_log")) {
                        let raycastblock = block.dimension.getBlockFromRay({ x: block.location.x, y: block.location.y + 2, z: block.location.z }, { x: 0, y: 1, z: 0 },).block
                        raycastblock.dimension.runCommand('fill ' + JSON.stringify(raycastblock.location.x) + ' ' + JSON.stringify(raycastblock.location.y) + ' ' + JSON.stringify(raycastblock.location.z) + ' ' + JSON.stringify(raycastblock.location.x) + ' ' + JSON.stringify(raycastblock.location.y) + ' ' + JSON.stringify(raycastblock.location.z) + ' air' + ' destroy')
                    }
                    world.sendMessage(JSON.stringify(block.dimension.getBlockFromRay({ x: block.location.x, y: block.location.y + 1, z: block.location.z }, { x: 0, y: 1, z: 0 },).block.typeId))
                }
            }
        },
    });

    //Sieve
    initEvent.blockComponentRegistry.registerCustomComponent('pcr:primitivesieve', {
        onTick: e => {
            const block = e.block
            if (block.north(1).hasTag('cogsignal') || block.south(1).hasTag('cogsignal') || block.east(1).hasTag('cogsignal') || block.west(1).hasTag('cogsignal')) {
                block.setPermutation(block.permutation.withState('vatonage:signal', 1))
                //Just this stuff
                crusher(block, "minecraft:sand", undefined, '"entities/sievegravelloot2"', "minecraft:falling_dust_sand_particle")
            }
        },
    });

    //Water Wheel
    initEvent.blockComponentRegistry.registerCustomComponent('pcr:waterwheel', {
        onTick: e => {
            const block = e.block
            if (block?.above(1).typeId == "minecraft:water") {
                block.setPermutation(block.permutation.withState('vatonage:signal', 1))
            }
        },
    });



    //Items
    initEvent.itemComponentRegistry.registerCustomComponent('pcr:firesticks', {
        //await needs async
        onUseOn: async (e) => {
            const block = e.block
            const blockface = e.blockFace
            const item = e.itemStack
            const player = e.source
            const offhanditem = player.getComponent("minecraft:equippable").getEquipment('Offhand')
            const inv = player.getComponent("minecraft:inventory").container




            if (item.typeId == 'sev:firestick' && offhanditem?.typeId == 'sev:fireflint' && blockface.match('Up')) {
                block.dimension.playSound('fire.ignite', { x: block.location.x, y: block.location.y, z: block.location.z })
                if (block.hasTag('pcr:charcoalify') || block.typeId == 'minecraft:hay_block') {
                    decrementstack(item, player, item.typeId)
                    offhanddurability(offhanditem, player)
                    block.above(1).setPermutation(BlockPermutation.resolve('minecraft:fire'))
                    if (await firedown(block, 7)) {
                        block.setPermutation(BlockPermutation.resolve('sev:charcoalblock'))
                        block.dimension.playSound('random.fizz', { x: block.location.x, y: block.location.y, z: block.location.z })


                    }
                }
                else {
                    block.above(1).setPermutation(BlockPermutation.resolve('minecraft:fire'))
                    offhanddurability(offhanditem, player)
                    decrementstack(item, player, item.typeId)
                }



            }

        },
    });

    //Otters
    initEvent.blockComponentRegistry.registerCustomComponent('pcr:otterbucketplace', {
        onPlace: e => {
            const block = e.block


            switch (block.typeId) {
                case "alfie:otter_a_block_baby":
                    block.dimension.runCommand("summon alfie:otter" + JSON.stringify(block.location.x) + ' ' + JSON.stringify(block.location.y) + ' ' + JSON.stringify(block.location.z) + "a_baby")
                    break
                case "alfie:otter_a_block":
                    block.dimension.runCommand("summon alfie:otter " + JSON.stringify(block.location.x) + ' ' + JSON.stringify(block.location.y) + ' ' + JSON.stringify(block.location.z) + " 0 0 a")
                    break
                case "alfie:otter_b_block":
                    block.dimension.runCommand("summon alfie:otter " + JSON.stringify(block.location.x) + ' ' + JSON.stringify(block.location.y) + ' ' + JSON.stringify(block.location.z) + " 0 0 b")
                    break
                case "alfie:otter_c_block":
                    block.dimension.runCommand("summon alfie:otter " + JSON.stringify(block.location.x) + ' ' + JSON.stringify(block.location.y) + ' ' + JSON.stringify(block.location.z) + " 0 0 c")
                    break
                case "alfie:otter_d_block":
                    block.dimension.runCommand("summon alfie:otter " + JSON.stringify(block.location.x) + ' ' + JSON.stringify(block.location.y) + ' ' + JSON.stringify(block.location.z) + " 0 0 d")
                    break
                case "alfie:otter_e_block":
                    block.dimension.runCommand("summon alfie:otter " + JSON.stringify(block.location.x) + ' ' + JSON.stringify(block.location.y) + ' ' + JSON.stringify(block.location.z) + " 0 0 e")
                    break
            }
            block.setPermutation(BlockPermutation.resolve('minecraft:flowing_water'))
            let newitem = new ItemStack('minecraft:bucket', 1)
            block.dimension.spawnItem(newitem, { x: block.location.x + 0.5, y: block.location.y + 1, z: block.location.z + 0.5 })

        },
    });


    //Vanilla Plus Updated (By me)
    initEvent.itemComponentRegistry.registerCustomComponent('vanilla:eobrella', {
        onUse: e => {
            const item = e.itemStack
            const player = e.source
            const inv = player.getComponent("minecraft:inventory").container
            const slot = player.selectedSlotIndex

            if (item.typeId == 'vp:eobrella') {
                add_effect(player, 'levitation', 60, 0)
                player.dimension.playSound('mob.snowgolem.death', { x: player.location.x, y: player.location.y, z: player.location.z })
                adjustItemDamage(inv, item, 1, slot)
            }
        },
    });

    //Gloomy
    initEvent.itemComponentRegistry.registerCustomComponent('pcr:hunger', {
        onCompleteUse: e => {
            const player = e.source
            add_effect(player, 'hunger', 100, 2)
        },
    });

    initEvent.itemComponentRegistry.registerCustomComponent('pcr:poison', {
        onCompleteUse: e => {
            const player = e.source
            add_effect(player, 'poison', 100, 2)
        },
    });

    initEvent.itemComponentRegistry.registerCustomComponent('pcr:onitemuse', {
        onMineBlock: e => {
            const item = e.itemStack
            const player = e.source
            const inv = player.getComponent("minecraft:inventory").container
            const slot = player.selectedSlotIndex
            adjustItemDamage(inv, item, 1, slot)
        },
        onHitEntity: e => {
            const item = e.itemStack
            const player = e.attackingEntity

            const inv = player.getComponent("minecraft:inventory").container
            const slot = player.selectedSlotIndex
            adjustItemDamage(inv, item, 1, slot)
        },
    });


});

function set_log(block, item, player, woodtype, type) {
    block.setPermutation(block.permutation.withState(woodtype, type))
    block.dimension.playSound('use.wood', { x: block.location.x, y: block.location.y, z: block.location.z })
    decrementstack(item, player, item.typeId)
}


function firedown(block, timedown) {
    var time = timedown

    return new Promise((resolve) => {
        function firetick() {
            if (!(block.above(1).typeId == "minecraft:fire")) {
                resolve(false)
                return
            }

            if (time == 0) {
                resolve(true)
                return
            }

            time -= 1

            system.runTimeout(firetick, 20)

        }

        system.run(firetick)
    })
}

function offhanddurability(offhanditem, player) {
    const durability = offhanditem.getComponent("durability")
    const maxdurability = offhanditem.getComponent("durability").maxDurability

    if (maxdurability == durability.damage) {
        player.getComponent("minecraft:equippable").setEquipment("Offhand", undefined)
        player.dimension.playSound("random.break", { x: player.location.x, y: player.location.y, z: player.location.z })
    }
    else {
        durability.damage++
        player.getComponent("minecraft:equippable").setEquipment("Offhand", offhanditem)
    }
}

function crusher(block, input, output, loot, particle) {
    if ((block.above(1).typeId == input || block.above(1).hasTag(input) || block.above(1).typeId.includes(input)) && block.below(1).isAir) {
        block.above(1).setPermutation(BlockPermutation.resolve('minecraft:air'))
        if (output != undefined) {
            block.below(1).setPermutation(BlockPermutation.resolve(output))
        }
        if (loot != undefined) {
            block.dimension.runCommandAsync('loot spawn ' + JSON.stringify(block.location.x) + ' ' + JSON.stringify(block.location.y - 1) + ' ' + JSON.stringify(block.location.z) + ' loot ' + loot)
        }
        if (particle == "minecraft:falling_dust_concrete_powder_particle") {
            for (let i = 0; i < 50; i++) {
                const molang = new MolangVariableMap();

                molang.setColorRGBA('variable.color', {
                    red: Math.random(),
                    green: Math.random() + 0.2,
                    blue: 0,
                    alpha: 1
                })

                const newLocation = {
                    x: block.above(1).location.x + Math.floor(Math.random()) - Math.random() + 1,
                    y: block.above(1).location.y + Math.floor(Math.random()) - Math.random() + 1,
                    z: block.above(1).location.z + Math.floor(Math.random()) - Math.random() + 1,
                }
                block.above(1).dimension.spawnParticle(particle, newLocation, molang);
            }
        } else {
            for (let i = 0; i < 50; i++) {
                const newLocation = {
                    x: block.above(1).location.x + Math.floor(Math.random()) - Math.random() + 1,
                    y: block.above(1).location.y + Math.floor(Math.random()) - Math.random() + 1,
                    z: block.above(1).location.z + Math.floor(Math.random()) - Math.random() + 1,
                }
                block.above(1).dimension.spawnParticle(particle, newLocation)
            }
        }
    }
}
