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 ¶
- Constants
- Variables
- func OfflineUUID(name string) uuid.UUID
- func PingAndList(addr string, port int) ([]byte, time.Duration, error)
- func PingAndListTimeout(addr string, port int, timeout time.Duration) ([]byte, time.Duration, error)
- type Auth
- type Client
- func (c *Client) AttackEntity(entityID int32, hand int) error
- func (c *Client) Chat(msg string) error
- func (c *Client) Conn() *mcnet.Conn
- func (c *Client) Dig(status, locX, locY, locZ, face int) error
- func (c *Client) Disconnect() error
- func (c *Client) DropItem() error
- func (c *Client) DropItemStack() error
- func (c *Client) HandleGame() error
- func (c *Client) JoinServer(addr string, port int) (err error)
- func (c *Client) JoinServerWithDialer(d Dialer, addr string) (err error)
- func (c *Client) PickItem(slot int) error
- func (c *Client) PluginMessage(channal string, msg []byte) error
- func (c *Client) Respawn() error
- func (c *Client) SelectItem(slot int) error
- func (c *Client) SendPacket(packet pk.Packet) error
- func (c *Client) SwapItem() error
- func (c *Client) SwingArm(hand int) error
- func (c *Client) UseBlock(hand, locX, locY, locZ, face int, cursorX, cursorY, cursorZ float32, ...) error
- func (c *Client) UseEntity(entityID int32, hand int) error
- func (c *Client) UseEntityAt(entityID int32, x, y, z float32, hand int) error
- func (c *Client) UseItem(hand int) error
- func (c *Client) UseItemEnd() error
- type Dialer
- type PlayInfo
- type PlayerAbilities
- type Position
- type Settings
Examples ¶
Constants ¶
const ( Jacket LeftSleeve RightSleeve LeftPantsLeg RightPantsLeg Hat )
Used by Settings.DisplayedSkinParts. For each bits set if shows match part.
const ProtocolVersion = 578
ProtocolVersion , the protocol version number of minecraft net protocol
Variables ¶
var DefaultSettings = Settings{ Locale: "zh_CN", ViewDistance: 15, ChatMode: 0, DisplayedSkinParts: Jacket | LeftSleeve | RightSleeve | LeftPantsLeg | RightPantsLeg | Hat, MainHand: 1, ReceiveMap: true, }
DefaultSettings are the default settings of client
var SessionURL = "https://sessionserver.mojang.com/session/minecraft/join"
验证服务器URL
Functions ¶
func OfflineUUID ¶
OfflineUUID return the UUID from player name in offline mode
Example ¶
fmt.Println(OfflineUUID("Tnze"))
Output: c7b9eece-2f2e-325c-8da8-6fc8f3d0edb0
func PingAndList ¶
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:
Types ¶
type Client ¶
type Client struct { Auth player.Player PlayInfo Wd world.World //the map data // 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() (c *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 ¶
AttackEntity used by player to left-clicks another entity. The attack version of UseEntity. Has the same limit.
func (*Client) Conn ¶
Conn return the MCConn of the Client. Only used when you want to handle the packets by yourself
func (*Client) Dig ¶
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) Disconnect ¶
Disconnect disconnect the server. Server will close the connection.
func (*Client) DropItemStack ¶
DropItemStack drop the entire selected stack
func (*Client) HandleGame ¶
HandleGame receive server packet and response them correctly. Note that HandleGame will block if you don't receive from Events.
func (*Client) JoinServer ¶
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 ¶ added in v1.15.2
JoinServerWithDialer is similar to JoinServer but using a Dialer.
func (*Client) PickItem ¶
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 ¶
PluginMessage is used by mods and plugins to send their data.
func (*Client) SelectItem ¶
SelectItem used to change the slot selection in hotbar. slot should from 0 to 8
func (*Client) SendPacket ¶
SendPacket send the packet to server.
func (*Client) SwingArm ¶
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 ¶
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 ¶
UseEntityAt is a variety of UseEntity with target location
func (*Client) UseItem ¶
UseItem use the item player handing. hand could be one of 0: main hand, 1: off hand
func (*Client) UseItemEnd ¶
UseItemEnd used to finish UseItem, like eating food, pulling back bows.
type Dialer ¶ added in v1.15.2
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 //难度 LevelType string //地图类型 ViewDistance int //视距 ReducedDebugInfo bool //减少调试信息 }
PlayInfo content player info in server.
type PlayerAbilities ¶
PlayerAbilities defines what player can do.