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