-
Notifications
You must be signed in to change notification settings - Fork 1
/
item.go
executable file
·87 lines (75 loc) · 2.39 KB
/
item.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
package mappings
import (
_ "embed"
"github.com/df-mc/dragonfly/server/world"
"github.com/sandertv/gophertunnel/minecraft/nbt"
"github.com/sandertv/gophertunnel/minecraft/protocol"
)
// MVItemMapping holds all data items related.
type MVItemMapping struct {
// items holds a list of all existing items in the game.
items []protocol.ItemEntry
// itemRuntimeIDsToNames holds a map to translate item runtime IDs to string IDs.
itemRuntimeIDsToNames map[int32]string
// itemNamesToRuntimeIDs holds a map to translate item string IDs to runtime IDs.
itemNamesToRuntimeIDs map[string]int32
recipes []protocol.Recipe
}
// ItemMapping returns MVItemMapping instance of all item entries and runtime ID maps from the resource JSON.
func itemMapping(itemRuntimeIDData []byte) MVItemMapping {
var m map[string]int32
err := nbt.Unmarshal(itemRuntimeIDData, &m)
if err != nil {
panic(err)
}
var items []protocol.ItemEntry
var itemRuntimeIDsToNames = make(map[int32]string)
var itemNamesToRuntimeIDs = make(map[string]int32)
for name, rid := range m {
items = append(items, protocol.ItemEntry{
Name: name,
RuntimeID: int16(rid),
})
itemNamesToRuntimeIDs[name] = rid
itemRuntimeIDsToNames[rid] = name
}
for _, it := range world.CustomItems() {
name, _ := it.EncodeItem()
rid, _, _ := world.ItemRuntimeID(it)
items = append(items, protocol.ItemEntry{
Name: name,
ComponentBased: true,
RuntimeID: int16(rid),
})
itemNamesToRuntimeIDs[name] = rid
itemRuntimeIDsToNames[rid] = name
}
return MVItemMapping{
items: items,
itemRuntimeIDsToNames: itemRuntimeIDsToNames,
itemNamesToRuntimeIDs: itemNamesToRuntimeIDs,
}
}
// ItemNameByID returns an item's name by its legacy ID.
func (m MVItemMapping) ItemNameByID(id int32) (string, bool) {
// TODO: Properly handle item aliases.
name, ok := m.itemRuntimeIDsToNames[id]
return name, ok
}
// ItemIDByName returns an item's ID by its name.
func (m MVItemMapping) ItemIDByName(name string) (int32, bool) {
// TODO: Properly handle item aliases.
id, ok := m.itemNamesToRuntimeIDs[name]
if !ok {
id = m.itemNamesToRuntimeIDs["minecraft:name_tag"]
}
return id, ok
}
// Items returns a slice of all item entries.
func (m MVItemMapping) Items() []protocol.ItemEntry {
return m.items
}
// Recipes returns a slice of all recipes.
func (m MVItemMapping) Recipes() []protocol.Recipe {
return m.recipes
}