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 package basic
import ( import (
"github.com/Tnze/go-mc/chat"
"unsafe" "unsafe"
"github.com/Tnze/go-mc/chat"
"github.com/Tnze/go-mc/data/packetid" "github.com/Tnze/go-mc/data/packetid"
"github.com/Tnze/go-mc/nbt" "github.com/Tnze/go-mc/nbt"
pk "github.com/Tnze/go-mc/net/packet" pk "github.com/Tnze/go-mc/net/packet"
@ -148,6 +149,7 @@ func (p *Player) handleLoginPacket(packet pk.Packet) error {
} }
return nil return nil
} }
func (p *Player) handleRespawnPacket(packet pk.Packet) error { func (p *Player) handleRespawnPacket(packet pk.Packet) error {
var copyMeta bool var copyMeta bool
err := packet.Scan( err := packet.Scan(

View File

@ -79,7 +79,9 @@ func (p *PlayerMessage) ReadFrom(r io.Reader) (n int64, err error) {
var timestamp pk.Long var timestamp pk.Long
var unsignedContent chat.Message var unsignedContent chat.Message
n, err = pk.Tuple{ n, err = pk.Tuple{
&hasMsgSign, pk.Opt{Has: &hasMsgSign, &hasMsgSign,
pk.Opt{
Has: &hasMsgSign,
Field: (*pk.ByteArray)(&p.signature), Field: (*pk.ByteArray)(&p.signature),
}, },
(*pk.UUID)(&p.sender), (*pk.UUID)(&p.sender),
@ -88,7 +90,9 @@ func (p *PlayerMessage) ReadFrom(r io.Reader) (n int64, err error) {
&timestamp, &timestamp,
(*pk.Long)(&p.salt), (*pk.Long)(&p.salt),
pk.Array(&p.prevMessages), pk.Array(&p.prevMessages),
&hasUnsignedContent, pk.Opt{Has: &hasUnsignedContent, &hasUnsignedContent,
pk.Opt{
Has: &hasUnsignedContent,
Field: &unsignedContent, Field: &unsignedContent,
}, },
(*pk.VarInt)(&p.filterType), (*pk.VarInt)(&p.filterType),

View File

@ -35,12 +35,14 @@ func (e *Events) AddGeneric(listeners ...PacketHandler) {
} }
} }
type PacketHandlerFunc func(p pk.Packet) error type (
type PacketHandler struct { PacketHandlerFunc func(p pk.Packet) error
PacketHandler struct {
ID packetid.ClientboundPacketID ID packetid.ClientboundPacketID
Priority int Priority int
F func(p pk.Packet) error F func(p pk.Packet) error
} }
)
// handlerHeap is PriorityQueue<PacketHandlerFunc> // handlerHeap is PriorityQueue<PacketHandlerFunc>
type handlerHeap []PacketHandler type handlerHeap []PacketHandler

View File

@ -1,9 +1,9 @@
package bot package bot
import ( import (
"encoding/hex"
"log" "log"
"encoding/hex"
"github.com/Tnze/go-mc/offline" "github.com/Tnze/go-mc/offline"
"github.com/Tnze/go-mc/yggdrasil" "github.com/Tnze/go-mc/yggdrasil"
) )
@ -33,10 +33,7 @@ func ExampleClient_JoinServer_offline() {
log.Println("Login success") log.Println("Login success")
// Register event handlers // Register event handlers
// c.Events.GameStart = onGameStartFunc // c.Events.AddListener(...)
// c.Events.ChatMsg = onChatMsgFunc
// c.Events.Disconnect = onDisconnectFunc
// ...
// JoinGame // JoinGame
err = c.HandleGame() err = c.HandleGame()

View File

@ -22,8 +22,10 @@ import (
) )
// ProtocolVersion is the protocol version number of minecraft net protocol // ProtocolVersion is the protocol version number of minecraft net protocol
const ProtocolVersion = 760 const (
const DefaultPort = mcnet.DefaultPort ProtocolVersion = 760
DefaultPort = mcnet.DefaultPort
)
// JoinServer connect a Minecraft server for playing the game. // JoinServer connect a Minecraft server for playing the game.
// Using roughly the same way to parse address as minecraft. // Using roughly the same way to parse address as minecraft.

View File

@ -207,6 +207,7 @@ var fmtCode = map[byte]string{
'o': "3", 'o': "3",
'r': "0", 'r': "0",
} }
var colors = map[string]string{ var colors = map[string]string{
Black: "30", Black: "30",
DarkBlue: "34", DarkBlue: "34",

View File

@ -1,3 +1,4 @@
//go:build generate
// +build generate // +build generate
// This program can automatically download language.json file and convert into .go // This program can automatically download language.json file and convert into .go
@ -17,9 +18,8 @@ import (
"text/template" "text/template"
) )
var (
// language=gohtml // language=gohtml
langTmpl = `// Code generated by downloader.go; DO NOT EDIT. var langTmpl = `// Code generated by downloader.go; DO NOT EDIT.
package {{.Name}} package {{.Name}}
{{if ne .Name "en_us"}} {{if ne .Name "en_us"}}
import "github.com/Tnze/go-mc/chat" import "github.com/Tnze/go-mc/chat"
@ -28,7 +28,6 @@ func init() { chat.SetLanguage(Map) }
{{end}} {{end}}
var Map = {{.LangMap | printf "%#v"}} var Map = {{.LangMap | printf "%#v"}}
` `
)
//go:generate go run $GOFILE //go:generate go run $GOFILE
//go:generate go fmt ./... //go:generate go fmt ./...
@ -112,12 +111,12 @@ func readLang(name string, r io.Reader) {
pName := strings.ReplaceAll(name, "_", "-") pName := strings.ReplaceAll(name, "_", "-")
// mkdir // mkdir
err = os.Mkdir(pName, 0777) err = os.Mkdir(pName, 0o777)
if err != nil && !os.IsExist(err) { if err != nil && !os.IsExist(err) {
panic(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 { if err != nil {
panic(err) panic(err)
} }

View File

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

View File

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

View File

@ -1,7 +1,6 @@
package main package main
import ( import (
botchat "github.com/Tnze/go-mc/bot/chat"
"log" "log"
"time" "time"
@ -9,6 +8,7 @@ import (
"github.com/Tnze/go-mc/bot" "github.com/Tnze/go-mc/bot"
"github.com/Tnze/go-mc/bot/basic" "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/chat"
_ "github.com/Tnze/go-mc/data/lang/en-us" _ "github.com/Tnze/go-mc/data/lang/en-us"
"github.com/Tnze/go-mc/data/packetid" "github.com/Tnze/go-mc/data/packetid"

View File

@ -21,16 +21,20 @@ import (
"github.com/Tnze/go-mc/level" "github.com/Tnze/go-mc/level"
) )
var address = flag.String("address", "127.0.0.1", "The server address") var (
var name = flag.String("name", "Daze", "The player's name") address = flag.String("address", "127.0.0.1", "The server address")
var playerID = flag.String("uuid", "", "The player's UUID") name = flag.String("name", "Daze", "The player's name")
var accessToken = flag.String("token", "", "AccessToken") playerID = flag.String("uuid", "", "The player's UUID")
accessToken = flag.String("token", "", "AccessToken")
)
var client *bot.Client var (
var player *basic.Player client *bot.Client
var chatHandler *botchat.Chat player *basic.Player
var worldManager *world.World chatHandler *botchat.Chat
var screenManager *screen.Manager worldManager *world.World
screenManager *screen.Manager
)
func main() { func main() {
flag.Parse() flag.Parse()

View File

@ -18,10 +18,12 @@ import (
pk "github.com/Tnze/go-mc/net/packet" pk "github.com/Tnze/go-mc/net/packet"
) )
var address = flag.String("address", "127.0.0.1", "The server address") var (
var client *bot.Client address = flag.String("address", "127.0.0.1", "The server address")
var player *basic.Player client *bot.Client
var screenManager *screen.Manager player *basic.Player
screenManager *screen.Manager
)
func main() { func main() {
flag.Parse() flag.Parse()
@ -62,8 +64,7 @@ func onCommands(p pk.Packet) error {
return nil return nil
} }
type Node struct { type Node struct{}
}
func (n Node) ReadFrom(r io.Reader) (int64, error) { func (n Node) ReadFrom(r io.Reader) (int64, error) {
var Flags pk.Byte var Flags pk.Byte
@ -71,7 +72,7 @@ func (n Node) ReadFrom(r io.Reader) (int64, error) {
var Redirect pk.VarInt var Redirect pk.VarInt
var Name pk.String var Name pk.String
var Parser pk.Identifier var Parser pk.Identifier
var Properties = Prop{Type: &Parser} Properties := Prop{Type: &Parser}
var SuggestionsType pk.Identifier var SuggestionsType pk.Identifier
m, err := pk.Tuple{ m, err := pk.Tuple{
&Flags, &Flags,

View File

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

View File

@ -13,8 +13,10 @@ import (
"github.com/Tnze/go-mc/save/region" "github.com/Tnze/go-mc/save/region"
) )
var decomp = flag.Bool("x", false, "decompress each chunk to NBT format") var (
var repack = flag.Bool("p", false, "repack .mcc file to .mca") decomp = flag.Bool("x", false, "decompress each chunk to NBT format")
repack = flag.Bool("p", false, "repack .mcc file to .mca")
)
func main() { func main() {
flag.Usage = usage flag.Usage = usage
@ -91,7 +93,7 @@ func unpack(f, o string) {
checkerr(err) 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) checkerr(err)
_, err = io.Copy(cf, r) _, err = io.Copy(cf, r)

View File

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

View File

@ -13,8 +13,10 @@ import (
"github.com/Tnze/go-mc/chat" "github.com/Tnze/go-mc/chat"
) )
var address = flag.String("address", "127.0.0.1", "The server address") var (
var number = flag.Int("number", 1023, "The number of clients") address = flag.String("address", "127.0.0.1", "The server address")
number = flag.Int("number", 1023, "The number of clients")
)
func main() { func main() {
flag.Parse() flag.Parse()

View File

@ -11,8 +11,10 @@ import (
"github.com/Tnze/go-mc/offline" "github.com/Tnze/go-mc/offline"
) )
const ProtocolVersion = 578 const (
const MaxPlayer = 200 ProtocolVersion = 578
MaxPlayer = 200
)
// Packet IDs // Packet IDs
const ( const (

View File

@ -13,8 +13,10 @@ import (
"github.com/Tnze/go-mc/offline" "github.com/Tnze/go-mc/offline"
) )
const ProtocolVersion = 756 const (
const MaxPlayer = 200 ProtocolVersion = 756
MaxPlayer = 200
)
// Packet IDs // Packet IDs
const ( const (

View File

@ -2,11 +2,12 @@ package main
import ( import (
"encoding/json" "encoding/json"
"log"
"github.com/Tnze/go-mc/chat" "github.com/Tnze/go-mc/chat"
"github.com/Tnze/go-mc/net" "github.com/Tnze/go-mc/net"
pk "github.com/Tnze/go-mc/net/packet" pk "github.com/Tnze/go-mc/net/packet"
"github.com/google/uuid" "github.com/google/uuid"
"log"
) )
func acceptListPing(conn net.Conn) { func acceptListPing(conn net.Conn) {

View File

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

View File

@ -1,14 +1,17 @@
package level package level
import ( import (
pk "github.com/Tnze/go-mc/net/packet"
"math/bits" "math/bits"
"reflect" "reflect"
"testing" "testing"
pk "github.com/Tnze/go-mc/net/packet"
) )
var data = []uint64{0x0020863148418841, 0x01018A7260F68C87} var (
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} 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) { func TestBitStorage_Get(t *testing.T) {
bs := NewBitStorage(5, 24, data) bs := NewBitStorage(5, 24, data)

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -11,7 +11,7 @@ import (
) )
func TestUnmarshal_string(t *testing.T) { func TestUnmarshal_string(t *testing.T) {
var data = []byte{ data := []byte{
0x08, 0x00, 0x04, 0x6e, 0x61, 0x6d, 0x65, 0x00, 0x09, 0x08, 0x00, 0x04, 0x6e, 0x61, 0x6d, 0x65, 0x00, 0x09,
0x42, 0x61, 0x6e, 0x61, 0x6e, 0x72, 0x61, 0x6d, 0x61, 0x42, 0x61, 0x6e, 0x61, 0x6e, 0x72, 0x61, 0x6d, 0x61,
} }
@ -38,7 +38,7 @@ func TestUnmarshal_string(t *testing.T) {
} }
func TestUnmarshal_simple(t *testing.T) { func TestUnmarshal_simple(t *testing.T) {
var data = []byte{ data := []byte{
0x0a, 0x00, 0x0b, 0x68, 0x65, 0x6c, 0x6c, 0x6f, 0x0a, 0x00, 0x0b, 0x68, 0x65, 0x6c, 0x6c, 0x6f,
0x20, 0x77, 0x6f, 0x72, 0x6c, 0x64, 0x08, 0x00, 0x20, 0x77, 0x6f, 0x72, 0x6c, 0x64, 0x08, 0x00,
0x04, 0x6e, 0x61, 0x6d, 0x65, 0x00, 0x09, 0x42, 0x04, 0x6e, 0x61, 0x6d, 0x65, 0x00, 0x09, 0x42,
@ -388,7 +388,7 @@ func TestDecoder_Decode_bool(t *testing.T) {
} }
func TestDecoder_Decode_ErrorString(t *testing.T) { func TestDecoder_Decode_ErrorString(t *testing.T) {
var data = []byte{ data := []byte{
0x08, 0x00, 0x04, 0x6e, 0x61, 0x6d, 0x65, 0xFF, 0xFE, 0x08, 0x00, 0x04, 0x6e, 0x61, 0x6d, 0x65, 0xFF, 0xFE,
0x42, 0x61, 0x6e, 0x61, 0x6e, 0x72, 0x61, 0x6d, 0x61, 0x42, 0x61, 0x6e, 0x61, 0x6e, 0x72, 0x61, 0x6d, 0x61,
} }
@ -401,7 +401,6 @@ func TestDecoder_Decode_ErrorString(t *testing.T) {
t.Error("should return a error if len < 0") t.Error("should return a error if len < 0")
} }
t.Log(err) t.Log(err)
} }
type TextBool bool type TextBool bool
@ -409,6 +408,7 @@ type TextBool bool
func (b TextBool) MarshalText() (text []byte, err error) { func (b TextBool) MarshalText() (text []byte, err error) {
return []byte(strconv.FormatBool(bool(b))), nil return []byte(strconv.FormatBool(bool(b))), nil
} }
func (b *TextBool) UnmarshalText(text []byte) (err error) { func (b *TextBool) UnmarshalText(text []byte) (err error) {
*((*bool)(b)), err = strconv.ParseBool(string(text)) *((*bool)(b)), err = strconv.ParseBool(string(text))
return return

View File

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

View File

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

View File

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

View File

@ -66,6 +66,7 @@ func TestSNBT_compound(t *testing.T) {
} }
} }
} }
func TestSNBT_list(t *testing.T) { func TestSNBT_list(t *testing.T) {
goods := []string{ goods := []string{
`[]`, `[a, 'b', "c", d]`, // List of 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 package CFB8
import "crypto/cipher" import "crypto/cipher"

View File

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

View File

@ -3,10 +3,11 @@ package packet
import ( import (
"bytes" "bytes"
"errors" "errors"
"github.com/google/uuid"
"io" "io"
"math" "math"
"github.com/google/uuid"
"github.com/Tnze/go-mc/nbt" "github.com/Tnze/go-mc/nbt"
) )
@ -77,8 +78,10 @@ type (
BitSet []int64 BitSet []int64
) )
const MaxVarIntLen = 5 const (
const MaxVarLongLen = 10 MaxVarIntLen = 5
MaxVarLongLen = 10
)
func (b Boolean) WriteTo(w io.Writer) (int64, error) { func (b Boolean) WriteTo(w io.Writer) (int64, error) {
var v byte var v byte
@ -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) { 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) num := uint32(v)
for { for {
b := num & 0x7F 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) { 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) num := uint64(v)
for { for {
b := num & 0x7F b := num & 0x7F

View File

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

View File

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

View File

@ -49,7 +49,7 @@ func (r *Realms) Worlds() ([]Server, error) {
// Server returns a single server listing about a server. // Server returns a single server listing about a server.
// you must be the owner of the server. // you must be the owner of the server.
func (r *Realms) Server(ID int) (s Server, err error) { func (r *Realms) Server(ID int) (s Server, err error) {
var resp = struct { resp := struct {
*Server *Server
*Error *Error
}{Server: &s} }{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. // SubscriptionLife returns the current life of a server subscription.
func (r *Realms) SubscriptionLife(s Server) (startDate int64, daysLeft int, Type string, err error) { func (r *Realms) SubscriptionLife(s Server) (startDate int64, daysLeft int, Type string, err error) {
var resp = struct { resp := struct {
StartDate *int64 StartDate *int64
DaysLeft *int DaysLeft *int
SubscriptionType *string SubscriptionType *string

View File

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

View File

@ -37,7 +37,7 @@ func At(cx, cz int) (int, int) {
// Open a .mca file and read the head. // Open a .mca file and read the head.
// Close the Region after used. // Close the Region after used.
func Open(name string) (r *Region, err error) { 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 { if err != nil {
return nil, err 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 // Create open .mca file with os.O_CREATE|os. O_EXCL, and init the region
func Create(name string) (*Region, error) { 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 { if err != nil {
return nil, err return nil, err
} }

View File

@ -51,7 +51,6 @@ func TestReadRegion(t *testing.T) {
// t.Log(b) // t.Log(b)
} }
} }
} }
func TestFindSpace(t *testing.T) { func TestFindSpace(t *testing.T) {

View File

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

View File

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

View File

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

View File

@ -3,6 +3,7 @@ package bvh
import ( import (
"container/heap" "container/heap"
"fmt" "fmt"
"golang.org/x/exp/constraints" "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 (
type searchItem[I constraints.Float, V any] struct { searchHeap[I constraints.Float, V any] []searchItem[I, V]
searchItem[I constraints.Float, V any] struct {
pointer *V pointer *V
parentTo **V parentTo **V
inheritedCost I inheritedCost I
} }
)
func (h searchHeap[I, V]) Len() int { return len(h) } 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 } 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 package bvh
import ( import (
"golang.org/x/exp/constraints"
"math" "math"
"golang.org/x/exp/constraints"
) )
type Vec2[I constraints.Signed | constraints.Float] [2]I 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] { 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]} 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] { 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]} 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] { 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])} 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] { 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])} return Vec3[I]{min(v[0], other[0]), min(v[1], other[1]), min(v[2], other[2])}
} }

View File

@ -105,6 +105,7 @@ func (a *Access) AvailableProfiles() []Profile {
func (a *Access) SetTokens(tokens Tokens) { func (a *Access) SetTokens(tokens Tokens) {
a.ar.Tokens = tokens a.ar.Tokens = tokens
} }
func (a *Access) GetTokens() Tokens { func (a *Access) GetTokens() Tokens {
return a.ar.Tokens return a.ar.Tokens
} }