rcon

package module
v0.7.0 Latest Latest
Warning

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

Go to latest
Published: Dec 4, 2022 License: MIT Imports: 11 Imported by: 0

README

Go RCON

A Minecraft RCON client

Getting Started

Use go get to install the library

go get github.com/Aton-Kish/gorcon

Import in your application

import (
	rcon "github.com/Aton-Kish/gorcon"
)

Usage

package main

import (
	"fmt"
	"log"

	rcon "github.com/Aton-Kish/gorcon"
)

func main() {
	conn, err := rcon.Dial("localhost:25575", "minecraft")
	if err != nil {
		log.Fatal(err)
	}
	defer conn.Close()

	// NOTE: `/player` is a carpet mod command
	res, err := conn.Command("/player jeb_ spawn")
	if err != nil {
		log.Fatal(err)
	}

	res, err = conn.Command("/give jeb_ minecraft:dirt 1")
	if err != nil {
		log.Fatal(err)
	}

	// NOTE: `/player` is a carpet mod command
	res, err = conn.Command("/player jeb_ kill")
	if err != nil {
		log.Fatal(err)
	}

	fmt.Println(res)
}

Development

doc
: install godoc
make install

: run godoc server
make doc

: uninstall godoc
make uninstall
unit test
make unittest
E2E test
: start minecraft docker container
: it takes a few minutes
make start

: dump packets for debug
make tcpdump

: E2E test
make e2etest

: stop minecraft docker container
make stop
Clean
make clean

License

This library is licensed under the MIT License, see LICENSE.

Documentation

Overview

Example (Command)
package main

import (
	"fmt"
	"log"

	rcon "github.com/Aton-Kish/gorcon"
)

func main() {
	conn, err := rcon.Dial("localhost:25575", "minecraft")
	if err != nil {
		log.Fatal(err)
	}
	defer conn.Close()

	// NOTE: `/player` is a carpet mod command
	res, err := conn.Command("/player jeb_ spawn")
	if err != nil {
		log.Fatal(err)
	}

	res, err = conn.Command("/give jeb_ minecraft:dirt 1")
	if err != nil {
		log.Fatal(err)
	}

	// NOTE: `/player` is a carpet mod command
	res, err = conn.Command("/player jeb_ kill")
	if err != nil {
		log.Fatal(err)
	}

	fmt.Println(res)
}
Output:

Example (NbtData)
package main

import (
	"fmt"
	"log"
	"regexp"

	nbt "github.com/Aton-Kish/gonbt"
	rcon "github.com/Aton-Kish/gorcon"
)

func main() {
	conn, err := rcon.Dial("localhost:25575", "minecraft")
	if err != nil {
		log.Fatal(err)
	}
	defer conn.Close()

	// NOTE: uuid `853c80ef-3c37-49fd-aa49-938b674adae6` is jeb_
	res, err := conn.Command("/data get entity 853c80ef-3c37-49fd-aa49-938b674adae6")
	if err != nil {
		log.Fatal(err)
	}

	re := regexp.MustCompile(`^[a-zA-Z0-9_]{3,16} has the following entity data: (.*)$`)
	g := re.FindStringSubmatch(res)
	if len(g) < 2 {
		log.Fatal("invalid data")
	}

	// NOTE: parse NBT data
	snbt := g[1]
	dat, err := nbt.Parse(snbt)
	if err != nil {
		log.Fatal(err)
	}

	json := nbt.Json(dat)
	fmt.Println(json)
}
Output:

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func SetLogger added in v0.6.0

func SetLogger(l liblog.Logger)

Types

type PacketError added in v0.4.0

type PacketError struct {
	Op  string
	Err error
}

func (*PacketError) Error added in v0.4.0

func (e *PacketError) Error() string

func (*PacketError) Unwrap added in v0.4.0

func (e *PacketError) Unwrap() error

type RCON added in v0.7.0

type RCON interface {
	net.Conn

	Command(command string) (string, error)
}

func Dial

func Dial(addr string, password string) (RCON, error)
Example
package main

import (
	"fmt"
	"log"

	rcon "github.com/Aton-Kish/gorcon"
)

func main() {
	conn, err := rcon.Dial("localhost:25575", "minecraft")
	if err != nil {
		log.Fatal(err)
	}
	defer conn.Close()

	res, err := conn.Command("/seed")
	if err != nil {
		log.Fatal(err)
	}

	fmt.Println(res)
}
Output:

func DialTimeout

func DialTimeout(addr string, password string, timeout time.Duration) (RCON, error)
Example
package main

import (
	"fmt"
	"log"
	"time"

	rcon "github.com/Aton-Kish/gorcon"
)

func main() {
	conn, err := rcon.DialTimeout("localhost:25575", "minecraft", 500*time.Millisecond)
	if err != nil {
		log.Fatal(err)
	}
	defer conn.Close()

	res, err := conn.Command("/seed")
	if err != nil {
		log.Fatal(err)
	}

	fmt.Println(res)
}
Output:

type RCONError added in v0.7.0

type RCONError struct {
	Op  string
	Err error
}

func (*RCONError) Error added in v0.7.0

func (e *RCONError) Error() string

func (*RCONError) Unwrap added in v0.7.0

func (e *RCONError) Unwrap() error

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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