This repository has been archived by the owner on Dec 31, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
main.go
217 lines (201 loc) · 4.66 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
package main
import (
"bufio"
_ "embed"
"github.com/BaiMeow/msauth"
"io/ioutil"
"log"
"net"
"os"
"strconv"
"time"
"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/entity"
_ "github.com/Tnze/go-mc/data/lang/zh-cn"
"github.com/Tnze/go-mc/data/packetid"
pk "github.com/Tnze/go-mc/net/packet"
"github.com/Tnze/go-mc/yggdrasil"
"github.com/google/uuid"
"github.com/mattn/go-colorable"
"github.com/spf13/viper"
)
var (
c *bot.Client
player *basic.Player
bobberID int32
watch chan bool
vp *viper.Viper
)
var updateBobber = bot.PacketHandler{
ID: packetid.ClientboundSetEntityData,
Priority: 1,
F: checkBobber,
}
var newEntity = bot.PacketHandler{
ID: packetid.ClientboundAddEntity,
Priority: 1,
F: newBobber,
}
//go:embed defaultConfig.toml
var defaultConfig []byte
func main() {
log.SetOutput(colorable.NewColorableStdout())
log.Println("自动钓鱼机器人")
log.Println("版本号:mc1.18.2")
vp = viper.New()
vp.SetConfigName("config")
vp.SetConfigType("toml")
vp.AddConfigPath(".")
if err := vp.ReadInConfig(); err != nil {
if _, ok := err.(viper.ConfigFileNotFoundError); ok {
ioutil.WriteFile("config.toml", defaultConfig, 0666)
log.Fatal("配置文件缺失,已创建默认配置文件,请打开\"config.toml\"修改并保存")
} else {
log.Fatal(err)
}
}
c = bot.NewClient()
player = basic.NewPlayer(c, basic.DefaultSettings)
switch vp.GetString("profile.login") {
case "offline":
c.Auth.Name = vp.GetString("profile.name")
case "mojang":
resp, err := yggdrasil.Authenticate(vp.GetString("profile.account"), vp.GetString("profile.passwd"))
if err != nil {
log.Fatal("Authenticate:", err)
}
log.Println("验证成功")
c.Auth.UUID, c.Auth.Name = resp.SelectedProfile()
c.Auth.AsTk = resp.AccessToken()
case "microsoft":
msauth.SetClient("67e646fb-20f3-4595-9830-56773a07637d", "")
msauth.SetRedirectURL("http://127.0.0.1:25595")
profile, astk, err := msauth.Login()
log.Println("验证成功")
if err != nil {
log.Fatal(err)
}
c.Auth.UUID = profile.Id
c.Auth.Name = profile.Name
c.Auth.AsTk = astk
default:
log.Fatal("无效的登陆模式")
}
//注册事件
basic.EventsListener{
GameStart: onGameStart,
ChatMsg: onChatMsg,
Disconnect: onDisconnect,
}.Attach(c)
c.Events.AddListener(updateBobber, newEntity)
addr := net.JoinHostPort(vp.GetString("setting.ip"), strconv.Itoa(vp.GetInt("setting.port")))
for {
if err := c.JoinServer(addr); err != nil {
log.Fatal(err)
}
if err := c.HandleGame(); err != nil {
log.Println(err)
}
log.Println("失去与服务器的连接,将在五秒后重连")
time.Sleep(5 * time.Second)
}
}
func onGameStart() error {
log.Println("加入游戏")
watch = make(chan bool)
go watchdog()
go listenMsg()
time.Sleep(3 * time.Second)
throw(1)
return nil
}
func onDisconnect(c chat.Message) error {
log.Println("断开连接:", c)
return nil
}
func checkBobber(p pk.Packet) error {
var EID pk.VarInt
p.Scan(&EID)
if int32(EID) != bobberID {
return nil
}
var (
hookedEID pk.VarInt
catchable pk.Boolean
)
p.Scan(&hookedEID, &catchable)
if catchable {
throw(2)
watch <- true
log.Println("gra~")
}
return nil
}
func newBobber(p pk.Packet) error {
var (
EID pk.VarInt
UUID pk.UUID
mobType pk.VarInt
x, y, z pk.Double
pitch, yaw pk.Angle
data pk.Int
)
p.Scan(&EID, &UUID, &mobType, &x, &y, &z, &pitch, &yaw, &data)
//判断是否为浮漂
if mobType != pk.VarInt(entity.FishingBobber.ID) {
return nil
}
if data == pk.Int(player.EID) {
bobberID = int32(EID)
}
return nil
}
func throw(times int) {
for ; times > 0; times-- {
if err := useItem(); err != nil {
log.Fatal("抛竿:", err)
return
}
if times > 1 {
time.Sleep(time.Millisecond * 500)
}
}
}
func watchdog() {
timeout := time.Second * time.Duration(vp.GetInt("setting.timeout"))
timer := time.NewTicker(timeout)
for {
select {
case <-timer.C:
log.Println("WatchDog:超时")
throw(1)
case <-watch:
}
timer.Reset(timeout)
}
}
func onChatMsg(msg chat.Message, _ byte, _ uuid.UUID) error {
log.Println(msg.ClearString())
return nil
}
func listenMsg() {
var send []byte
for {
Reader := bufio.NewReader(os.Stdin)
send, _, _ = Reader.ReadLine()
if err := sendMsg(string(send)); err != nil {
log.Println(err)
}
}
}
func useItem() error {
return c.Conn.WritePacket(pk.Packet{ID: packetid.ServerboundUseItem, Data: []byte{0}})
}
func sendMsg(str string) error {
if str == "/throw" {
return useItem()
}
return c.Conn.WritePacket(pk.Marshal(packetid.ServerboundChat, pk.String(str)))
}