From 72b1820826c187737dc5ed2526881a42c0104e6b Mon Sep 17 00:00:00 2001 From: Tnze Date: Sun, 5 Jan 2020 01:14:38 +0800 Subject: [PATCH] =?UTF-8?q?=E7=94=A8=E6=9B=B4=E7=AE=80=E5=8D=95=E7=9A=84?= =?UTF-8?q?=E6=96=B9=E5=BC=8F=E4=B8=BAbot=E5=8C=85=E6=94=AF=E6=8C=81?= =?UTF-8?q?=E4=BB=A3=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- bot/mcbot.go | 23 ++++++++++++++++++----- 1 file changed, 18 insertions(+), 5 deletions(-) diff --git a/bot/mcbot.go b/bot/mcbot.go index b6c9c9f..c1e5910 100644 --- a/bot/mcbot.go +++ b/bot/mcbot.go @@ -16,19 +16,26 @@ const ProtocolVersion = 575 // JoinServer connect a Minecraft server for playing the game. func (c *Client) JoinServer(addr string, port int) (err error) { - //Connect - conn, err := mcnet.DialMC(fmt.Sprintf("%s:%d", addr, port)) + conn, err := net.Dial("tcp", fmt.Sprintf("%s:%d", addr, port)) if err != nil { err = fmt.Errorf("bot: connect server fail: %v", err) return } + return c.join(conn) +} - //JoinConn - return c.JoinConn(conn.Socket) +// JoinServerWithDialer is similar to JoinServer but using a Dialer. +func (c *Client) JoinServerWithDialer(d Dialer, addr string) (err error) { + conn, err := d.Dial("tcp", addr) + if err != nil { + err = fmt.Errorf("bot: connect server fail: %v", err) + return + } + return c.join(conn) } // JoinConn join a Minecraft server through a connection for playing the game. -func (c *Client) JoinConn(conn net.Conn) (err error) { +func (c *Client) join(conn net.Conn) (err error) { //Set Conn c.conn = mcnet.WrapConn(conn) @@ -104,6 +111,12 @@ func (c *Client) JoinConn(conn net.Conn) (err error) { } } +// A Dialer is a means to establish a connection. +type Dialer interface { + // Dial connects to the given address via the proxy. + Dial(network, addr string) (c net.Conn, err error) +} + // Conn return the MCConn of the Client. // Only used when you want to handle the packets by yourself func (c *Client) Conn() *mcnet.Conn {