parent
274f10109e
commit
491ae2c7e3
41
main.go
41
main.go
|
@ -1,12 +1,16 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"flag"
|
||||
"os"
|
||||
"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/packetid"
|
||||
"github.com/Tnze/go-mc/net/packet"
|
||||
"github.com/Tnze/go-mc/yggdrasil"
|
||||
"github.com/google/uuid"
|
||||
"github.com/peterbourgon/ff/v3"
|
||||
|
@ -19,6 +23,7 @@ func main() {
|
|||
emailFlag := fs.String("email", "", "Login Email")
|
||||
passwordFlag := fs.String("password", "", "Login Password")
|
||||
ipFlag := fs.String("ip", "", "Server IP")
|
||||
debugFlag := fs.Bool("debug", false, "Debug Logging")
|
||||
if err := ff.Parse(fs, os.Args[1:],
|
||||
ff.WithEnvVarPrefix("AFK"),
|
||||
ff.WithConfigFileFlag("config"),
|
||||
|
@ -28,6 +33,9 @@ func main() {
|
|||
beaver.Fatal(err)
|
||||
return
|
||||
}
|
||||
if *debugFlag {
|
||||
beaver.Console.Level = beaver.DEBUG
|
||||
}
|
||||
|
||||
client := bot.NewClient()
|
||||
|
||||
|
@ -45,17 +53,50 @@ func main() {
|
|||
}
|
||||
beaver.Info("Login success")
|
||||
|
||||
client.Events.AddListener(bot.PacketHandler{Priority: 64, ID: packetid.KeepAliveClientbound, F: keepalive(client)})
|
||||
basic.EventsListener{
|
||||
ChatMsg: onChatMsg,
|
||||
}.Attach(client)
|
||||
|
||||
go onChatSend(client)
|
||||
|
||||
err = client.HandleGame()
|
||||
if err != nil {
|
||||
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 {
|
||||
beaver.Info(c)
|
||||
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)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue