-
-
Notifications
You must be signed in to change notification settings - Fork 30
/
bedrock_status.go
68 lines (56 loc) · 1.5 KB
/
bedrock_status.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
package main
import (
"context"
"flag"
"fmt"
"github.com/google/subcommands"
"github.com/itzg/go-flagsfiller"
"log"
"net"
"strconv"
"time"
)
type statusBedrockCmd struct {
Host string `default:"localhost"`
Port int `default:"19132"`
RetryInterval time.Duration `usage:"if retry-limit is non-zero, status will be retried at this interval" default:"10s"`
RetryLimit int `usage:"if non-zero, failed status will be retried this many times before exiting"`
}
func (c *statusBedrockCmd) Name() string {
return "status-bedrock"
}
func (c *statusBedrockCmd) Synopsis() string {
return "Retrieves and displays the status of the given Minecraft Bedrock Dedicated server"
}
func (c *statusBedrockCmd) Usage() string {
return ""
}
func (c *statusBedrockCmd) SetFlags(flags *flag.FlagSet) {
filler := flagsfiller.New()
err := filler.Fill(flags, c)
if err != nil {
log.Fatal(err)
}
}
func (c *statusBedrockCmd) Execute(ctx context.Context, f *flag.FlagSet, args ...interface{}) subcommands.ExitStatus {
address := net.JoinHostPort(c.Host, strconv.Itoa(c.Port))
if c.RetryInterval <= 0 {
c.RetryInterval = 1 * time.Second
}
for {
info, err := PingBedrockServer(address, 0)
if err != nil {
if c.RetryLimit > 0 {
c.RetryLimit--
time.Sleep(c.RetryInterval)
continue
}
log.Fatal(err)
return subcommands.ExitFailure
}
fmt.Printf("%s : version=%s online=%d max=%d",
address,
info.Version, info.Players, info.MaxPlayers)
return subcommands.ExitSuccess
}
}