NBT parser can unmarshal TagCompound as map[string]interface{} now

This commit is contained in:
Tnze
2019-07-31 20:25:41 +08:00
parent b076c797cd
commit 4bd9c44f30
4 changed files with 26 additions and 2 deletions

View File

@ -13,7 +13,7 @@ There's some library in Go support you to create your Minecraft client or server
- [x] Mojang authenticate - [x] Mojang authenticate
- [x] Minecraft network protocol - [x] Minecraft network protocol
- [x] RCON protocol - [x] RCON protocol
- [ ] World container / encoding - [ ] Saves decoding /encoding
bot: bot:
- [x] Swing arm - [x] Swing arm

View File

@ -290,6 +290,7 @@ func (d *Decoder) unmarshal(val reflect.Value, tagType byte, tagName string) err
} }
case TagCompound: case TagCompound:
var buf map[string]interface{}
switch vk := val.Kind(); vk { switch vk := val.Kind(); vk {
default: default:
return errors.New("cannot parse TagCompound as " + vk.String()) return errors.New("cannot parse TagCompound as " + vk.String())
@ -315,8 +316,13 @@ func (d *Decoder) unmarshal(val reflect.Value, tagType byte, tagName string) err
} }
} }
} }
case reflect.Map:
if !reflect.TypeOf(buf).AssignableTo(val.Type()) {
return errors.New("cannot parse TagCompound as " + vk.String())
}
fallthrough
case reflect.Interface: case reflect.Interface:
buf := make(map[string]interface{}) buf = make(map[string]interface{})
for { for {
tt, tn, err := d.readTag() tt, tn, err := d.readTag()
if err != nil { if err != nil {

View File

@ -28,6 +28,7 @@ type PlayerData struct {
HurtTime int16 HurtTime int16
Health float32 Health float32
HurtByTimestamp int32 HurtByTimestamp int32
PortalCooldown int32
Invulnerable byte Invulnerable byte
SeenCredits byte `nbt:"seenCredits"` SeenCredits byte `nbt:"seenCredits"`
@ -35,6 +36,8 @@ type PlayerData struct {
Score int32 Score int32
AbsorptionAmount float32 AbsorptionAmount float32
Inventory, EnderItems []Item
XpLevel int32 XpLevel int32
XpP float32 XpP float32
XpTotal int32 XpTotal int32
@ -49,6 +52,7 @@ type PlayerData struct {
Base float64 Base float64
Name string Name string
} }
Abilities struct { Abilities struct {
FlySpeed float32 `nbt:"flySpeed"` FlySpeed float32 `nbt:"flySpeed"`
WalkSpeed float32 `nbt:"warkSpeed"` WalkSpeed float32 `nbt:"warkSpeed"`
@ -58,6 +62,20 @@ type PlayerData struct {
MayBuild byte `nbt:"mayBuild"` MayBuild byte `nbt:"mayBuild"`
MayFly byte `nbt:"mayFly"` MayFly byte `nbt:"mayFly"`
} `nbt:"abilities"` } `nbt:"abilities"`
RecipeBook struct {
IsFilteringCraftable byte `nbt:"isFilteringCraftable"`
IsFurnaceFilteringCraftable byte `nbt:"isFurnaceFilteringCraftable"`
IsFurnaceGUIOpen byte `nbt:"isFurnaceGuiOpen"`
IsGUIOpen byte `nbt:"isGuiOpen"`
} `nbt:"recipeBook"`
}
type Item struct {
Count byte
Slot byte
ID string `nbt:"id"`
Tag map[string]interface{} `nbt:"tag"`
} }
func ReadPlayerData(r io.Reader) (data PlayerData, err error) { func ReadPlayerData(r io.Reader) (data PlayerData, err error) {