feat: add plugin Minecraft 服务器状态查询 (#1135)

This commit is contained in:
RefactoringHero
2025-03-25 11:30:40 +08:00
committed by GitHub
parent e6e6dd4565
commit e292b69ee5
11 changed files with 1414 additions and 89 deletions

View File

@@ -0,0 +1,62 @@
package minecraftobserver
import (
"encoding/json"
"github.com/RomiChan/syncx"
"github.com/Tnze/go-mc/bot"
"time"
)
var (
// pingServerUnreachableCounter Ping服务器不可达计数器防止bot本体网络抖动导致误报
pingServerUnreachableCounter = syncx.Map[string, pingServerUnreachableCounterDef]{}
// 计数器阈值
pingServerUnreachableCounterThreshold = int64(3)
// 时间阈值
pingServerUnreachableCounterTimeThreshold = time.Minute * 30
)
type pingServerUnreachableCounterDef struct {
count int64
firstUnreachableTime time.Time
}
func addPingServerUnreachableCounter(addr string, ts time.Time) (int64, time.Time) {
key := addr
get, ok := pingServerUnreachableCounter.Load(key)
if !ok {
pingServerUnreachableCounter.Store(key, pingServerUnreachableCounterDef{
count: 1,
firstUnreachableTime: ts,
})
return 1, ts
}
// 存在则更新,时间戳不变
pingServerUnreachableCounter.Store(key, pingServerUnreachableCounterDef{
count: get.count + 1,
firstUnreachableTime: get.firstUnreachableTime,
})
return get.count + 1, get.firstUnreachableTime
}
func resetPingServerUnreachableCounter(addr string) {
key := addr
pingServerUnreachableCounter.Delete(key)
}
// getMinecraftServerStatus 获取Minecraft服务器状态
func getMinecraftServerStatus(addr string) (*serverPingAndListResp, error) {
var s serverPingAndListResp
resp, delay, err := bot.PingAndListTimeout(addr, time.Second*5)
if err != nil {
//logrus.Errorln(logPrefix+"PingAndList error: ", err)
return nil, err
}
err = json.Unmarshal(resp, &s)
if err != nil {
//logrus.Errorln(logPrefix+"Parse json response fail: ", err)
return nil, err
}
s.Delay = delay
return &s, nil
}