-
Notifications
You must be signed in to change notification settings - Fork 1
/
minecraft-status.go
81 lines (66 loc) · 1.82 KB
/
minecraft-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
69
70
71
72
73
74
75
76
77
78
79
80
81
package minecraftstatus
import (
"encoding/json"
"github.com/geNAZt/minecraft-status/data"
"github.com/geNAZt/minecraft-status/protocol"
"reflect"
"time"
)
func GetStatus(host string, animatedFavicon bool) (*data.Status, error) {
// Create Client
conn, err := protocol.NewNetClient(host)
if err != nil {
return nil, err
}
// Send handshake and switch protocol state
conn.SendHandshake()
conn.State = protocol.Status
// Send the request so the server sends us a status
conn.SendStatusRequest()
statusPacket, errPacket := conn.ReadPacket()
if errPacket != nil {
return nil, errPacket
}
// Now we need to send a Ping
conn.SendClientStatusPing()
_, errPingPacket := conn.ReadPacket()
if errPingPacket != nil {
return nil, errPingPacket
}
// Parse the status
status := &data.Status{}
errJson := json.Unmarshal([]byte(statusPacket.(protocol.StatusResponse).Data), status)
if errJson != nil {
return nil, errJson
}
status.Favicons = []data.Favicon{}
// Wait for additional Favicons (animated motd hack)
for {
starttime := time.Now()
additionalStatusPacket, errPacket := conn.ReadPacket()
if errPacket != nil {
break
}
// Check if packet is correct
if !reflect.TypeOf(additionalStatusPacket).AssignableTo(reflect.TypeOf(protocol.StatusResponse{})) {
continue
}
// Parse the status
additionalStatus := &data.Status{}
errJson := json.Unmarshal([]byte(additionalStatusPacket.(protocol.StatusResponse).Data), additionalStatus)
if errJson != nil {
continue
}
status = additionalStatus
if animatedFavicon {
favicon := data.Favicon{
Icon: status.Favicon,
DisplayTime: int32(time.Now().Sub(starttime) / time.Millisecond),
}
status.Favicons = append(status.Favicons, favicon)
status.Favicon = additionalStatus.Favicon
}
}
conn.Close()
return status, nil
}