Provide .Block() method for block.State
This commit is contained in:
@ -37,6 +37,31 @@ type (
|
|||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
|
func (s *State) Block() (Block, error) {
|
||||||
|
// Get an empty Block object
|
||||||
|
block, ok := FromID[s.Name]
|
||||||
|
if !ok {
|
||||||
|
return nil, UnknownBlockErr{s.Name}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Set property values
|
||||||
|
if s.Properties.Type != nbt.TagEnd {
|
||||||
|
err := s.Properties.Unmarshal(&block)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return block, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
type UnknownBlockErr struct {
|
||||||
|
Name string
|
||||||
|
}
|
||||||
|
|
||||||
|
func (u UnknownBlockErr) Error() string {
|
||||||
|
return "unknown block: " + u.Name
|
||||||
|
}
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
var states []State
|
var states []State
|
||||||
// decompress
|
// decompress
|
||||||
@ -51,15 +76,12 @@ func init() {
|
|||||||
ToStateID = make(map[Block]StateID, len(states))
|
ToStateID = make(map[Block]StateID, len(states))
|
||||||
StateList = make([]Block, 0, len(states))
|
StateList = make([]Block, 0, len(states))
|
||||||
for _, state := range states {
|
for _, state := range states {
|
||||||
block := FromID[state.Name]
|
block, err := state.Block()
|
||||||
if state.Properties.Type != nbt.TagEnd {
|
if err != nil {
|
||||||
err := state.Properties.Unmarshal(&block)
|
panic(err)
|
||||||
if err != nil {
|
|
||||||
panic(err)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
if _, ok := ToStateID[block]; ok {
|
if _, ok := ToStateID[block]; ok {
|
||||||
panic(fmt.Errorf("state %#v already exist", block))
|
panic(fmt.Errorf("state %#v already exists", block))
|
||||||
}
|
}
|
||||||
ToStateID[block] = StateID(len(StateList))
|
ToStateID[block] = StateID(len(StateList))
|
||||||
StateList = append(StateList, block)
|
StateList = append(StateList, block)
|
||||||
|
Reference in New Issue
Block a user