From b1422863d05571428af22db75be2bf264c5a4369 Mon Sep 17 00:00:00 2001 From: JunDao Date: Fri, 3 May 2019 19:49:13 +0800 Subject: [PATCH] add SwingArm, Respawn and UseItem function --- .gitignore | 2 ++ bot/event.go | 8 ++++--- bot/ingame.go | 61 ++++++++++++++++++++++++++------------------------- bot/motion.go | 30 +++++++++++++++++++++++++ 4 files changed, 68 insertions(+), 33 deletions(-) create mode 100644 bot/motion.go diff --git a/.gitignore b/.gitignore index f1c181e..25eafb5 100644 --- a/.gitignore +++ b/.gitignore @@ -10,3 +10,5 @@ # Output of the go coverage tool, specifically when used with LiteIDE *.out + +cmd/test/test.go diff --git a/bot/event.go b/bot/event.go index acc19ed..1a308ca 100644 --- a/bot/event.go +++ b/bot/event.go @@ -5,7 +5,9 @@ import ( ) type eventBroker struct { - GameStart func() error - ChatMsg func(msg chat.Message) error - Disconnect func(reason chat.Message) error + GameStart func() error + ChatMsg func(msg chat.Message) error + Disconnect func(reason chat.Message) error + HealhtChange func() error + Die func() error } diff --git a/bot/ingame.go b/bot/ingame.go index 7afeb1b..9454201 100644 --- a/bot/ingame.go +++ b/bot/ingame.go @@ -93,7 +93,7 @@ func (c *Client) handlePacket(p pk.Packet) (disconnect bool, err error) { case data.WindowItems: err = handleWindowItemsPacket(c, p) case data.UpdateHealth: - //// err = handleUpdateHealthPacket(c, p) + err = handleUpdateHealthPacket(c, p) case data.ChatMessageClientbound: err = handleChatMessagePacket(c, p) case data.BlockChange: @@ -268,29 +268,39 @@ func handleChatMessagePacket(c *Client, p pk.Packet) (err error) { return err } -// func handleUpdateHealthPacket(c *Client, p pk.Packet) (err error) { -// var ( -// Health pk.Float -// Food pk.VarInt -// FoodSaturation pk.Float -// ) +func handleUpdateHealthPacket(c *Client, p pk.Packet) (err error) { + var ( + Health pk.Float + Food pk.VarInt + FoodSaturation pk.Float + ) -// err = p.Scan(&Health, &Food, &FoodSaturation) -// if err != nil { -// return -// } + err = p.Scan(&Health, &Food, &FoodSaturation) + if err != nil { + return + } -// c.player.Health = Health -// c.player.Food = Food -// c.player.FoodSaturation = FoodSaturation + c.Health = float32(Health) + c.Food = int32(Food) + c.FoodSaturation = float32(FoodSaturation) -// if c.player.Health < 1 { //player is dead -// sendPlayerPositionAndLookPacket(c) -// time.Sleep(time.Second * 2) //wait for 2 sec make it more like a human -// sendClientStatusPacket(c, 0) //status 0 means perform respawn -// } -// return -// } + if c.Events.HealhtChange != nil { + err = c.Events.HealhtChange() + if err != nil { + return + } + } + if c.Health < 1 { //player is dead + sendPlayerPositionAndLookPacket(c) + if c.Events.Die != nil { + err = c.Events.Die() + if err != nil { + return + } + } + } + return +} func handleJoinGamePacket(c *Client, p pk.Packet) error { var ( @@ -652,15 +662,6 @@ func sendPlayerPositionAndLookPacket(c *Client) { // } // } -// //hand could be 0: main hand, 1: off hand -// func sendAnimationPacket(g *Client, hand int32) { -// data := pk.PackVarInt(hand) -// g.sendChan <- pk.Packet{ -// ID: 0x27, -// Data: data, -// } -// } - // func sendPlayerDiggingPacket(g *Client, status int32, x, y, z int, face Face) { // data := pk.PackVarInt(status) // data = append(data, pk.PackPosition(x, y, z)...) diff --git a/bot/motion.go b/bot/motion.go new file mode 100644 index 0000000..dea6df2 --- /dev/null +++ b/bot/motion.go @@ -0,0 +1,30 @@ +package bot + +import ( + "github.com/Tnze/go-mc/data" + pk "github.com/Tnze/go-mc/net/packet" +) + +//SwingArm swing player's arm. +//hand could be 0: main hand, 1: off hand +func (c *Client) SwingArm(hand int) error { + return c.conn.WritePacket(pk.Marshal( + data.AnimationServerbound, + pk.VarInt(hand), + )) +} + +//Respawn the player when it was dead. +func (c *Client) Respawn() error { + return c.conn.WritePacket(pk.Marshal( + data.ClientStatus, + pk.VarInt(0), + )) +} + +func (c *Client) UseItem(hand int) error { + return c.conn.WritePacket(pk.Marshal( + data.UseItem, + pk.VarInt(hand), + )) +}