mirror of
https://github.com/jie65535/mirai-console-jms-plugin.git
synced 2025-12-08 18:41:35 +08:00
Initial commit
This commit is contained in:
18
src/main/kotlin/HttpUtil.kt
Normal file
18
src/main/kotlin/HttpUtil.kt
Normal file
@@ -0,0 +1,18 @@
|
||||
package top.jie65535
|
||||
|
||||
import okhttp3.OkHttpClient
|
||||
import okhttp3.Request
|
||||
import java.util.concurrent.TimeUnit
|
||||
|
||||
object HttpUtil {
|
||||
private val okHttpClient: OkHttpClient by lazy {
|
||||
OkHttpClient.Builder()
|
||||
.readTimeout(30, TimeUnit.SECONDS)
|
||||
.build()
|
||||
}
|
||||
|
||||
fun get(url: String): ByteArray {
|
||||
val request = Request.Builder().url(url).build()
|
||||
return okHttpClient.newCall(request).execute().body!!.bytes()
|
||||
}
|
||||
}
|
||||
83
src/main/kotlin/JMSCommand.kt
Normal file
83
src/main/kotlin/JMSCommand.kt
Normal file
@@ -0,0 +1,83 @@
|
||||
package top.jie65535
|
||||
|
||||
import net.mamoe.mirai.console.command.CommandSender
|
||||
import net.mamoe.mirai.console.command.CompositeCommand
|
||||
import net.mamoe.mirai.contact.Contact.Companion.sendImage
|
||||
import net.mamoe.mirai.utils.ExternalResource.Companion.toExternalResource
|
||||
|
||||
object JMSCommand : CompositeCommand(
|
||||
JMinecraftSkin, "jms",
|
||||
description = "Minecraft Skin Commands"
|
||||
) {
|
||||
@SubCommand("uuid")
|
||||
@Description("查询玩家UUID")
|
||||
suspend fun CommandSender.getUuid(username: String) {
|
||||
try {
|
||||
sendMessage(MinecraftPlayer(username).uuid)
|
||||
} catch (e: Throwable) {
|
||||
JMinecraftSkin.logger.error("获取玩家UUID失败", e)
|
||||
e.message?.let { sendMessage(it) }
|
||||
}
|
||||
}
|
||||
|
||||
@SubCommand("avatar")
|
||||
@Description("查询玩家头像")
|
||||
suspend fun CommandSender.getAvatar(username: String) {
|
||||
try {
|
||||
sendImage(MinecraftPlayer(username).avatar)
|
||||
} catch (e: Throwable) {
|
||||
JMinecraftSkin.logger.error("获取玩家头像失败", e)
|
||||
e.message?.let { sendMessage(it) }
|
||||
}
|
||||
}
|
||||
|
||||
@SubCommand("head")
|
||||
@Description("查询玩家头模型")
|
||||
suspend fun CommandSender.getHead(username: String) {
|
||||
try {
|
||||
sendImage(MinecraftPlayer(username).head)
|
||||
} catch (e: Throwable) {
|
||||
JMinecraftSkin.logger.error("查询玩家头模型失败", e)
|
||||
e.message?.let { sendMessage(it) }
|
||||
}
|
||||
}
|
||||
|
||||
@SubCommand("body")
|
||||
@Description("查询玩家皮肤模型")
|
||||
suspend fun CommandSender.getBody(username: String) {
|
||||
try {
|
||||
sendImage(MinecraftPlayer(username).body)
|
||||
} catch (e: Throwable) {
|
||||
JMinecraftSkin.logger.error("查询玩家皮肤模型失败", e)
|
||||
e.message?.let { sendMessage(it) }
|
||||
}
|
||||
}
|
||||
|
||||
@SubCommand("skin")
|
||||
@Description("获取玩家皮肤文件")
|
||||
suspend fun CommandSender.getSkin(username: String) {
|
||||
try {
|
||||
sendImage(MinecraftPlayer(username).skin)
|
||||
} catch (e: Throwable) {
|
||||
JMinecraftSkin.logger.error("获取玩家皮肤文件失败", e)
|
||||
e.message?.let { sendMessage(it) }
|
||||
}
|
||||
}
|
||||
|
||||
@SubCommand("cape")
|
||||
@Description("获取玩家披风文件")
|
||||
suspend fun CommandSender.getCape(username: String) {
|
||||
try {
|
||||
sendImage(MinecraftPlayer(username).cape)
|
||||
} catch (e: Throwable) {
|
||||
JMinecraftSkin.logger.error("获取玩家披风文件失败", e)
|
||||
e.message?.let { sendMessage(it) }
|
||||
}
|
||||
}
|
||||
|
||||
private suspend fun CommandSender.sendImage(data: ByteArray) {
|
||||
data.toExternalResource().use {
|
||||
subject?.sendImage(it)
|
||||
}
|
||||
}
|
||||
}
|
||||
13
src/main/kotlin/JMSPluginData.kt
Normal file
13
src/main/kotlin/JMSPluginData.kt
Normal file
@@ -0,0 +1,13 @@
|
||||
package top.jie65535
|
||||
|
||||
import net.mamoe.mirai.console.data.AutoSavePluginData
|
||||
import net.mamoe.mirai.console.data.value
|
||||
|
||||
object JMSPluginData : AutoSavePluginData("data") {
|
||||
/**
|
||||
* 玩家昵称与ID缓存
|
||||
*
|
||||
* key: 昵称 value: uuid
|
||||
*/
|
||||
val idMap: MutableMap<String, String> by value()
|
||||
}
|
||||
28
src/main/kotlin/JMinecraftSkin.kt
Normal file
28
src/main/kotlin/JMinecraftSkin.kt
Normal file
@@ -0,0 +1,28 @@
|
||||
package top.jie65535
|
||||
|
||||
import net.mamoe.mirai.console.command.CommandManager.INSTANCE.register
|
||||
import net.mamoe.mirai.console.plugin.jvm.JvmPluginDescription
|
||||
import net.mamoe.mirai.console.plugin.jvm.KotlinPlugin
|
||||
import net.mamoe.mirai.utils.info
|
||||
|
||||
object JMinecraftSkin : KotlinPlugin(
|
||||
JvmPluginDescription(
|
||||
id = "top.jie65535.mirai-console-jms-plugin",
|
||||
name = "J Minecraft Skin",
|
||||
version = "0.1.0",
|
||||
) {
|
||||
author("jie65535")
|
||||
info("MC皮肤查询插件")
|
||||
}
|
||||
) {
|
||||
|
||||
override fun onEnable() {
|
||||
JMSPluginData.reload()
|
||||
JMSCommand.register()
|
||||
logger.info { "Plugin loaded" }
|
||||
}
|
||||
|
||||
override fun onDisable() {
|
||||
JMSPluginData.save()
|
||||
}
|
||||
}
|
||||
39
src/main/kotlin/MinecraftPlayer.kt
Normal file
39
src/main/kotlin/MinecraftPlayer.kt
Normal file
@@ -0,0 +1,39 @@
|
||||
package top.jie65535
|
||||
|
||||
/**
|
||||
* Minecraft 玩家封装
|
||||
*
|
||||
* @param username 玩家昵称
|
||||
*/
|
||||
class MinecraftPlayer(username: String) {
|
||||
|
||||
/**
|
||||
* 玩家UUID
|
||||
*/
|
||||
val uuid: String = MinecraftUuidService.getUuid(username)
|
||||
|
||||
/**
|
||||
* 头像
|
||||
*/
|
||||
val avatar get() = MinecraftSkinService.getAvatars(uuid)
|
||||
|
||||
/**
|
||||
* 头模型渲染
|
||||
*/
|
||||
val head get() = MinecraftSkinService.getHeadRenders(uuid)
|
||||
|
||||
/**
|
||||
* 皮肤模型渲染
|
||||
*/
|
||||
val body get() = MinecraftSkinService.getBodyRenders(uuid)
|
||||
|
||||
/**
|
||||
* 皮肤文件
|
||||
*/
|
||||
val skin get() = MinecraftSkinService.getSkins(uuid)
|
||||
|
||||
/**
|
||||
* 披风文件
|
||||
*/
|
||||
val cape get() = MinecraftSkinService.getCapes(uuid)
|
||||
}
|
||||
39
src/main/kotlin/MinecraftSkinService.kt
Normal file
39
src/main/kotlin/MinecraftSkinService.kt
Normal file
@@ -0,0 +1,39 @@
|
||||
package top.jie65535
|
||||
|
||||
/**
|
||||
* Minecraft Skin Service
|
||||
*
|
||||
* https://crafatar.com/
|
||||
*/
|
||||
object MinecraftSkinService {
|
||||
|
||||
/**
|
||||
* 获取头像
|
||||
*/
|
||||
fun getAvatars(uuid: String)
|
||||
= HttpUtil.get("https://crafatar.com/avatars/$uuid")
|
||||
|
||||
/**
|
||||
* 获取玩家头模型渲染图
|
||||
*/
|
||||
fun getHeadRenders(uuid: String)
|
||||
= HttpUtil.get("https://crafatar.com/renders/head/$uuid")
|
||||
|
||||
/**
|
||||
* 获取玩家皮肤模型渲染图
|
||||
*/
|
||||
fun getBodyRenders(uuid: String)
|
||||
= HttpUtil.get("https://crafatar.com/renders/body/$uuid")
|
||||
|
||||
/**
|
||||
* 获取玩家皮肤源文件
|
||||
*/
|
||||
fun getSkins(uuid: String)
|
||||
= HttpUtil.get("https://crafatar.com/skin/$uuid")
|
||||
|
||||
/**
|
||||
* 获取玩家皮肤源文件
|
||||
*/
|
||||
fun getCapes(uuid: String)
|
||||
= HttpUtil.get("https://crafatar.com/capes/$uuid")
|
||||
}
|
||||
38
src/main/kotlin/MinecraftUuidService.kt
Normal file
38
src/main/kotlin/MinecraftUuidService.kt
Normal file
@@ -0,0 +1,38 @@
|
||||
package top.jie65535
|
||||
|
||||
import kotlinx.serialization.ExperimentalSerializationApi
|
||||
import kotlinx.serialization.decodeFromString
|
||||
import kotlinx.serialization.json.Json
|
||||
import kotlinx.serialization.json.JsonElement
|
||||
import kotlinx.serialization.json.JsonObject
|
||||
import kotlinx.serialization.json.jsonPrimitive
|
||||
import net.mamoe.mirai.console.util.cast
|
||||
|
||||
/**
|
||||
* Minecraft UUID Service
|
||||
*
|
||||
* https://tenapi.cn/
|
||||
*/
|
||||
object MinecraftUuidService {
|
||||
|
||||
/**
|
||||
* 根据游戏角色名获取UUID
|
||||
*/
|
||||
@OptIn(ExperimentalSerializationApi::class)
|
||||
fun getUuid(username: String) : String {
|
||||
var uuid = JMSPluginData.idMap[username]
|
||||
if (uuid != null) {
|
||||
return uuid
|
||||
}
|
||||
|
||||
val retJson = HttpUtil.get("https://tenapi.cn/mc/?uid=$username").decodeToString()
|
||||
val response = Json.decodeFromString<JsonObject>(retJson)
|
||||
if (response["code"]!!.jsonPrimitive.content == "200") {
|
||||
uuid = response["id"]!!.jsonPrimitive.content
|
||||
} else {
|
||||
throw Exception(response["msg"]!!.jsonPrimitive.content)
|
||||
}
|
||||
JMSPluginData.idMap[username] = uuid
|
||||
return uuid
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
top.jie65535.JMinecraftSkin
|
||||
Reference in New Issue
Block a user