Update docs
This commit is contained in:
47
README.md
47
README.md
@ -1,7 +1,7 @@
|
|||||||
# Go-MC
|
# 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://goreportcard.com/report/github.com/Tnze/go-mc)
|
||||||
[](https://travis-ci.org/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.
|
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包本身是跨版本的。
|
由于`go-mc/net`实现的是MC底层的网络协议,而这个协议在MC更新时其实并不会有改动,MC更新时其实只是包的ID和内容的定义发生了变化,所以net包本身是跨版本的。
|
||||||
|
@ -1,13 +1,17 @@
|
|||||||
# NBT
|
# NBT [](https://pkg.go.dev/github.com/Tnze/go-mc/nbt)
|
||||||
This package implement the Named Binary Tag format of Minecraft. It supports all formats
|
This package implement the [Named Binary Tag](https://wiki.vg/NBT) format of Minecraft.
|
||||||
of NBT including compact arrays for longs.
|
|
||||||
|
The API is very similar to the standard library `encoding/json`. If you (high probability) have used that, it is easy to use this.
|
||||||
|
|
||||||
|
# Basic Usage
|
||||||
|
> I don't know why `Marshal` looks like that, and **I will change it** to `func Marshal(v interface{}) ([]byte, error)`.
|
||||||
|
> **Use `Encoder` is recommended now.**
|
||||||
|
|
||||||
# Usage
|
|
||||||
For the following NBT tag:
|
For the following NBT tag:
|
||||||
|
|
||||||
```
|
```
|
||||||
TAG_Compound("hello world") {
|
TAG_Compound("hello world") {
|
||||||
TAG_String('name'): 'Bananrama'
|
TAG_String("name"): "Bananrama"
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
@ -20,7 +24,7 @@ import "bytes"
|
|||||||
import "github.com/Tnze/go-mc/nbt"
|
import "github.com/Tnze/go-mc/nbt"
|
||||||
|
|
||||||
type Compound struct {
|
type Compound struct {
|
||||||
Name string `nbt:"name"` // Note that if name is private (name), the field will not be used
|
Name string `nbt:"name"` // The field must be started with the capital letter
|
||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
@ -33,7 +37,10 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
# Struct field tags
|
# Struct field tags
|
||||||
|
|
||||||
There are two tags supported:
|
There are two tags supported:
|
||||||
- nbt
|
- nbt
|
||||||
- nbt_type
|
- nbt_type
|
||||||
@ -49,5 +56,5 @@ type Compound struct {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
# Docs
|
|
||||||
[](https://godoc.org/github.com/Tnze/go-mc/nbt)
|
|
||||||
|
Reference in New Issue
Block a user