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 PingAndListContext(ctx context.Context, addr string) ([]byte, time.Duration, error)
- func PingAndListTimeout(addr string, timeout time.Duration) ([]byte, time.Duration, error)
- type Auth
- type Client
- type ConfigData
- type ConfigErr
- type Conn
- type DisconnectErr
- type Events
- type JoinOptions
- type LoginErr
- type PacketHandler
- type PacketHandlerError
- type PacketHandlerFunc
- type Position
Examples ¶
Constants ¶
const ( ProtocolVersion = 764 DefaultPort = mcnet.DefaultPort )
ProtocolVersion is 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)
Output:
func PingAndListContext ¶ added in v1.18.1
Types ¶
type Client ¶
type Client struct { Conn *Conn Auth Auth Name string UUID uuid.UUID Events Events LoginPlugin map[string]func(data []byte) ([]byte, error) ConfigData }
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 = "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 ¶ added in v1.15.1
JoinServerWithDialer is similar to JoinServer but using a net.Dialer.
func (*Client) JoinServerWithOptions ¶ added in v1.19.2
func (c *Client) JoinServerWithOptions(addr string, options JoinOptions) (err error)
type ConfigData ¶ added in v1.20.2
type Conn ¶ added in v1.19.4
Conn is a concurrently-safe warpper of net.Conn with packet queue. Note that not all methods are concurrently-safe.
type DisconnectErr ¶ added in v1.16.5
func (DisconnectErr) Error ¶ added in v1.16.5
func (d DisconnectErr) Error() string
type Events ¶ added in v1.16.5
type Events struct {
// contains filtered or unexported fields
}
func (*Events) AddGeneric ¶ added in v1.16.5
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 ¶ added in v1.16.5
func (e *Events) AddListener(listeners ...PacketHandler)
type JoinOptions ¶ added in v1.19.2
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 QueueRead queue.Queue[pk.Packet] QueueWrite queue.Queue[pk.Packet] }
type PacketHandler ¶ added in v1.16.5
type PacketHandlerError ¶ added in v1.16.5
type PacketHandlerError struct { ID packetid.ClientboundPacketID Err error }
func (PacketHandlerError) Error ¶ added in v1.16.5
func (d PacketHandlerError) Error() string
func (PacketHandlerError) Unwrap ¶ added in v1.16.5
func (d PacketHandlerError) Unwrap() error
type PacketHandlerFunc ¶ added in v1.16.5
Source Files ¶
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. |