mcrpc

package module
v0.0.0-...-3fe01af Latest Latest
Warning

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

Go to latest
Published: Mar 25, 2026 License: MIT Imports: 11 Imported by: 0

README

MCRPC - Minecraft Server Management Protocol Client

CI Go Report Card GoDoc

A Go client library for the Minecraft Server Management Protocol (MSMP), providing JSON-RPC 2.0 over WebSocket support for managing Minecraft Java Edition dedicated servers.

[!Warning]

Part of this project is handled by the AI Agent. (Using OpenCode)

Most code structure is mostly handled by myself. Only repetitive tasks, tests, and long codes are written by the AI.

While I try my best to make non fully vibe-coded library, there may some errors occur.

Features

  • Full Protocol Support: Complete implementation of all MSMP methods
  • Secure Connections: TLS support with client certificates
  • Event Notifications: Real-time event handling for player joins, server status, etc.
  • Type-Safe API: Strongly typed request/response structures
  • Concurrent-Safe: Thread-safe operations for multi-goroutine use

Installation

go get github.com/fi-xz/mcrpc

Quick Start

package main

import (
    "context"
    "crypto/tls"
    "fmt"
    "log"

    "github.com/fi-xz/mcrpc"
)

func main() {
    ctx := context.Background()

    // Load certificate (if using TLS)
    cert, err := tls.LoadX509KeyPair("cert.crt", "cert.pem")
    if err != nil {
        log.Fatal(err)
    }

    // Create client
    client, err := mcrpc.CreateWithTLS(ctx, "localhost", 8080, "your-secret", &cert, true)
    if err != nil {
        log.Fatal(err)
    }
    defer client.Close()

    // Get server status
    status, err := client.GetServerStatus(ctx)
    if err != nil {
        log.Fatal(err)
    }

    fmt.Printf("Server: %s, Players: %d\n", status.Version.Name, len(status.Players))
}

API Overview

Server Management
  • GetServerStatus() - Get current server state
  • SaveServer(ctx, flush) - Save world data
  • StopServer(ctx) - Stop the server
  • SendSystemMessage(ctx, message) - Broadcast messages
Player Management
  • GetPlayers(ctx) - List online players
  • KickPlayers(ctx, players) - Kick players from server
Access Control
  • Allowlist: GetAllowlist, SetAllowlist, AddAllowlist, RemoveAllowlist, ClearAllowlist
  • Bans: GetBanlist, SetBanlist, AddBanlist, RemoveBanlist, ClearBanlist
  • IP Bans: GetIPBanlist, SetIPBanlist, AddIPBanlist, RemoveIPBanlist, ClearIPBanlist
  • Operators: GetOperators, SetOperators, AddOperators, RemoveOperators, ClearOperators
Server Settings

All settings support both Get and Set methods:

  • Autosave, Difficulty, MaxPlayers, MOTD
  • GameMode, ViewDistance, SimulationDistance
  • AllowFlight, Allowlist, ForceGameMode
  • SpawnProtectionRadius, PlayerIdleTimeout
  • AcceptTransfers, StatusReplies, and more
Game Rules
  • GetGameRules(ctx) - Get all game rules
  • UpdateGameRule(ctx, rule) - Update a game rule
Notifications

Register handlers for server events:

client.OnPlayerJoined = func(player mcrpc.Player) {
    log.Printf("Player joined: %s", player.Name)
}

client.OnServerStatus = func(status mcrpc.ServerState) {
    log.Printf("Players online: %d", len(status.Players))
}

Available handlers:

  • Server: OnServerStarted, OnServerStopping, OnServerSaving, OnServerSaved, OnServerStatus, OnServerActivity
  • Players: OnPlayerJoined, OnPlayerLeft
  • Operators: OnOperatorAdded, OnOperatorRemoved
  • Allowlist: OnAllowlistAdded, OnAllowlistRemoved
  • Bans: OnBanAdded, OnBanRemoved
  • IP Bans: OnIPBanAdded, OnIPBanRemoved
  • Game Rules: OnGameruleUpdated

Configuration

Enable management protocol on your server

Modify server.properties:

management-server-enabled=true
management-server-host=0.0.0.0
management-server-port=8080
management-server-secret=your-40-character-secret-here
management-server-tls-enabled=true
management-server-tls-keystore=keystore.p12
management-server-tls-keystore-password=your-password
TLS Certificate

Generate a PKCS12 keystore:

keytool -genkeypair -alias mcrpc -keyalg RSA -keysize 2048 \
  -storetype PKCS12 -keystore keystore.p12 -validity 3650

Testing

Run the test suite:

# Run all tests
go test -v ./...

# Run with race detector
go test -race ./...

# Run with coverage
go test -cover ./...

Requirements

  • Go 1.21 or later
  • Minecraft Java Edition 1.21.9+ with management protocol enabled

License

MIT License - see LICENSE for details.

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

Resources

Documentation

Overview

Package mcrpc provides allowlist management functionality for Minecraft servers.

Package mcrpc provides ban management functionality for Minecraft servers.

Package mcrpc provides game rule management functionality for Minecraft servers.

Package mcrpc provides a JSON-RPC 2.0 over WebSocket client for managing Minecraft Java Edition servers. It implements the Minecraft Server Management Protocol (MSMP) and supports server management, player management, bans, operators, allowlists, server settings, and real-time notifications.

Package mcrpc provides notification handling functionality for Minecraft servers.

Package mcrpc provides operator management functionality for Minecraft servers.

Package mcrpc provides player management functionality for Minecraft servers.

Package mcrpc provides server management functionality for Minecraft servers.

Package mcrpc provides server settings management functionality for Minecraft servers.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type IPBan

type IPBan = types.IPBan

IPBan is an alias for types.IPBan, representing a banned IP address.

type IncomingIPBan

type IncomingIPBan = types.IncomingIPBan

IncomingIPBan is an alias for types.IncomingIPBan, representing an incoming IP ban with player information.

type KickPlayer

type KickPlayer = types.KickPlayer

KickPlayer is an alias for types.KickPlayer, representing a player to be kicked with a message.

type MCRPCClient

type MCRPCClient struct {
	// JSONRPCConn is the underlying JSON-RPC 2.0 connection.
	JSONRPCConn *jsonrpc2.Conn
	// WebsocketConn is the underlying WebSocket connection.
	WebsocketConn *websocket.Conn

	// OnNotification is called for all notifications when no specific handler is set.
	OnNotification func(method string, params json.RawMessage)

	// Server notification handlers
	OnServerStarted  func()
	OnServerStopping func()
	OnServerSaving   func()
	OnServerSaved    func()
	OnServerStatus   func(status ServerState)
	OnServerActivity func()

	// Player notification handlers
	OnPlayerJoined func(player Player)
	OnPlayerLeft   func(player Player)

	// Operator notification handlers
	OnOperatorAdded   func(operator Operator)
	OnOperatorRemoved func(operator Operator)

	// Allowlist notification handlers
	OnAllowlistAdded   func(player Player)
	OnAllowlistRemoved func(player Player)

	// Ban notification handlers
	OnBanAdded   func(ban UserBan)
	OnBanRemoved func(player Player)

	// IP Ban notification handlers
	OnIPBanAdded   func(ban IPBan)
	OnIPBanRemoved func(ip string)

	// Gamerule notification handlers
	OnGameruleUpdated func(gamerule TypedGameRule)
	// contains filtered or unexported fields
}

MCRPCClient is a client for connecting to and managing Minecraft servers via JSON-RPC 2.0 over WebSocket. It provides methods for server management, player management, and real-time notifications.

func Create

func Create(context context.Context, host string, port int, secret string) (*MCRPCClient, error)

Create establishes a WebSocket connection to the Minecraft server without TLS. The secret is used for authentication with the server.

func CreateWithTLS

func CreateWithTLS(context context.Context, host string, port int, secret string, cert *tls.Certificate, insecure bool) (*MCRPCClient, error)

CreateWithTLS establishes a WebSocket connection to the Minecraft server with TLS. The cert parameter is used for client certificate authentication. If insecure is true, the server's certificate will not be verified.

func (*MCRPCClient) AddAllowlist

func (c *MCRPCClient) AddAllowlist(context context.Context, add []Player) ([]Player, error)

AddAllowlist adds the specified players to the allowlist.

func (*MCRPCClient) AddBanlist

func (c *MCRPCClient) AddBanlist(context context.Context, add []UserBan) ([]UserBan, error)

AddBanlist adds the specified bans to the ban list.

func (*MCRPCClient) AddIPBanlist

func (c *MCRPCClient) AddIPBanlist(context context.Context, add []IncomingIPBan) ([]IPBan, error)

AddIPBanlist adds the specified IP bans to the ban list.

func (*MCRPCClient) AddOperators

func (c *MCRPCClient) AddOperators(context context.Context, add []Operator) ([]Operator, error)

AddOperators adds the specified players as operators.

func (*MCRPCClient) ClearAllowlist

func (c *MCRPCClient) ClearAllowlist(context context.Context) ([]Player, error)

ClearAllowlist removes all players from the allowlist.

func (*MCRPCClient) ClearBanlist

func (c *MCRPCClient) ClearBanlist(context context.Context) ([]UserBan, error)

ClearBanlist removes all bans from the ban list.

func (*MCRPCClient) ClearIPBanlist

func (c *MCRPCClient) ClearIPBanlist(context context.Context) ([]IPBan, error)

ClearIPBanlist removes all IP bans from the ban list.

func (*MCRPCClient) ClearOperators

func (c *MCRPCClient) ClearOperators(context context.Context) ([]Operator, error)

ClearOperators removes all players from the operator list.

func (*MCRPCClient) Close

func (c *MCRPCClient) Close() error

Close closes the WebSocket connection and the underlying JSON-RPC connection. It is safe to call Close multiple times.

func (*MCRPCClient) DisconnectNotify

func (c *MCRPCClient) DisconnectNotify() <-chan struct{}

DisconnectNotify returns a channel that is closed when the connection is closed.

func (*MCRPCClient) GetAcceptTransfers

func (c *MCRPCClient) GetAcceptTransfers(context context.Context) (bool, error)

GetAcceptTransfers retrieves whether the server accepts player transfers.

func (*MCRPCClient) GetAllowFlight

func (c *MCRPCClient) GetAllowFlight(context context.Context) (bool, error)

GetAllowFlight retrieves whether flight is allowed in Survival mode.

func (*MCRPCClient) GetAllowlist

func (c *MCRPCClient) GetAllowlist(context context.Context) ([]Player, error)

GetAllowlist retrieves the current allowlist of players.

func (*MCRPCClient) GetAutosaveEnabled

func (c *MCRPCClient) GetAutosaveEnabled(context context.Context) (bool, error)

GetAutosaveEnabled retrieves whether automatic world saving is enabled.

func (*MCRPCClient) GetBanlist

func (c *MCRPCClient) GetBanlist(context context.Context) ([]UserBan, error)

GetBanlist retrieves the current list of banned players.

func (*MCRPCClient) GetDifficulty

func (c *MCRPCClient) GetDifficulty(context context.Context) (string, error)

GetDifficulty retrieves the current difficulty level of the server.

func (*MCRPCClient) GetEnforceAllowlist

func (c *MCRPCClient) GetEnforceAllowlist(context context.Context) (bool, error)

GetEnforceAllowlist retrieves whether allowlist enforcement is enabled.

func (*MCRPCClient) GetEntityBroadcastRange

func (c *MCRPCClient) GetEntityBroadcastRange(context context.Context) (int, error)

GetEntityBroadcastRange retrieves the entity broadcast range percentage.

func (*MCRPCClient) GetForceGameMode

func (c *MCRPCClient) GetForceGameMode(context context.Context) (bool, error)

GetForceGameMode retrieves whether players are forced to use the default game mode.

func (*MCRPCClient) GetGameMode

func (c *MCRPCClient) GetGameMode(context context.Context) (string, error)

GetGameMode retrieves the server's default game mode.

func (*MCRPCClient) GetGameRules

func (c *MCRPCClient) GetGameRules(context context.Context) ([]TypedGameRule, error)

GetGameRules retrieves all game rules and their current values.

func (*MCRPCClient) GetHideOnlinePlayers

func (c *MCRPCClient) GetHideOnlinePlayers(context context.Context) (bool, error)

GetHideOnlinePlayers retrieves whether online player info is hidden from status queries.

func (*MCRPCClient) GetIPBanlist

func (c *MCRPCClient) GetIPBanlist(context context.Context) ([]IPBan, error)

GetIPBanlist retrieves the current list of banned IP addresses.

func (*MCRPCClient) GetMOTD

func (c *MCRPCClient) GetMOTD(context context.Context) (string, error)

GetMOTD retrieves the server's message of the day.

func (*MCRPCClient) GetMaxPlayers

func (c *MCRPCClient) GetMaxPlayers(context context.Context) (int, error)

GetMaxPlayers retrieves the maximum number of players allowed on the server.

func (*MCRPCClient) GetOperatorPermissionLevel

func (c *MCRPCClient) GetOperatorPermissionLevel(context context.Context) (int, error)

GetOperatorPermissionLevel retrieves the operator permission level.

func (*MCRPCClient) GetOperators

func (c *MCRPCClient) GetOperators(context context.Context) ([]Operator, error)

GetOperators retrieves the current list of operators.

func (*MCRPCClient) GetPauseWhenEmptySeconds

func (c *MCRPCClient) GetPauseWhenEmptySeconds(context context.Context) (int, error)

GetPauseWhenEmptySeconds retrieves the pause when empty timeout in seconds.

func (*MCRPCClient) GetPlayerIdleTimeout

func (c *MCRPCClient) GetPlayerIdleTimeout(context context.Context) (int, error)

GetPlayerIdleTimeout retrieves the player idle timeout in seconds.

func (*MCRPCClient) GetPlayers

func (c *MCRPCClient) GetPlayers(context context.Context) ([]Player, error)

GetPlayers retrieves the list of currently online players.

func (*MCRPCClient) GetServerStatus

func (c *MCRPCClient) GetServerStatus(context context.Context) (ServerState, error)

GetServerStatus retrieves the current status of the server including online players and version information.

func (*MCRPCClient) GetSimulationDistance

func (c *MCRPCClient) GetSimulationDistance(context context.Context) (int, error)

GetSimulationDistance retrieves the simulation distance in chunks.

func (*MCRPCClient) GetSpawnProtectionRadius

func (c *MCRPCClient) GetSpawnProtectionRadius(context context.Context) (int, error)

GetSpawnProtectionRadius retrieves the spawn protection radius in blocks.

func (*MCRPCClient) GetStatusHeartbeatInterval

func (c *MCRPCClient) GetStatusHeartbeatInterval(context context.Context) (int, error)

GetStatusHeartbeatInterval retrieves the status heartbeat interval in seconds.

func (*MCRPCClient) GetStatusReplies

func (c *MCRPCClient) GetStatusReplies(context context.Context) (bool, error)

GetStatusReplies retrieves whether the server responds to status requests.

func (*MCRPCClient) GetUseAllowlist

func (c *MCRPCClient) GetUseAllowlist(context context.Context) (bool, error)

GetUseAllowlist retrieves whether the allowlist is enabled.

func (*MCRPCClient) GetViewDistance

func (c *MCRPCClient) GetViewDistance(context context.Context) (int, error)

GetViewDistance retrieves the view distance in chunks.

func (*MCRPCClient) IsClosed

func (c *MCRPCClient) IsClosed() bool

IsClosed returns true if the connection has been closed.

func (*MCRPCClient) KickPlayers

func (c *MCRPCClient) KickPlayers(context context.Context, kick []KickPlayer) ([]Player, error)

KickPlayers kicks the specified players from the server with custom messages.

func (*MCRPCClient) RemoveAllowlist

func (c *MCRPCClient) RemoveAllowlist(context context.Context, remove []Player) ([]Player, error)

RemoveAllowlist removes the specified players from the allowlist.

func (*MCRPCClient) RemoveBanlist

func (c *MCRPCClient) RemoveBanlist(context context.Context, remove []Player) ([]UserBan, error)

RemoveBanlist removes the specified players from the ban list.

func (*MCRPCClient) RemoveIPBanlist

func (c *MCRPCClient) RemoveIPBanlist(context context.Context, ip []string) ([]IPBan, error)

RemoveIPBanlist removes the specified IP addresses from the ban list.

func (*MCRPCClient) RemoveOperators

func (c *MCRPCClient) RemoveOperators(context context.Context, remove []Player) ([]Operator, error)

RemoveOperators removes the specified players from the operator list.

func (*MCRPCClient) SaveServer

func (c *MCRPCClient) SaveServer(context context.Context, flush bool) (bool, error)

SaveServer saves the current server state. If flush is true, all pending changes are flushed to disk.

func (*MCRPCClient) SendSystemMessage

func (c *MCRPCClient) SendSystemMessage(context context.Context, message types.SystemMessage) (bool, error)

SendSystemMessage sends a system message to the specified players on the server.

func (*MCRPCClient) SetAcceptTransfers

func (c *MCRPCClient) SetAcceptTransfers(context context.Context, accept bool) (bool, error)

SetAcceptTransfers sets whether the server accepts player transfers.

func (*MCRPCClient) SetAllowFlight

func (c *MCRPCClient) SetAllowFlight(context context.Context, allowed bool) (bool, error)

SetAllowFlight sets whether flight is allowed in Survival mode.

func (*MCRPCClient) SetAllowlist

func (c *MCRPCClient) SetAllowlist(context context.Context, players []Player) ([]Player, error)

SetAllowlist sets the allowlist to the specified list of players, replacing the existing list.

func (*MCRPCClient) SetAutosaveEnabled

func (c *MCRPCClient) SetAutosaveEnabled(context context.Context, enable bool) (bool, error)

SetAutosaveEnabled enables or disables automatic world saving.

func (*MCRPCClient) SetBanlist

func (c *MCRPCClient) SetBanlist(context context.Context, bans []UserBan) ([]UserBan, error)

SetBanlist sets the ban list to the specified list of bans, replacing the existing list.

func (*MCRPCClient) SetDifficulty

func (c *MCRPCClient) SetDifficulty(context context.Context, difficulty string) (string, error)

SetDifficulty sets the difficulty level of the server (peaceful, easy, normal, hard).

func (*MCRPCClient) SetEnforceAllowlist

func (c *MCRPCClient) SetEnforceAllowlist(context context.Context, enforce bool) (bool, error)

SetEnforceAllowlist enables or disables allowlist enforcement.

func (*MCRPCClient) SetEntityBroadcastRange

func (c *MCRPCClient) SetEntityBroadcastRange(context context.Context, percentagePoints int) (int, error)

SetEntityBroadcastRange sets the entity broadcast range percentage.

func (*MCRPCClient) SetForceGameMode

func (c *MCRPCClient) SetForceGameMode(context context.Context, force bool) (bool, error)

SetForceGameMode sets whether players are forced to use the default game mode.

func (*MCRPCClient) SetGameMode

func (c *MCRPCClient) SetGameMode(context context.Context, mode string) (string, error)

SetGameMode sets the server's default game mode (creative, survival, spectator, adventure).

func (*MCRPCClient) SetHideOnlinePlayers

func (c *MCRPCClient) SetHideOnlinePlayers(context context.Context, hide bool) (bool, error)

SetHideOnlinePlayers sets whether online player info is hidden from status queries.

func (*MCRPCClient) SetIPBanlist

func (c *MCRPCClient) SetIPBanlist(context context.Context, banlist []IPBan) ([]IPBan, error)

SetIPBanlist sets the IP ban list to the specified list, replacing the existing list.

func (*MCRPCClient) SetMOTD

func (c *MCRPCClient) SetMOTD(context context.Context, message string) (string, error)

SetMOTD sets the server's message of the day.

func (*MCRPCClient) SetMaxPlayers

func (c *MCRPCClient) SetMaxPlayers(context context.Context, max int) (int, error)

SetMaxPlayers sets the maximum number of players allowed on the server.

func (*MCRPCClient) SetOperatorPermissionLevel

func (c *MCRPCClient) SetOperatorPermissionLevel(context context.Context, level int) (int, error)

SetOperatorPermissionLevel sets the operator permission level.

func (*MCRPCClient) SetOperators

func (c *MCRPCClient) SetOperators(context context.Context, operators []Operator) ([]Operator, error)

SetOperators sets the operator list to the specified list, replacing the existing list.

func (*MCRPCClient) SetPauseWhenEmptySeconds

func (c *MCRPCClient) SetPauseWhenEmptySeconds(context context.Context, seconds int) (int, error)

SetPauseWhenEmptySeconds sets the pause when empty timeout in seconds.

func (*MCRPCClient) SetPlayerIdleTimeout

func (c *MCRPCClient) SetPlayerIdleTimeout(context context.Context, seconds int) (int, error)

SetPlayerIdleTimeout sets the player idle timeout in seconds.

func (*MCRPCClient) SetSimulationDistance

func (c *MCRPCClient) SetSimulationDistance(context context.Context, distance int) (int, error)

SetSimulationDistance sets the simulation distance in chunks.

func (*MCRPCClient) SetSpawnProtectionRadius

func (c *MCRPCClient) SetSpawnProtectionRadius(context context.Context, radius int) (int, error)

SetSpawnProtectionRadius sets the spawn protection radius in blocks.

func (*MCRPCClient) SetStatusHeartbeatInterval

func (c *MCRPCClient) SetStatusHeartbeatInterval(context context.Context, seconds int) (int, error)

SetStatusHeartbeatInterval sets the status heartbeat interval in seconds.

func (*MCRPCClient) SetStatusReplies

func (c *MCRPCClient) SetStatusReplies(context context.Context, enable bool) (bool, error)

SetStatusReplies sets whether the server responds to status requests.

func (*MCRPCClient) SetUseAllowlist

func (c *MCRPCClient) SetUseAllowlist(context context.Context, use bool) (bool, error)

SetUseAllowlist enables or disables the allowlist.

func (*MCRPCClient) SetViewDistance

func (c *MCRPCClient) SetViewDistance(context context.Context, distance int) (int, error)

SetViewDistance sets the view distance in chunks.

func (*MCRPCClient) StopServer

func (c *MCRPCClient) StopServer(context context.Context) (bool, error)

StopServer stops the Minecraft server.

func (*MCRPCClient) UpdateGameRule

func (c *MCRPCClient) UpdateGameRule(context context.Context, gamerule types.UntypedGameRule) (TypedGameRule, error)

UpdateGameRule updates the value of a specific game rule.

type Operator

type Operator = types.Operator

Operator is an alias for types.Operator, representing a server operator.

type Player

type Player = types.Player

Player is an alias for types.Player, representing a Minecraft player.

type ServerState

type ServerState = types.ServerState

ServerState is an alias for types.ServerState, representing the current state of the server.

type TypedGameRule

type TypedGameRule = types.TypedGameRule

TypedGameRule is an alias for types.TypedGameRule, representing a game rule with a typed value.

type UserBan

type UserBan = types.UserBan

UserBan is an alias for types.UserBan, representing a banned player.

Directories

Path Synopsis
internal
protocol
Package protocol contains request parameter types for Minecraft Server Management Protocol methods.
Package protocol contains request parameter types for Minecraft Server Management Protocol methods.
types
Package types contains data types used by the Minecraft Server Management Protocol.
Package types contains data types used by the Minecraft Server Management Protocol.

Jump to

Keyboard shortcuts

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