Files
go-mc/cmd/autofish/autofish.go
Tnze 708097608c autofish's colorable output for windows
and non color output for daze
2019-07-28 19:31:51 +08:00

90 lines
1.5 KiB
Go

package main
import (
"github.com/Tnze/go-mc/bot"
"github.com/Tnze/go-mc/chat"
"github.com/mattn/go-colorable"
"log"
"time"
)
const timeout = 45
var (
c *bot.Client
watch chan time.Time
)
func main() {
log.SetOutput(colorable.NewColorableStdout())
c = bot.NewClient()
//Login
err := c.JoinServer("localhost", 25565)
if err != nil {
log.Fatal(err)
}
log.Println("Login success")
//Register event handlers
c.Events.GameStart = onGameStart
c.Events.ChatMsg = onChatMsg
c.Events.Disconnect = onDisconnect
c.Events.SoundPlay = onSound
//JoinGame
err = c.HandleGame()
if err != nil {
log.Fatal(err)
}
}
func onGameStart() error {
log.Println("Game start")
watch = make(chan time.Time)
go watchDog()
return c.UseItem(0)
}
func onSound(name string, category int, x, y, z float64, volume, pitch float32) error {
if name == "entity.fishing_bobber.splash" {
if err := c.UseItem(0); err != nil { //retrieve
return err
}
log.Println("gra~")
time.Sleep(time.Millisecond * 300)
if err := c.UseItem(0); err != nil { //throw
return err
}
watch <- time.Now()
}
return nil
}
func onChatMsg(c chat.Message, pos byte) error {
log.Println("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 := c.UseItem(0); err != nil {
panic(err)
}
}
to.Reset(time.Second * timeout)
}
}