server

package
v1.20.1 Latest Latest
Warning

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

Go to latest
Published: Nov 14, 2023 License: MIT Imports: 26 Imported by: 3

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         |        Others..       |
|--------------------|------------+----+---------------|-----------------------+
| MojangLoginHandler |  PingInfo  |     PlayerList     |  [go-mc/server], etc. |
+--------------------+------------+--------------------+-----------------------+

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 go-mc/server. You can also write your version.

Index

Constants

View Source
const (
	ProtocolName    = "1.19.4"
	ProtocolVersion = 762
)

Variables

This section is empty.

Functions

This section is empty.

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 *user.PublicKey, properties []user.Property, protocol int32, conn *net.Conn)
}

type GameProfile added in v1.19.0

type GameProfile struct {
	ID   uuid.UUID
	Name string
}

type KeepAlive added in v1.18.1

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

func NewKeepAlive added in v1.18.1

func NewKeepAlive() (k *KeepAlive)

func (*KeepAlive) AddPlayerDelayUpdateHandler added in v1.18.1

func (k *KeepAlive) AddPlayerDelayUpdateHandler(f func(c KeepAliveClient, delay time.Duration))

func (*KeepAlive) ClientJoin added in v1.19.0

func (k *KeepAlive) ClientJoin(client KeepAliveClient)

func (*KeepAlive) ClientLeft added in v1.19.0

func (k *KeepAlive) ClientLeft(client KeepAliveClient)

func (*KeepAlive) ClientTick added in v1.19.0

func (k *KeepAlive) ClientTick(client KeepAliveClient)

func (*KeepAlive) Run added in v1.18.1

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

Run implement Component for KeepAlive

type KeepAliveClient added in v1.19.0

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

type ListPingHandler

type ListPingHandler interface {
	// Name of the server.
	// Vanilla server uses its version name, like "1.19.3".
	Name() string

	// The Protocol number.
	// Usually implemented as returning the protocol number the server currently used.
	// If the server supports multiple protocols, should be implemented as returning clientProtocol
	Protocol(clientProtocol int32) int

	MaxPlayer() int

	OnlinePlayer() int

	// PlayerSamples is a short list of players in the server.
	// Vanilla server returns up to 10 players in the list.
	PlayerSamples() []PlayerSample

	// Description also called MOTD, Message Of The Day.
	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 added in v1.18.1

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 the client by "LoginDisconnect" packet.

type LoginFailErr added in v1.19.0

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

func (LoginFailErr) Error added in v1.19.0

func (l LoginFailErr) Error() string

type LoginHandler

type LoginHandler interface {
	AcceptLogin(conn *net.Conn, protocol int32) (name string, id uuid.UUID, profilePubKey *user.PublicKey, properties []user.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 an optional field and can be set to nil.
	LoginChecker
	// contains filtered or unexported fields
}

MojangLoginHandler is a standard LoginHandler that implement both online and offline login progress. This implementation also supports custom LoginChecker. None of Custom login packets (also called LoginPluginRequest/Response) is supported for this implementation. To do that, implement your own LoginHandler imitate this code.

func (*MojangLoginHandler) AcceptLogin

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

AcceptLogin implement LoginHandler for MojangLoginHandler

type Packet757 added in v1.18.1

type Packet757 pk.Packet

Packet758 is a packet in protocol 757. We are using type system to force programmers to update packets.

type Packet758 added in v1.18.2

type Packet758 pk.Packet

Packet758 is a packet in protocol 757. We are using type system to force programmers to update packets.

type PacketQueue added in v1.18.1

type PacketQueue = queue.Queue[pk.Packet]

type PingInfo added in v1.18.1

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

PingInfo implement ListPingHandler.

func NewPingInfo added in v1.18.1

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 added in v1.18.1

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

func (*PingInfo) FavIcon added in v1.18.1

func (p *PingInfo) FavIcon() string

func (*PingInfo) Name added in v1.18.1

func (p *PingInfo) Name() string

func (*PingInfo) Protocol added in v1.18.1

func (p *PingInfo) Protocol(int32) int

type PlayerList added in v1.18.1

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 added in v1.18.1

func NewPlayerList(maxPlayers int) *PlayerList

NewPlayerList create a PlayerList which implement ListPingHandler.

func (*PlayerList) CheckPlayer added in v1.18.1

func (p *PlayerList) CheckPlayer(string, uuid.UUID, int32) (ok bool, reason chat.Message)

CheckPlayer implements LoginChecker for PlayerList

func (*PlayerList) ClientJoin added in v1.19.0

func (p *PlayerList) ClientJoin(client PlayerListClient, player PlayerSample)

func (*PlayerList) ClientLeft added in v1.19.0

func (p *PlayerList) ClientLeft(client PlayerListClient)

func (*PlayerList) Len added in v1.19.0

func (p *PlayerList) Len() int

func (*PlayerList) MaxPlayer added in v1.18.1

func (p *PlayerList) MaxPlayer() int

func (*PlayerList) OnlinePlayer added in v1.18.1

func (p *PlayerList) OnlinePlayer() int

func (*PlayerList) PlayerSamples added in v1.18.1

func (p *PlayerList) PlayerSamples() (sample []PlayerSample)

func (*PlayerList) Range added in v1.19.0

func (p *PlayerList) Range(f func(PlayerListClient, PlayerSample))

type PlayerListClient added in v1.19.0

type PlayerListClient interface {
	SendDisconnect(reason chat.Message)
}

type PlayerSample

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

type Pos added in v1.19.0

type Pos struct{ X, Y, Z float64 }

type Rot added in v1.19.0

type Rot struct{ Yaw, Pitch float32 }

type Server

type Server struct {
	*log.Logger
	ListPingHandler
	LoginHandler
	GamePlay
}

func (*Server) AcceptConn added in v1.19.2

func (s *Server) AcceptConn(conn *net.Conn)

func (*Server) Listen

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

type WritePacketError added in v1.18.1

type WritePacketError struct {
	Err error
	ID  int32
}

func (WritePacketError) Error added in v1.18.1

func (s WritePacketError) Error() string

func (WritePacketError) Unwrap added in v1.18.1

func (s WritePacketError) Unwrap() error

Directories

Path Synopsis
internal
bvh

Jump to

Keyboard shortcuts

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