Documentation ¶
Overview ¶
Package server provide a minecraft server framework. You can build the server you want by combining the various functional modules provided here. An example can be found in examples/frameworkServer.
This package is under rapid development, and any API may be subject to break changes ¶
A server is roughly divided into two parts: Gate and GamePlay
+---------------------------------------------------------------------+ | Go-MC Server Framework | +--------------------------------------+------------------------------+ | Gate | GamePlay | +--------------------+-----------------+ | | LoginHandler | ListPingHandler | | +--------------------+------------+----+---------------+--------------+ | MojangLoginHandler | PingInfo | PlayerList | Others.... | +--------------------+------------+--------------------+--------------+
Gate, which is used to respond to the client login request, provide login verification, respond to the List Ping Request and providing the online players' information.
Gameplay, which is used to handle all things after a player successfully logs in (that is, after the LoginSuccess package is sent), and is responsible for functions including player status, chunk management, keep alive, chat, etc.
The implement of Gameplay is provided at https://github.com/go-mc/server.
Index ¶
- Constants
- Variables
- func Attach(s *Server)
- func ChatCommand(s *Server, p pk.Packet) error
- type GamePlay
- type GameProfile
- type KeepAlive
- func (k *KeepAlive) AddPlayerDelayUpdateHandler(f func(c KeepAliveClient, delay time.Duration))
- func (k *KeepAlive) ClientJoin(client KeepAliveClient)
- func (k *KeepAlive) ClientLeft(client KeepAliveClient)
- func (k *KeepAlive) ClientTick(client KeepAliveClient)
- func (k *KeepAlive) Run(ctx context.Context)
- type KeepAliveClient
- type Limiter
- type ListPingHandler
- type LoginChecker
- type LoginFailErr
- type LoginHandler
- type MojangLoginHandler
- type PingInfo
- type PlayerList
- func (p *PlayerList) CheckPlayer(name string, uuid uuid.UUID, protocol int32) (ok bool, reason chat.Message)
- func (p *PlayerList) ClientJoin(client KeepAliveClient, sample PlayerSample)
- func (p *PlayerList) ClientLeft(client KeepAliveClient)
- func (p *PlayerList) Len() int
- func (p *PlayerList) MaxPlayer() int
- func (p *PlayerList) OnlinePlayer() int
- func (p *PlayerList) PlayerSamples() (sample []PlayerSample)
- func (p *PlayerList) Range(f func(PlayerListClient, PlayerSample))
- type PlayerListClient
- type PlayerSample
- type Server
- func (s *Server) AcceptPlayer(name string, id uuid.UUID, profilePubKey *auth.PublicKey, ...)
- func (s *Server) BroadcastNewPlayer(c *client.ServerClient, sample PlayerSample)
- func (s *Server) Description() string
- func (s *Server) FavIcon() string
- func (s *Server) Listen(addr string) error
- func (s *Server) MaxPlayer() int
- func (s *Server) Name() string
- func (s *Server) OnlinePlayer() int
- func (s *Server) PlayerSamples() []PlayerSample
- func (s *Server) Protocol() int
- type ServerSettings
Constants ¶
const ProtocolName = "1.19"
const ProtocolVersion = 759
Variables ¶
var DefaultServerSettings = ServerSettings{ Name: "Go-MC Server", MaxPlayers: 20, ViewDistance: 10, ListenAddress: "0.0.0.0:25565", MessageOfTheDay: "Welcome to Go-MC Server", Icon: "", NetworkCompressionThreshold: 1, OnlineMode: true, LevelName: "world", EnforceSecureProfile: false, ChunkLoadingLimiter: Limiter{ Every: duration{Duration: 50 * time.Millisecond}, N: 100, }, PlayerChunkLoadingLimiter: Limiter{ Every: duration{Duration: 50 * time.Millisecond}, N: 100, }, }
Functions ¶
Types ¶
type GamePlay ¶
type GamePlay interface { // AcceptPlayer handle everything after "LoginSuccess" is sent. // // Note: the connection will be closed after this function returned. // You don't need to close the connection, but to keep not returning while the player is playing. AcceptPlayer(name string, id uuid.UUID, profilePubKey *auth.PublicKey, properties []auth.Property, protocol int32, conn *net.Conn) }
type GameProfile ¶
type KeepAlive ¶
type KeepAlive struct {
// contains filtered or unexported fields
}
func NewKeepAlive ¶
func NewKeepAlive() (k *KeepAlive)
func (*KeepAlive) AddPlayerDelayUpdateHandler ¶
func (k *KeepAlive) AddPlayerDelayUpdateHandler(f func(c KeepAliveClient, delay time.Duration))
func (*KeepAlive) ClientJoin ¶
func (k *KeepAlive) ClientJoin(client KeepAliveClient)
func (*KeepAlive) ClientLeft ¶
func (k *KeepAlive) ClientLeft(client KeepAliveClient)
func (*KeepAlive) ClientTick ¶
func (k *KeepAlive) ClientTick(client KeepAliveClient)
type KeepAliveClient ¶
type ListPingHandler ¶
type ListPingHandler interface { // Name of the server version Name() string // Protocol number Protocol() int MaxPlayer() int OnlinePlayer() int // PlayerSamples is a short list of some player in the server PlayerSamples() []PlayerSample 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,". // // This method can return empty string if no icon is set. FavIcon() string }
ListPingHandler collect server running status info which is used to handle client ping and list progress.
type LoginChecker ¶
type LoginChecker interface {
CheckPlayer(name string, id uuid.UUID, protocol int32) (ok bool, reason chat.Message)
}
LoginChecker is the interface to check if a player is allowed to log in the server. The checking could be anything, server player number, protocol version, blacklist or whitelist. If a player is not allowed to, the reason should be returned and will be sent to client by "LoginDisconnect" packet.
type LoginFailErr ¶
type LoginFailErr struct {
// contains filtered or unexported fields
}
func (LoginFailErr) Error ¶
func (l LoginFailErr) Error() string
type LoginHandler ¶
type LoginHandler interface {
AcceptLogin(conn *net.Conn, protocol int32) (name string, id uuid.UUID, profilePubKey *auth.PublicKey, properties []auth.Property, err error)
}
LoginHandler is used to handle player login process, that is, from clientbound "LoginStart" packet to serverbound "LoginSuccess" packet.
type MojangLoginHandler ¶
type MojangLoginHandler struct { // OnlineMode enables to check player's account. // And also encrypt the connection after login. OnlineMode bool // EnforceSecureProfile enforce to check the player's profile public key EnforceSecureProfile bool // Threshold set the smallest size of raw network payload to compress. // Set to 0 to compress all packets. Set to -1 to disable compression. Threshold int // LoginChecker is used to apply some checks before sending "LoginSuccess" packet // (e.g. blacklist or is server full). // This is optional field and can be set to nil. LoginChecker }
MojangLoginHandler is a standard LoginHandler that implement both online and offline login progress. This implementation also support custom LoginChecker. None of Custom login packet (also called LoginPluginRequest/Response) is support by this implementation. To do that, implement your own LoginHandler imitate this code.
func NewMojangLoginHandler ¶
func NewMojangLoginHandler() *MojangLoginHandler
type PingInfo ¶
type PingInfo struct {
// contains filtered or unexported fields
}
PingInfo implement ListPingHandler.
func NewPingInfo ¶
NewPingInfo crate a new PingInfo, the icon can be nil. Panic if icon's size is not 64x64.
func (*PingInfo) Description ¶
type PlayerList ¶
type PlayerList struct {
// contains filtered or unexported fields
}
PlayerList is a player list based on linked-list. This struct should not be copied after used.
func NewPlayerList ¶
func NewPlayerList(maxPlayers int) *PlayerList
NewPlayerList create a PlayerList which implement ListPingHandler.
func (*PlayerList) CheckPlayer ¶
func (p *PlayerList) CheckPlayer(name string, uuid uuid.UUID, protocol int32) (ok bool, reason chat.Message)
CheckPlayer implements LoginChecker for PlayerList
func (*PlayerList) ClientJoin ¶
func (p *PlayerList) ClientJoin(client KeepAliveClient, sample PlayerSample)
func (*PlayerList) ClientLeft ¶
func (p *PlayerList) ClientLeft(client KeepAliveClient)
func (*PlayerList) Len ¶
func (p *PlayerList) Len() int
func (*PlayerList) MaxPlayer ¶
func (p *PlayerList) MaxPlayer() int
func (*PlayerList) OnlinePlayer ¶
func (p *PlayerList) OnlinePlayer() int
func (*PlayerList) PlayerSamples ¶
func (p *PlayerList) PlayerSamples() (sample []PlayerSample)
func (*PlayerList) Range ¶
func (p *PlayerList) Range(f func(PlayerListClient, PlayerSample))
type PlayerListClient ¶
type PlayerSample ¶
type Server ¶
type Server struct { *zap.Logger ListPingHandler LoginHandler World *world.World PlayerList *PlayerList Keepalive *KeepAlive Commands *command.Graph // contains filtered or unexported fields }
func NewServer ¶
func NewServer(settings ServerSettings) *Server
func (*Server) AcceptPlayer ¶
func (*Server) BroadcastNewPlayer ¶
func (s *Server) BroadcastNewPlayer(c *client.ServerClient, sample PlayerSample)
func (*Server) Description ¶
func (*Server) OnlinePlayer ¶
func (*Server) PlayerSamples ¶
func (s *Server) PlayerSamples() []PlayerSample
type ServerSettings ¶
type ServerSettings struct { Name string `toml:"name"` MaxPlayers int `toml:"max-players"` ViewDistance int32 `toml:"view-distance"` ListenAddress string `toml:"listen-address"` MessageOfTheDay string `toml:"motd"` Icon string `toml:"icon"` NetworkCompressionThreshold int `toml:"network-compression-threshold"` OnlineMode bool `toml:"online-mode"` LevelName string `toml:"level-name"` EnforceSecureProfile bool `toml:"enforce-secure-profile"` ChunkLoadingLimiter Limiter `toml:"chunk-loading-limiter"` PlayerChunkLoadingLimiter Limiter `toml:"player-chunk-loading-limiter"` }