frameworkServer works perfect now

This commit is contained in:
Tnze
2022-03-09 17:21:28 +08:00
parent aa8e611644
commit 0bbd279dd1
8 changed files with 73 additions and 34320 deletions

View File

@ -5,6 +5,7 @@ import (
"compress/zlib"
_ "embed"
"fmt"
"math/bits"
"github.com/Tnze/go-mc/nbt"
)
@ -17,21 +18,20 @@ type Block interface {
//go:embed block_states.nbt
var blockStates []byte
var toStateID = make(map[Block]int)
var toStateID map[Block]int
var fromStateID []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 State struct {
Name string
Properties nbt.RawMessage
}
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)
}
var states []struct {
Name string
Properties nbt.RawMessage
}
var states []State
// decompress
z, err := zlib.NewReader(bytes.NewReader(blockStates))
if err != nil {
@ -41,6 +41,8 @@ func init() {
if _, err = nbt.NewDecoder(z).Decode(&states); err != nil {
panic(err)
}
toStateID = make(map[Block]int, len(states))
fromStateID = make([]Block, 0, len(states))
for _, state := range states {
block := fromID[state.Name]
if state.Properties.Type != nbt.TagEnd {
@ -49,14 +51,29 @@ func init() {
panic(err)
}
}
regState(block)
if _, ok := toStateID[block]; ok {
panic(fmt.Errorf("state %#v already exist", block))
}
toStateID[block] = len(fromStateID)
fromStateID = append(fromStateID, block)
}
BitsPerBlock = bits.Len(uint(len(fromStateID)))
}
func FromStateID(stateID int) Block {
return fromStateID[stateID]
func FromStateID(stateID int) (b Block, ok bool) {
if stateID >= 0 && stateID < len(fromStateID) {
b = fromStateID[stateID]
ok = true
}
return
}
func ToStateID(b Block) int {
return toStateID[b]
func DefaultBlock(id string) (b Block, ok bool) {
b, ok = fromID[id]
return
}
func ToStateID(b Block) (i int, ok bool) {
i, ok = toStateID[b]
return
}