Split the function of PlayerList and PingInfo
This commit is contained in:
@ -1,13 +1,17 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"encoding/base64"
|
||||
"encoding/json"
|
||||
|
||||
"errors"
|
||||
"github.com/Tnze/go-mc/chat"
|
||||
"github.com/Tnze/go-mc/data/packetid"
|
||||
"github.com/Tnze/go-mc/net"
|
||||
pk "github.com/Tnze/go-mc/net/packet"
|
||||
"github.com/google/uuid"
|
||||
"image"
|
||||
"image/png"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type ListPingHandler interface {
|
||||
@ -16,7 +20,9 @@ type ListPingHandler interface {
|
||||
MaxPlayer() int
|
||||
OnlinePlayer() int
|
||||
PlayerSamples() []PlayerSample
|
||||
|
||||
Description() *chat.Message
|
||||
FavIcon() string
|
||||
}
|
||||
|
||||
type PlayerSample struct {
|
||||
@ -64,12 +70,69 @@ func (s *Server) listResp() ([]byte, error) {
|
||||
FavIcon string `json:"favicon,omitempty"`
|
||||
}
|
||||
|
||||
list.Version.Name = s.ListPingHandler.Name()
|
||||
list.Version.Protocol = s.ListPingHandler.Protocol()
|
||||
list.Players.Max = s.ListPingHandler.MaxPlayer()
|
||||
list.Players.Online = s.ListPingHandler.OnlinePlayer()
|
||||
list.Players.Sample = s.ListPingHandler.PlayerSamples()
|
||||
list.Description = s.ListPingHandler.Description()
|
||||
list.Version.Name = s.Name()
|
||||
list.Version.Protocol = s.Protocol()
|
||||
list.Players.Max = s.MaxPlayer()
|
||||
list.Players.Online = s.OnlinePlayer()
|
||||
list.Players.Sample = s.PlayerSamples()
|
||||
list.Description = s.Description()
|
||||
list.FavIcon = s.FavIcon()
|
||||
|
||||
return json.Marshal(list)
|
||||
}
|
||||
|
||||
// PingInfo implement ListPingHandler.
|
||||
type PingInfo struct {
|
||||
name string
|
||||
protocol int
|
||||
*PlayerList
|
||||
description chat.Message
|
||||
favicon string
|
||||
}
|
||||
|
||||
// NewPingInfo crate a new PingInfo, the icon can be nil.
|
||||
func NewPingInfo(list *PlayerList, name string, protocol int, motd chat.Message, icon image.Image) (p *PingInfo, err error) {
|
||||
var favIcon string
|
||||
if icon != nil {
|
||||
if !icon.Bounds().Size().Eq(image.Point{X: 64, Y: 64}) {
|
||||
return nil, errors.New("icon size is not 64x64")
|
||||
}
|
||||
// Encode icon into string "data:image/png;base64,......" format
|
||||
var sb strings.Builder
|
||||
sb.WriteString("data:image/png;base64,")
|
||||
w := base64.NewEncoder(base64.StdEncoding, &sb)
|
||||
err = png.Encode(w, icon)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
err = w.Close()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
favIcon = sb.String()
|
||||
}
|
||||
p = &PingInfo{
|
||||
name: name,
|
||||
protocol: protocol,
|
||||
PlayerList: list,
|
||||
description: motd,
|
||||
favicon: favIcon,
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func (p *PingInfo) Name() string {
|
||||
return p.name
|
||||
}
|
||||
|
||||
func (p *PingInfo) Protocol() int {
|
||||
return p.protocol
|
||||
}
|
||||
|
||||
func (p *PingInfo) FavIcon() string {
|
||||
return p.favicon
|
||||
}
|
||||
|
||||
func (p *PingInfo) Description() *chat.Message {
|
||||
return &p.description
|
||||
}
|
||||
|
Reference in New Issue
Block a user