// 生物骑乘事件
ForgeEvents.onEvent("net.minecraftforge.event.entity.EntityMountEvent", event => {
    let entity = event.entity
    let mount = event.entityBeingMounted
    let mName = mount.level.dimension.path
    let x = mount.x
    let y = mount.y
    let z = mount.z
    //玩家离开登记的坐骑时送回坐骑
    if (mount.tags.contains("snk_mount") && event.dismounting) {
        entity.server.scheduleInTicks(3, () => {
            //切换战略模式也会触发停止骑乘监听，需要排除此情况
            if (mount.getPassengers().length > 0) return
            let pUuid = entity.uuid.toString()
            let allow = true
            entity.server.entities.forEach(stable => {
                if (allow === false) return
                let sData = stable.persistentData
                let sUuid = sData.getString("pUuid")
                if (stable.tags.contains('snk_stable') && sUuid === pUuid) {
                    mount.teleportTo(stable.level.dimension.path, stable.x, stable.y + 3, stable.z, 0, 0)
                    allow = false
                }
            })
        })
    }
    //玩家离开召唤的矿车时删除矿车
    if (mount.tags.contains("snk_player_minecart") && event.dismounting) {
        event.level.server.runCommandSilent(`execute in ${mName} run kill @e[x=${x},y=${y},z=${z},limit=1,distance=..1,tag=snk_player_minecart]`)
    }
    //科妮莉亚骑乘任何实体时杀死实体
    if (entity.type === "aquamirae:captain_cornelia" && event.mounting) {
        event.level.server.runCommandSilent(`execute in ${mName} run kill @e[x=${x},y=${y},z=${z},distance=..0.5,type=!aquamirae:captain_cornelia]`)
    }
    //祈灵师乘任何实体时杀死实体
    if (entity.type === "mowziesmobs:frostmaw" && event.mounting) {
        event.level.server.runCommandSilent(`execute in ${mName} run kill @e[x=${x},y=${y},z=${z},distance=..0.5,type=!mowziesmobs:frostmaw]`)
    }
})

//宠物入队
ForgeEvents.onEvent("net.minecraftforge.event.entity.living.AnimalTameEvent", event => {
    let entity = event.entity
    let player = event.getTamer()
    let team = player.team.name
    let eUuid = entity.uuid.toString()
    entity.persistentData.putString("pUuid", player.uuid.toString())
    player.server.runCommandSilent(`execute as ${eUuid} run team join ${team} @s`)
})