add SwingArm, Respawn and UseItem function

This commit is contained in:
JunDao
2019-05-03 19:49:13 +08:00
parent 150a5c6f8c
commit b1422863d0
4 changed files with 68 additions and 33 deletions

2
.gitignore vendored
View File

@ -10,3 +10,5 @@
# Output of the go coverage tool, specifically when used with LiteIDE # Output of the go coverage tool, specifically when used with LiteIDE
*.out *.out
cmd/test/test.go

View File

@ -8,4 +8,6 @@ type eventBroker struct {
GameStart func() error GameStart func() error
ChatMsg func(msg chat.Message) error ChatMsg func(msg chat.Message) error
Disconnect func(reason chat.Message) error Disconnect func(reason chat.Message) error
HealhtChange func() error
Die func() error
} }

View File

@ -93,7 +93,7 @@ func (c *Client) handlePacket(p pk.Packet) (disconnect bool, err error) {
case data.WindowItems: case data.WindowItems:
err = handleWindowItemsPacket(c, p) err = handleWindowItemsPacket(c, p)
case data.UpdateHealth: case data.UpdateHealth:
//// err = handleUpdateHealthPacket(c, p) err = handleUpdateHealthPacket(c, p)
case data.ChatMessageClientbound: case data.ChatMessageClientbound:
err = handleChatMessagePacket(c, p) err = handleChatMessagePacket(c, p)
case data.BlockChange: case data.BlockChange:
@ -268,29 +268,39 @@ func handleChatMessagePacket(c *Client, p pk.Packet) (err error) {
return err return err
} }
// func handleUpdateHealthPacket(c *Client, p pk.Packet) (err error) { func handleUpdateHealthPacket(c *Client, p pk.Packet) (err error) {
// var ( var (
// Health pk.Float Health pk.Float
// Food pk.VarInt Food pk.VarInt
// FoodSaturation pk.Float FoodSaturation pk.Float
// ) )
// err = p.Scan(&Health, &Food, &FoodSaturation) err = p.Scan(&Health, &Food, &FoodSaturation)
// if err != nil { if err != nil {
// return return
// } }
// c.player.Health = Health c.Health = float32(Health)
// c.player.Food = Food c.Food = int32(Food)
// c.player.FoodSaturation = FoodSaturation c.FoodSaturation = float32(FoodSaturation)
// if c.player.Health < 1 { //player is dead if c.Events.HealhtChange != nil {
// sendPlayerPositionAndLookPacket(c) err = c.Events.HealhtChange()
// time.Sleep(time.Second * 2) //wait for 2 sec make it more like a human if err != nil {
// sendClientStatusPacket(c, 0) //status 0 means perform respawn return
// } }
// 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 { func handleJoinGamePacket(c *Client, p pk.Packet) error {
var ( 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) { // func sendPlayerDiggingPacket(g *Client, status int32, x, y, z int, face Face) {
// data := pk.PackVarInt(status) // data := pk.PackVarInt(status)
// data = append(data, pk.PackPosition(x, y, z)...) // data = append(data, pk.PackPosition(x, y, z)...)

30
bot/motion.go Normal file
View File

@ -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),
))
}