//利用hotai修改了tetra模组源码

//放置锻造锤逻辑
BlockEvents.rightClicked(event => {
    let player = event.entity
    let level = player.level
    let block = event.block
    let item = player.getMainHandItem()
    let face = event.facing
    if (event.hand != 'MAIN_HAND') return

    let x = block.x
    let y = block.y
    let z = block.z
    switch (face) {
        case 'up':
            y = y + 2
            break
        case 'down':
            y = y - 1
            break
        case 'north':
            z = z - 1
            break
        case 'south':
            z = z + 1
            break
        case 'east':
            x = x + 1
            break
        case 'west':
            x = x - 1
            break
    }
    let pos = new BlockPos(x, y, z)
    let pos_head = new BlockPos(x, y - 1, z)


    if (item == 'tetra:hammer_modular') {
        let block_down = level.getBlock(pos_head)
        if (block_down != 'minecraft:air' && block_down != 'minecraft:cave_air') {
            player.tell('有方块阻挡，无法放置锻造锤')
            return
        }

        level.setBlockAndUpdate(pos, Block.getBlock('tetra:hammer_base').defaultBlockState())
        level.setBlockAndUpdate(pos_head, Block.getBlock('tetra:hammer_head').defaultBlockState())

        level.getBlockEntity(pos).persistentData.put('modular_hammer_nbt', item.nbt)
        level.getBlockEntity(pos_head).persistentData.put('modular_hammer_nbt', item.nbt)

        //提供锤等级
        let hammer_level = simpleGetTetraEffectLevel(item, 'hammer_level')
        level.getBlockEntity(pos).persistentData.putInt('modular_hammer', hammer_level)

        //减少故障概率
        let kubejs_reliable = simpleGetTetraEffectLevel(item, 'kubejs_reliable')
        //增加故障概率
        let kubejs_not_reliable = simpleGetTetraEffectLevel(item, 'kubejs_reliable')
        level.getBlockEntity(pos).persistentData.putInt('modular_hammer_reliable', kubejs_reliable - kubejs_not_reliable)



        item.shrink(1)


    }
})
//回收/破坏锻造锤逻辑
BlockEvents.broken(event => {
    let player = event.entity
    let level = player.level
    let block = event.block
    if (block != 'tetra:hammer_base' && block != 'tetra:hammer_head') return

    if (level.getBlockEntity(block.pos)) {
        if (level.getBlockEntity(block.pos).persistentData.get('modular_hammer_nbt') != null) {
            let nbt = level.getBlockEntity(block.pos).persistentData.get('modular_hammer_nbt')
            let item = Item.of('tetra:hammer_modular')
            item.getOrCreateTag().merge(nbt)
            updateItemEffects(item)
            event.block.popItem(item)
            event.block.set('minecraft:air')
            event.cancel()
        }
    }
})
