support block state & nbt improvement
This commit is contained in:
@ -1,28 +1,62 @@
|
||||
package block
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"compress/zlib"
|
||||
_ "embed"
|
||||
"fmt"
|
||||
|
||||
"github.com/Tnze/go-mc/nbt"
|
||||
)
|
||||
|
||||
type Block interface {
|
||||
ID() string
|
||||
}
|
||||
|
||||
// This file stores all possible block states into a TAG_List with zlib compressed.
|
||||
//go:embed block_states.nbt
|
||||
var blockStates []byte
|
||||
|
||||
var toStateID = make(map[Block]int)
|
||||
var fromStateID []Block
|
||||
var fromID = make(map[string]Block)
|
||||
|
||||
func init() {
|
||||
//regState := func(s Block) {
|
||||
// if _, ok := toStateID[s]; ok {
|
||||
// panic(fmt.Errorf("state %#v already exist", s))
|
||||
// }
|
||||
// toStateID[s] = len(fromStateID)
|
||||
// fromStateID = append(fromStateID, s)
|
||||
//}
|
||||
//regBlock := func(b Block) {
|
||||
// fromID[b.ID()] = b
|
||||
// b.forEachState(regState)
|
||||
//}
|
||||
//regBlock(Air{})
|
||||
regState := func(s Block) {
|
||||
if _, ok := toStateID[s]; ok {
|
||||
panic(fmt.Errorf("state %#v already exist", s))
|
||||
}
|
||||
toStateID[s] = len(fromStateID)
|
||||
fromStateID = append(fromStateID, s)
|
||||
}
|
||||
var states []struct {
|
||||
Name string
|
||||
Properties nbt.RawMessage
|
||||
}
|
||||
// decompress
|
||||
z, err := zlib.NewReader(bytes.NewReader(blockStates))
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
// decode all states
|
||||
if _, err = nbt.NewDecoder(z).Decode(&states); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
for _, state := range states {
|
||||
block := fromID[state.Name]
|
||||
if state.Properties.Type != nbt.TagEnd {
|
||||
err := state.Properties.Unmarshal(&block)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
regState(block)
|
||||
}
|
||||
}
|
||||
|
||||
func NewFromStateID(stateID int) Block {
|
||||
func FromStateID(stateID int) Block {
|
||||
return fromStateID[stateID]
|
||||
}
|
||||
|
||||
func ToStateID(b Block) int {
|
||||
return toStateID[b]
|
||||
}
|
||||
|
Reference in New Issue
Block a user