server

package
v0.0.0-...-18e7427 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 10, 2023 License: MIT Imports: 25 Imported by: 0

README

Server

This package provide a very basic framework for server development.
For more example, go to this repo.

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

View Source
const ProtocolName = "1.19"
View Source
const ProtocolVersion = 759

Variables

View Source
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

func Attach

func Attach(s *Server)

func ChatCommand

func ChatCommand(s *Server, p pk.Packet) error

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 GameProfile struct {
	ID   uuid.UUID
	Name string
}

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)

func (*KeepAlive) Run

func (k *KeepAlive) Run(ctx context.Context)

Run implement Component for KeepAlive

type KeepAliveClient

type KeepAliveClient interface {
	SendKeepAlive(id int64)
	SendDisconnect(reason chat.Message)
}

type Limiter

type Limiter struct {
	Every duration `toml:"every"`
	N     int
}

func (*Limiter) Limiter

func (l *Limiter) Limiter() *rate.Limiter

Limiter convert this to *rate.Limiter

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

func (*MojangLoginHandler) AcceptLogin

func (d *MojangLoginHandler) AcceptLogin(conn *net.Conn, protocol int32) (name string, id uuid.UUID, profilePubKey *auth.PublicKey, properties []auth.Property, err error)

AcceptLogin implement LoginHandler for MojangLoginHandler

type PingInfo

type PingInfo struct {
	// contains filtered or unexported fields
}

PingInfo implement ListPingHandler.

func NewPingInfo

func NewPingInfo(name string, protocol int, motd chat.Message, icon image.Image) (p *PingInfo)

NewPingInfo crate a new PingInfo, the icon can be nil. Panic if icon's size is not 64x64.

func (*PingInfo) Description

func (p *PingInfo) Description() *chat.Message

func (*PingInfo) FavIcon

func (p *PingInfo) FavIcon() string

func (*PingInfo) Name

func (p *PingInfo) Name() string

func (*PingInfo) Protocol

func (p *PingInfo) Protocol() int

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 PlayerListClient interface {
	SendDisconnect(reason chat.Message)
}

type PlayerSample

type PlayerSample struct {
	Name string    `json:"name"`
	ID   uuid.UUID `json:"id"`
}

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 (s *Server) AcceptPlayer(name string, id uuid.UUID, profilePubKey *auth.PublicKey, properties []auth.Property, protocol int32, conn *net.Conn, sample PlayerSample)

func (*Server) BroadcastNewPlayer

func (s *Server) BroadcastNewPlayer(c *client.ServerClient, sample PlayerSample)

func (*Server) Description

func (s *Server) Description() string

func (*Server) FavIcon

func (s *Server) FavIcon() string

func (*Server) Listen

func (s *Server) Listen(addr string) error

Listen starts listening on the specified address.

func (*Server) MaxPlayer

func (s *Server) MaxPlayer() int

func (*Server) Name

func (s *Server) Name() string

func (*Server) OnlinePlayer

func (s *Server) OnlinePlayer() int

func (*Server) PlayerSamples

func (s *Server) PlayerSamples() []PlayerSample

func (*Server) Protocol

func (s *Server) Protocol() int

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"`
}

Directories

Path Synopsis
internal
bvh

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL