forked from df-mc/dragonfly
-
Notifications
You must be signed in to change notification settings - Fork 0
/
inventory.go
72 lines (66 loc) · 1.72 KB
/
inventory.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
package playerdb
import (
"bytes"
"github.com/df-mc/dragonfly/server/internal/nbtconv"
"github.com/df-mc/dragonfly/server/item"
"github.com/df-mc/dragonfly/server/player"
"github.com/sandertv/gophertunnel/minecraft/nbt"
)
func invToData(data player.InventoryData) jsonInventoryData {
d := jsonInventoryData{
MainHandSlot: data.MainHandSlot,
OffHand: encodeItem(data.OffHand),
}
for slot, i := range data.Items {
itemData := encodeItem(i)
if itemData == nil {
continue
}
d.Items = append(d.Items, jsonSlot{
Slot: slot,
Item: itemData,
})
}
d.Boots = encodeItem(data.Boots)
d.Leggings = encodeItem(data.Leggings)
d.Chestplate = encodeItem(data.Chestplate)
d.Helmet = encodeItem(data.Helmet)
return d
}
func dataToInv(data jsonInventoryData) player.InventoryData {
d := player.InventoryData{
MainHandSlot: data.MainHandSlot,
OffHand: decodeItem(data.OffHand),
Items: make([]item.Stack, 36),
}
for _, i := range data.Items {
d.Items[i.Slot] = decodeItem(i.Item)
}
d.Boots = decodeItem(data.Boots)
d.Leggings = decodeItem(data.Leggings)
d.Chestplate = decodeItem(data.Chestplate)
d.Helmet = decodeItem(data.Helmet)
return d
}
func encodeItem(item item.Stack) []byte {
if item.Empty() {
return nil
}
var b bytes.Buffer
itemNBT := nbtconv.WriteItem(item, true)
encoder := nbt.NewEncoderWithEncoding(&b, nbt.LittleEndian)
err := encoder.Encode(itemNBT)
if err != nil {
return nil
}
return b.Bytes()
}
func decodeItem(data []byte) item.Stack {
var itemNBT map[string]interface{}
decoder := nbt.NewDecoderWithEncoding(bytes.NewBuffer(data), nbt.LittleEndian)
err := decoder.Decode(&itemNBT)
if err != nil {
return item.Stack{}
}
return nbtconv.ReadItem(itemNBT, nil)
}