bot

package
v1.20.2 Latest Latest
Warning

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

Go to latest
Published: Aug 8, 2023 License: MIT Imports: 28 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 (
	ProtocolVersion = 762
	DefaultPort     = mcnet.DefaultPort
)

ProtocolVersion is 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 PingAndListContext

func PingAndListContext(ctx context.Context, addr string) ([]byte, time.Duration, error)

func PingAndListTimeout

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

PingAndListTimeout is the version of PingAndList with max request time.

Types

type Auth

type Auth struct {
	Name string
	UUID string
	AsTk string
}

Auth includes an account

type Client

type Client struct {
	Conn *Conn
	Auth Auth

	Name string
	UUID uuid.UUID

	Events      Events
	LoginPlugin map[string]func(data []byte) ([]byte, error)
}

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) (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 its 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.AddListener(...)

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

Example (Online)
c := NewClient()

// Login Mojang account to get AccessToken
// To use Microsoft Account, see issue #106
// https://github.com/Tnze/go-mc/issues/106
auth, err := yggdrasil.Authenticate("Your E-mail", "Your Password")
if err != nil {
	panic(err)
}

// As long as you set these three fields correctly,
// the client can connect to the online-mode server
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(dialer *net.Dialer, addr string) (err error)

JoinServerWithDialer is similar to JoinServer but using a net.Dialer.

func (*Client) JoinServerWithOptions

func (c *Client) JoinServerWithOptions(addr string, options JoinOptions) (err error)

type Conn

type Conn struct {
	*net.Conn
	// contains filtered or unexported fields
}

Conn is a concurrently-safe warpper of net.Conn with packet queue. Note that not all methods are concurrently-safe.

func (*Conn) Close

func (c *Conn) Close() error

func (*Conn) ReadPacket

func (c *Conn) ReadPacket(p *pk.Packet) error

func (*Conn) WritePacket

func (c *Conn) WritePacket(p pk.Packet) error

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 JoinOptions

type JoinOptions struct {
	MCDialer mcnet.MCDialer
	Context  context.Context

	// Indicate not to fetch and sending player's PubKey
	NoPublicKey bool

	// Specify the player PubKey to use.
	// If nil, it will be obtained from Mojang when joining
	KeyPair *user.KeyPairResp
}

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       packetid.ClientboundPacketID
	Priority int
	F        func(p pk.Packet) error
}

type PacketHandlerError

type PacketHandlerError struct {
	ID  packetid.ClientboundPacketID
	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
Package basic provides some basic packet handler which client needs.
Package basic provides some basic packet handler which client needs.
Package playerlist contains a PlayerList struct that used to manage player information.
Package playerlist contains a PlayerList struct that used to manage player information.

Jump to

Keyboard shortcuts

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