github.com/df-mc/dragonfly@v0.9.13/server/internal/nbtconv/item.go (about)

     1  package nbtconv
     2  
     3  import (
     4  	"github.com/df-mc/dragonfly/server/item/inventory"
     5  )
     6  
     7  // InvFromNBT decodes the data of an NBT slice into the inventory passed.
     8  func InvFromNBT(inv *inventory.Inventory, items []any) {
     9  	for _, itemData := range items {
    10  		data, _ := itemData.(map[string]any)
    11  		it := Item(data, nil)
    12  		if it.Empty() {
    13  			continue
    14  		}
    15  		_ = inv.SetItem(int(Uint8(data, "Slot")), it)
    16  	}
    17  }
    18  
    19  // InvToNBT encodes an inventory to a data slice which may be encoded as NBT.
    20  func InvToNBT(inv *inventory.Inventory) []map[string]any {
    21  	var items []map[string]any
    22  	for index, i := range inv.Slots() {
    23  		if i.Empty() {
    24  			continue
    25  		}
    26  		data := WriteItem(i, true)
    27  		data["Slot"] = byte(index)
    28  		items = append(items, data)
    29  	}
    30  	return items
    31  }