From 0c48980309c91dd91b3ab63a06afda1ed2a61a20 Mon Sep 17 00:00:00 2001 From: Tnze Date: Fri, 16 Aug 2019 12:46:09 +0800 Subject: [PATCH 1/2] Disconnect method, SendPacket and ReceivePacket. --- bot/event.go | 10 ++++++++-- bot/ingame.go | 10 ++++++++++ bot/motion.go | 15 +++++++++++++++ go.mod | 6 +----- go.sum | 8 -------- 5 files changed, 34 insertions(+), 15 deletions(-) diff --git a/bot/event.go b/bot/event.go index 83da7f1..d121c05 100644 --- a/bot/event.go +++ b/bot/event.go @@ -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 { @@ -15,6 +17,10 @@ type eventBroker struct { PluginMessage func(channel string, data []byte) error HeldItemChange func(slot int) error - WindowsItem func(id byte, slots []entity.Slot) error - WindowsItemChange func(id byte, slotID int, slot entity.Slot)error + WindowsItem func(id byte, slots []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) } diff --git a/bot/ingame.go b/bot/ingame.go index ebffe03..b5ea495 100644 --- a/bot/ingame.go +++ b/bot/ingame.go @@ -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) diff --git a/bot/motion.go b/bot/motion.go index 8d2f5c1..1e1d227 100644 --- a/bot/motion.go +++ b/bot/motion.go @@ -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) +} diff --git a/go.mod b/go.mod index cd55480..5bf52ac 100644 --- a/go.mod +++ b/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 diff --git a/go.sum b/go.sum index 0b81a39..b864886 100644 --- a/go.sum +++ b/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= From 0d3be8e733bedd8e9df82e0f818df213bb0cc06b Mon Sep 17 00:00:00 2001 From: Tnze Date: Fri, 16 Aug 2019 13:29:57 +0800 Subject: [PATCH 2/2] Update README.md --- README.md | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index eb4168a..afe4c5b 100644 --- a/README.md +++ b/README.md @@ -8,13 +8,13 @@ There's some library in Go support you to create your Minecraft client or server. 这是一些Golang库,用于帮助你编写自己的Minecraft客户端或服务器, - [x] Chat -- [x] Parse NBT -- [x] Simple MC robot lib +- [x] NBT - [x] Yggdrasil -- [x] Minecraft network protocol +- [x] Realms Server - [x] RCON protocol - [x] Saves decoding /encoding -- [x] Realms Server +- [x] Minecraft network protocol +- [x] Simple MC robot lib bot: - [x] Swing arm @@ -27,6 +27,7 @@ bot: - [x] Attack entity - [x] Use/Place block - [x] Mine block +- [x] Custom packets - [ ] Record entities