Skip to content

Commit

Permalink
feat:增加戳一戳输出帮助
Browse files Browse the repository at this point in the history
feat:增加任何人可以查看服务器列表
chore:依赖最新的1.2.3库
build:1.0.3
  • Loading branch information
limbang committed Jun 17, 2021
1 parent 62c8a5c commit 88e03f9
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 33 deletions.
4 changes: 2 additions & 2 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ plugins {
}

group = "top.limbang"
version = "1.0.2"
version = "1.0.3"

repositories {

Expand All @@ -19,5 +19,5 @@ repositories {
dependencies {
testImplementation("junit:junit:4.12")
testImplementation("org.slf4j:slf4j-simple:1.7.30")
implementation("top.limbang:doctor-all:1.0.9")
implementation("top.limbang.doctor:doctor-client:1.2.3")
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ object MinecraftPluginCompositeCommand : CompositeCommand(
return
}
val mutableList = MinecraftPluginData.adminMap[group.id]
mutableList?.firstOrNull{it == user.id} ?: mutableList?.add(user.id)
mutableList?.firstOrNull { it == user.id } ?: mutableList?.add(user.id)

if (mutableList == null){
if (mutableList == null) {
MinecraftPluginData.adminMap[group.id] = mutableListOf<Long>().also { it.add(user.id) }
}

Expand All @@ -38,10 +38,10 @@ object MinecraftPluginCompositeCommand : CompositeCommand(
sendMessage("本条消息只能在群配置.")
return
}
val mutableList =MinecraftPluginData.adminMap[group.id]
val mutableList = MinecraftPluginData.adminMap[group.id]

if(mutableList != null){
if(mutableList.remove(user.id)){
if (mutableList != null) {
if (mutableList.remove(user.id)) {
sendMessage("[${group.id}]群错误@提醒配置删除成功.")
return
}
Expand Down Expand Up @@ -107,14 +107,6 @@ object MinecraftPluginCompositeCommand : CompositeCommand(

@SubCommand("list", "列表")
suspend fun CommandSender.list() {
if (MinecraftPluginData.serverMap.isEmpty()) {
sendMessage("无服务器列表...")
return
}
var names = ""
for ((name, address) in MinecraftPluginData.serverMap) {
names += "[$name]地址:${address.address} 端口:${address.port}\n"
}
sendMessage("服务器列表为:\n$names")
sendMessage(MiraiConsoleMinecraftPlugin.getServerList())
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import net.mamoe.mirai.console.plugin.jvm.JvmPluginDescription
import net.mamoe.mirai.console.plugin.jvm.KotlinPlugin
import net.mamoe.mirai.contact.Group
import net.mamoe.mirai.contact.Member
import net.mamoe.mirai.event.events.NudgeEvent
import net.mamoe.mirai.event.globalEventChannel
import net.mamoe.mirai.event.subscribeGroupMessages
import top.limbang.mirai.minecraft.utils.PingUtils
Expand All @@ -16,7 +17,7 @@ import top.limbang.mirai.minecraft.utils.TPSUtils
object MiraiConsoleMinecraftPlugin : KotlinPlugin(
JvmPluginDescription(
id = "top.limbang.mirai-console-minecraft-plugin",
version = "1.0.0",
version = "1.0.3",
) {
author("limbang")
info("""Minecraft""")
Expand All @@ -37,13 +38,33 @@ object MiraiConsoleMinecraftPlugin : KotlinPlugin(
handle(group, it, sender)
}
}
globalEventChannel().subscribeAlways<NudgeEvent> {
if (target.id == bot.id) {
subject.sendMessage(
"Minecraft 插件使用说明:\n" +
"!为中英文通用,!和服务器名称之间可以有空格,和tps之间也一样\n" +
"Ping服务器:!前缀加服务器名称,如!服务器名称\n" +
"TPS:!前缀加服务器名称,tps结尾,如!服务器名称tps\n" +
"查看服务器列表:!list"
)
}
}
}

/**
* 处理ping服务器,和登录服务器发送tps命令
*/
private suspend fun handle(group: Group, mgs: String, sender: Member) {
if (!mgs.endsWith("tps"))
PingUtils.pingServer(group, mgs, sender)
else
TPSUtils.getTps(group, mgs.substringBefore("tps").trim(),sender)
if (mgs == "list") {
group.sendMessage(getServerList())
return
}
launch {
if (!mgs.endsWith("tps"))
PingUtils.pingServer(group, mgs, sender)
else
TPSUtils.getTps(group, mgs.substringBefore("tps").trim(), sender)
}
}

fun sendMessage(group: Group, message: String) {
Expand All @@ -52,5 +73,14 @@ object MiraiConsoleMinecraftPlugin : KotlinPlugin(
}
}

fun getServerList(): String {
if (MinecraftPluginData.serverMap.isEmpty())
return "无服务器列表..."
var names = ""
for ((name, address) in MinecraftPluginData.serverMap) {
names += "[$name]地址:${address.address} 端口:${address.port}\n"
}
return "服务器列表为:\n$names"
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ object PingUtils {
val serverInfo = MinecraftPluginData.serverMap[mgs] ?: return
val serverListInfo: ServerInfo
try {
val json = MinecraftClient.ping(serverInfo.address, serverInfo.port).get(2000, TimeUnit.MILLISECONDS)
val json = MinecraftClient.ping(serverInfo.address, serverInfo.port).get(3000, TimeUnit.MILLISECONDS)
if (json == null) {
sendErrorImage(group, sender)
return
Expand Down
29 changes: 18 additions & 11 deletions src/main/kotlin/top/limbang/mirai/minecraft/utils/TPSUtils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,36 +7,43 @@ import net.mamoe.mirai.contact.Contact.Companion.sendImage
import net.mamoe.mirai.contact.Group
import net.mamoe.mirai.contact.Member
import top.limbang.doctor.client.MinecraftClient
import top.limbang.doctor.client.event.JoinGameEvent
import top.limbang.doctor.client.running.AutoVersionForgePlugin
import top.limbang.doctor.client.running.TpsPlugin
import top.limbang.doctor.client.running.tpsTools
import top.limbang.doctor.client.utils.substringBetween
import top.limbang.doctor.network.handler.onPacket
import top.limbang.doctor.network.handler.oncePacket
import top.limbang.doctor.protocol.definition.play.client.JoinGamePacket
import top.limbang.doctor.protocol.definition.play.client.PlayerPositionAndLookPacket
import top.limbang.mirai.minecraft.MinecraftPluginData
import top.limbang.mirai.minecraft.MiraiConsoleMinecraftPlugin
import java.util.*

object TPSUtils {

suspend fun getTps(group: Group, mgs: String,sender: Member) {
suspend fun getTps(group: Group, mgs: String, sender: Member) {
val serverInfo = MinecraftPluginData.serverMap[mgs] ?: return

val decode = Base64.getDecoder()
val password = String(decode.decode(serverInfo.loginInfo.password))
val client = MinecraftClient()
val client = MinecraftClient.builder()
.user(serverInfo.loginInfo.username, password)
.authServerUrl(serverInfo.loginInfo.authServerUrl)
.sessionServerUrl(serverInfo.loginInfo.sessionServerUrl)
.plugin(TpsPlugin())
.plugin(AutoVersionForgePlugin())
.build()

if (!client.start(serverInfo.address, serverInfo.port)) {
sendErrorImage(group,"${sender.nameCard},好像获取失败了哦!!!")
if (!client.start(serverInfo.address, serverInfo.port,3000)) {
sendErrorImage(group, "${sender.nameCard},好像获取失败了哦!!!")
return
}

client.once(JoinGameEvent) {
MiraiConsoleMinecraftPlugin.sendMessage(group, "登陆成功,开始发送 forge tps 指令")
client.oncePacket<JoinGamePacket> {
MiraiConsoleMinecraftPlugin.sendMessage(group, "登陆[$mgs]成功,开始发送 forge tps 指令")
}.onPacket<PlayerPositionAndLookPacket> {
GlobalScope.launch {
val forgeTps = client.getForgeTps()
val forgeTps = client.tpsTools.getTpsSuspend()
var outMsg = "[$mgs]低于20TPS的维度如下:\n"
forgeTps.forEach { tpsEntity ->
val dim = tpsEntity.dim.substringBetween("Dim", "(").trim()
Expand All @@ -54,9 +61,9 @@ object TPSUtils {

private suspend fun sendErrorImage(group: Group, sendMgs: String) {
val image = ImageErrorMessage.createImage(
MiraiConsoleMinecraftPlugin.getResourceAsStream("9.jpg")!!,
sendMgs
)
MiraiConsoleMinecraftPlugin.getResourceAsStream("9.jpg")!!,
sendMgs
)
group.sendImage(image)
}
}
Expand Down

0 comments on commit 88e03f9

Please sign in to comment.