Start AFK and handle bad packets
Signed-off-by: jolheiser <john.olheiser@gmail.com>pull/1/head
parent
337c535a00
commit
3d7d9a64fa
36
main.go
36
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()
|
||||
go func() {
|
||||
for {
|
||||
err := client.HandleGame()
|
||||
if err != nil {
|
||||
log.Fatal().Err(err).Msg("")
|
||||
log.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
|
||||
}
|
||||
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,6 +96,11 @@ func onChatSend(c *bot.Client) {
|
|||
if strings.TrimSpace(msg) == "" {
|
||||
continue
|
||||
}
|
||||
sendChat(c, msg)
|
||||
}
|
||||
}
|
||||
|
||||
func sendChat(c *bot.Client, msg string) {
|
||||
if err := c.Conn.WritePacket(packet.Marshal(
|
||||
packetid.ServerboundChat,
|
||||
packet.String(msg),
|
||||
|
@ -103,4 +108,3 @@ func onChatSend(c *bot.Client) {
|
|||
log.Err(err).Msg("")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue