diff --git a/minecraft/conn.go b/minecraft/conn.go index ebd1ee19..1c4857d0 100644 --- a/minecraft/conn.go +++ b/minecraft/conn.go @@ -577,6 +577,17 @@ func (conn *Conn) Context() context.Context { return conn.ctx } +// Disconnect disconnects the connection by first sending a disconnect packet with the message passed, and +// closing the connection after. If the message passed is empty, the client will be immediately sent to the +// server list instead of a disconnect screen. +func (conn *Conn) Disconnect(message string) error { + _ = conn.WritePacket(&packet.Disconnect{ + HideDisconnectionScreen: message == "", + Message: message, + }) + return conn.close(conn.closeErr(message)) +} + // takeDeferredPacket locks the deferred packets lock and takes the next packet from the list of deferred // packets. If none was found, it returns false, and if one was found, the data and true is returned. func (conn *Conn) takeDeferredPacket() (*packetData, bool) { diff --git a/minecraft/listener.go b/minecraft/listener.go index e78cb218..81f82cb3 100644 --- a/minecraft/listener.go +++ b/minecraft/listener.go @@ -6,11 +6,6 @@ import ( "crypto/rand" "errors" "fmt" - "github.com/sandertv/gophertunnel/minecraft/internal" - "github.com/sandertv/gophertunnel/minecraft/protocol" - "github.com/sandertv/gophertunnel/minecraft/protocol/login" - "github.com/sandertv/gophertunnel/minecraft/protocol/packet" - "github.com/sandertv/gophertunnel/minecraft/resource" "log/slog" "math" "net" @@ -18,6 +13,12 @@ import ( "sync" "sync/atomic" "time" + + "github.com/sandertv/gophertunnel/minecraft/internal" + "github.com/sandertv/gophertunnel/minecraft/protocol" + "github.com/sandertv/gophertunnel/minecraft/protocol/login" + "github.com/sandertv/gophertunnel/minecraft/protocol/packet" + "github.com/sandertv/gophertunnel/minecraft/resource" ) // ListenConfig holds settings that may be edited to change behaviour of a Listener. @@ -187,11 +188,7 @@ func (listener *Listener) Accept() (net.Conn, error) { // closing the connection after. If the message passed is empty, the client will be immediately sent to the // server list instead of a disconnect screen. func (listener *Listener) Disconnect(conn *Conn, message string) error { - _ = conn.WritePacket(&packet.Disconnect{ - HideDisconnectionScreen: message == "", - Message: message, - }) - return conn.close(conn.closeErr(message)) + return conn.Disconnect(message) } // AddResourcePack adds a new resource pack to the listener's resource packs.