Disconnect method, SendPacket and ReceivePacket.
This commit is contained in:
@ -3,6 +3,8 @@ package bot
|
||||
import (
|
||||
"github.com/Tnze/go-mc/bot/world/entity"
|
||||
"github.com/Tnze/go-mc/chat"
|
||||
|
||||
pk "github.com/Tnze/go-mc/net/packet"
|
||||
)
|
||||
|
||||
type eventBroker struct {
|
||||
@ -16,5 +18,9 @@ type eventBroker struct {
|
||||
HeldItemChange func(slot int) error
|
||||
|
||||
WindowsItem func(id byte, slots []entity.Slot) error
|
||||
WindowsItemChange func(id byte, slotID int, slot entity.Slot)error
|
||||
WindowsItemChange func(id byte, slotID int, slot entity.Slot) error
|
||||
|
||||
// ReceivePacket will be called when new packet arrive.
|
||||
// Default handler will run only if pass == false.
|
||||
ReceivePacket func(p pk.Packet) (pass bool, err error)
|
||||
}
|
||||
|
@ -51,6 +51,16 @@ func (c *Client) HandleGame() error {
|
||||
}
|
||||
|
||||
func (c *Client) handlePacket(p pk.Packet) (disconnect bool, err error) {
|
||||
if c.Events.ReceivePacket != nil {
|
||||
pass, err := c.Events.ReceivePacket(p)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
if pass {
|
||||
return false, nil
|
||||
}
|
||||
}
|
||||
|
||||
switch p.ID {
|
||||
case data.JoinGame:
|
||||
err = handleJoinGamePacket(c, p)
|
||||
|
@ -2,6 +2,7 @@ package bot
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"github.com/Tnze/go-mc/chat"
|
||||
"strconv"
|
||||
|
||||
"github.com/Tnze/go-mc/data"
|
||||
@ -178,3 +179,17 @@ func (c *Client) UseItemEnd() error {
|
||||
func (c *Client) SwapItem() error {
|
||||
return c.playerAction(6, 0, 0, 0, 0)
|
||||
}
|
||||
|
||||
// Disconnect send disconnect packet to server.
|
||||
// Server will close the connection after receive this packet.
|
||||
func (c *Client) Disconnect(reason chat.Message) error {
|
||||
return c.conn.WritePacket(pk.Marshal(
|
||||
data.DisconnectPlay,
|
||||
reason,
|
||||
))
|
||||
}
|
||||
|
||||
// SendPacket send the packet to server.
|
||||
func (c *Client) SendPacket(packet pk.Packet) error {
|
||||
return c.conn.WritePacket(packet)
|
||||
}
|
||||
|
6
go.mod
6
go.mod
@ -2,8 +2,4 @@ module github.com/Tnze/go-mc
|
||||
|
||||
go 1.12
|
||||
|
||||
require (
|
||||
github.com/google/uuid v1.1.1
|
||||
github.com/mattn/go-colorable v0.1.2
|
||||
github.com/satori/go.uuid v1.2.0 // indirect
|
||||
)
|
||||
require github.com/google/uuid v1.1.1
|
||||
|
8
go.sum
8
go.sum
@ -1,10 +1,2 @@
|
||||
github.com/google/uuid v1.1.1 h1:Gkbcsh/GbpXz7lPftLA3P6TYMwjCLYm83jiFQZF/3gY=
|
||||
github.com/google/uuid v1.1.1/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
|
||||
github.com/mattn/go-colorable v0.1.2 h1:/bC9yWikZXAL9uJdulbSfyVNIR3n3trXl+v8+1sx8mU=
|
||||
github.com/mattn/go-colorable v0.1.2/go.mod h1:U0ppj6V5qS13XJ6of8GYAs25YV2eR4EVcfRqFIhoBtE=
|
||||
github.com/mattn/go-isatty v0.0.8 h1:HLtExJ+uU2HOZ+wI0Tt5DtUDrx8yhUqDcp7fYERX4CE=
|
||||
github.com/mattn/go-isatty v0.0.8/go.mod h1:Iq45c/XA43vh69/j3iqttzPXn0bhXyGjM0Hdxcsrc5s=
|
||||
github.com/satori/go.uuid v1.2.0 h1:0uYX9dsZ2yD7q2RtLRtPSdGDWzjeM3TbMJP9utgA0ww=
|
||||
github.com/satori/go.uuid v1.2.0/go.mod h1:dA0hQrYB0VpLJoorglMZABFdXlWrHn1NEOzdhQKdks0=
|
||||
golang.org/x/sys v0.0.0-20190222072716-a9d3bda3a223 h1:DH4skfRX4EBpamg7iV4ZlCpblAHI6s6TDM39bFZumv8=
|
||||
golang.org/x/sys v0.0.0-20190222072716-a9d3bda3a223/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
|
||||
|
Reference in New Issue
Block a user