server

package
v1.18.2 Latest Latest
Warning

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

Go to latest
Published: May 21, 2022 License: MIT Imports: 24 Imported by: 3

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 will provide later.

Index

Constants

View Source
const ProtocolName = "1.18.2"
View Source
const ProtocolVersion = 758

Variables

This section is empty.

Functions

This section is empty.

Types

type Component added in v1.18.1

type Component interface {
	Init(g *Game)
	Run(ctx context.Context)
	AddPlayer(p *Player)
	RemovePlayer(p *Player)
}

type Game added in v1.18.1

type Game struct {
	Dim Level
	// contains filtered or unexported fields
}

func NewGame added in v1.18.1

func NewGame(dim Level, components ...Component) *Game

func (*Game) AcceptPlayer added in v1.18.1

func (g *Game) AcceptPlayer(name string, id uuid.UUID, protocol int32, conn *net.Conn)

func (*Game) AddHandler added in v1.18.1

func (g *Game) AddHandler(ph *PacketHandler)

func (*Game) Run added in v1.18.1

func (g *Game) Run(ctx context.Context)

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, protocol int32, conn *net.Conn)
}

type GlobalChat added in v1.18.1

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

func NewGlobalChat added in v1.18.1

func NewGlobalChat() *GlobalChat

func (*GlobalChat) AddPlayer added in v1.18.1

func (g *GlobalChat) AddPlayer(player *Player)

func (*GlobalChat) Init added in v1.18.1

func (g *GlobalChat) Init(game *Game)

func (*GlobalChat) RemovePlayer added in v1.18.1

func (g *GlobalChat) RemovePlayer(p *Player)

func (*GlobalChat) Run added in v1.18.1

func (g *GlobalChat) Run(ctx context.Context)

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

func (k *KeepAlive) AddPlayer(player *Player)

AddPlayer implement Component for KeepAlive

func (*KeepAlive) AddPlayerDelayUpdateHandler added in v1.18.1

func (k *KeepAlive) AddPlayerDelayUpdateHandler(f func(p *Player, delay time.Duration))

func (*KeepAlive) Init added in v1.18.1

func (k *KeepAlive) Init(g *Game)

Init implement Component for KeepAlive

func (*KeepAlive) RemovePlayer added in v1.18.1

func (k *KeepAlive) RemovePlayer(p *Player)

RemovePlayer implement Component for KeepAlive

func (*KeepAlive) Run added in v1.18.1

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

Run implement Component for KeepAlive

type Level added in v1.18.1

type Level interface {
	Init(g *Game)
	Info() LevelInfo
	PlayerJoin(p *Player)
	PlayerQuit(p *Player)
}

type LevelInfo added in v1.18.1

type LevelInfo struct {
	Name       string
	HashedSeed uint64
}

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

type LoginHandler

type LoginHandler interface {
	AcceptLogin(conn *net.Conn, protocol int32) (name string, id uuid.UUID, 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

	// 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 (*MojangLoginHandler) AcceptLogin

func (d *MojangLoginHandler) AcceptLogin(conn *net.Conn, protocol int32) (name string, id uuid.UUID, err error)

AcceptLogin implement LoginHandler for MojangLoginHandler

type Packet757 added in v1.18.1

type Packet757 pk.Packet

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

type PacketHandler struct {
	ID int32
	F  packetHandlerFunc
}

type PacketQueue added in v1.18.1

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

func NewPacketQueue added in v1.18.1

func NewPacketQueue() (p *PacketQueue)

func (*PacketQueue) Close added in v1.18.1

func (p *PacketQueue) Close()

func (*PacketQueue) Pull added in v1.18.1

func (p *PacketQueue) Pull() (packet pk.Packet, ok bool)

func (*PacketQueue) Push added in v1.18.1

func (p *PacketQueue) Push(packet pk.Packet)

type PingInfo added in v1.18.1

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

PingInfo implement ListPingHandler.

func NewPingInfo added in v1.18.1

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

NewPingInfo crate a new PingInfo, the icon can be nil.

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() int

type Player added in v1.18.1

type Player struct {
	*net.Conn

	Name string
	uuid.UUID
	EntityID int32
	Gamemode byte
	// contains filtered or unexported fields
}

func NewPlayer added in v1.18.2

func NewPlayer(conn *net.Conn, name string, id uuid.UUID, eid int32, gamemode byte) (p *Player)

func (*Player) Close added in v1.18.2

func (p *Player) Close()

func (*Player) GetErr added in v1.18.1

func (p *Player) GetErr() error

func (*Player) PutErr added in v1.18.1

func (p *Player) PutErr(err error)

func (*Player) WritePacket added in v1.18.1

func (p *Player) WritePacket(packet Packet758)

WritePacket to player client. The type of parameter will update per version.

type PlayerDelaySource added in v1.18.2

type PlayerDelaySource interface {
	AddPlayerDelayUpdateHandler(f func(p *Player, delay time.Duration))
}

type PlayerInfo added in v1.18.2

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

func NewPlayerInfo added in v1.18.2

func NewPlayerInfo(updateFreq time.Duration, delaySource PlayerDelaySource) *PlayerInfo

func (*PlayerInfo) AddPlayer added in v1.18.2

func (p *PlayerInfo) AddPlayer(player *Player)

func (*PlayerInfo) Init added in v1.18.2

func (p *PlayerInfo) Init(*Game)

func (*PlayerInfo) RemovePlayer added in v1.18.2

func (p *PlayerInfo) RemovePlayer(player *Player)

func (*PlayerInfo) Run added in v1.18.2

func (p *PlayerInfo) Run(ctx context.Context)

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

func (p *PlayerList) AddPlayer(player *Player)

AddPlayer implement Component for PlayerList

func (*PlayerList) CheckPlayer added in v1.18.1

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

CheckPlayer implement LoginChecker for PlayerList

func (*PlayerList) Init added in v1.18.1

func (p *PlayerList) Init(*Game)

Init implement Component for PlayerList

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

func (p *PlayerList) RemovePlayer(player *Player)

RemovePlayer implement Component for PlayerList

func (*PlayerList) Run added in v1.18.1

func (p *PlayerList) Run(context.Context)

Run implement Component for PlayerList

type PlayerSample

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

type Server

type Server struct {
	ListPingHandler
	LoginHandler
	GamePlay
}

func (*Server) Listen

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

type SimpleDim added in v1.18.1

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

func NewSimpleDim added in v1.18.1

func NewSimpleDim(secs int) *SimpleDim

func (*SimpleDim) Info added in v1.18.1

func (s *SimpleDim) Info() LevelInfo

func (*SimpleDim) Init added in v1.18.2

func (s *SimpleDim) Init(*Game)

func (*SimpleDim) LoadChunk added in v1.18.1

func (s *SimpleDim) LoadChunk(pos level.ChunkPos, c *level.Chunk)

func (*SimpleDim) PlayerJoin added in v1.18.1

func (s *SimpleDim) PlayerJoin(p *Player)

func (*SimpleDim) PlayerQuit added in v1.18.1

func (s *SimpleDim) PlayerQuit(*Player)

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