bot

package
v1.15.2 Latest Latest
Warning

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

Go to latest
Published: Oct 3, 2021 License: MIT Imports: 31 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 cmd/ .

Index

Examples

Constants

View Source
const (
	Jacket
	LeftSleeve
	RightSleeve
	LeftPantsLeg
	RightPantsLeg
	Hat
)

Used by Settings.DisplayedSkinParts. For each bits set if shows match part.

View Source
const ProtocolVersion = 754

ProtocolVersion , the protocol version number of minecraft net protocol

Variables

View Source
var DefaultSettings = Settings{
	Locale:             "zh_CN",
	ViewDistance:       15,
	ChatMode:           0,
	DisplayedSkinParts: Jacket | LeftSleeve | RightSleeve | LeftPantsLeg | RightPantsLeg | Hat,
	MainHand:           1,
	ReceiveMap:         true,
	Brand:              "vanilla",
}

DefaultSettings are the default settings of client

Functions

func OfflineUUID

func OfflineUUID(name string) uuid.UUID

OfflineUUID return the UUID from player name in offline mode

Example
fmt.Println(OfflineUUID("Tnze"))
Output:

	c7b9eece-2f2e-325c-8da8-6fc8f3d0edb0

func PingAndList

func PingAndList(addr string, port int, protocolVersion int) ([]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, port int, protocolVersion int, 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 {
	Auth

	player.Player
	PlayInfo
	ServInfo

	Wd      world.World //the map data
	Inputs  path.Inputs
	Physics phy.State

	// Delegate allows you push a function to let HandleGame run.
	// Do not send at the same goroutine!
	Delegate chan func() error
	Events   eventBroker
	// contains filtered or unexported fields
}

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) AttackEntity

func (c *Client) AttackEntity(entityID int32, hand int) error

AttackEntity used by player to left-clicks another entity. The attack version of UseEntity. Has the same limit.

func (*Client) Chat

func (c *Client) Chat(msg string) error

Chat send chat as chat message or command at textbox.

func (*Client) Close

func (c *Client) Close() error

func (*Client) Conn

func (c *Client) Conn() *mcnet.Conn

Conn return the MCConn of the Client. Only used when you want to handle the packets by yourself

func (*Client) Dig

func (c *Client) Dig(status, locX, locY, locZ, face int) error

Dig used to start, end or cancel a digging status is 0 for start digging, 1 for cancel and 2 if client think it done. To digging a block without cancel, use status 0 and 2 once each.

func (*Client) DropItem

func (c *Client) DropItem() error

DropItem drop one item in selected stack

func (*Client) DropItemStack

func (c *Client) DropItemStack() error

DropItemStack drop the entire selected stack

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, port int) (err error)

JoinServer connect a Minecraft server for playing the game.

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

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

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

// Regist 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.Name = auth.SelectedProfile()
c.AsTk = auth.AccessToken()

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

// Regist 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 Dialer, addr string) (err error)

JoinServerWithDialer is similar to JoinServer but using a Dialer.

func (*Client) PickItem

func (c *Client) PickItem(slot int) error

PickItem used to swap out an empty space on the hotbar with the item in the given inventory slot. The Notchain client uses this for pick block functionality (middle click) to retrieve items from the inventory.

The server will first search the player's hotbar for an empty slot, starting from the current slot and looping around to the slot before it. If there are no empty slots, it will start a second search from the current slot and find the first slot that does not contain an enchanted item. If there still are no slots that meet that criteria, then the server will use the currently selected slot. After finding the appropriate slot, the server swaps the items and then change player's selected slot (cause the HeldItemChange event).

func (*Client) PluginMessage

func (c *Client) PluginMessage(channel string, msg []byte) error

PluginMessage is used by mods and plugins to send their data.

func (*Client) Respawn

func (c *Client) Respawn() error

Respawn the player when it was dead.

func (*Client) SelectItem

func (c *Client) SelectItem(slot int) error

SelectItem used to change the slot selection in hotbar. slot should from 0 to 8

func (*Client) SendCloseWindow

func (c *Client) SendCloseWindow(windowID byte) error

func (*Client) SendMessage

func (c *Client) SendMessage(msg string) error

SendMessage sends a chat message.

func (*Client) SendPacket

func (c *Client) SendPacket(packet pk.Packet) error

SendPacket send the packet to server.

func (*Client) SwapItem

func (c *Client) SwapItem() error

SwapItem used to swap the items in hands.

func (*Client) SwingArm

func (c *Client) SwingArm(hand int) error

SwingArm swing player's arm. hand could be one of 0: main hand, 1: off hand. It's just animation.

func (*Client) UseBlock

func (c *Client) UseBlock(hand, locX, locY, locZ, face int, cursorX, cursorY, cursorZ float32, insideBlock bool) error

UseBlock is used to place or use a block. hand is the hand from which the block is placed; 0: main hand, 1: off hand. face is the face on which the block is placed.

Cursor position is the position of the crosshair on the block: cursorX, from 0 to 1 increasing from west to east; cursorY, from 0 to 1 increasing from bottom to top; cursorZ, from 0 to 1 increasing from north to south.

insideBlock is true when the player's head is inside of a block's collision.

func (*Client) UseEntity

func (c *Client) UseEntity(entityID int32, hand int) error

UseEntity used by player to right-clicks another entity. hand could be one of 0: main hand, 1: off hand. A Notchian server only accepts this packet if the entity being attacked/used is visible without obstruction and within a 4-unit radius of the player's position.

func (*Client) UseEntityAt

func (c *Client) UseEntityAt(entityID int32, x, y, z float32, hand int) error

UseEntityAt is a variety of UseEntity with target location

func (*Client) UseItem

func (c *Client) UseItem(hand int) error

UseItem use the item player handing. hand could be one of 0: main hand, 1: off hand

func (*Client) UseItemEnd

func (c *Client) UseItemEnd() error

UseItemEnd used to finish UseItem, like eating food, pulling back bows.

type Dialer

type Dialer interface {
	// Dial connects to the given address via the proxy.
	Dial(network, addr string) (c net.Conn, err error)
}

A Dialer is a means to establish a connection.

type PlayInfo

type PlayInfo struct {
	Gamemode         int      //游戏模式
	Hardcore         bool     //是否是极限模式
	Dimension        int      //维度
	Difficulty       int      //难度
	ViewDistance     int      //视距
	ReducedDebugInfo bool     //减少调试信息
	WorldName        string   //当前世界的名字
	IsDebug          bool     //调试
	IsFlat           bool     //超平坦世界
	SpawnPosition    Position //主世界出生点
}

PlayInfo content player info in server.

type PlayerAbilities

type PlayerAbilities struct {
	Flags               int8
	FlyingSpeed         float32
	FieldofViewModifier float32
}

PlayerAbilities defines what player can do.

type Position

type Position struct {
	X, Y, Z int
}

Position is a 3D vector.

type ServInfo

type ServInfo struct {
	Brand string
}

ServInfo contains information about the server implementation.

type Settings

type Settings struct {
	Locale             string //地区
	ViewDistance       int    //视距
	ChatMode           int    //聊天模式
	ChatColors         bool   //聊天颜色
	DisplayedSkinParts uint8  //皮肤显示
	MainHand           int    //主手
	ReceiveMap         bool   //接收地图数据
	Brand              string // The brand string presented to the server.
}

Settings of client

Directories

Path Synopsis
Package path implements pathfinding.
Package path implements pathfinding.
Package phy implements a minimal physics simulation necessary for realistic bot behavior.
Package phy implements a minimal physics simulation necessary for realistic bot behavior.

Jump to

Keyboard shortcuts

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