@@ -10,6 +10,7 @@ import (
1010 "go.minekube.com/common/minecraft/component/codec/legacy"
1111 "go.minekube.com/gate/pkg/edition/java/forge/modinfo"
1212 "go.minekube.com/gate/pkg/edition/java/proto/util"
13+ protoversion "go.minekube.com/gate/pkg/edition/java/proto/version"
1314 "go.minekube.com/gate/pkg/gate/proto"
1415 "go.minekube.com/gate/pkg/util/componentutil"
1516 "go.minekube.com/gate/pkg/util/favicon"
@@ -19,11 +20,11 @@ import (
1920
2021// ServerPing is a 1.7 and above server list ping response.
2122type ServerPing struct {
22- Version Version `json:"version,omitempty" yaml:"version,omitempty"`
23- Players * Players `json:"players,omitempty" yaml:"players,omitempty"`
24- Description * component.Text `json:"description" yaml:"description"`
25- Favicon favicon.Favicon `json:"favicon,omitempty" yaml:"favicon,omitempty"`
26- ModInfo * modinfo.ModInfo `json:"modinfo,omitempty" yaml:"modinfo,omitempty"`
23+ Version Version `json:"version,omitempty" yaml:"version,omitempty"`
24+ Players * Players `json:"players,omitempty" yaml:"players,omitempty"`
25+ Description component.Component `json:"description" yaml:"description"`
26+ Favicon favicon.Favicon `json:"favicon,omitempty" yaml:"favicon,omitempty"`
27+ ModInfo * modinfo.ModInfo `json:"modinfo,omitempty" yaml:"modinfo,omitempty"`
2728}
2829
2930// Make sure ServerPing implements the interfaces at compile time.
@@ -66,7 +67,7 @@ func (p *ServerPing) UnmarshalJSON(data []byte) error {
6667 out .Alias .Description = & component.Text {} // empty component
6768 } else {
6869 var err error
69- out .Alias .Description , err = componentutil .ParseTextComponent (out .Version .Protocol , string (out .Description ))
70+ out .Alias .Description , err = componentutil .ParseComponent (out .Version .Protocol , string (out .Description ))
7071 if err != nil {
7172 return fmt .Errorf ("error decoding description: %w" , err )
7273 }
@@ -89,7 +90,7 @@ func (p *ServerPing) UnmarshalYAML(value *yaml.Node) error {
8990 }
9091
9192 var err error
92- p .Description , err = componentutil .ParseTextComponent (out .Version .Protocol , out .Description )
93+ p .Description , err = componentutil .ParseComponent (out .Version .Protocol , out .Description )
9394 if err != nil {
9495 return fmt .Errorf ("error decoding description: %w" , err )
9596 }
@@ -99,22 +100,47 @@ func (p *ServerPing) UnmarshalYAML(value *yaml.Node) error {
99100}
100101
101102func (p * ServerPing ) MarshalYAML () (any , error ) {
102- b := new (strings.Builder )
103- err := (& legacy.Legacy {}).Marshal (b , p .Description )
104- if err != nil {
105- return nil , fmt .Errorf ("error encoding description: %w" , err )
103+ description := ""
104+ if text , ok := p .Description .(* component.Text ); ok {
105+ b := new (strings.Builder )
106+ if err := (& legacy.Legacy {}).Marshal (b , text ); err == nil {
107+ description = b .String ()
108+ } else {
109+ description , err = p .marshalDescriptionJSON ()
110+ if err != nil {
111+ return nil , err
112+ }
113+ }
114+ } else if p .Description != nil {
115+ var err error
116+ description , err = p .marshalDescriptionJSON ()
117+ if err != nil {
118+ return nil , err
119+ }
106120 }
107121
108122 type Alias ServerPing
109123 return & struct {
110124 Description string
111125 * Alias
112126 }{
113- Description : b . String () ,
127+ Description : description ,
114128 Alias : (* Alias )(p ),
115129 }, nil
116130}
117131
132+ func (p * ServerPing ) marshalDescriptionJSON () (string , error ) {
133+ protocol := p .Version .Protocol
134+ if protocol == 0 {
135+ protocol = protoversion .MaximumVersion .Protocol
136+ }
137+ b , err := util .Marshal (protocol , p .Description )
138+ if err != nil {
139+ return "" , fmt .Errorf ("error encoding description: %w" , err )
140+ }
141+ return string (b ), nil
142+ }
143+
118144type Version struct {
119145 Protocol proto.Protocol `json:"protocol,omitempty" yaml:"protocol,omitempty"`
120146 Name string `json:"name,omitempty" yaml:"name,omitempty"`
0 commit comments