//玩家登录事件,初始化玩家阶段
PlayerEvents.loggedIn(event => {
    let player = event.player
    if (!player.persistentData.contains('stages')) {
        player.persistentData.putInt('stages', 1)
    }
})

//二阶段之前禁止玩家使用锻造遗迹
BlockEvents.rightClicked(
    [
        'tetra:forged_crate',
        'tetra:forged_container',
        'tetra:hammer_base'
    ],
    event => {
        let player = event.player
        //不检测创造模式
        if (player.isCreative()) return
        let value = player.persistentData.getInt('stages')
        if (value < 2) {
            player.tell('阶段二才能使用/打开该方块')
            event.cancel()
        }
    })
BlockEvents.broken(event => {
    let player = event.player
    //不检测创造模式
    if (player.isCreative()) return
    let value = player.persistentData.getInt('stages')
    if (value < 2 && event.block == 'tetra:forged_crate') {
        player.tell('阶段二才能破坏该方块')
        event.cancel()
    }
})

//阶段二才允许玩家进入地狱和染梦世界
function PlayerChangedDimensionEvent_2(event) {
    let player = event.entity
    let x = player.x
    let y = player.y
    let z = player.z
    if (player.isPlayer()) {
        let value = player.persistentData.getInt('stages')
        //不检测创造模式
        if (player.isCreative() || value >= 2) return
        if (event.dimension == 'minecraft:the_nether' || event.dimension == "pasterdream:dyedream_world") {
            player.tell('阶段二才能进入该维度，任务书ctrl+f搜索‘阶段’即可找到对应章节')
            player.getServer().scheduleInTicks(2, () => {
                player.teleportTo('minecraft:overworld', x, y, z, 0, 0)
            })
        }
    }
}

//阶段三才允许玩家进入末地和二轮世界
function PlayerChangedDimensionEvent_3(event) {
    let player = event.entity
    let x = player.x
    let y = player.y
    let z = player.z
    if (player.type == 'minecraft:player') {
        let value = player.persistentData.getInt('stages')
        //不检测创造模式
        if (player.isCreative() || value >= 3) return
        if (event.dimension == "minecraft:the_end" || event.dimension == "jerotesvillage:second_round_world") {
            player.tell('阶段三才能进入该维度，任务书ctrl+f搜索‘阶段’即可找到对应章节')
            player.getServer().scheduleInTicks(2, () => {
                player.teleportTo('minecraft:overworld', x + 5, y, z + 5, 0, 0)
            })
        }
    }
}

const entity_spawn_limit = [
    { entity: 'minecraft:wither', limit_dimension: 'minecraft:the_nether', messages: '凋零只允许在地狱生成' },
]
//限定特定维度生成实体
EntityEvents.spawned(event => {
    let entityType = event.entity.type
    let dimension = event.entity.level.getDimension()


    // 检查实体是否在限制列表中
    let limitConfig = entity_spawn_limit.find(config => config.entity === entityType)

    if (limitConfig) {
        // 检查维度是否符合限制
        if (dimension !== limitConfig.limit_dimension) {
            event.entity.server.tell(limitConfig.messages)
            event.cancel()
        }
    }
})
//禁止跨阶段打boss
//写的比较死（史）但是我不想动脑子了
NativeEvents.onEvent($LivingAttackEvent, event => {
    if (event.source.actual && event.entity) {
        let player = event.source.actual
        let entity = event.entity
        if (boss_form_2_to_3.includes(entity.type)) {
            if (player && player.isPlayer()) {
                //获取玩家阶段
                let player_stage_level = player.persistentData.getInt('stages')

                //如果是二阶段boss只有二阶段需要限制
                if (!stage_boss_3.includes(entity.type)) {
                    //如果玩家阶段等于1则不攻击玩家
                    if (player_stage_level == 1) {
                        //不检测创造模式
                        if (player.isCreative()) return
                        player.tell('此boss为二阶段boss，请二阶段再打')
                        event.setCanceled(true);
                    }
                }
            }
        }
    }
});
NativeEvents.onEvent($LivingChangeTargetEvent, event => {
    if (!event.getEntity().getLevel().isClientSide()) {
        let entity = event.entity
        //是否是二三阶段boss
        if (boss_form_2_to_3.includes(entity.type)) {
            let player = event.getNewTarget()
            //不检测创造模式
            if (player && player.isPlayer() && player.isCreative()) return
            if (player && player.isPlayer()) {
                //获取玩家阶段
                let player_stage_level = player.persistentData.getInt('stages')
                //如果是二阶段boss只有二阶段需要限制
                if (!stage_boss_3.includes(entity.type)) {
                    //如果玩家阶段等于1则不攻击玩家
                    if (player_stage_level == 1) {
                        event.setCanceled(true);
                    }
                }
            }
        }
    }
})