forked from df-mc/dragonfly
/
conversion.go
46 lines (40 loc) · 1.15 KB
/
conversion.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
package chunk
import (
"bytes"
_ "embed"
"github.com/sandertv/gophertunnel/minecraft/nbt"
)
// legacyBlockEntry represents a block entry used in versions prior to 1.13.
type legacyBlockEntry struct {
ID int32 `nbt:"oldid,omitempty"`
Meta int16 `nbt:"val,omitempty"`
}
var (
//go:embed legacy_states.nbt
legacyMappingsData []byte
// legacyMappings allows simple conversion from a legacy block entry to a new one.
legacyMappings = make(map[legacyBlockEntry]blockEntry)
)
// upgradeLegacyEntry upgrades a legacy block entry to a new one.
func upgradeLegacyEntry(id int32, meta int16) (blockEntry, bool) {
entry, ok := legacyMappings[legacyBlockEntry{ID: id, Meta: meta}]
if !ok {
// Also try cases where the meta should be disregarded.
entry, ok = legacyMappings[legacyBlockEntry{ID: id}]
}
return entry, ok
}
// init creates conversions for each legacy entry.
func init() {
dec := nbt.NewDecoder(bytes.NewBuffer(legacyMappingsData))
var entry struct {
Legacy legacyBlockEntry `nbt:"legacy"`
Updated blockEntry `nbt:"updated"`
}
for {
if err := dec.Decode(&entry); err != nil {
break
}
legacyMappings[entry.Legacy] = entry.Updated
}
}