mcr

package module
v0.1.5 Latest Latest
Warning

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

Go to latest
Published: Aug 20, 2024 License: MIT Imports: 5 Imported by: 3

README

mcr

Dependency-free Minecraft remote console client written in Golang

usage

import (
	"log"
	"os"

	"github.com/jake-young-dev/mcr"
)

func main() {
	//create new client
	client := mcr.NewClient(os.Getenv("rcon_address"))

	//connect to server and authenticate with password
	err := client.Connect(os.Getenv("rcon_password"))
	if err != nil {
		panic(err)
	}
	defer client.Close() //always call close to clean up your connections

	response, err := client.Command("list") //run "list" command on minecraft server
	if err != nil {
		panic(err)
	}

	log.Println(response)
}

notes

  • To prevent using connections prematurely the client does not connect to the server on creation, the Connect method must be called
  • To cleanup connections after use call the Close method, it is recommended to defer the Close after the call to Connect
  • The client can be reused after closing by calling the Connect method again

Documentation

Index

Constants

View Source
const (
	//type value for failed authentication requests
	FailureType = int32(-1)

	//type value for command requests
	CommandType //2
	//type value for authentication requests
	AuthenticationType //3

	//tcp constants
	Protocol = "tcp"
	Timeout  = time.Second * 10

	//request id reset value
	ResetID        = 1
	HeaderSize     = 8
	HeaderSizeWPad = 10
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Client

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

minecraft remote console client

func NewClient

func NewClient(addr string) *Client

creates and returns a new remote console client using the supplied address (addr). The Connect method must be called before the client can be used to send commands

func (*Client) Close

func (c *Client) Close() error

closes remote console connection, nil's out the server value in client struct, and resets the request id. The remote console client can be reused by calling the Connect method again

func (*Client) Command

func (c *Client) Command(cmd string) (string, error)

sends a command to the minecraft server and returns the server response, an error is returned if the client has not connected to the server before attempting to send a command. Command examples can be found on the minecraft wiki: https://minecraft.wiki/w/Commands

func (*Client) Connect

func (c *Client) Connect(password string) error

connects to minecraft server and authenticates the client. Ensure to call or defer the call to the Close method to clean up the connection

type IClient

type IClient interface {
	Connect(password string) error
	Command(cmd string) (string, error)
	Close() error
	// contains filtered or unexported methods
}

Jump to

Keyboard shortcuts

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