bot can hear sounds now. add some motion API.

This commit is contained in:
JunDao
2019-05-04 00:11:24 +08:00
parent b1422863d0
commit c8505386c6
5 changed files with 865 additions and 38 deletions

View File

@ -6,8 +6,9 @@ import (
type eventBroker struct {
GameStart func() error
ChatMsg func(msg chat.Message) error
ChatMsg func(msg chat.Message, pos byte) error
Disconnect func(reason chat.Message) error
HealhtChange func() error
Die func() error
SoundPlay func(name string, category int, x, y, z float64, volume, pitch float32) error
}

View File

@ -105,48 +105,59 @@ func (c *Client) handlePacket(p pk.Packet) (disconnect bool, err error) {
disconnect = true
case 0x16:
// err = handleSetSlotPacket(g, reader)
case 0x51:
// err = handleSoundEffect(g, reader)
case data.SoundEffect:
err = handleSoundEffect(c, p)
case data.NamedSoundEffect:
err = handleNamedSoundEffect(c, p)
default:
// fmt.Printf("ignore pack id %X\n", p.ID)
}
return
}
// func handleSoundEffect(g *Client, r *bytes.Reader) error {
// SoundID, err := pk.UnpackVarInt(r)
// if err != nil {
// return err
// }
// SoundCategory, err := pk.UnpackVarInt(r)
// if err != nil {
// return err
// }
func handleSoundEffect(c *Client, p pk.Packet) error {
var (
SoundID pk.VarInt
SoundCategory pk.VarInt
x, y, z pk.Int
Volume, Pitch pk.Float
)
err := p.Scan(&SoundID, &SoundCategory, &x, &y, &z, &Volume, &Pitch)
if err != nil {
return err
}
// x, err := pk.UnpackInt32(r)
// if err != nil {
// return err
// }
// y, err := pk.UnpackInt32(r)
// if err != nil {
// return err
// }
// z, err := pk.UnpackInt32(r)
// if err != nil {
// return err
// }
// Volume, err := pk.UnpackFloat(r)
// if err != nil {
// return err
// }
// Pitch, err := pk.UnpackFloat(r)
// if err != nil {
// return err
// }
// g.events <- SoundEffectEvent{SoundID, SoundCategory, float64(x) / 8, float64(y) / 8, float64(z) / 8, Volume, Pitch}
if c.Events.SoundPlay != nil {
c.Events.SoundPlay(
data.SoundNames[SoundID], int(SoundCategory),
float64(x)/8, float64(y)/8, float64(z)/8,
float32(Volume), float32(Pitch))
}
// return nil
// }
return nil
}
func handleNamedSoundEffect(c *Client, p pk.Packet) error {
var (
SoundName pk.String
SoundCategory pk.VarInt
x, y, z pk.Int
Volume, Pitch pk.Float
)
err := p.Scan(&SoundName, &SoundCategory, &x, &y, &z, &Volume, &Pitch)
if err != nil {
return err
}
if c.Events.SoundPlay != nil {
c.Events.SoundPlay(
string(SoundName), int(SoundCategory),
float64(x)/8, float64(y)/8, float64(z)/8,
float32(Volume), float32(Pitch))
}
return nil
}
func handleDisconnectPacket(c *Client, p pk.Packet) error {
var reason chat.Message
@ -262,7 +273,7 @@ func handleChatMessagePacket(c *Client, p pk.Packet) (err error) {
}
if c.Events.ChatMsg != nil {
err = c.Events.ChatMsg(s)
err = c.Events.ChatMsg(s, byte(pos))
}
return err

View File

@ -1,12 +1,13 @@
package bot
import (
"fmt"
"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
//hand could be one of 0: main hand, 1: off hand
func (c *Client) SwingArm(hand int) error {
return c.conn.WritePacket(pk.Marshal(
data.AnimationServerbound,
@ -22,9 +23,23 @@ func (c *Client) Respawn() error {
))
}
//UseItem use the item player handing.
//hand could be one of 0: main hand, 1: off hand
func (c *Client) UseItem(hand int) error {
return c.conn.WritePacket(pk.Marshal(
data.UseItem,
pk.VarInt(hand),
))
}
//Chat send chat as chat message or command at textbox.
func (c *Client) Chat(msg string) error {
if len(msg) > 256 {
return fmt.Errorf("message too long")
}
return c.conn.WritePacket(pk.Marshal(
data.ChatMessageServerbound,
pk.String(msg),
))
}