Start AFK and handle bad packets

Signed-off-by: jolheiser <john.olheiser@gmail.com>
pull/1/head
jolheiser 2022-01-19 12:46:52 -06:00
parent 337c535a00
commit 3d7d9a64fa
Signed by: jolheiser
GPG Key ID: B853ADA5DA7BBF7A
1 changed files with 28 additions and 24 deletions

38
main.go
View File

@ -4,11 +4,13 @@ import (
"bufio" "bufio"
"flag" "flag"
"os" "os"
"os/signal"
"strings" "strings"
"github.com/Tnze/go-mc/bot" "github.com/Tnze/go-mc/bot"
"github.com/Tnze/go-mc/bot/basic" "github.com/Tnze/go-mc/bot/basic"
"github.com/Tnze/go-mc/chat" "github.com/Tnze/go-mc/chat"
_ "github.com/Tnze/go-mc/data/lang/en-us"
"github.com/Tnze/go-mc/data/packetid" "github.com/Tnze/go-mc/data/packetid"
"github.com/Tnze/go-mc/net/packet" "github.com/Tnze/go-mc/net/packet"
"github.com/Tnze/go-mc/yggdrasil" "github.com/Tnze/go-mc/yggdrasil"
@ -42,6 +44,7 @@ func main() {
} }
client := bot.NewClient() client := bot.NewClient()
basic.NewPlayer(client, basic.DefaultSettings)
auth, err := yggdrasil.Authenticate(*emailFlag, *passwordFlag) auth, err := yggdrasil.Authenticate(*emailFlag, *passwordFlag)
if err != nil { if err != nil {
@ -57,31 +60,28 @@ func main() {
} }
log.Info().Msg("Login success") log.Info().Msg("Login success")
client.Events.AddListener(bot.PacketHandler{Priority: 64, ID: packetid.ClientboundKeepAlive, F: keepalive(client)})
basic.EventsListener{ basic.EventsListener{
GameStart: func() error {
sendChat(client, "/afk")
return nil
},
ChatMsg: onChatMsg, ChatMsg: onChatMsg,
}.Attach(client) }.Attach(client)
go onChatSend(client) go onChatSend(client)
err = client.HandleGame() go func() {
for {
err := client.HandleGame()
if err != nil { if err != nil {
log.Fatal().Err(err).Msg("") log.Err(err).Msg("")
} }
} }
}()
func keepalive(c *bot.Client) func(packet.Packet) error { ch := make(chan os.Signal, 1)
return func(pk packet.Packet) error { signal.Notify(ch, os.Interrupt, os.Kill)
log.Debug().Msg("keepalive") <-ch
var id packet.Long
if err := pk.Scan(&id); err != nil {
return err
}
return c.Conn.WritePacket(packet.Marshal(
packetid.ServerboundKeepAlive,
id,
))
}
} }
func onChatMsg(c chat.Message, _ byte, _ uuid.UUID) error { func onChatMsg(c chat.Message, _ byte, _ uuid.UUID) error {
@ -96,11 +96,15 @@ func onChatSend(c *bot.Client) {
if strings.TrimSpace(msg) == "" { if strings.TrimSpace(msg) == "" {
continue continue
} }
sendChat(c, msg)
}
}
func sendChat(c *bot.Client, msg string) {
if err := c.Conn.WritePacket(packet.Marshal( if err := c.Conn.WritePacket(packet.Marshal(
packetid.ServerboundChat, packetid.ServerboundChat,
packet.String(msg), packet.String(msg),
)); err != nil { )); err != nil {
log.Err(err).Msg("") log.Err(err).Msg("")
} }
}
} }