From 632df9138cae20713260b4e1d934206f749db645 Mon Sep 17 00:00:00 2001 From: Tnze Date: Sun, 12 Feb 2023 12:57:34 +0800 Subject: [PATCH] update the description text of the examples --- examples/mcadump/mcadump.go | 59 ++++++++----------- examples/mcping/mcping.go | 1 - examples/pressureTest/main.go | 12 ++-- examples/simpleServer1.15.2/main.go | 9 ++- examples/simpleServer1.17.1/main.go | 5 +- .../luncher.go => yggdrasil/launcher.go} | 5 ++ 6 files changed, 50 insertions(+), 41 deletions(-) rename examples/{luncher/luncher.go => yggdrasil/launcher.go} (61%) diff --git a/examples/mcadump/mcadump.go b/examples/mcadump/mcadump.go index 53a66e4..65eb5f1 100644 --- a/examples/mcadump/mcadump.go +++ b/examples/mcadump/mcadump.go @@ -1,3 +1,5 @@ +// This is an example of how to use the go-mc/save/region package to read and write .mca files. +// It can unpack .mca files to .mcc files, or repack .mcc files to .mca files, controlled by the -p flags. package main import ( @@ -29,8 +31,7 @@ func main() { usage() } for _, f := range args[1:] { - fs, err := filepath.Glob(f) - checkerr(err) + fs := must(filepath.Glob(f)) if *repack { for _, f := range fs { @@ -49,24 +50,12 @@ func usage() { os.Exit(1) } -// we use this function to check for laziness. Don't scold me >_< -func checkerr(err error) { - if err != nil { - _, _ = fmt.Fprintln(os.Stderr, err) - os.Exit(1) - } -} - func unpack(f, o string) { var x, z int rn := filepath.Base(f) - _, err := fmt.Sscanf(rn, "r.%d.%d.mca", &x, &z) - if err != nil { - checkerr(fmt.Errorf("cannot use %s as mca file name: %v", rn, err)) - } + must(fmt.Sscanf(rn, "r.%d.%d.mca", &x, &z)) - r, err := region.Open(f) - checkerr(err) + r := must(region.Open(f)) defer r.Close() for i := 0; i < 32; i++ { @@ -75,12 +64,12 @@ func unpack(f, o string) { continue } - data, err := r.ReadSector(i, j) - checkerr(err) + data := must(r.ReadSector(i, j)) var r io.Reader = bytes.NewReader(data[1:]) - fn := fmt.Sprintf("c.%d.%d.mcc", x*32+i, z*32+j) + fn := fmt.Sprintf("c.%d.%d.mcc", x<<5+i, z<<5+j) if *decomp { + var err error fn += ".nbt" // 解压后就是一个标准的NBT文件,可以加个.nbt后缀 switch data[0] { default: @@ -90,14 +79,12 @@ func unpack(f, o string) { case 2: r, err = zlib.NewReader(r) } - checkerr(err) + must(0, err) } - cf, err := os.OpenFile(filepath.Join(o, fn), os.O_CREATE|os.O_RDWR|os.O_EXCL, 0o666) - checkerr(err) + cf := must(os.OpenFile(filepath.Join(o, fn), os.O_CREATE|os.O_RDWR|os.O_EXCL, 0o666)) - _, err = io.Copy(cf, r) - checkerr(err) + must(io.Copy(cf, r)) } } } @@ -105,23 +92,27 @@ func unpack(f, o string) { func pack(f, o string) { var x, z int rn := filepath.Base(f) - _, err := fmt.Sscanf(rn, "c.%d.%d.mcc", &x, &z) - if err != nil { - checkerr(fmt.Errorf("cannot use %s as mcc file name: %v", rn, err)) - } + must(fmt.Sscanf(rn, "c.%d.%d.mcc", &x, &z)) fn := fmt.Sprintf("r.%d.%d.mca", x>>5, z>>5) r, err := region.Open(fn) if err != nil && os.IsNotExist(err) { - r, err = region.Create(filepath.Join(o, fn)) + r = must(region.Create(filepath.Join(o, fn))) + } else { + must(0, err) } - checkerr(err) defer r.Close() - mcc, err := os.ReadFile(f) - checkerr(err) + mcc := must(os.ReadFile(f)) rx, rz := region.In(x, z) - err = r.WriteSector(rx, rz, mcc) - checkerr(err) + must(0, r.WriteSector(rx, rz, mcc)) +} + +func must[T any](v T, err error) T { + if err != nil { + _, _ = fmt.Fprintln(os.Stderr, err) + os.Exit(1) + } + return v } diff --git a/examples/mcping/mcping.go b/examples/mcping/mcping.go index f4102cb..d348b51 100644 --- a/examples/mcping/mcping.go +++ b/examples/mcping/mcping.go @@ -7,7 +7,6 @@ import ( "flag" "fmt" "image" - _ "image/jpeg" "image/png" "os" "strings" diff --git a/examples/pressureTest/main.go b/examples/pressureTest/main.go index 4b86707..83ccaa4 100644 --- a/examples/pressureTest/main.go +++ b/examples/pressureTest/main.go @@ -1,3 +1,9 @@ +// This example is used to apply pressure test on the server. +// Testing the performance of the login handling system of go-mc/server package. +// It can be used to test other server implementations too. +// +// This program will create a lot of clients and let them log in to the server. +// The number of clients can be set by the -number flag. package main import ( @@ -6,8 +12,6 @@ import ( "strconv" "time" - //"github.com/mattn/go-colorable" - "github.com/Tnze/go-mc/bot" "github.com/Tnze/go-mc/bot/basic" "github.com/Tnze/go-mc/chat" @@ -20,7 +24,6 @@ var ( func main() { flag.Parse() - // log.SetOutput(colorable.NewColorableStdout()) for i := 0; i < *number; i++ { go func(i int) { @@ -60,6 +63,7 @@ func (i *individual) run(address string) { log.Printf("[%d]Login fail: %v", i.id, err) return } + defer i.client.Close() log.Printf("[%d]Login success", i.id) // JoinGame @@ -79,7 +83,7 @@ type DisconnectErr struct { } func (d DisconnectErr) Error() string { - return "disconnect: " + d.Reason.String() + return "disconnect: " + d.Reason.ClearString() } func onDisconnect(reason chat.Message) error { diff --git a/examples/simpleServer1.15.2/main.go b/examples/simpleServer1.15.2/main.go index 27dbe18..b1ac20e 100644 --- a/examples/simpleServer1.15.2/main.go +++ b/examples/simpleServer1.15.2/main.go @@ -1,4 +1,11 @@ -// Example minecraft 1.15.2 server +// This example is a minimal minecraft 1.15.2 server allowing vanilla clients or go-mc/bot to connect. +// +// This example handles "ping and list", so you can see its motd and player count in server list. +// Players can join the server and seeing an empty world. No authentication profile is checked. +// The KeepAlive packet is not handled, so client might exit 20 seconds later. +// +// It doesn't use go-mc/server but the go-mc/net package to handle the connection and implement basic 1.15.2 protocol, +// proving that even latest go-mc also support old minecraft versions. package main import ( diff --git a/examples/simpleServer1.17.1/main.go b/examples/simpleServer1.17.1/main.go index edb879d..a76b6c52 100644 --- a/examples/simpleServer1.17.1/main.go +++ b/examples/simpleServer1.17.1/main.go @@ -1,4 +1,7 @@ -// Example minecraft 1.17.1 server +// This example is a minimal minecraft 1.17.1 server allowing vanilla clients or go-mc/bot to connect. +// Has the same functionality as "simpleServer1.15.2", but with 1.17.1 protocol. +// +// It is used to test DimensionCodec stuffs during go-mc development. package main import ( diff --git a/examples/luncher/luncher.go b/examples/yggdrasil/launcher.go similarity index 61% rename from examples/luncher/luncher.go rename to examples/yggdrasil/launcher.go index f6e4dd0..85f7371 100644 --- a/examples/luncher/luncher.go +++ b/examples/yggdrasil/launcher.go @@ -1,3 +1,8 @@ +// This example used to act as a launcher, log in and obtain the access token. +// The Yggdrasil Authentication is no longer available. This example doesn't work now. +// +// For now, you should use Microsoft Authentication. The description and example code can be found here: +// https://wiki.vg/Microsoft_Authentication_Scheme package main import (