重写了ping示例,现在它是一个实用的工具了
This commit is contained in:
5
cmd/mcping/README.md
Normal file
5
cmd/mcping/README.md
Normal file
@ -0,0 +1,5 @@
|
||||
# mcping
|
||||
适用于Minecraft: Java Edition的ping工具
|
||||
|
||||
Useage:
|
||||
`mcping <hostname>[:port]`
|
89
cmd/mcping/mcping.go
Normal file
89
cmd/mcping/mcping.go
Normal file
@ -0,0 +1,89 @@
|
||||
// Useage: go run cmd/ping/ping.go localhost
|
||||
package main
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"os"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"github.com/Tnze/go-mc/bot"
|
||||
"github.com/Tnze/go-mc/chat"
|
||||
uuid "github.com/satori/go.uuid"
|
||||
)
|
||||
|
||||
type status struct {
|
||||
Description chat.Message
|
||||
Players struct {
|
||||
Max int
|
||||
Online int
|
||||
Sample []struct {
|
||||
ID uuid.UUID
|
||||
Name string
|
||||
}
|
||||
}
|
||||
Version struct {
|
||||
Name string
|
||||
Protocol int
|
||||
}
|
||||
//favicon ignored
|
||||
}
|
||||
|
||||
func main() {
|
||||
addr, port := getAddr()
|
||||
|
||||
fmt.Printf("MCPING (%s:%d):\n", addr, port)
|
||||
|
||||
resp, delay, err := bot.PingAndList(addr, port)
|
||||
if err != nil {
|
||||
fmt.Printf("ping and list server fail: %v", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
var s status
|
||||
err = json.Unmarshal(resp, &s)
|
||||
if err != nil {
|
||||
fmt.Print("unmarshal resp fail:", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
fmt.Print(s)
|
||||
fmt.Println("Delay:", delay)
|
||||
}
|
||||
|
||||
func getAddr() (string, int) {
|
||||
const useage = "Useage: mcping <hostname>[:port]"
|
||||
if len(os.Args) < 2 {
|
||||
fmt.Println("no host name.", useage)
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
addr := strings.Split(os.Args[1], ":")
|
||||
var port int
|
||||
switch len(addr) {
|
||||
case 1:
|
||||
port = 25565
|
||||
case 2:
|
||||
var err error
|
||||
port, err = strconv.Atoi(addr[1])
|
||||
if err != nil {
|
||||
fmt.Println(err, useage)
|
||||
os.Exit(1)
|
||||
}
|
||||
}
|
||||
|
||||
return addr[0], port
|
||||
}
|
||||
|
||||
func (s status) String() string {
|
||||
var sb strings.Builder
|
||||
fmt.Fprintln(&sb, "Server:", s.Version.Name)
|
||||
fmt.Fprintln(&sb, "Protocol:", s.Version.Protocol)
|
||||
fmt.Fprintln(&sb, "Description:", s.Description)
|
||||
fmt.Fprintf(&sb, "Players: %d/%d\n", s.Players.Online, s.Players.Max)
|
||||
for _, v := range s.Players.Sample {
|
||||
fmt.Fprintf(&sb, "- [%s] %v\n", v.Name, v.ID)
|
||||
}
|
||||
return sb.String()
|
||||
}
|
@ -1,21 +0,0 @@
|
||||
// Useage: go run cmd/ping/ping.go localhost
|
||||
package main
|
||||
|
||||
import (
|
||||
"log"
|
||||
"os"
|
||||
|
||||
"github.com/Tnze/go-mc/bot"
|
||||
)
|
||||
|
||||
func main() {
|
||||
if len(os.Args) < 2 {
|
||||
log.Fatalln("no host name. Useage: ping [hostname]")
|
||||
}
|
||||
|
||||
resp, delay, err := bot.PingAndList(os.Args[1], 25565)
|
||||
if err != nil {
|
||||
log.Fatalf("ping and list server fail: %v", err)
|
||||
}
|
||||
log.Println("Status:"+string(resp), "\nDealy:", delay)
|
||||
}
|
6
go.mod
6
go.mod
@ -2,4 +2,8 @@ module github.com/Tnze/go-mc
|
||||
|
||||
go 1.12
|
||||
|
||||
require github.com/satori/go.uuid v1.2.0
|
||||
require (
|
||||
github.com/kr/pretty v0.1.0 // indirect
|
||||
github.com/satori/go.uuid v1.2.0
|
||||
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 // indirect
|
||||
)
|
||||
|
7
go.sum
7
go.sum
@ -1,2 +1,9 @@
|
||||
github.com/kr/pretty v0.1.0 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI=
|
||||
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
|
||||
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
|
||||
github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE=
|
||||
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
|
||||
github.com/satori/go.uuid v1.2.0 h1:0uYX9dsZ2yD7q2RtLRtPSdGDWzjeM3TbMJP9utgA0ww=
|
||||
github.com/satori/go.uuid v1.2.0/go.mod h1:dA0hQrYB0VpLJoorglMZABFdXlWrHn1NEOzdhQKdks0=
|
||||
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 h1:qIbj1fsPNlZgppZ+VLlY7N33q108Sa+fhmuc+sWQYwY=
|
||||
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||
|
Reference in New Issue
Block a user