feat: support kick

This commit is contained in:
Vaala Cat
2023-06-19 19:21:26 +08:00
parent 730aea3e3b
commit c278b3f516
16 changed files with 447 additions and 90 deletions

View File

@@ -13,7 +13,6 @@ import (
"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"
"github.com/sirupsen/logrus"
)
@@ -36,9 +35,8 @@ func Run() error {
DisguisedChat: onDisguisedMsg,
})
conf.ScreenManager = screen.NewManager(client, screen.EventsListener{
Open: nil,
SetSlot: onScreenSlotChange,
Close: nil,
Open: nil,
Close: nil,
})
err := client.JoinServer(
@@ -54,16 +52,38 @@ func Run() error {
return client.HandleGame()
}
func SendMsg(msg string) error {
if err := conf.ChatHandler.SendMessage(msg); err != nil {
return err
}
return nil
func SendMsg(msg string) {
go func() {
err := conf.ChatHandler.SendMessage(msg)
if err != nil {
logrus.Error("send msg error: ", err)
}
}()
}
func SendMsgToPlayer(msg string, playerName string) {
go func() {
err := sendCommand(fmt.Sprintf("tell %s %s", playerName, msg))
if err != nil {
logrus.Error("send msg to player error: ", err)
}
}()
}
func onSystemMsg(msg chat.Message, overlay bool) error {
go func() {
log.Printf("System: %v", msg)
log.Printf("System: %v", msg.String())
switch msg.Translate {
case EventPlayerJoined:
userName, err := GetJoinedPlayer(msg)
if err != nil {
logrus.Error("user join error ", err)
break
}
go HandleJoinGame(userName)
default:
break
}
m := tgbotapi.NewMessage(conf.GetBotSettings().GroupID, fmt.Sprintf("%v", msg))
conf.Bot.Send(m)
}()
@@ -110,30 +130,14 @@ func onDeath() error {
}
func onGameStart() error {
log.Println("Game start")
go func() {
log.Println("Game start")
// SendMsgToPlayer("Hello", "test")
go CronKick()
}()
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