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
*.out
cmd/test/test.go

View File

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

View File

@ -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)...)

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