format repo with "gofumpt" tool

This commit is contained in:
Tnze
2022-11-26 20:37:57 +08:00
parent 7814e7b1ab
commit fad92fe364
61 changed files with 333 additions and 268 deletions

View File

@ -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(

View File

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

View File

@ -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) {
&timestamp,
(*pk.Long)(&p.salt),
pk.Array(&p.prevMessages),
&hasUnsignedContent, pk.Opt{Has: &hasUnsignedContent,
&hasUnsignedContent,
pk.Opt{
Has: &hasUnsignedContent,
Field: &unsignedContent,
},
(*pk.VarInt)(&p.filterType),

View File

@ -35,12 +35,14 @@ func (e *Events) AddGeneric(listeners ...PacketHandler) {
}
}
type PacketHandlerFunc func(p pk.Packet) error
type PacketHandler struct {
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

View File

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

View File

@ -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

View File

@ -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

View File

@ -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,

View File

@ -57,15 +57,15 @@ const (
type Message struct {
Text string `json:"text"`
Bold bool `json:"bold,omitempty"` //粗体
Italic bool `json:"italic,omitempty"` //斜体
UnderLined bool `json:"underlined,omitempty"` //下划线
StrikeThrough bool `json:"strikethrough,omitempty"` //删除线
Obfuscated bool `json:"obfuscated,omitempty"` //随机
Bold bool `json:"bold,omitempty"` // 粗体
Italic bool `json:"italic,omitempty"` // 斜体
UnderLined bool `json:"underlined,omitempty"` // 下划线
StrikeThrough bool `json:"strikethrough,omitempty"` // 删除线
Obfuscated bool `json:"obfuscated,omitempty"` // 随机
// Font of the message, could be one of minecraft:uniform, minecraft:alt or minecraft:default
// This option is only valid on 1.16+, otherwise the property is ignored.
Font string `json:"font,omitempty"` //字体
Color string `json:"color,omitempty"` //颜色
Font string `json:"font,omitempty"` // 字体
Color string `json:"color,omitempty"` // 颜色
// Insertion contains text to insert. Only used for messages in chat.
// When shift is held, clicking the component inserts the given text
@ -207,6 +207,7 @@ var fmtCode = map[byte]string{
'o': "3",
'r': "0",
}
var colors = map[string]string{
Black: "30",
DarkBlue: "34",
@ -241,7 +242,7 @@ func (m Message) ClearString() string {
text, _ := TransCtrlSeq(m.Text, false)
msg.WriteString(text)
//handle translate
// handle translate
if m.Translate != "" {
args := make([]interface{}, len(m.With))
for i, v := range m.With {
@ -286,7 +287,7 @@ func (m Message) String() string {
text, ok := TransCtrlSeq(m.Text, true)
msg.WriteString(text)
//handle translate
// handle translate
if m.Translate != "" {
args := make([]interface{}, len(m.With))
for i, v := range m.With {
@ -323,9 +324,9 @@ func TransCtrlSeq(str string, ansi bool) (dst string, change bool) {
change = true
return "\033[" + f + "m" // enable, add ANSI code
}
return "" //disable, remove the § code
return "" // disable, remove the § code
}
return str //not a § code
return str // not a § code
},
)
return

View File

@ -17,7 +17,7 @@ import (
const (
version = "1.17"
infoURL = "https://raw.githubusercontent.com/PrismarineJS/minecraft-data/master/data/pc/" + version + "/entities.json"
//language=gohtml
// language=gohtml
entityTmpl = `// Code generated by gen_entity.go DO NOT EDIT.
// Package entity stores information about entities in Minecraft.
package entity

View File

@ -17,7 +17,7 @@ import (
const (
version = "1.17"
infoURL = "https://raw.githubusercontent.com/PrismarineJS/minecraft-data/master/data/pc/" + version + "/items.json"
//language=gohtml
// language=gohtml
itemTmpl = `// Code generated by gen_item.go DO NOT EDIT.
// Package item stores information about items in Minecraft.
package item

View File

@ -1,4 +1,5 @@
//+build generate
//go:build generate
// +build generate
// This program can automatically download language.json file and convert into .go
package main
@ -17,9 +18,8 @@ import (
"text/template"
)
var (
//language=gohtml
langTmpl = `// Code generated by downloader.go; DO NOT EDIT.
// language=gohtml
var langTmpl = `// Code generated by downloader.go; DO NOT EDIT.
package {{.Name}}
{{if ne .Name "en_us"}}
import "github.com/Tnze/go-mc/chat"
@ -28,7 +28,6 @@ func init() { chat.SetLanguage(Map) }
{{end}}
var Map = {{.LangMap | printf "%#v"}}
`
)
//go:generate go run $GOFILE
//go:generate go fmt ./...
@ -90,7 +89,7 @@ func main() {
}
func lang(name, hash string) {
//download language
// download language
LangURL := "http://resources.download.minecraft.net/" + hash[:2] + "/" + hash
resp, err := http.Get(LangURL)
if err != nil {
@ -112,12 +111,12 @@ func readLang(name string, r io.Reader) {
pName := strings.ReplaceAll(name, "_", "-")
// mkdir
err = os.Mkdir(pName, 0777)
err = os.Mkdir(pName, 0o777)
if err != nil && !os.IsExist(err) {
panic(err)
}
f, err := os.OpenFile(filepath.Join(pName, name+".go"), os.O_CREATE|os.O_RDWR|os.O_TRUNC, 0666)
f, err := os.OpenFile(filepath.Join(pName, name+".go"), os.O_CREATE|os.O_RDWR|os.O_TRUNC, 0o666)
if err != nil {
panic(err)
}
@ -143,7 +142,7 @@ var javaN = regexp.MustCompile(`%[0-9]\$s`)
// Java use %2$s to refer to the second arg, but Golang use %2s, so we need this
func trans(m map[string]string) {
//replace "%[0-9]\$s" with "%[0-9]s"
// replace "%[0-9]\$s" with "%[0-9]s"
for i := range m {
c := m[i]
if javaN.MatchString(c) {
@ -169,7 +168,7 @@ func assetIndexURL() (string, error) {
Latest struct {
Release string `json:"release"`
} `json:"latest"`
Versions []struct{
Versions []struct {
ID string `json:"id"`
URL string `json:"url"`
} `json:"versions"`
@ -196,8 +195,8 @@ func assetIndexURL() (string, error) {
return "", errors.New("could not determine versionURL")
}
var version struct{
AssetIndex struct{
var version struct {
AssetIndex struct {
URL string `json:"url"`
} `json:"assetIndex"`
}

View File

@ -2,9 +2,10 @@ package packetid
//go:generate stringer -type ClientboundPacketID
//go:generate stringer -type ServerboundPacketID
type ClientboundPacketID int32
type ServerboundPacketID int32
type (
ClientboundPacketID int32
ServerboundPacketID int32
)
// Login Clientbound
const (

View File

@ -71,7 +71,7 @@ func main() {
log.Fatal(err)
}
err = os.WriteFile("blockentitytype/blockentitytype.go", formattedSource, 0666)
err = os.WriteFile("blockentitytype/blockentitytype.go", formattedSource, 0o666)
if err != nil {
log.Fatal(err)
}

View File

@ -16,7 +16,7 @@ import (
const (
version = "1.17.1"
protocolURL = "https://pokechu22.github.io/Burger/" + version + ".json"
//language=gohtml
// language=gohtml
soundTmpl = `// Code generated by gen_soundid.go. DO NOT EDIT.
package soundid

View File

@ -1,7 +1,6 @@
package main
import (
botchat "github.com/Tnze/go-mc/bot/chat"
"log"
"time"
@ -9,6 +8,7 @@ import (
"github.com/Tnze/go-mc/bot"
"github.com/Tnze/go-mc/bot/basic"
botchat "github.com/Tnze/go-mc/bot/chat"
"github.com/Tnze/go-mc/chat"
_ "github.com/Tnze/go-mc/data/lang/en-us"
"github.com/Tnze/go-mc/data/packetid"
@ -26,7 +26,7 @@ var (
)
func main() {
//log.SetOutput(colorable.NewColorableStdout()) // optional for colorable output
// log.SetOutput(colorable.NewColorableStdout()) // optional for colorable output
c = bot.NewClient()
p = basic.NewPlayer(c, basic.DefaultSettings, basic.EventsListener{
GameStart: onGameStart,
@ -36,18 +36,18 @@ func main() {
})
bc = botchat.NewChat(c, p, botchat.EventsHandler{PlayerChatMessage: onChatMsg})
//Register event handlers
// Register event handlers
c.Events.AddListener(soundListener)
//Login
// Login
err := c.JoinServer("127.0.0.1")
if err != nil {
log.Fatal(err)
}
log.Println("Login success")
//JoinGame
// JoinGame
err = c.HandleGame()
if err != nil {
log.Fatal(err)
@ -96,12 +96,12 @@ func UseItem(hand int32) error {
//goland:noinspection SpellCheckingInspection
func onSound(id int, category int, x, y, z float64, volume, pitch float32) error {
if id == 369 {
if err := UseItem(0); err != nil { //retrieve
if err := UseItem(0); err != nil { // retrieve
return err
}
log.Println("gra~")
time.Sleep(time.Millisecond * 300)
if err := UseItem(0); err != nil { //throw
if err := UseItem(0); err != nil { // throw
return err
}
watch <- time.Now()

View File

@ -21,20 +21,24 @@ import (
"github.com/Tnze/go-mc/level"
)
var address = flag.String("address", "127.0.0.1", "The server address")
var name = flag.String("name", "Daze", "The player's name")
var playerID = flag.String("uuid", "", "The player's UUID")
var accessToken = flag.String("token", "", "AccessToken")
var (
address = flag.String("address", "127.0.0.1", "The server address")
name = flag.String("name", "Daze", "The player's name")
playerID = flag.String("uuid", "", "The player's UUID")
accessToken = flag.String("token", "", "AccessToken")
)
var client *bot.Client
var player *basic.Player
var chatHandler *botchat.Chat
var worldManager *world.World
var screenManager *screen.Manager
var (
client *bot.Client
player *basic.Player
chatHandler *botchat.Chat
worldManager *world.World
screenManager *screen.Manager
)
func main() {
flag.Parse()
//log.SetOutput(colorable.NewColorableStdout())
// log.SetOutput(colorable.NewColorableStdout())
client = bot.NewClient()
client.Auth = bot.Auth{
Name: *name,
@ -61,14 +65,14 @@ func main() {
Close: nil,
})
//Login
// Login
err := client.JoinServer(*address)
if err != nil {
log.Fatal(err)
}
log.Println("Login success")
//JoinGame
// JoinGame
for {
if err = client.HandleGame(); err == nil {
panic("HandleGame never return nil")
@ -103,7 +107,7 @@ func onDeath() error {
func onGameStart() error {
log.Println("Game start")
return nil //if err isn't nil, HandleGame() will return it.
return nil // if err isn't nil, HandleGame() will return it.
}
func onPlayerMsg(msg chat.Message) error {

View File

@ -18,14 +18,16 @@ import (
pk "github.com/Tnze/go-mc/net/packet"
)
var address = flag.String("address", "127.0.0.1", "The server address")
var client *bot.Client
var player *basic.Player
var screenManager *screen.Manager
var (
address = flag.String("address", "127.0.0.1", "The server address")
client *bot.Client
player *basic.Player
screenManager *screen.Manager
)
func main() {
flag.Parse()
//log.SetOutput(colorable.NewColorableStdout())
// log.SetOutput(colorable.NewColorableStdout())
client = bot.NewClient()
client.Auth.Name = "Daze"
player = basic.NewPlayer(client, basic.DefaultSettings, basic.EventsListener{})
@ -35,14 +37,14 @@ func main() {
F: onCommands,
})
//Login
// Login
err := client.JoinServer(*address)
if err != nil {
log.Fatal(err)
}
log.Println("Login success")
//JoinGame
// JoinGame
for {
if err = client.HandleGame(); err == nil {
panic("HandleGame never return nil")
@ -62,8 +64,7 @@ func onCommands(p pk.Packet) error {
return nil
}
type Node struct {
}
type Node struct{}
func (n Node) ReadFrom(r io.Reader) (int64, error) {
var Flags pk.Byte
@ -71,7 +72,7 @@ func (n Node) ReadFrom(r io.Reader) (int64, error) {
var Redirect pk.VarInt
var Name pk.String
var Parser pk.Identifier
var Properties = Prop{Type: &Parser}
Properties := Prop{Type: &Parser}
var SuggestionsType pk.Identifier
m, err := pk.Tuple{
&Flags,

View File

@ -8,8 +8,10 @@ import (
"github.com/Tnze/go-mc/yggdrasil"
)
var user = flag.String("user", "", "Can be an email address or player name for unmigrated accounts")
var pswd = flag.String("password", "", "Your password")
var (
user = flag.String("user", "", "Can be an email address or player name for unmigrated accounts")
pswd = flag.String("password", "", "Your password")
)
func main() {
flag.Parse()

View File

@ -13,8 +13,10 @@ import (
"github.com/Tnze/go-mc/save/region"
)
var decomp = flag.Bool("x", false, "decompress each chunk to NBT format")
var repack = flag.Bool("p", false, "repack .mcc file to .mca")
var (
decomp = flag.Bool("x", false, "decompress each chunk to NBT format")
repack = flag.Bool("p", false, "repack .mcc file to .mca")
)
func main() {
flag.Usage = usage
@ -79,7 +81,7 @@ func unpack(f, o string) {
fn := fmt.Sprintf("c.%d.%d.mcc", x*32+i, z*32+j)
if *decomp {
fn += ".nbt" //解压后就是一个标准的NBT文件可以加个.nbt后缀
fn += ".nbt" // 解压后就是一个标准的NBT文件可以加个.nbt后缀
switch data[0] {
default:
err = fmt.Errorf("unknown compression type 0x%02x", data[0])
@ -91,7 +93,7 @@ func unpack(f, o string) {
checkerr(err)
}
cf, err := os.OpenFile(filepath.Join(o, fn), os.O_CREATE|os.O_RDWR|os.O_EXCL, 0666)
cf, err := os.OpenFile(filepath.Join(o, fn), os.O_CREATE|os.O_RDWR|os.O_EXCL, 0o666)
checkerr(err)
_, err = io.Copy(cf, r)

View File

@ -20,8 +20,10 @@ import (
"github.com/Tnze/go-mc/chat"
)
var protocol = flag.Int("p", 578, "The protocol version number sent during ping")
var favicon = flag.String("f", "", "If specified, the server's icon will be save to")
var (
protocol = flag.Int("p", 578, "The protocol version number sent during ping")
favicon = flag.String("f", "", "If specified, the server's icon will be save to")
)
type status struct {
Description chat.Message

View File

@ -13,12 +13,14 @@ import (
"github.com/Tnze/go-mc/chat"
)
var address = flag.String("address", "127.0.0.1", "The server address")
var number = flag.Int("number", 1023, "The number of clients")
var (
address = flag.String("address", "127.0.0.1", "The server address")
number = flag.Int("number", 1023, "The number of clients")
)
func main() {
flag.Parse()
//log.SetOutput(colorable.NewColorableStdout())
// log.SetOutput(colorable.NewColorableStdout())
for i := 0; i < *number; i++ {
go func(i int) {
@ -52,7 +54,7 @@ func newIndividual(id int, name string) (i *individual) {
}
func (i *individual) run(address string) {
//Login
// Login
err := i.client.JoinServer(address)
if err != nil {
log.Printf("[%d]Login fail: %v", i.id, err)
@ -60,7 +62,7 @@ func (i *individual) run(address string) {
}
log.Printf("[%d]Login success", i.id)
//JoinGame
// JoinGame
if err = i.client.HandleGame(); err == nil {
panic("HandleGame never return nil")
}

View File

@ -11,8 +11,10 @@ import (
"github.com/Tnze/go-mc/offline"
)
const ProtocolVersion = 578
const MaxPlayer = 200
const (
ProtocolVersion = 578
MaxPlayer = 200
)
// Packet IDs
const (
@ -45,11 +47,11 @@ func acceptConn(conn net.Conn) {
}
switch intention {
default: //unknown error
default: // unknown error
log.Printf("Unknown handshake intention: %v", intention)
case 1: //for status
case 1: // for status
acceptListPing(conn)
case 2: //for login
case 2: // for login
handlePlaying(conn, protocol)
}
}
@ -103,19 +105,19 @@ type PlayerInfo struct {
// acceptLogin check player's account
func acceptLogin(conn net.Conn) (info PlayerInfo, err error) {
//login start
// login start
var p pk.Packet
err = conn.ReadPacket(&p)
if err != nil {
return
}
err = p.Scan((*pk.String)(&info.Name)) //decode username as pk.String
err = p.Scan((*pk.String)(&info.Name)) // decode username as pk.String
if err != nil {
return
}
//auth
// auth
const OnlineMode = false
if OnlineMode {
log.Panic("Not Implement")
@ -146,7 +148,7 @@ func handshake(conn net.Conn) (protocol, intention int32, err error) {
// loginSuccess send LoginSuccess packet to client
func loginSuccess(conn net.Conn, name string, uuid uuid.UUID) error {
return conn.WritePacket(pk.Marshal(0x02,
pk.String(uuid.String()), //uuid as string with hyphens
pk.String(uuid.String()), // uuid as string with hyphens
pk.String(name),
))
}

View File

@ -20,9 +20,9 @@ func acceptListPing(conn net.Conn) {
}
switch p.ID {
case 0x00: //List
case 0x00: // List
err = conn.WritePacket(pk.Marshal(0x00, pk.String(listResp())))
case 0x01: //Ping
case 0x01: // Ping
err = conn.WritePacket(p)
}
if err != nil {

View File

@ -13,8 +13,10 @@ import (
"github.com/Tnze/go-mc/offline"
)
const ProtocolVersion = 756
const MaxPlayer = 200
const (
ProtocolVersion = 756
MaxPlayer = 200
)
// Packet IDs
const (
@ -47,11 +49,11 @@ func acceptConn(conn net.Conn) {
}
switch intention {
default: //unknown error
default: // unknown error
log.Printf("Unknown handshake intention: %v", intention)
case 1: //for status
case 1: // for status
acceptListPing(conn)
case 2: //for login
case 2: // for login
handlePlaying(conn, protocol)
}
}
@ -106,19 +108,19 @@ type PlayerInfo struct {
// acceptLogin check player's account
func acceptLogin(conn net.Conn) (info PlayerInfo, err error) {
//login start
// login start
var p pk.Packet
err = conn.ReadPacket(&p)
if err != nil {
return
}
err = p.Scan((*pk.String)(&info.Name)) //decode username as pk.String
err = p.Scan((*pk.String)(&info.Name)) // decode username as pk.String
if err != nil {
return
}
//auth
// auth
const OnlineMode = false
if OnlineMode {
log.Panic("Not Implement")

View File

@ -2,11 +2,12 @@ package main
import (
"encoding/json"
"log"
"github.com/Tnze/go-mc/chat"
"github.com/Tnze/go-mc/net"
pk "github.com/Tnze/go-mc/net/packet"
"github.com/google/uuid"
"log"
)
func acceptListPing(conn net.Conn) {
@ -18,9 +19,9 @@ func acceptListPing(conn net.Conn) {
}
switch p.ID {
case 0x00: //List
case 0x00: // List
err = conn.WritePacket(pk.Marshal(0x00, pk.String(listResp())))
case 0x01: //Ping
case 0x01: // Ping
err = conn.WritePacket(p)
}
if err != nil {

View File

@ -8,8 +8,10 @@ import (
pk "github.com/Tnze/go-mc/net/packet"
)
const indexOutOfBounds = "index out of bounds"
const valueOutOfBounds = "value out of bounds"
const (
indexOutOfBounds = "index out of bounds"
valueOutOfBounds = "value out of bounds"
)
// BitStorage implement the compacted data array used in chunk storage and heightmaps.
// You can think of this as a []intN whose N is called "bits" in NewBitStorage.

View File

@ -1,14 +1,17 @@
package level
import (
pk "github.com/Tnze/go-mc/net/packet"
"math/bits"
"reflect"
"testing"
pk "github.com/Tnze/go-mc/net/packet"
)
var data = []uint64{0x0020863148418841, 0x01018A7260F68C87}
var want = []int{1, 2, 2, 3, 4, 4, 5, 6, 6, 4, 8, 0, 7, 4, 3, 13, 15, 16, 9, 14, 10, 12, 0, 2}
var (
data = []uint64{0x0020863148418841, 0x01018A7260F68C87}
want = []int{1, 2, 2, 3, 4, 4, 5, 6, 6, 4, 8, 0, 7, 4, 3, 13, 15, 16, 9, 14, 10, 12, 0, 2}
)
func TestBitStorage_Get(t *testing.T) {
bs := NewBitStorage(5, 24, data)

View File

@ -20,18 +20,22 @@ type Block interface {
//go:embed block_states.nbt
var blockStates []byte
var ToStateID map[Block]StateID
var StateList []Block
var (
ToStateID map[Block]StateID
StateList []Block
)
// BitsPerBlock indicates how many bits are needed to represent all possible
// block states. This value is used to determine the size of the global palette.
var BitsPerBlock int
type StateID int
type State struct {
type (
StateID int
State struct {
Name string
Properties nbt.RawMessage
}
}
)
func init() {
var states []State

View File

@ -70,7 +70,7 @@ func genSourceFile(states []State) {
panic(err)
}
err = os.WriteFile("blocks.go", formattedSource, 0666)
err = os.WriteFile("blocks.go", formattedSource, 0o666)
if err != nil {
panic(err)
}

View File

@ -78,7 +78,7 @@ func main() {
if err != nil {
log.Panic(err)
}
err = os.WriteFile("properties_enum.go", formattedSource, 0666)
err = os.WriteFile("properties_enum.go", formattedSource, 0o666)
if err != nil {
log.Panic(err)
}

View File

@ -406,6 +406,7 @@ type Section struct {
func (s *Section) GetBlock(i int) BlocksState {
return s.States.Get(i)
}
func (s *Section) SetBlock(i int, v BlocksState) {
if block.IsAir(s.States.Get(i)) {
s.BlockCount--

View File

@ -13,8 +13,10 @@ import (
type State interface {
~int
}
type BlocksState = block.StateID
type BiomesState int
type (
BlocksState = block.StateID
BiomesState int
)
type PaletteContainer[T State] struct {
bits int
@ -206,6 +208,7 @@ func (b biomesCfg) bits(bits int) int {
return biome.BitsPerBiome
}
}
func (b biomesCfg) create(bits int) palette[BiomesState] {
switch bits {
case 0:

View File

@ -33,7 +33,7 @@ func (d *Decoder) Decode(v interface{}) (string, error) {
if val.Kind() != reflect.Ptr {
return "", errors.New("nbt: non-pointer passed to Decode")
}
//start read NBT
// start read NBT
tagType, tagName, err := d.readTag()
if err != nil {
return tagName, fmt.Errorf("nbt: %w", err)
@ -246,7 +246,7 @@ func (d *Decoder) unmarshal(val reflect.Value, tagType byte) error {
if err != nil {
return err
}
vt := val.Type() //receiver must be []int or []int32
vt := val.Type() // receiver must be []int or []int32
if vt.Kind() == reflect.Interface {
vt = reflect.TypeOf([]int32{}) // pass
} else if vt.Kind() == reflect.Array && vt.Len() != int(aryLen) {
@ -277,7 +277,7 @@ func (d *Decoder) unmarshal(val reflect.Value, tagType byte) error {
if err != nil {
return err
}
vt := val.Type() //receiver must be []int or []int64
vt := val.Type() // receiver must be []int or []int64
if vt.Kind() == reflect.Interface {
vt = reflect.TypeOf([]int64{}) // pass
} else if vt.Kind() != reflect.Slice {
@ -591,7 +591,7 @@ func (d *Decoder) readTag() (tagType byte, tagName string, err error) {
c := d.checkCompressed(tagType)
err = fmt.Errorf("nbt: unknown Tag %#02x, which seems like %s header and you should uncompress it first", tagType, c)
case TagEnd:
default: //Read Tag
default: // Read Tag
tagName, err = d.readString()
}
return

View File

@ -11,12 +11,12 @@ import (
)
func TestUnmarshal_string(t *testing.T) {
var data = []byte{
data := []byte{
0x08, 0x00, 0x04, 0x6e, 0x61, 0x6d, 0x65, 0x00, 0x09,
0x42, 0x61, 0x6e, 0x61, 0x6e, 0x72, 0x61, 0x6d, 0x61,
}
//Unmarshal to string
// Unmarshal to string
var Name string
if err := Unmarshal(data, &Name); err != nil {
t.Fatal(err)
@ -26,7 +26,7 @@ func TestUnmarshal_string(t *testing.T) {
t.Errorf("Unmarshal NBT fail: get %q, want %q", Name, "Bananrama")
}
//Unmarshal to interface{}
// Unmarshal to interface{}
var infName interface{}
if err := Unmarshal(data, &infName); err != nil {
t.Fatal(err)
@ -38,7 +38,7 @@ func TestUnmarshal_string(t *testing.T) {
}
func TestUnmarshal_simple(t *testing.T) {
var data = []byte{
data := []byte{
0x0a, 0x00, 0x0b, 0x68, 0x65, 0x6c, 0x6c, 0x6f,
0x20, 0x77, 0x6f, 0x72, 0x6c, 0x64, 0x08, 0x00,
0x04, 0x6e, 0x61, 0x6d, 0x65, 0x00, 0x09, 0x42,
@ -46,7 +46,7 @@ func TestUnmarshal_simple(t *testing.T) {
0x00,
}
//test parse
// test parse
var value struct {
Name string `nbt:"name"`
}
@ -57,7 +57,7 @@ func TestUnmarshal_simple(t *testing.T) {
t.Errorf("Unmarshal NBT fail: get %q, want %q", value.Name, "Bananrama")
}
//test rawRead
// test rawRead
var empty struct{}
if err := Unmarshal(data, &empty); err != nil {
t.Fatal(err)
@ -184,7 +184,7 @@ func MakeBigTestStruct() BigTestStruct {
}
func TestDecoder_Decode_bigTest(t *testing.T) {
//test parse
// test parse
var value BigTestStruct
r, err := gzip.NewReader(bytes.NewReader(bigTestData[:]))
if err != nil {
@ -199,7 +199,7 @@ func TestDecoder_Decode_bigTest(t *testing.T) {
t.Errorf("parse fail, expect %v, get %v", want, value)
}
//test rawRead
// test rawRead
var empty struct{}
r, err = gzip.NewReader(bytes.NewReader(bigTestData[:]))
if err != nil {
@ -345,7 +345,7 @@ func TestDecoder_Decode_ByteArray(t *testing.T) {
want = []byte{0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07}
)
//Unmarshal to []byte
// Unmarshal to []byte
if err := Unmarshal(data, &value); err != nil {
t.Fatal(err)
}
@ -354,7 +354,7 @@ func TestDecoder_Decode_ByteArray(t *testing.T) {
}
// t.Log(value)
//Unmarshal to interface{}
// Unmarshal to interface{}
if err := Unmarshal(data, &infValue); err != nil {
t.Fatal(err)
}
@ -377,7 +377,7 @@ func TestDecoder_Decode_bool(t *testing.T) {
}
var value bool
for i, v := range data {
//Unmarshal to []byte
// Unmarshal to []byte
if err := Unmarshal(v, &value); err != nil {
t.Fatal(err)
}
@ -388,12 +388,12 @@ func TestDecoder_Decode_bool(t *testing.T) {
}
func TestDecoder_Decode_ErrorString(t *testing.T) {
var data = []byte{
data := []byte{
0x08, 0x00, 0x04, 0x6e, 0x61, 0x6d, 0x65, 0xFF, 0xFE,
0x42, 0x61, 0x6e, 0x61, 0x6e, 0x72, 0x61, 0x6d, 0x61,
}
//Unmarshal to string
// Unmarshal to string
var Name string
err := Unmarshal(data, &Name)
@ -401,7 +401,6 @@ func TestDecoder_Decode_ErrorString(t *testing.T) {
t.Error("should return a error if len < 0")
}
t.Log(err)
}
type TextBool bool
@ -409,6 +408,7 @@ type TextBool bool
func (b TextBool) MarshalText() (text []byte, err error) {
return []byte(strconv.FormatBool(bool(b))), nil
}
func (b *TextBool) UnmarshalText(text []byte) (err error) {
*((*bool)(b)), err = strconv.ParseBool(string(text))
return

View File

@ -390,7 +390,8 @@ func (e *Encoder) writeInt32(n int32) error {
func (e *Encoder) writeInt64(n int64) error {
_, err := e.w.Write([]byte{
byte(n >> 56), byte(n >> 48), byte(n >> 40), byte(n >> 32),
byte(n >> 24), byte(n >> 16), byte(n >> 8), byte(n)})
byte(n >> 24), byte(n >> 16), byte(n >> 8), byte(n),
})
return err
}

View File

@ -12,7 +12,8 @@ import (
func TestEncoder_Encode_intArray(t *testing.T) {
// Test marshal pure Int array
v := []int32{0, -10, 3}
out := []byte{TagIntArray, 0x00, 0x00, 0, 0, 0, 3,
out := []byte{
TagIntArray, 0x00, 0x00, 0, 0, 0, 3,
0x00, 0x00, 0x00, 0x00,
0xff, 0xff, 0xff, 0xf6,
0x00, 0x00, 0x00, 0x03,
@ -27,7 +28,8 @@ func TestEncoder_Encode_intArray(t *testing.T) {
v2 := struct {
Ary []int32 `nbt:"ary"`
}{[]int32{0, -10, 3}}
out = []byte{TagCompound, 0x00, 0x00,
out = []byte{
TagCompound, 0x00, 0x00,
TagIntArray, 0x00, 0x03, 'a', 'r', 'y', 0, 0, 0, 3,
0x00, 0x00, 0x00, 0x00, // 0
0xff, 0xff, 0xff, 0xf6, // -10
@ -72,7 +74,8 @@ func TestEncoder_encodeBool(t *testing.T) {
func TestEncoder_Encode_floatArray(t *testing.T) {
// Test marshal pure Int array
v := []float32{0.3, -100, float32(math.NaN())}
out := []byte{TagList, 0x00, 0x00, TagFloat, 0, 0, 0, 3,
out := []byte{
TagList, 0x00, 0x00, TagFloat, 0, 0, 0, 3,
0x3e, 0x99, 0x99, 0x9a, // 0.3
0xc2, 0xc8, 0x00, 0x00, // -100
0x7f, 0xc0, 0x00, 0x00, // NaN
@ -86,8 +89,10 @@ func TestEncoder_Encode_floatArray(t *testing.T) {
func TestEncoder_Encode_string(t *testing.T) {
v := "Test"
out := []byte{TagString, 0x00, 0x00, 0, 4,
'T', 'e', 's', 't'}
out := []byte{
TagString, 0x00, 0x00, 0, 4,
'T', 'e', 's', 't',
}
if data, err := Marshal(v); err != nil {
t.Error(err)

View File

@ -62,7 +62,7 @@ func ExampleDecoder_Decode_singleTagString() {
}
func ExampleEncoder_Encode_tagCompound() {
var value = struct {
value := struct {
Name string `nbt:"name"`
}{"Tnze"}

View File

@ -398,7 +398,7 @@ func (d *decodeState) scanNext() {
d.opcode = d.scan.step(&d.scan, d.data[d.off])
d.off++
} else {
//d.opcode = d.scan.eof()
// d.opcode = d.scan.eof()
d.off = len(d.data) + 1 // mark processed EOF with len+1
}
}

View File

@ -6,7 +6,7 @@ import (
)
func TestSNBT_checkScanCode(t *testing.T) {
//t.SkipNow()
// t.SkipNow()
var s scanner
s.reset()
for _, c := range []byte(`{b:[vanilla],c:0D}`) {
@ -66,6 +66,7 @@ func TestSNBT_compound(t *testing.T) {
}
}
}
func TestSNBT_list(t *testing.T) {
goods := []string{
`[]`, `[a, 'b', "c", d]`, // List of string

View File

@ -1,5 +1,4 @@
//From https://play.golang.org/p/LTbId4b6M2
// Package CFB8 is copied from https://play.golang.org/p/LTbId4b6M2
package CFB8
import "crypto/cipher"

View File

@ -213,8 +213,8 @@ func (c *Conn) WritePacket(p pk.Packet) error {
// SetCipher load the decode/encode stream to this Conn
func (c *Conn) SetCipher(ecoStream, decoStream cipher.Stream) {
//加密连接
c.Reader = cipher.StreamReader{ //Set receiver for AES
// 加密连接
c.Reader = cipher.StreamReader{ // Set receiver for AES
S: decoStream,
R: c.Socket,
}

View File

@ -37,6 +37,7 @@ func TestVarInt_WriteTo(t *testing.T) {
}
}
}
func TestVarInt_ReadFrom(t *testing.T) {
for i, v := range PackedVarInts {
var vi pk.VarInt
@ -51,7 +52,7 @@ func TestVarInt_ReadFrom(t *testing.T) {
func TestVarInt_ReadFrom_tooLongData(t *testing.T) {
var vi pk.VarInt
var data = []byte{0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x01}
data := []byte{0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x01}
if _, err := vi.ReadFrom(bytes.NewReader(data)); err != nil {
t.Logf("unpack \"% x\" error: %v", data, err)
} else {
@ -87,6 +88,7 @@ func TestVarLong_WriteTo(t *testing.T) {
}
}
}
func TestVarLong_ReadFrom(t *testing.T) {
for i, v := range PackedVarLongs {
var vi pk.VarLong

View File

@ -3,10 +3,11 @@ package packet
import (
"bytes"
"errors"
"github.com/google/uuid"
"io"
"math"
"github.com/google/uuid"
"github.com/Tnze/go-mc/nbt"
)
@ -23,62 +24,64 @@ type FieldEncoder io.WriterTo
type FieldDecoder io.ReaderFrom
type (
//Boolean of True is encoded as 0x01, false as 0x00.
// Boolean of True is encoded as 0x01, false as 0x00.
Boolean bool
//Byte is signed 8-bit integer, two's complement
// Byte is signed 8-bit integer, two's complement
Byte int8
//UnsignedByte is unsigned 8-bit integer
// UnsignedByte is unsigned 8-bit integer
UnsignedByte uint8
//Short is signed 16-bit integer, two's complement
// Short is signed 16-bit integer, two's complement
Short int16
//UnsignedShort is unsigned 16-bit integer
// UnsignedShort is unsigned 16-bit integer
UnsignedShort uint16
//Int is signed 32-bit integer, two's complement
// Int is signed 32-bit integer, two's complement
Int int32
//Long is signed 64-bit integer, two's complement
// Long is signed 64-bit integer, two's complement
Long int64
//A Float is a single-precision 32-bit IEEE 754 floating point number
// A Float is a single-precision 32-bit IEEE 754 floating point number
Float float32
//A Double is a double-precision 64-bit IEEE 754 floating point number
// A Double is a double-precision 64-bit IEEE 754 floating point number
Double float64
//String is sequence of Unicode scalar values
// String is sequence of Unicode scalar values
String string
//Chat is encoded as a String with max length of 32767.
// Chat is encoded as a String with max length of 32767.
// Deprecated: Use chat.Message
Chat = String
//Identifier is encoded as a String with max length of 32767.
// Identifier is encoded as a String with max length of 32767.
Identifier = String
//VarInt is variable-length data encoding a two's complement signed 32-bit integer
// VarInt is variable-length data encoding a two's complement signed 32-bit integer
VarInt int32
//VarLong is variable-length data encoding a two's complement signed 64-bit integer
// VarLong is variable-length data encoding a two's complement signed 64-bit integer
VarLong int64
//Position x as a 26-bit integer, followed by y as a 12-bit integer, followed by z as a 26-bit integer (all signed, two's complement)
// Position x as a 26-bit integer, followed by y as a 12-bit integer, followed by z as a 26-bit integer (all signed, two's complement)
Position struct {
X, Y, Z int
}
//Angle is rotation angle in steps of 1/256 of a full turn
// Angle is rotation angle in steps of 1/256 of a full turn
Angle Byte
//UUID encoded as an unsigned 128-bit integer
// UUID encoded as an unsigned 128-bit integer
UUID uuid.UUID
//ByteArray is []byte with prefix VarInt as length
// ByteArray is []byte with prefix VarInt as length
ByteArray []byte
//PluginMessageData is only used in LoginPlugin,and it will read all left bytes
// PluginMessageData is only used in LoginPlugin,and it will read all left bytes
PluginMessageData []byte
//BitSet represents Java's BitSet, a list of bits.
// BitSet represents Java's BitSet, a list of bits.
BitSet []int64
)
const MaxVarIntLen = 5
const MaxVarLongLen = 10
const (
MaxVarIntLen = 5
MaxVarLongLen = 10
)
func (b Boolean) WriteTo(w io.Writer) (int64, error) {
var v byte
@ -112,7 +115,7 @@ func (s String) WriteTo(w io.Writer) (int64, error) {
}
func (s *String) ReadFrom(r io.Reader) (n int64, err error) {
var l VarInt //String length
var l VarInt // String length
nn, err := l.ReadFrom(r)
if err != nil {
@ -245,7 +248,7 @@ func (l *Long) ReadFrom(r io.Reader) (n int64, err error) {
}
func (v VarInt) WriteTo(w io.Writer) (n int64, err error) {
var vi = make([]byte, 0, MaxVarIntLen)
vi := make([]byte, 0, MaxVarIntLen)
num := uint32(v)
for {
b := num & 0x7F
@ -282,7 +285,7 @@ func (v *VarInt) ReadFrom(r io.Reader) (n int64, err error) {
}
func (v VarLong) WriteTo(w io.Writer) (n int64, err error) {
var vi = make([]byte, 0, MaxVarLongLen)
vi := make([]byte, 0, MaxVarLongLen)
num := uint64(v)
for {
b := num & 0x7F
@ -340,7 +343,7 @@ func (p *Position) ReadFrom(r io.Reader) (n int64, err error) {
y := int(v & 0xFFF)
z := int(v << 26 >> 38)
//处理负数
// 处理负数
if x >= 1<<25 {
x -= 1 << 26
}

View File

@ -3,8 +3,9 @@ package packet_test
import (
"bytes"
"fmt"
pk "github.com/Tnze/go-mc/net/packet"
"testing"
pk "github.com/Tnze/go-mc/net/packet"
)
func ExampleAry_WriteTo() {
@ -34,12 +35,12 @@ func ExampleAry_ReadFrom() {
func TestAry_ReadFrom(t *testing.T) {
var ary []pk.String
var bin = []byte{
bin := []byte{
0, 0, 0, 2,
4, 'T', 'n', 'z', 'e',
0,
}
var data = pk.Ary[pk.Int]{Ary: &ary}
data := pk.Ary[pk.Int]{Ary: &ary}
if _, err := data.ReadFrom(bytes.NewReader(bin)); err != nil {
t.Fatal(err)
}

View File

@ -24,14 +24,14 @@ func DialRCON(addr string, password string) (client RCONClientConn, err error) {
return
}
//Login
// Login
err = c.WritePacket(c.ReqID, 3, password)
if err != nil {
err = fmt.Errorf("login fail: %v", err)
return
}
//Login resp
// Login resp
r, _, _, err := c.ReadPacket()
if err != nil {
err = fmt.Errorf("read login resp fail: %v", err)
@ -55,7 +55,7 @@ type RCONConn struct {
}
func (r *RCONConn) ReadPacket() (RequestID, Type int32, Payload string, err error) {
//read packet length
// read packet length
var Length int32
err = binary.Read(r, binary.LittleEndian, &Length)
if err != nil {
@ -63,7 +63,7 @@ func (r *RCONConn) ReadPacket() (RequestID, Type int32, Payload string, err erro
return
}
//check length
// check length
if Length < 4+4+0+2 {
err = errors.New("packet too short")
return
@ -73,7 +73,7 @@ func (r *RCONConn) ReadPacket() (RequestID, Type int32, Payload string, err erro
return
}
//read packet data
// read packet data
buf := make([]byte, Length)
err = binary.Read(r, binary.LittleEndian, &buf)
if err != nil {
@ -91,11 +91,11 @@ func (r *RCONConn) WritePacket(RequestID, Type int32, Payload string) error {
buf := new(bytes.Buffer)
for _, v := range []interface{}{
int32(4 + 4 + len(Payload) + 2), //Length
RequestID, //Request ID
Type, //Type
[]byte(Payload), //Payload
[]byte{0, 0}, //pad
int32(4 + 4 + len(Payload) + 2), // Length
RequestID, // Request ID
Type, // Type
[]byte(Payload), // Payload
[]byte{0, 0}, // pad
} {
err := binary.Write(buf, binary.LittleEndian, v)
if err != nil {
@ -136,7 +136,7 @@ func (r *RCONConn) AcceptLogin(password string) error {
r.ReqID = R
//Check packet type
// Check packet type
if T != 3 {
return fmt.Errorf("not a login packet: %d", T)
}
@ -165,7 +165,7 @@ func (r *RCONConn) AcceptCmd() (string, error) {
r.ReqID = R
//Check packet type
// Check packet type
if T != 2 {
return P, fmt.Errorf("not a command packet: %d", T)
}

View File

@ -2,12 +2,13 @@ package offline
import (
"crypto/md5"
"github.com/google/uuid"
)
// NameToUUID return the UUID from player name in offline mode
func NameToUUID(name string) uuid.UUID {
var version = 3
version := 3
h := md5.New()
h.Write([]byte("OfflinePlayer:"))
h.Write([]byte(name))

View File

@ -23,7 +23,7 @@ type Server struct {
MiniGameID *int
MinigameImage *string
ActiveSlot int
//Slots interface{}
// Slots interface{}
Member bool
}
@ -49,7 +49,7 @@ func (r *Realms) Worlds() ([]Server, error) {
// Server returns a single server listing about a server.
// you must be the owner of the server.
func (r *Realms) Server(ID int) (s Server, err error) {
var resp = struct {
resp := struct {
*Server
*Error
}{Server: &s}
@ -124,7 +124,7 @@ func (r *Realms) Ops(s Server) (ops []string, err error) {
// SubscriptionLife returns the current life of a server subscription.
func (r *Realms) SubscriptionLife(s Server) (startDate int64, daysLeft int, Type string, err error) {
var resp = struct {
resp := struct {
StartDate *int64
DaysLeft *int
SubscriptionType *string

View File

@ -1,8 +1,9 @@
package save
import (
"github.com/Tnze/go-mc/nbt"
"io"
"github.com/Tnze/go-mc/nbt"
)
type PlayerData struct {

View File

@ -37,7 +37,7 @@ func At(cx, cz int) (int, int) {
// Open a .mca file and read the head.
// Close the Region after used.
func Open(name string) (r *Region, err error) {
f, err := os.OpenFile(name, os.O_RDWR, 0666)
f, err := os.OpenFile(name, os.O_RDWR, 0o666)
if err != nil {
return nil, err
}
@ -85,7 +85,7 @@ func Load(f io.ReadWriteSeeker) (r *Region, err error) {
// Create open .mca file with os.O_CREATE|os. O_EXCL, and init the region
func Create(name string) (*Region, error) {
f, err := os.OpenFile(name, os.O_CREATE|os.O_RDWR|os.O_EXCL, 0666)
f, err := os.OpenFile(name, os.O_CREATE|os.O_RDWR|os.O_EXCL, 0o666)
if err != nil {
return nil, err
}
@ -206,13 +206,13 @@ func (r *Region) WriteSector(x, z int, data []byte) error {
if err != nil {
return err
}
//data length
// data length
err = binary.Write(r.f, binary.BigEndian, int32(len(data)))
if err != nil {
return err
}
//data
// data
_, err = r.f.Write(data)
if err != nil {
return err

View File

@ -48,10 +48,9 @@ func TestReadRegion(t *testing.T) {
if err != nil {
t.Error(err)
}
//t.Log(b)
// t.Log(b)
}
}
}
func TestFindSpace(t *testing.T) {

View File

@ -28,7 +28,7 @@ const verifyTokenLen = 16
// Encrypt a connection, with authentication
func Encrypt(conn *net.Conn, name string, profilePubKey *rsa.PublicKey) (*Resp, error) {
//generate keys
// generate keys
key, err := rsa.GenerateKey(rand.Reader, 1024)
if err != nil {
return nil, err
@ -39,30 +39,30 @@ func Encrypt(conn *net.Conn, name string, profilePubKey *rsa.PublicKey) (*Resp,
return nil, err
}
//encryption request
// encryption request
nonce, err := encryptionRequest(conn, publicKey)
if err != nil {
return nil, err
}
//encryption response
// encryption response
SharedSecret, err := encryptionResponse(conn, profilePubKey, nonce, key)
if err != nil {
return nil, err
}
//encryption the connection
// encryption the connection
block, err := aes.NewCipher(SharedSecret)
if err != nil {
return nil, errors.New("load aes encryption key fail")
}
conn.SetCipher( //启用加密
conn.SetCipher( // 启用加密
CFB8.NewCFB8Encrypt(block, SharedSecret),
CFB8.NewCFB8Decrypt(block, SharedSecret),
)
hash := authDigest("", SharedSecret, publicKey)
resp, err := authentication(name, hash) //auth
resp, err := authentication(name, hash) // auth
if err != nil {
return nil, errors.New("auth servers down")
}
@ -136,7 +136,7 @@ func encryptionResponse(conn *net.Conn, profilePubKey *rsa.PublicKey, nonce []by
}
}
//confirm to verify token
// confirm to verify token
SharedSecret, err := rsa.DecryptPKCS1v15(rand.Reader, key, ESharedSecret)
if err != nil {
return nil, err

View File

@ -15,17 +15,17 @@ func TestResp(t *testing.T) {
}
wantID := uuid.Must(uuid.Parse("853c80ef3c3749fdaa49938b674adae6"))
//check UUID
// check UUID
if resp.ID != wantID {
t.Errorf("uuid doesn't match: %v, want %s", resp.ID, wantID)
}
//check name
// check name
if resp.Name != "jeb_" {
t.Errorf("name doesn't match: %s, want %s", resp.Name, "jeb_")
}
//check texture
// check texture
texture, err := resp.Texture()
if err != nil {
t.Fatal(err)

View File

@ -10,8 +10,10 @@ import (
// Packet758 is a packet in protocol 757.
// We are using type system to force programmers to update packets.
type Packet758 pk.Packet
type Packet757 pk.Packet
type (
Packet758 pk.Packet
Packet757 pk.Packet
)
type WritePacketError struct {
Err error

View File

@ -70,8 +70,10 @@ type Node struct {
Parser Parser
Run HandlerFunc
}
type Literal Node
type Argument Node
type (
Literal Node
Argument Node
)
func (n *Node) parse(cmd string) (left string, value ParsedData, err error) {
switch n.kind & 0x03 {

View File

@ -1,4 +1,6 @@
package server
type Pos struct{ X, Y, Z float64 }
type Rot struct{ Yaw, Pitch float32 }
type (
Pos struct{ X, Y, Z float64 }
Rot struct{ Yaw, Pitch float32 }
)

View File

@ -3,6 +3,7 @@ package bvh
import (
"container/heap"
"fmt"
"golang.org/x/exp/constraints"
)
@ -194,12 +195,14 @@ func TouchBound[B interface{ Touch(B) bool }](other B) func(bound B) bool {
}
}
type searchHeap[I constraints.Float, V any] []searchItem[I, V]
type searchItem[I constraints.Float, V any] struct {
type (
searchHeap[I constraints.Float, V any] []searchItem[I, V]
searchItem[I constraints.Float, V any] struct {
pointer *V
parentTo **V
inheritedCost I
}
}
)
func (h searchHeap[I, V]) Len() int { return len(h) }
func (h searchHeap[I, V]) Less(i, j int) bool { return h[i].inheritedCost < h[j].inheritedCost }

View File

@ -1,8 +1,9 @@
package bvh
import (
"golang.org/x/exp/constraints"
"math"
"golang.org/x/exp/constraints"
)
type Vec2[I constraints.Signed | constraints.Float] [2]I
@ -22,6 +23,7 @@ type Vec3[I constraints.Signed | constraints.Float] [3]I
func (v Vec3[I]) Add(other Vec3[I]) Vec3[I] {
return Vec3[I]{v[0] + other[0], v[1] + other[1], v[2] + other[2]}
}
func (v Vec3[I]) Sub(other Vec3[I]) Vec3[I] {
return Vec3[I]{v[0] - other[0], v[1] - other[1], v[2] - other[2]}
}
@ -29,6 +31,7 @@ func (v Vec3[I]) Mul(i I) Vec3[I] { return Vec3[I]{v[0] * i, v[1] * i, v[2] * i}
func (v Vec3[I]) Max(other Vec3[I]) Vec3[I] {
return Vec3[I]{max(v[0], other[0]), max(v[1], other[1]), max(v[2], other[2])}
}
func (v Vec3[I]) Min(other Vec3[I]) Vec3[I] {
return Vec3[I]{min(v[0], other[0]), min(v[1], other[1]), min(v[2], other[2])}
}

View File

@ -50,14 +50,14 @@ func (s *Server) acceptListPing(conn *net.Conn) {
}
switch p.ID {
case packetid.StatusResponse: //List
case packetid.StatusResponse: // List
var resp []byte
resp, err = s.listResp()
if err != nil {
break
}
err = conn.WritePacket(pk.Marshal(0x00, pk.String(resp)))
case packetid.StatusPongResponse: //Ping
case packetid.StatusPongResponse: // Ping
err = conn.WritePacket(p)
}
if err != nil {

View File

@ -59,7 +59,7 @@ type authResp struct {
type Profile struct {
ID string `json:"id"`
Name string `json:"name"`
//Legacy bool `json:"legacy"` // we don't care
// Legacy bool `json:"legacy"` // we don't care
}
// Authenticate authenticates a user using their password.
@ -105,6 +105,7 @@ func (a *Access) AvailableProfiles() []Profile {
func (a *Access) SetTokens(tokens Tokens) {
a.ar.Tokens = tokens
}
func (a *Access) GetTokens() Tokens {
return a.ar.Tokens
}

View File

@ -17,7 +17,7 @@ type refreshPayload struct {
func (a *Access) Refresh(profile *Profile) error {
pl := refreshPayload{
Tokens: a.ar.Tokens,
SelectedProfile: profile, //used to change profile, don't use now
SelectedProfile: profile, // used to change profile, don't use now
RequestUser: true,
}