Update docs
This commit is contained in:
47
README.md
47
README.md
@ -1,7 +1,7 @@
|
||||
# Go-MC
|
||||

|
||||

|
||||
[](https://godoc.org/github.com/Tnze/go-mc)
|
||||
[](https://pkg.go.dev/github.com/Tnze/go-mc)
|
||||
[](https://goreportcard.com/report/github.com/Tnze/go-mc)
|
||||
[](https://travis-ci.org/Tnze/go-mc)
|
||||
|
||||
@ -82,6 +82,51 @@ if err != nil {
|
||||
}
|
||||
```
|
||||
|
||||
### Advanced usage
|
||||
|
||||
Sometimes you are handling packet like this:
|
||||
|
||||
| **Field Name** | Field Type | **Notes** |
|
||||
| :------------: | :-----------------: | :---------------------------------------- |
|
||||
| World Count | VarInt | Size of the following array. |
|
||||
| World Names | Array of Identifier | Identifiers for all worlds on the server. |
|
||||
|
||||
That is, the first field is an integer type and the second field is an array (a `[]string` in this case). The integer represents the length of array.
|
||||
|
||||
Traditionally, you can use the following method to read such a field:
|
||||
|
||||
```go
|
||||
r := bytes.Reader(p.Data)
|
||||
// Read WorldCount
|
||||
var WorldCount pk.VarInt
|
||||
if err := WorldCount.ReadFrom(r); err != nil {
|
||||
return err
|
||||
}
|
||||
// Read WorldNames
|
||||
WorldNames := make([]pk.Identifier, WorldCount)
|
||||
for i := 0; i < int(WorldCount); i++ {
|
||||
if err := WorldNames[i].ReadFrom(r); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
But this is tediously long an not compatible with `p.Scan()` method.
|
||||
|
||||
In the latest version, two new types is added: `pk.Ary` and `pk.Opt`. Dedicated to handling "Array of ...." and "Optional ...." fields.
|
||||
|
||||
```go
|
||||
var WorldCount pk.VarInt
|
||||
var WorldNames = []pk.Identifier{}
|
||||
if err := p.Scan(&WorldCount, pk.Ary{&WorldCount, &WorldNames}); err != nil {
|
||||
return err
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
|
||||
---
|
||||
|
||||
As the `go-mc/net` package implements the minecraft network protocol, there is no update between the versions at this level. So net package actually supports any version. It's just that the ID and content of the package are different between different versions.
|
||||
|
||||
由于`go-mc/net`实现的是MC底层的网络协议,而这个协议在MC更新时其实并不会有改动,MC更新时其实只是包的ID和内容的定义发生了变化,所以net包本身是跨版本的。
|
||||
|
Reference in New Issue
Block a user