Add chat send and keepalive

Signed-off-by: Etzelia <etzelia@hotmail.com>
pull/1/head
Etzelia 2021-07-31 18:48:51 -05:00
parent 274f10109e
commit 491ae2c7e3
No known key found for this signature in database
GPG Key ID: 708511AE7ABC5314
1 changed files with 41 additions and 0 deletions

41
main.go
View File

@ -1,12 +1,16 @@
package main package main
import ( import (
"bufio"
"flag" "flag"
"os" "os"
"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/packetid"
"github.com/Tnze/go-mc/net/packet"
"github.com/Tnze/go-mc/yggdrasil" "github.com/Tnze/go-mc/yggdrasil"
"github.com/google/uuid" "github.com/google/uuid"
"github.com/peterbourgon/ff/v3" "github.com/peterbourgon/ff/v3"
@ -19,6 +23,7 @@ func main() {
emailFlag := fs.String("email", "", "Login Email") emailFlag := fs.String("email", "", "Login Email")
passwordFlag := fs.String("password", "", "Login Password") passwordFlag := fs.String("password", "", "Login Password")
ipFlag := fs.String("ip", "", "Server IP") ipFlag := fs.String("ip", "", "Server IP")
debugFlag := fs.Bool("debug", false, "Debug Logging")
if err := ff.Parse(fs, os.Args[1:], if err := ff.Parse(fs, os.Args[1:],
ff.WithEnvVarPrefix("AFK"), ff.WithEnvVarPrefix("AFK"),
ff.WithConfigFileFlag("config"), ff.WithConfigFileFlag("config"),
@ -28,6 +33,9 @@ func main() {
beaver.Fatal(err) beaver.Fatal(err)
return return
} }
if *debugFlag {
beaver.Console.Level = beaver.DEBUG
}
client := bot.NewClient() client := bot.NewClient()
@ -45,17 +53,50 @@ func main() {
} }
beaver.Info("Login success") beaver.Info("Login success")
client.Events.AddListener(bot.PacketHandler{Priority: 64, ID: packetid.KeepAliveClientbound, F: keepalive(client)})
basic.EventsListener{ basic.EventsListener{
ChatMsg: onChatMsg, ChatMsg: onChatMsg,
}.Attach(client) }.Attach(client)
go onChatSend(client)
err = client.HandleGame() err = client.HandleGame()
if err != nil { if err != nil {
beaver.Fatal(err) beaver.Fatal(err)
} }
} }
func keepalive(c *bot.Client) func(packet.Packet) error {
return func(pk packet.Packet) error {
beaver.Debug("keepalive")
var id packet.Long
if err := pk.Scan(&id); err != nil {
return err
}
return c.Conn.WritePacket(packet.Marshal(
packetid.KeepAliveServerbound,
id,
))
}
}
func onChatMsg(c chat.Message, _ byte, _ uuid.UUID) error { func onChatMsg(c chat.Message, _ byte, _ uuid.UUID) error {
beaver.Info(c) beaver.Info(c)
return nil return nil
} }
func onChatSend(c *bot.Client) {
scanner := bufio.NewScanner(os.Stdin)
for scanner.Scan() {
msg := scanner.Text()
if strings.TrimSpace(msg) == "" {
continue
}
if err := c.Conn.WritePacket(packet.Marshal(
packetid.ChatServerbound,
packet.String(msg),
)); err != nil {
beaver.Error(err)
}
}
}