From b7e73e8c31212ebab6b36297acb009643850d424 Mon Sep 17 00:00:00 2001 From: Vaala Cat <-e> Date: Wed, 17 May 2023 15:27:27 +0800 Subject: [PATCH] fix: add some event issue --- conf/mc.go | 10 +++--- go.mod | 1 + go.sum | 2 ++ services/handler.go | 3 -- services/mc/mc.go | 75 +++++++++++++++++++++++++++++++++++++++++++-- 5 files changed, 82 insertions(+), 9 deletions(-) diff --git a/conf/mc.go b/conf/mc.go index a3f6fc4..106b1f3 100644 --- a/conf/mc.go +++ b/conf/mc.go @@ -5,11 +5,13 @@ import ( "github.com/Tnze/go-mc/bot/basic" "github.com/Tnze/go-mc/bot/msg" "github.com/Tnze/go-mc/bot/playerlist" + "github.com/Tnze/go-mc/bot/screen" ) var ( - Client *bot.Client - Player *basic.Player - ChatHandler *msg.Manager - PlayerList *playerlist.PlayerList + Client *bot.Client + Player *basic.Player + ChatHandler *msg.Manager + PlayerList *playerlist.PlayerList + ScreenManager *screen.Manager ) diff --git a/go.mod b/go.mod index f7873a5..5a978ed 100644 --- a/go.mod +++ b/go.mod @@ -11,5 +11,6 @@ require ( require ( github.com/google/uuid v1.3.0 // indirect + github.com/iancoleman/strcase v0.2.0 // indirect golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8 // indirect ) diff --git a/go.sum b/go.sum index 69ca57d..3a68176 100644 --- a/go.sum +++ b/go.sum @@ -7,6 +7,8 @@ github.com/go-telegram-bot-api/telegram-bot-api/v5 v5.5.1 h1:wG8n/XJQ07TmjbITcGi github.com/go-telegram-bot-api/telegram-bot-api/v5 v5.5.1/go.mod h1:A2S0CWkNylc2phvKXWBBdD3K0iGnDBGbzRpISP2zBl8= github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I= github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= +github.com/iancoleman/strcase v0.2.0 h1:05I4QRnGpI0m37iZQRuskXh+w77mr6Z41lwQzuHLwW0= +github.com/iancoleman/strcase v0.2.0/go.mod h1:iwCmte+B7n89clKwxIoIXy/HfoL7AsD47ZCWhYzw7ho= github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0= github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= diff --git a/services/handler.go b/services/handler.go index dfbd37b..7304d4d 100644 --- a/services/handler.go +++ b/services/handler.go @@ -4,8 +4,6 @@ import ( "tg-mc/services/mc" "tg-mc/services/tgbot" "tg-mc/services/utils" - - "github.com/sirupsen/logrus" ) func Run() { @@ -13,7 +11,6 @@ func Run() { for { if err := mc.Run(); err != nil { utils.SendMsg("致命错误:" + err.Error()) - logrus.Panic(err) } } } diff --git a/services/mc/mc.go b/services/mc/mc.go index 26cab9e..9223321 100644 --- a/services/mc/mc.go +++ b/services/mc/mc.go @@ -6,12 +6,15 @@ import ( "log" "strings" "tg-mc/conf" + "time" "github.com/Tnze/go-mc/bot" "github.com/Tnze/go-mc/bot/basic" "github.com/Tnze/go-mc/bot/msg" "github.com/Tnze/go-mc/bot/playerlist" + "github.com/Tnze/go-mc/bot/screen" "github.com/Tnze/go-mc/chat" + "github.com/Tnze/go-mc/data/item" tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5" ) @@ -20,13 +23,23 @@ func Run() error { conf.Client.Auth.Name = conf.GetBotSettings().MCBotName client := conf.Client - conf.Player = basic.NewPlayer(client, basic.DefaultSettings, basic.EventsListener{}) + conf.Player = basic.NewPlayer(client, basic.DefaultSettings, basic.EventsListener{ + GameStart: onGameStart, + Disconnect: onDisconnect, + HealthChange: onHealthChange, + Death: onDeath, + }) conf.PlayerList = playerlist.New(client) conf.ChatHandler = msg.New(client, conf.Player, conf.PlayerList, msg.EventsHandler{ SystemChat: onSystemMsg, PlayerChatMessage: onPlayerMsg, DisguisedChat: onDisguisedMsg, }) + conf.ScreenManager = screen.NewManager(client, screen.EventsListener{ + Open: nil, + SetSlot: onScreenSlotChange, + Close: nil, + }) err := client.JoinServer( conf.GetBotSettings().MCServer, @@ -44,7 +57,7 @@ func Run() error { return errors.New("handle game error") } if errors.As(err, &perr) { - log.Print(perr) + return err } else { return err } @@ -84,3 +97,61 @@ func onDisguisedMsg(msg chat.Message) error { return nil } +func onDeath() error { + log.Println("Died and Respawned") + // If we exclude Respawn(...) then the player won't press the "Respawn" button upon death + go func() { + time.Sleep(time.Second * 5) + err := conf.Player.Respawn() + if err != nil { + log.Print(err) + } + }() + return nil +} + +func onGameStart() error { + log.Println("Game start") + if err := conf.ChatHandler.SendMessage("Hello, world"); err != nil { + return err + } + return nil // if err isn't nil, HandleGame() will return it. +} + +func onScreenSlotChange(id, index int) error { + if id == -2 { + log.Printf("Slot: inventory: %v", conf.ScreenManager.Inventory.Slots[index]) + } else if id == -1 && index == -1 { + log.Printf("Slot: cursor: %v", conf.ScreenManager.Cursor) + } else { + container, ok := conf.ScreenManager.Screens[id] + if ok { + // Currently, only inventory container is supported + switch container.(type) { + case *screen.Inventory: + slot := container.(*screen.Inventory).Slots[index] + itemInfo := item.ByID[item.ID(slot.ID)] + log.Printf("Slot: Screen[%d].Slot[%d]: [%v] * %d | NBT: %v", id, index, itemInfo, slot.Count, slot.NBT) + } + } + } + return nil +} + +func onHealthChange(health float32, foodLevel int32, foodSaturation float32) error { + log.Printf("Health: %.2f, FoodLevel: %d, FoodSaturation: %.2f", health, foodLevel, foodSaturation) + return nil +} + +type DisconnectErr struct { + Reason chat.Message +} + +func (d DisconnectErr) Error() string { + return "disconnect: " + d.Reason.String() +} + +func onDisconnect(reason chat.Message) error { + // return an error value so that we can stop main loop + return DisconnectErr{Reason: reason} +}