From 2f88446ebc7a76ea2f7b651268870d7dd0517dac Mon Sep 17 00:00:00 2001 From: Tnze Date: Sat, 7 Jan 2023 12:05:42 +0800 Subject: [PATCH] brake changes: support dynamic protocols number in ping list handler. --- server/ping.go | 28 +++++++++++++++++++--------- server/server.go | 2 +- 2 files changed, 20 insertions(+), 10 deletions(-) diff --git a/server/ping.go b/server/ping.go index 242506a..6b43733 100644 --- a/server/ping.go +++ b/server/ping.go @@ -18,16 +18,26 @@ import ( // ListPingHandler collect server running status info // which is used to handle client ping and list progress. type ListPingHandler interface { - // Name of the server version + // Name of the server. + // Vanilla server uses its version name, like "1.19.3". Name() string - // Protocol number - Protocol() int + + // The Protocol number. + // Usually implemented as returning the protocol number the server currently used. + // If the server supports multiple protocols, should be implemented as returning clientProtocol + Protocol(clientProtocol int32) int + MaxPlayer() int + OnlinePlayer() int - // PlayerSamples is a short list of some player in the server + + // PlayerSamples is a short list of players in the server. + // Vanilla server returns up to 10 players in the list. PlayerSamples() []PlayerSample + // Description also called MOTD, Message Of The Day. Description() *chat.Message + // FavIcon should be a PNG image that is Base64 encoded // (without newlines: \n, new lines no longer work since 1.13) // and prepended with "data:image/png;base64,". @@ -41,7 +51,7 @@ type PlayerSample struct { ID uuid.UUID `json:"id"` } -func (s *Server) acceptListPing(conn *net.Conn) { +func (s *Server) acceptListPing(conn *net.Conn, clientProtocol int32) { var p pk.Packet for i := 0; i < 2; i++ { // Ping or List. Only allow check twice err := conn.ReadPacket(&p) @@ -52,7 +62,7 @@ func (s *Server) acceptListPing(conn *net.Conn) { switch p.ID { case packetid.StatusResponse: // List var resp []byte - resp, err = s.listResp() + resp, err = s.listResp(clientProtocol) if err != nil { break } @@ -66,7 +76,7 @@ func (s *Server) acceptListPing(conn *net.Conn) { } } -func (s *Server) listResp() ([]byte, error) { +func (s *Server) listResp(clientProtocol int32) ([]byte, error) { var list struct { Version struct { Name string `json:"name"` @@ -82,7 +92,7 @@ func (s *Server) listResp() ([]byte, error) { } list.Version.Name = s.Name() - list.Version.Protocol = s.Protocol() + list.Version.Protocol = s.Protocol(clientProtocol) list.Players.Max = s.MaxPlayer() list.Players.Online = s.OnlinePlayer() list.Players.Sample = s.PlayerSamples() @@ -133,7 +143,7 @@ func (p *PingInfo) Name() string { return p.name } -func (p *PingInfo) Protocol() int { +func (p *PingInfo) Protocol(int32) int { return p.protocol } diff --git a/server/server.go b/server/server.go index d57f4c5..74d3729 100644 --- a/server/server.go +++ b/server/server.go @@ -73,7 +73,7 @@ func (s *Server) AcceptConn(conn *net.Conn) { switch intention { case 1: // list ping - s.acceptListPing(conn) + s.acceptListPing(conn, protocol) case 2: // login name, id, profilePubKey, properties, err := s.AcceptLogin(conn, protocol) if err != nil {