bot

package
v1.16.5-pre2-vacpp Latest Latest
Warning

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

Go to latest
Published: Apr 26, 2021 License: MIT Imports: 23 Imported by: 0

Documentation

Overview

Package bot implements a simple Minecraft client that can join a server or just ping it for getting information.

Runnable example could be found at examples/ .

Index

Examples

Constants

View Source
const DefaultPort = 25565
View Source
const ProtocolVersion = 754

ProtocolVersion , the protocol version number of minecraft net protocol

Variables

This section is empty.

Functions

func PingAndList

func PingAndList(addr string) ([]byte, time.Duration, error)

PingAndList check server status and list online player. Returns a JSON data with server status, and the delay.

For more information for JSON format, see https://wiki.vg/Server_List_Ping#Response

Example
resp, delay, err := PingAndList("localhost:25565")
if err != nil {
	log.Fatalf("ping and list server fail: %v", err)
}

log.Println("Status:", string(resp))
log.Println("Delay:", delay)
Output:

func PingAndListTimeout

func PingAndListTimeout(addr string, timeout time.Duration) ([]byte, time.Duration, error)

PingAndListTimeout PingAndLIstTimeout is the version of PingAndList with max request time.

Types

type Auth

type Auth struct {
	Name string
	UUID string
	AsTk string
}

Auth includes a account

type Client

type Client struct {
	Conn *net.Conn
	Auth Auth

	Name string
	UUID uuid.UUID

	Events Events
}

Client is used to access Minecraft server

func NewClient

func NewClient() *Client

NewClient init and return a new Client.

A new Client has default name "Steve" and zero UUID. It is usable for an offline-mode game.

For online-mode, you need login your Mojang account and load your Name, UUID and AccessToken to client.

func (*Client) Close

func (c *Client) Close() error

func (*Client) HandleGame

func (c *Client) HandleGame() error

HandleGame receive server packet and response them correctly. Note that HandleGame will block if you don't receive from Events.

func (*Client) JoinServer

func (c *Client) JoinServer(addr string, protVer uint64) (err error)

JoinServer connect a Minecraft server for playing the game. Using roughly the same way to parse address as minecraft.

Example (Offline)
c := NewClient()
c.Auth.Name = "Tnze" // set it's name before login.

id := offline.NameToUUID(c.Auth.Name) // optional, get uuid of offline mode game
c.Auth.UUID = hex.EncodeToString(id[:])

//Login
err := c.JoinServer("127.0.0.1")
if err != nil {
	log.Fatal(err)
}
log.Println("Login success")

// Register event handlers
// 	c.Events.GameStart = onGameStartFunc
// 	c.Events.ChatMsg = onChatMsgFunc
// 	c.Events.Disconnect = onDisconnectFunc
//	...

//JoinGame
err = c.HandleGame()
if err != nil {
	log.Fatal(err)
}
Output:

Example (Online)
c := NewClient()

//Login Mojang account to get AccessToken
auth, err := yggdrasil.Authenticate("Your E-mail", "Your Password")
if err != nil {
	panic(err)
}

c.Auth.UUID, c.Auth.Name = auth.SelectedProfile()
c.Auth.AsTk = auth.AccessToken()

//Connect server
err = c.JoinServer("127.0.0.1")
if err != nil {
	log.Fatal(err)
}
log.Println("Login success")

// Register event handlers
// 	c.Events.GameStart = onGameStartFunc
// 	c.Events.ChatMsg = onChatMsgFunc
// 	c.Events.Disconnect = onDisconnectFunc
//	...

//Join the game
err = c.HandleGame()
if err != nil {
	log.Fatal(err)
}
Output:

func (*Client) JoinServerWithDialer

func (c *Client) JoinServerWithDialer(d *net.Dialer, addr string, protVer uint64) (err error)

JoinServerWithDialer is similar to JoinServer but using a Dialer.

type DisconnectErr

type DisconnectErr chat.Message

func (DisconnectErr) Error

func (d DisconnectErr) Error() string

type Events

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

func (*Events) AddGeneric

func (e *Events) AddGeneric(listeners ...PacketHandler)

AddGeneric adds listeners like AddListener, but the packet ID is ignored. Generic listener is always called before specific packet listener.

func (*Events) AddListener

func (e *Events) AddListener(listeners ...PacketHandler)

type LoginErr

type LoginErr struct {
	Stage string
	Err   error
}

func (LoginErr) Error

func (l LoginErr) Error() string

func (LoginErr) Unwrap

func (l LoginErr) Unwrap() error

type PacketHandler

type PacketHandler struct {
	ID       int32
	Priority int
	F        func(p pk.Packet) error
}

type PacketHandlerError

type PacketHandlerError struct {
	ID  int32
	Err error
}

func (PacketHandlerError) Error

func (d PacketHandlerError) Error() string

func (PacketHandlerError) Unwrap

func (d PacketHandlerError) Unwrap() error

type PacketHandlerFunc

type PacketHandlerFunc func(p pk.Packet) error

type Position

type Position struct {
	X, Y, Z int
}

Position is a 3D vector.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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