-
Notifications
You must be signed in to change notification settings - Fork 114
/
block.go
68 lines (60 loc) · 1.39 KB
/
block.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
package block
import (
"bytes"
"compress/gzip"
_ "embed"
"fmt"
"math/bits"
"github.com/Tnze/go-mc/nbt"
)
type Block interface {
ID() string
}
// This file stores all possible block states into a TAG_List with gzip compressed.
//
//go:generate go run ./generator/blocks/main.go
//go:embed block_states.nbt
var blockStates []byte
var (
ToStateID map[Block]StateID
StateList []Block
)
// BitsPerBlock indicates how many bits are needed to represent all possible
// block states. This value is used to determine the size of the global palette.
var BitsPerBlock int
type (
StateID int
State struct {
Name string
Properties nbt.RawMessage
}
)
func init() {
var states []State
// decompress
z, err := gzip.NewReader(bytes.NewReader(blockStates))
if err != nil {
panic(err)
}
// decode all states
if _, err = nbt.NewDecoder(z).Decode(&states); err != nil {
panic(err)
}
ToStateID = make(map[Block]StateID, len(states))
StateList = make([]Block, 0, len(states))
for _, state := range states {
block := FromID[state.Name]
if state.Properties.Type != nbt.TagEnd {
err := state.Properties.Unmarshal(&block)
if err != nil {
panic(err)
}
}
if _, ok := ToStateID[block]; ok {
panic(fmt.Errorf("state %#v already exist", block))
}
ToStateID[block] = StateID(len(StateList))
StateList = append(StateList, block)
}
BitsPerBlock = bits.Len(uint(len(StateList)))
}