//检测玩家各种行为来发放提示
function title_prompt(message_name, player) {
    if (!player.persistentData.getBoolean(message_name + '_prompt')) {
        if (player.persistentData.get('title_prompt') ?? 0 > 0) return

        //防止重叠
        let time = player.persistentData.getInt('ti_shi_cd') ?? 0
        let CD = Math.abs(player.age - time)
        if (CD < 20 * 30) return
        player.persistentData.putInt('ti_shi_cd', player.age)

        player.persistentData.putBoolean(message_name + '_prompt', true)
        player.sendData('kubejs:' + message_name)
        player.persistentData.putBoolean('title_prompt',true)
    }
}

//饰品变动事件
NativeEvents.onEvent($CurioChangeEvent, event => {
    let player = event.getEntity()
    let item = event.getTo().item
    if (item == 'zu_li_jian1'){
        title_prompt('zu_li_jian1', player)
    }
})
// 物品拾取事件
NativeEvents.onEvent($PlayerEvent.ItemPickupEvent, event => {
    let player = event.getEntity()
    let item = event.getStack().item.id
    //捡到铜锭或粗铜
    if (item == 'minecraft:copper_ingot' || item == 'minecraft:raw_copper'){
        title_prompt('copper', player)
    }
 
})
TetraJSEvents.workbenchCraft(event => {
    let { player, level, currentSchematic, currentSlot, targetStack, upgradedStack, materials, workbenchTile } = event;
    // 防止单人的时候client侧出问题
    if (level.clientSide) return;
    let pos = workbenchTile.getBlockPos()
    let new_pos = new BlockPos(pos.x, pos.y + 1, pos.z)

    let IntegrityGain = upgradedStack.item.getIntegrityGain(upgradedStack)
    let IntegrityCost = upgradedStack.item.getIntegrityCost(upgradedStack)

    let materialItem = materials[0].id


    //检测锻造锤是否损坏
    if (level.getBlockEntity(new_pos) && level.getBlockEntity(new_pos) instanceof $HammerHeadBlockEntity && level.getBlockEntity(new_pos).isJammed()) {
        title_prompt('jammed', player)
    }
    //检测改造饰品
    if ($CuriosApi.getCurio(upgradedStack).isPresent()) {
        title_prompt('curios', player)
    }

    //检测改造盔甲
    if (tetra_armor.includes(upgradedStack.getId())) {
        title_prompt('armor', player)
    }
    //schematic的类型为improvement
    if (currentSchematic.getType() == 'improvement') {
        title_prompt('improvement', player)
    }
    //完整度不够用提示
    if (IntegrityCost > 0 && IntegrityGain == IntegrityCost){
        title_prompt('integrity', player)
    }
    //末地烛提示
    if (materialItem == 'minecraft:end_rod'){
        title_prompt('end_rod', player)
    }
    //制作焰型剑提醒熔岩吸收需要打磨
    if (currentSchematic.key == "sword/flamberge/flamberge_blade"){
        title_prompt('flamberge_blade', player)
    }
    //四级锤获取的提示
    if ( materialItem == 'minecraft:iron_ingot') {
        if (currentSchematic.key == "double/basic_hammer/basic_hammer_right" || currentSchematic.key == "double/basic_hammer/basic_hammer_left"){
            title_prompt('level_hammer', player)
        }
    }
})

BlockEvents.rightClicked(event => {
    let player = event.entity
    let item = player.getMainHandItem()
    let block = event.block

    //首次改造工作台
    if (item == 'tetra:modular_double' && block.id == 'minecraft:crafting_table') {
        title_prompt('crafting_table', player)
    }
    //教玩家如何查看可用材料
    if (block.id == 'tetra:basic_workbench') {
        title_prompt('material', player)
    }
    //假人提示
    if (item == 'powerful_dummy:dummy_stand'){
        title_prompt('dummy_stand', player)
    }
    //村民交易提示
    if (item == 'easy_villagers:trader'){
        title_prompt('trade', player)
    }
})
BlockEvents.broken(event => {
    let player = event.entity
    let block = event.block
    if (block.id == 'tetra:hammer_base'||
        block.id == 'tetra:hammer_head'
    ) {
        title_prompt('modular_hammer_head', player)
    }
})
//玩家刻随机发放提示
PlayerEvents.tick(event => {
    let player = event.entity
    if (!player) return
    if (player.age % 20 != 0) return

    //随机工具等级提示
    if (Math.random() < 0.0005) {
        title_prompt('tool_level', player)
    }

})
NativeEvents.onEvent($LivingHurtEvent, event => {
    //关闭屏幕上的数字显示
    if (event.entity.isPlayer()) {
        title_prompt('damage_number', event.entity)
    }
})