format repo with "gofumpt" tool
This commit is contained in:
@ -1,9 +1,10 @@
|
||||
package basic
|
||||
|
||||
import (
|
||||
"github.com/Tnze/go-mc/chat"
|
||||
"unsafe"
|
||||
|
||||
"github.com/Tnze/go-mc/chat"
|
||||
|
||||
"github.com/Tnze/go-mc/data/packetid"
|
||||
"github.com/Tnze/go-mc/nbt"
|
||||
pk "github.com/Tnze/go-mc/net/packet"
|
||||
@ -123,7 +124,7 @@ func (p *Player) handleLoginPacket(packet pk.Packet) error {
|
||||
if err != nil {
|
||||
return Error{err}
|
||||
}
|
||||
err = p.c.Conn.WritePacket(pk.Marshal( //PluginMessage packet
|
||||
err = p.c.Conn.WritePacket(pk.Marshal( // PluginMessage packet
|
||||
int32(packetid.ServerboundCustomPayload),
|
||||
pk.Identifier("minecraft:brand"),
|
||||
pk.String(p.Settings.Brand),
|
||||
@ -148,6 +149,7 @@ func (p *Player) handleLoginPacket(packet pk.Packet) error {
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (p *Player) handleRespawnPacket(packet pk.Packet) error {
|
||||
var copyMeta bool
|
||||
err := packet.Scan(
|
||||
|
@ -2,12 +2,12 @@ package basic
|
||||
|
||||
// Settings of client
|
||||
type Settings struct {
|
||||
Locale string //地区
|
||||
ViewDistance int //视距
|
||||
ChatMode int //聊天模式
|
||||
ChatColors bool //聊天颜色
|
||||
DisplayedSkinParts uint8 //皮肤显示
|
||||
MainHand int //主手
|
||||
Locale string // 地区
|
||||
ViewDistance int // 视距
|
||||
ChatMode int // 聊天模式
|
||||
ChatColors bool // 聊天颜色
|
||||
DisplayedSkinParts uint8 // 皮肤显示
|
||||
MainHand int // 主手
|
||||
|
||||
// Enables filtering of text on signs and written book titles.
|
||||
// Currently, always false (i.e. the filtering is disabled)
|
||||
|
@ -79,7 +79,9 @@ func (p *PlayerMessage) ReadFrom(r io.Reader) (n int64, err error) {
|
||||
var timestamp pk.Long
|
||||
var unsignedContent chat.Message
|
||||
n, err = pk.Tuple{
|
||||
&hasMsgSign, pk.Opt{Has: &hasMsgSign,
|
||||
&hasMsgSign,
|
||||
pk.Opt{
|
||||
Has: &hasMsgSign,
|
||||
Field: (*pk.ByteArray)(&p.signature),
|
||||
},
|
||||
(*pk.UUID)(&p.sender),
|
||||
@ -88,7 +90,9 @@ func (p *PlayerMessage) ReadFrom(r io.Reader) (n int64, err error) {
|
||||
×tamp,
|
||||
(*pk.Long)(&p.salt),
|
||||
pk.Array(&p.prevMessages),
|
||||
&hasUnsignedContent, pk.Opt{Has: &hasUnsignedContent,
|
||||
&hasUnsignedContent,
|
||||
pk.Opt{
|
||||
Has: &hasUnsignedContent,
|
||||
Field: &unsignedContent,
|
||||
},
|
||||
(*pk.VarInt)(&p.filterType),
|
||||
|
14
bot/event.go
14
bot/event.go
@ -35,12 +35,14 @@ func (e *Events) AddGeneric(listeners ...PacketHandler) {
|
||||
}
|
||||
}
|
||||
|
||||
type PacketHandlerFunc func(p pk.Packet) error
|
||||
type PacketHandler struct {
|
||||
ID packetid.ClientboundPacketID
|
||||
Priority int
|
||||
F func(p pk.Packet) error
|
||||
}
|
||||
type (
|
||||
PacketHandlerFunc func(p pk.Packet) error
|
||||
PacketHandler struct {
|
||||
ID packetid.ClientboundPacketID
|
||||
Priority int
|
||||
F func(p pk.Packet) error
|
||||
}
|
||||
)
|
||||
|
||||
// handlerHeap is PriorityQueue<PacketHandlerFunc>
|
||||
type handlerHeap []PacketHandler
|
||||
|
@ -1,9 +1,9 @@
|
||||
package bot
|
||||
|
||||
import (
|
||||
"encoding/hex"
|
||||
"log"
|
||||
|
||||
"encoding/hex"
|
||||
"github.com/Tnze/go-mc/offline"
|
||||
"github.com/Tnze/go-mc/yggdrasil"
|
||||
)
|
||||
@ -25,7 +25,7 @@ func ExampleClient_JoinServer_offline() {
|
||||
id := offline.NameToUUID(c.Auth.Name) // optional, get uuid of offline mode game
|
||||
c.Auth.UUID = hex.EncodeToString(id[:])
|
||||
|
||||
//Login
|
||||
// Login
|
||||
err := c.JoinServer("127.0.0.1")
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
@ -33,12 +33,9 @@ func ExampleClient_JoinServer_offline() {
|
||||
log.Println("Login success")
|
||||
|
||||
// Register event handlers
|
||||
// c.Events.GameStart = onGameStartFunc
|
||||
// c.Events.ChatMsg = onChatMsgFunc
|
||||
// c.Events.Disconnect = onDisconnectFunc
|
||||
// ...
|
||||
// c.Events.AddListener(...)
|
||||
|
||||
//JoinGame
|
||||
// JoinGame
|
||||
err = c.HandleGame()
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
@ -61,7 +58,7 @@ func ExampleClient_JoinServer_online() {
|
||||
c.Auth.UUID, c.Auth.Name = auth.SelectedProfile()
|
||||
c.Auth.AsTk = auth.AccessToken()
|
||||
|
||||
//Connect server
|
||||
// Connect server
|
||||
err = c.JoinServer("127.0.0.1")
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
@ -74,7 +71,7 @@ func ExampleClient_JoinServer_online() {
|
||||
// c.Events.Disconnect = onDisconnectFunc
|
||||
// ...
|
||||
|
||||
//Join the game
|
||||
// Join the game
|
||||
err = c.HandleGame()
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
|
@ -12,12 +12,12 @@ import (
|
||||
func (c *Client) HandleGame() error {
|
||||
var p pk.Packet
|
||||
for {
|
||||
//Read packets
|
||||
// Read packets
|
||||
if err := c.Conn.ReadPacket(&p); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
//handle packets
|
||||
// handle packets
|
||||
err := c.handlePacket(p)
|
||||
if err != nil {
|
||||
return err
|
||||
|
20
bot/mcbot.go
20
bot/mcbot.go
@ -22,8 +22,10 @@ import (
|
||||
)
|
||||
|
||||
// ProtocolVersion is the protocol version number of minecraft net protocol
|
||||
const ProtocolVersion = 760
|
||||
const DefaultPort = mcnet.DefaultPort
|
||||
const (
|
||||
ProtocolVersion = 760
|
||||
DefaultPort = mcnet.DefaultPort
|
||||
)
|
||||
|
||||
// JoinServer connect a Minecraft server for playing the game.
|
||||
// Using roughly the same way to parse address as minecraft.
|
||||
@ -98,15 +100,15 @@ func (c *Client) join(ctx context.Context, d *mcnet.Dialer, addr string) error {
|
||||
return LoginErr{"login start", err}
|
||||
}
|
||||
for {
|
||||
//Receive Packet
|
||||
// Receive Packet
|
||||
var p pk.Packet
|
||||
if err = c.Conn.ReadPacket(&p); err != nil {
|
||||
return LoginErr{"receive packet", err}
|
||||
}
|
||||
|
||||
//Handle Packet
|
||||
// Handle Packet
|
||||
switch p.ID {
|
||||
case packetid.LoginDisconnect: //LoginDisconnect
|
||||
case packetid.LoginDisconnect: // LoginDisconnect
|
||||
var reason chat.Message
|
||||
err = p.Scan(&reason)
|
||||
if err != nil {
|
||||
@ -114,12 +116,12 @@ func (c *Client) join(ctx context.Context, d *mcnet.Dialer, addr string) error {
|
||||
}
|
||||
return LoginErr{"disconnect", DisconnectErr(reason)}
|
||||
|
||||
case packetid.LoginEncryptionRequest: //Encryption Request
|
||||
case packetid.LoginEncryptionRequest: // Encryption Request
|
||||
if err := handleEncryptionRequest(c, p); err != nil {
|
||||
return LoginErr{"encryption", err}
|
||||
}
|
||||
|
||||
case packetid.LoginSuccess: //Login Success
|
||||
case packetid.LoginSuccess: // Login Success
|
||||
err := p.Scan(
|
||||
(*pk.UUID)(&c.UUID),
|
||||
(*pk.String)(&c.Name),
|
||||
@ -129,14 +131,14 @@ func (c *Client) join(ctx context.Context, d *mcnet.Dialer, addr string) error {
|
||||
}
|
||||
return nil
|
||||
|
||||
case packetid.LoginCompression: //Set Compression
|
||||
case packetid.LoginCompression: // Set Compression
|
||||
var threshold pk.VarInt
|
||||
if err := p.Scan(&threshold); err != nil {
|
||||
return LoginErr{"compression", err}
|
||||
}
|
||||
c.Conn.SetThreshold(int(threshold))
|
||||
|
||||
case packetid.LoginPluginRequest: //Login Plugin Request
|
||||
case packetid.LoginPluginRequest: // Login Plugin Request
|
||||
var (
|
||||
msgid pk.VarInt
|
||||
channel pk.Identifier
|
||||
|
@ -79,11 +79,11 @@ func pingAndList(ctx context.Context, addr string, conn *mcnet.Conn) (data []byt
|
||||
}
|
||||
|
||||
const Handshake = 0x00
|
||||
//握手
|
||||
// 握手
|
||||
err = conn.WritePacket(pk.Marshal(
|
||||
Handshake, //Handshake packet ID
|
||||
pk.VarInt(ProtocolVersion), //Protocol version
|
||||
pk.String(host), //Server's address
|
||||
Handshake, // Handshake packet ID
|
||||
pk.VarInt(ProtocolVersion), // Protocol version
|
||||
pk.String(host), // Server's address
|
||||
pk.UnsignedShort(port),
|
||||
pk.Byte(1),
|
||||
))
|
||||
@ -91,8 +91,8 @@ func pingAndList(ctx context.Context, addr string, conn *mcnet.Conn) (data []byt
|
||||
return nil, 0, fmt.Errorf("bot: send handshake packect fail: %v", err)
|
||||
}
|
||||
|
||||
//LIST
|
||||
//请求服务器状态
|
||||
// LIST
|
||||
// 请求服务器状态
|
||||
err = conn.WritePacket(pk.Marshal(
|
||||
packetid.StatusRequest,
|
||||
))
|
||||
@ -101,7 +101,7 @@ func pingAndList(ctx context.Context, addr string, conn *mcnet.Conn) (data []byt
|
||||
}
|
||||
|
||||
var p pk.Packet
|
||||
//服务器返回状态
|
||||
// 服务器返回状态
|
||||
if err := conn.ReadPacket(&p); err != nil {
|
||||
return nil, 0, fmt.Errorf("bot: recv list packect fail: %v", err)
|
||||
}
|
||||
@ -111,7 +111,7 @@ func pingAndList(ctx context.Context, addr string, conn *mcnet.Conn) (data []byt
|
||||
return nil, 0, fmt.Errorf("bot: scan list packect fail: %v", err)
|
||||
}
|
||||
|
||||
//PING
|
||||
// PING
|
||||
startTime := time.Now()
|
||||
err = conn.WritePacket(pk.Marshal(
|
||||
packetid.StatusPingRequest,
|
||||
|
Reference in New Issue
Block a user