pk.NBT() support parameter optionalTagName
This commit is contained in:
@ -4,7 +4,6 @@ import (
|
|||||||
"bytes"
|
"bytes"
|
||||||
_ "embed"
|
_ "embed"
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/Tnze/go-mc/nbt"
|
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
pk "github.com/Tnze/go-mc/net/packet"
|
pk "github.com/Tnze/go-mc/net/packet"
|
||||||
@ -146,7 +145,6 @@ func ExamplePacket_Scan_joinGame() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func ExampleMarshal_setSlot() {
|
func ExampleMarshal_setSlot() {
|
||||||
var buf bytes.Buffer
|
|
||||||
type Enchantment struct {
|
type Enchantment struct {
|
||||||
ID int16 `nbt:"id"`
|
ID int16 `nbt:"id"`
|
||||||
Lvl int16 `nbt:"lvl"`
|
Lvl int16 `nbt:"lvl"`
|
||||||
@ -155,42 +153,34 @@ func ExampleMarshal_setSlot() {
|
|||||||
StoredEnchantments []Enchantment
|
StoredEnchantments []Enchantment
|
||||||
Unbreakable int32
|
Unbreakable int32
|
||||||
}
|
}
|
||||||
for _, pf := range [][]pk.FieldEncoder{
|
for _, pf := range []struct {
|
||||||
{
|
WindowID byte
|
||||||
pk.Byte(0),
|
Slot int16
|
||||||
pk.Short(5),
|
Present bool
|
||||||
pk.Boolean(false),
|
ItemID int
|
||||||
pk.Opt{Has: false},
|
ItemCount byte
|
||||||
},
|
NBT interface{}
|
||||||
{
|
}{
|
||||||
pk.Byte(0),
|
{WindowID: 0, Slot: 5, Present: false},
|
||||||
pk.Short(5),
|
{WindowID: 0, Slot: 5, Present: true, ItemID: 0x01, ItemCount: 1, NBT: pk.Byte(0)},
|
||||||
pk.Boolean(true),
|
{WindowID: 0, Slot: 5, Present: true, ItemID: 0x01, ItemCount: 1, NBT: pk.NBT(SlotNBT{
|
||||||
pk.Opt{Has: true, Field: pk.Tuple{
|
|
||||||
pk.VarInt(0x01), // ItemID
|
|
||||||
pk.Byte(1), // ItemCount
|
|
||||||
pk.Byte(nbt.TagEnd), // NBT, 0 when this is no data
|
|
||||||
}},
|
|
||||||
},
|
|
||||||
{
|
|
||||||
pk.Byte(0),
|
|
||||||
pk.Short(5),
|
|
||||||
pk.Boolean(true),
|
|
||||||
pk.Opt{Has: true, Field: pk.Tuple{
|
|
||||||
pk.VarInt(0x01), // ItemID
|
|
||||||
pk.Byte(1), // ItemCount
|
|
||||||
pk.NBT(SlotNBT{
|
|
||||||
StoredEnchantments: []Enchantment{
|
StoredEnchantments: []Enchantment{
|
||||||
{ID: 01, Lvl: 02},
|
{ID: 01, Lvl: 02},
|
||||||
{ID: 03, Lvl: 04},
|
{ID: 03, Lvl: 04},
|
||||||
},
|
},
|
||||||
Unbreakable: 1, // true
|
Unbreakable: 1, // true
|
||||||
}), // NBT
|
})},
|
||||||
}},
|
|
||||||
},
|
|
||||||
} {
|
} {
|
||||||
buf.Reset()
|
p := pk.Marshal(0x15,
|
||||||
p := pk.Marshal(0x15, pf...)
|
pk.Byte(pf.WindowID),
|
||||||
|
pk.Short(pf.Slot),
|
||||||
|
pk.Boolean(pf.Present),
|
||||||
|
pk.Opt{Has: pf.Present, Field: pk.Tuple{
|
||||||
|
pk.VarInt(pf.ItemID),
|
||||||
|
pk.Byte(pf.ItemCount),
|
||||||
|
pf.NBT,
|
||||||
|
}},
|
||||||
|
)
|
||||||
fmt.Printf("%02X % 02X\n", p.ID, p.Data)
|
fmt.Printf("%02X % 02X\n", p.ID, p.Data)
|
||||||
}
|
}
|
||||||
// Output:
|
// Output:
|
||||||
|
@ -426,16 +426,22 @@ func (d *Double) ReadFrom(r io.Reader) (n int64, err error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
//NBT encode a value as Named Binary Tag
|
//NBT encode a value as Named Binary Tag
|
||||||
func NBT(v interface{}) Field {
|
func NBT(v interface{}, optionalTagName ...string) Field {
|
||||||
|
if len(optionalTagName) > 0 {
|
||||||
|
return nbtField{V: v, FieldName: optionalTagName[0]}
|
||||||
|
}
|
||||||
return nbtField{V: v}
|
return nbtField{V: v}
|
||||||
}
|
}
|
||||||
|
|
||||||
type nbtField struct{ V interface{} }
|
type nbtField struct {
|
||||||
|
V interface{}
|
||||||
|
FieldName string
|
||||||
|
}
|
||||||
|
|
||||||
// Encode a nbtField
|
// Encode a nbtField
|
||||||
func (n nbtField) WriteTo(w io.Writer) (int64, error) {
|
func (n nbtField) WriteTo(w io.Writer) (int64, error) {
|
||||||
var buf bytes.Buffer
|
var buf bytes.Buffer
|
||||||
if err := nbt.NewEncoder(&buf).Encode(n.V); err != nil {
|
if err := nbt.NewEncoder(&buf).Encode(n.V, n.FieldName); err != nil {
|
||||||
return 0, err
|
return 0, err
|
||||||
}
|
}
|
||||||
return buf.WriteTo(w)
|
return buf.WriteTo(w)
|
||||||
|
Reference in New Issue
Block a user