parent
274f10109e
commit
491ae2c7e3
41
main.go
41
main.go
|
@ -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)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue