Skip to content

Commit

Permalink
Merge pull request #48 from dreamscached/string
Browse files Browse the repository at this point in the history
Implement Stringer protocol
  • Loading branch information
dreamscached authored Feb 9, 2023
2 parents 30b7b4c + 63fded9 commit 09cff17
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 0 deletions.
7 changes: 7 additions & 0 deletions ping_14.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,13 @@ type Status14 struct {
MaxPlayers int
}

// String returns a user-friendly representation of a server status response.
// It contains presumed (1.4+) Minecraft Server version, online count and naturalized MOTD.
func (s *Status14) String() string {
return fmt.Sprintf("Minecraft Server (1.4+), %d/%d players online, MOTD: %s",
s.OnlinePlayers, s.MaxPlayers, naturalizeMOTD(s.MOTD))
}

// Ping14 pings 1.4 to 1.6 (exclusively) Minecraft servers (Notchian servers of more late versions also respond to
// this ping packet.)
//
Expand Down
9 changes: 9 additions & 0 deletions ping_16.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ const Ping16ProtocolVersionIncompatible byte = 127

// Full version/snapshot to protocol version mapping list
// Extracted from https://wiki.vg/Protocol_version_numbers#Versions_before_the_Netty_rewrite
//
//goland:noinspection GoUnusedConst
const (
// Ping16ProtocolVersion13w39b holds protocol version (=80) for Minecraft 13w39b.
Expand Down Expand Up @@ -421,6 +422,13 @@ type Status16 struct {
MaxPlayers int
}

// String returns a user-friendly representation of a server status response.
// It contains Minecraft Server version, protocol version number, online count and naturalized MOTD.
func (s *Status16) String() string {
return fmt.Sprintf("Minecraft Server (1.6+ or older, %s, protocol version %d), %d/%d players online, MOTD: %s",
s.ServerVersion, s.ProtocolVersion, s.OnlinePlayers, s.MaxPlayers, naturalizeMOTD(s.MOTD))
}

// IsIncompatible checks if response returned an incompatible protocol version (=127), meaning
// this server cannot be joined unless client version is 1.7+.
func (s *Status16) IsIncompatible() bool {
Expand All @@ -429,6 +437,7 @@ func (s *Status16) IsIncompatible() bool {

// Ping16 pings 1.6 to 1.7 (exclusively) Minecraft servers (Notchian servers of more late versions also respond
// to this ping packet.)
//
//goland:noinspection GoUnusedExportedFunction
func Ping16(host string, port int) (*Status16, error) {
return defaultPinger.Ping16(host, port)
Expand Down
7 changes: 7 additions & 0 deletions ping_17.go
Original file line number Diff line number Diff line change
Expand Up @@ -1723,6 +1723,13 @@ type Status17 struct {
EnforcesSecureChat bool
}

// String returns a user-friendly representation of a server status response.
// It contains Minecraft Server version, protocol version number, online count and naturalized MOTD.
func (s *Status17) String() string {
return fmt.Sprintf("Minecraft Server (1.7+, %s, protocol version %d), %d/%d players online, description:\n%s",
s.VersionName, s.ProtocolVersion, s.OnlinePlayers, s.MaxPlayers, naturalizeMOTD(s.Description.String()))
}

// DescriptionText collects text components of Description together into normal string.
//
// Deprecated: this function is deprecated and is retained for compatibility. Newer software
Expand Down
8 changes: 8 additions & 0 deletions ping_beta18.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,16 @@ type StatusBeta18 struct {
MaxPlayers int
}

// String returns a user-friendly representation of a server status response.
// It contains presumed (Beta 1.8+) Minecraft Server version, online count and naturalized MOTD.
func (s *StatusBeta18) String() string {
return fmt.Sprintf("Minecraft Server (Beta 1.8+), %d/%d players online, MOTD: %s",
s.OnlinePlayers, s.MaxPlayers, naturalizeMOTD(s.MOTD))
}

// PingBeta18 pings Beta 1.8 to Release 1.4 (exclusively) Minecraft servers (Notchian servers of more late versions
// also respond to this ping packet.)
//
//goland:noinspection GoUnusedExportedFunction
func PingBeta18(host string, port int) (*StatusBeta18, error) {
return defaultPinger.PingBeta18(host, port)
Expand Down
12 changes: 12 additions & 0 deletions util.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"errors"
"image"
"io"
"regexp"
"strings"
)

var errStackEmpty = errors.New("stack is empty")
Expand Down Expand Up @@ -35,3 +37,13 @@ type UnmarshalFunc func([]byte, interface{}) error

// ImageDecodeFunc is a function that conforms to png.Decode function signature.
type ImageDecodeFunc func(io.Reader) (image.Image, error)

// naturalizeMOTD 'naturalizes' MOTD (or since 1.7+, description) strings and turns them
// into one-line, stripped of any formatting strings. Newlines are replaced with spaces and
// legacy §-formatting is omitted.
func naturalizeMOTD(s string) string {
s = strings.ReplaceAll(s, "\n", " ")
s = regexp.MustCompile("\u00a7[a-f0-9k-or]").ReplaceAllString(s, "")
s = strings.TrimSpace(s)
return s
}

0 comments on commit 09cff17

Please sign in to comment.