This repository has been archived by the owner on Jun 5, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.go
75 lines (63 loc) · 1.73 KB
/
main.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
package main
import (
"flag"
"fmt"
"github.com/BRA1L0R/go-mcproto"
"github.com/BRA1L0R/go-mcproto/packets"
"github.com/BRA1L0R/go-mcproto/packets/models"
"github.com/BRA1L0R/go-mcproto/varint"
)
type UpdateHealth struct {
packets.MinecraftPacket
Health float32 `mc:"inherit"`
Food int `mc:"varint"`
FoodSaturation float32 `mc:"inherit"`
}
var (
host = flag.String("host", "127.0.0.1", "Server host")
port = flag.Uint("port", 25565, "Server port")
username = flag.String("username", "GolangKeepalive", "In-game username")
)
func main() {
flag.Parse()
client := mcproto.Client{}
err := client.Initialize(*host, uint16(*port), 754, *username)
if err != nil {
panic(err)
}
for {
packet, err := client.ReceivePacket()
if err != nil {
if err == varint.ErrVarIntTooBig {
fmt.Println("Received a varint which was too big")
}
continue
}
switch packet.PacketID { // Update Health
case 0x49:
healthPacket := new(UpdateHealth)
err := packet.DeserializeData(healthPacket)
if err != nil {
panic(err)
}
fmt.Printf("Health Update: %vH %vF\n", healthPacket.Health, healthPacket.Food)
case 0x1F:
receivedKeepalive := new(models.KeepAlivePacket)
err := packet.DeserializeData(receivedKeepalive)
if err != nil {
panic(err)
}
// serverBoundKeepalive := models.KeepAlivePacket{
// CompressedPacket: packets.NewCompressedPacket(0x10),
// KeepAliveID: receivedKeepalive.KeepAliveID,
// }
serverBoundKeepalive := new(models.KeepAlivePacket)
serverBoundKeepalive.PacketID = 0x10
serverBoundKeepalive.KeepAliveID = receivedKeepalive.KeepAliveID
err = client.WritePacket(serverBoundKeepalive)
if err != nil {
panic(err)
}
}
}
}