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 ¶
- Constants
- func PingAndList(addr string) ([]byte, time.Duration, error)
- func PingAndListTimeout(addr string, timeout time.Duration) ([]byte, time.Duration, error)
- type Auth
- type Client
- type DisconnectErr
- type Events
- type LoginErr
- type PacketHandler
- type PacketHandlerError
- type PacketHandlerFunc
- type Position
Examples ¶
Constants ¶
const DefaultPort = 25565
const ProtocolVersion = 754
ProtocolVersion , the protocol version number of minecraft net protocol
Variables ¶
This section is empty.
Functions ¶
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)
Types ¶
type Client ¶
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) 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. Using roughly the same way to parse address as minecraft.
Example (Offline) ¶
c := NewClient()
c.Auth.Name = "RavMda" // 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)
}
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)
}
type DisconnectErr ¶
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 PacketHandler ¶
type PacketHandlerError ¶
func (PacketHandlerError) Error ¶
func (d PacketHandlerError) Error() string
func (PacketHandlerError) Unwrap ¶
func (d PacketHandlerError) Unwrap() error