2021-07-31 03:58:29 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2021-07-31 23:48:51 +00:00
|
|
|
"bufio"
|
2021-07-31 03:58:29 +00:00
|
|
|
"flag"
|
|
|
|
"os"
|
2021-07-31 23:48:51 +00:00
|
|
|
"strings"
|
2021-07-31 03:58:29 +00:00
|
|
|
|
|
|
|
"github.com/Tnze/go-mc/bot"
|
|
|
|
"github.com/Tnze/go-mc/bot/basic"
|
|
|
|
"github.com/Tnze/go-mc/chat"
|
2021-07-31 23:48:51 +00:00
|
|
|
"github.com/Tnze/go-mc/data/packetid"
|
|
|
|
"github.com/Tnze/go-mc/net/packet"
|
2021-07-31 03:58:29 +00:00
|
|
|
"github.com/Tnze/go-mc/yggdrasil"
|
|
|
|
"github.com/google/uuid"
|
|
|
|
"github.com/peterbourgon/ff/v3"
|
|
|
|
"github.com/peterbourgon/ff/v3/fftoml"
|
|
|
|
"go.jolheiser.com/beaver"
|
|
|
|
)
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
fs := flag.NewFlagSet("afk", flag.ExitOnError)
|
|
|
|
emailFlag := fs.String("email", "", "Login Email")
|
|
|
|
passwordFlag := fs.String("password", "", "Login Password")
|
|
|
|
ipFlag := fs.String("ip", "", "Server IP")
|
2021-07-31 23:48:51 +00:00
|
|
|
debugFlag := fs.Bool("debug", false, "Debug Logging")
|
2021-07-31 03:58:29 +00:00
|
|
|
if err := ff.Parse(fs, os.Args[1:],
|
|
|
|
ff.WithEnvVarPrefix("AFK"),
|
|
|
|
ff.WithConfigFileFlag("config"),
|
|
|
|
ff.WithAllowMissingConfigFile(true),
|
|
|
|
ff.WithConfigFileParser(fftoml.New().Parse),
|
|
|
|
); err != nil {
|
|
|
|
beaver.Fatal(err)
|
|
|
|
return
|
|
|
|
}
|
2021-07-31 23:48:51 +00:00
|
|
|
if *debugFlag {
|
|
|
|
beaver.Console.Level = beaver.DEBUG
|
|
|
|
}
|
2021-07-31 03:58:29 +00:00
|
|
|
|
|
|
|
client := bot.NewClient()
|
|
|
|
|
|
|
|
auth, err := yggdrasil.Authenticate(*emailFlag, *passwordFlag)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
client.Auth.UUID, client.Auth.Name = auth.SelectedProfile()
|
|
|
|
client.Auth.AsTk = auth.AccessToken()
|
|
|
|
|
|
|
|
err = client.JoinServer(*ipFlag)
|
|
|
|
if err != nil {
|
|
|
|
beaver.Fatal(err)
|
|
|
|
}
|
|
|
|
beaver.Info("Login success")
|
|
|
|
|
2021-07-31 23:48:51 +00:00
|
|
|
client.Events.AddListener(bot.PacketHandler{Priority: 64, ID: packetid.KeepAliveClientbound, F: keepalive(client)})
|
2021-07-31 03:58:29 +00:00
|
|
|
basic.EventsListener{
|
|
|
|
ChatMsg: onChatMsg,
|
|
|
|
}.Attach(client)
|
|
|
|
|
2021-07-31 23:48:51 +00:00
|
|
|
go onChatSend(client)
|
|
|
|
|
2021-07-31 03:58:29 +00:00
|
|
|
err = client.HandleGame()
|
|
|
|
if err != nil {
|
|
|
|
beaver.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-31 23:48:51 +00:00
|
|
|
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,
|
|
|
|
))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-31 03:58:29 +00:00
|
|
|
func onChatMsg(c chat.Message, _ byte, _ uuid.UUID) error {
|
|
|
|
beaver.Info(c)
|
|
|
|
return nil
|
|
|
|
}
|
2021-07-31 23:48:51 +00:00
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|