forked from Tnze/go-mc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
autofish.go
152 lines (128 loc) · 3.15 KB
/
autofish.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
package main
import (
"log"
"time"
//"github.com/mattn/go-colorable"
"github.com/SkyVillageMC/go-mc/bot"
"github.com/SkyVillageMC/go-mc/bot/basic"
"github.com/SkyVillageMC/go-mc/bot/msg"
"github.com/SkyVillageMC/go-mc/bot/playerlist"
"github.com/SkyVillageMC/go-mc/chat"
_ "github.com/SkyVillageMC/go-mc/data/lang/en-us"
"github.com/SkyVillageMC/go-mc/data/packetid"
pk "github.com/SkyVillageMC/go-mc/net/packet"
)
const timeout = 45
var (
c *bot.Client
p *basic.Player
playerList *playerlist.PlayerList
chatHandler *msg.Manager
watch chan time.Time
)
func main() {
// log.SetOutput(colorable.NewColorableStdout()) // optional for colorable output
c = bot.NewClient()
p = basic.NewPlayer(c, basic.DefaultSettings, basic.EventsListener{
GameStart: onGameStart,
Disconnect: onDisconnect,
Death: onDeath,
})
playerList = playerlist.New(c)
chatHandler = msg.New(c, p, playerList, msg.EventsHandler{
SystemChat: onSystemChat,
PlayerChatMessage: onPlayerChat,
DisguisedChat: onDisguisedChat,
})
// Register event handlers
c.Events.AddListener(soundListener)
// Login
err := c.JoinServer("127.0.0.1")
if err != nil {
log.Fatal(err)
}
log.Println("Login success")
// JoinGame
err = c.HandleGame()
if err != nil {
log.Fatal(err)
}
}
func onDeath() error {
log.Println("Died and Respawned")
// If we exclude Respawn(...) then the player won't press the "Respawn" button upon death
return p.Respawn()
}
func onGameStart() error {
log.Println("Game start")
watch = make(chan time.Time)
go watchDog()
return UseItem(0)
}
var soundListener = bot.PacketHandler{
ID: packetid.ClientboundSound,
Priority: 0,
F: func(p pk.Packet) error {
var (
SoundID pk.VarInt
SoundCategory pk.VarInt
X, Y, Z pk.Int
Volume, Pitch pk.Float
)
if err := p.Scan(&SoundID, &SoundCategory, &X, &Y, &Z, &Volume, &Pitch); err != nil {
return err
}
return onSound(int(SoundID), int(SoundCategory), float64(X)/8, float64(Y)/8, float64(Z)/8, float32(Volume), float32(Pitch))
},
}
func UseItem(hand int32) error {
return c.Conn.WritePacket(pk.Marshal(
packetid.ServerboundUseItem,
pk.VarInt(hand),
))
}
//goland:noinspection SpellCheckingInspection
func onSound(id int, category int, x, y, z float64, volume, pitch float32) error {
if id == 369 {
if err := UseItem(0); err != nil { // retrieve
return err
}
log.Println("gra~")
time.Sleep(time.Millisecond * 300)
if err := UseItem(0); err != nil { // throw
return err
}
watch <- time.Now()
}
return nil
}
func onSystemChat(c chat.Message, overlay bool) error {
log.Printf("System Chat: %v, Overlay: %v", c, overlay)
return nil
}
func onPlayerChat(c chat.Message, _ bool) error {
log.Println("Player Chat:", c)
return nil
}
func onDisguisedChat(c chat.Message) error {
log.Println("Disguised Chat:", c)
return nil
}
func onDisconnect(c chat.Message) error {
log.Println("Disconnect:", c)
return nil
}
func watchDog() {
to := time.NewTimer(time.Second * timeout)
for {
select {
case <-watch:
case <-to.C:
log.Println("rethrow")
if err := UseItem(0); err != nil {
panic(err)
}
}
to.Reset(time.Second * timeout)
}
}