support block state & nbt improvement

This commit is contained in:
Tnze
2022-03-09 16:12:47 +08:00
parent 86592931c6
commit aa8e611644
915 changed files with 6566 additions and 8605 deletions

View File

@ -101,7 +101,7 @@ func main() {
var column save.Chunk
for task := range c {
if err := column.Load(task.data); err != nil {
log.Printf("Decode column (%d.%d) error: %v", task.pos[0], task.pos[1], err)
log.Printf("UnmarshalNBT column (%d.%d) error: %v", task.pos[0], task.pos[1], err)
}
//pos := [2]int{int(column.Level.PosX), int(column.Level.PosZ)}
//if pos != task.pos {

View File

@ -1,11 +0,0 @@
package block
type AcaciaButton struct {
Face string
Facing string
Powered string
}
func (AcaciaButton) ID() string {
return "minecraft:acacia_button"
}

View File

@ -1,13 +0,0 @@
package block
type AcaciaDoor struct {
Facing string
Half string
Hinge string
Open string
Powered string
}
func (AcaciaDoor) ID() string {
return "minecraft:acacia_door"
}

View File

@ -1,13 +0,0 @@
package block
type AcaciaFence struct {
East string
North string
South string
Waterlogged string
West string
}
func (AcaciaFence) ID() string {
return "minecraft:acacia_fence"
}

View File

@ -1,12 +0,0 @@
package block
type AcaciaFenceGate struct {
Facing string
In_wall string
Open string
Powered string
}
func (AcaciaFenceGate) ID() string {
return "minecraft:acacia_fence_gate"
}

View File

@ -1,10 +0,0 @@
package block
type AcaciaLeaves struct {
Distance string
Persistent string
}
func (AcaciaLeaves) ID() string {
return "minecraft:acacia_leaves"
}

View File

@ -1,9 +0,0 @@
package block
type AcaciaLog struct {
Axis string
}
func (AcaciaLog) ID() string {
return "minecraft:acacia_log"
}

View File

@ -1,8 +0,0 @@
package block
type AcaciaPlanks struct {
}
func (AcaciaPlanks) ID() string {
return "minecraft:acacia_planks"
}

View File

@ -1,9 +0,0 @@
package block
type AcaciaPressurePlate struct {
Powered string
}
func (AcaciaPressurePlate) ID() string {
return "minecraft:acacia_pressure_plate"
}

View File

@ -1,9 +0,0 @@
package block
type AcaciaSapling struct {
Stage string
}
func (AcaciaSapling) ID() string {
return "minecraft:acacia_sapling"
}

View File

@ -1,10 +0,0 @@
package block
type AcaciaSign struct {
Rotation string
Waterlogged string
}
func (AcaciaSign) ID() string {
return "minecraft:acacia_sign"
}

View File

@ -1,10 +0,0 @@
package block
type AcaciaSlab struct {
Type string
Waterlogged string
}
func (AcaciaSlab) ID() string {
return "minecraft:acacia_slab"
}

View File

@ -1,12 +0,0 @@
package block
type AcaciaStairs struct {
Facing string
Half string
Shape string
Waterlogged string
}
func (AcaciaStairs) ID() string {
return "minecraft:acacia_stairs"
}

View File

@ -1,13 +0,0 @@
package block
type AcaciaTrapdoor struct {
Facing string
Half string
Open string
Powered string
Waterlogged string
}
func (AcaciaTrapdoor) ID() string {
return "minecraft:acacia_trapdoor"
}

View File

@ -1,10 +0,0 @@
package block
type AcaciaWallSign struct {
Facing string
Waterlogged string
}
func (AcaciaWallSign) ID() string {
return "minecraft:acacia_wall_sign"
}

View File

@ -1,9 +0,0 @@
package block
type AcaciaWood struct {
Axis string
}
func (AcaciaWood) ID() string {
return "minecraft:acacia_wood"
}

View File

@ -1,11 +0,0 @@
package block
type ActivatorRail struct {
Powered string
Shape string
Waterlogged string
}
func (ActivatorRail) ID() string {
return "minecraft:activator_rail"
}

View File

@ -1,8 +0,0 @@
package block
type Air struct {
}
func (Air) ID() string {
return "minecraft:air"
}

View File

@ -1,8 +0,0 @@
package block
type Allium struct {
}
func (Allium) ID() string {
return "minecraft:allium"
}

View File

@ -1,8 +0,0 @@
package block
type AmethystBlock struct {
}
func (AmethystBlock) ID() string {
return "minecraft:amethyst_block"
}

View File

@ -1,10 +0,0 @@
package block
type AmethystCluster struct {
Facing string
Waterlogged string
}
func (AmethystCluster) ID() string {
return "minecraft:amethyst_cluster"
}

View File

@ -1,8 +0,0 @@
package block
type AncientDebris struct {
}
func (AncientDebris) ID() string {
return "minecraft:ancient_debris"
}

View File

@ -1,8 +0,0 @@
package block
type Andesite struct {
}
func (Andesite) ID() string {
return "minecraft:andesite"
}

View File

@ -1,10 +0,0 @@
package block
type AndesiteSlab struct {
Type string
Waterlogged string
}
func (AndesiteSlab) ID() string {
return "minecraft:andesite_slab"
}

View File

@ -1,12 +0,0 @@
package block
type AndesiteStairs struct {
Facing string
Half string
Shape string
Waterlogged string
}
func (AndesiteStairs) ID() string {
return "minecraft:andesite_stairs"
}

View File

@ -1,14 +0,0 @@
package block
type AndesiteWall struct {
East string
North string
South string
Up string
Waterlogged string
West string
}
func (AndesiteWall) ID() string {
return "minecraft:andesite_wall"
}

View File

@ -1,9 +0,0 @@
package block
type Anvil struct {
Facing string
}
func (Anvil) ID() string {
return "minecraft:anvil"
}

View File

@ -1,9 +0,0 @@
package block
type AttachedMelonStem struct {
Facing string
}
func (AttachedMelonStem) ID() string {
return "minecraft:attached_melon_stem"
}

View File

@ -1,9 +0,0 @@
package block
type AttachedPumpkinStem struct {
Facing string
}
func (AttachedPumpkinStem) ID() string {
return "minecraft:attached_pumpkin_stem"
}

View File

@ -1,8 +0,0 @@
package block
type Azalea struct {
}
func (Azalea) ID() string {
return "minecraft:azalea"
}

View File

@ -1,10 +0,0 @@
package block
type AzaleaLeaves struct {
Distance string
Persistent string
}
func (AzaleaLeaves) ID() string {
return "minecraft:azalea_leaves"
}

View File

@ -1,8 +0,0 @@
package block
type AzureBluet struct {
}
func (AzureBluet) ID() string {
return "minecraft:azure_bluet"
}

View File

@ -1,11 +0,0 @@
package block
type Bamboo struct {
Age string
Leaves string
Stage string
}
func (Bamboo) ID() string {
return "minecraft:bamboo"
}

View File

@ -1,8 +0,0 @@
package block
type BambooSapling struct {
}
func (BambooSapling) ID() string {
return "minecraft:bamboo_sapling"
}

View File

@ -1,10 +0,0 @@
package block
type Barrel struct {
Facing string
Open string
}
func (Barrel) ID() string {
return "minecraft:barrel"
}

View File

@ -1,8 +0,0 @@
package block
type Barrier struct {
}
func (Barrier) ID() string {
return "minecraft:barrier"
}

View File

@ -1,9 +0,0 @@
package block
type Basalt struct {
Axis string
}
func (Basalt) ID() string {
return "minecraft:basalt"
}

View File

@ -1,8 +0,0 @@
package block
type Beacon struct {
}
func (Beacon) ID() string {
return "minecraft:beacon"
}

View File

@ -1,8 +0,0 @@
package block
type Bedrock struct {
}
func (Bedrock) ID() string {
return "minecraft:bedrock"
}

View File

@ -1,10 +0,0 @@
package block
type BeeNest struct {
Facing string
Honey_level string
}
func (BeeNest) ID() string {
return "minecraft:bee_nest"
}

View File

@ -1,10 +0,0 @@
package block
type Beehive struct {
Facing string
Honey_level string
}
func (Beehive) ID() string {
return "minecraft:beehive"
}

View File

@ -1,9 +0,0 @@
package block
type Beetroots struct {
Age string
}
func (Beetroots) ID() string {
return "minecraft:beetroots"
}

View File

@ -1,11 +0,0 @@
package block
type Bell struct {
Attachment string
Facing string
Powered string
}
func (Bell) ID() string {
return "minecraft:bell"
}

View File

@ -1,11 +0,0 @@
package block
type BigDripleaf struct {
Facing string
Tilt string
Waterlogged string
}
func (BigDripleaf) ID() string {
return "minecraft:big_dripleaf"
}

View File

@ -1,10 +0,0 @@
package block
type BigDripleafStem struct {
Facing string
Waterlogged string
}
func (BigDripleafStem) ID() string {
return "minecraft:big_dripleaf_stem"
}

View File

@ -1,11 +0,0 @@
package block
type BirchButton struct {
Face string
Facing string
Powered string
}
func (BirchButton) ID() string {
return "minecraft:birch_button"
}

View File

@ -1,13 +0,0 @@
package block
type BirchDoor struct {
Facing string
Half string
Hinge string
Open string
Powered string
}
func (BirchDoor) ID() string {
return "minecraft:birch_door"
}

View File

@ -1,13 +0,0 @@
package block
type BirchFence struct {
East string
North string
South string
Waterlogged string
West string
}
func (BirchFence) ID() string {
return "minecraft:birch_fence"
}

View File

@ -1,12 +0,0 @@
package block
type BirchFenceGate struct {
Facing string
In_wall string
Open string
Powered string
}
func (BirchFenceGate) ID() string {
return "minecraft:birch_fence_gate"
}

View File

@ -1,10 +0,0 @@
package block
type BirchLeaves struct {
Distance string
Persistent string
}
func (BirchLeaves) ID() string {
return "minecraft:birch_leaves"
}

View File

@ -1,9 +0,0 @@
package block
type BirchLog struct {
Axis string
}
func (BirchLog) ID() string {
return "minecraft:birch_log"
}

View File

@ -1,8 +0,0 @@
package block
type BirchPlanks struct {
}
func (BirchPlanks) ID() string {
return "minecraft:birch_planks"
}

View File

@ -1,9 +0,0 @@
package block
type BirchPressurePlate struct {
Powered string
}
func (BirchPressurePlate) ID() string {
return "minecraft:birch_pressure_plate"
}

View File

@ -1,9 +0,0 @@
package block
type BirchSapling struct {
Stage string
}
func (BirchSapling) ID() string {
return "minecraft:birch_sapling"
}

View File

@ -1,10 +0,0 @@
package block
type BirchSign struct {
Rotation string
Waterlogged string
}
func (BirchSign) ID() string {
return "minecraft:birch_sign"
}

View File

@ -1,10 +0,0 @@
package block
type BirchSlab struct {
Type string
Waterlogged string
}
func (BirchSlab) ID() string {
return "minecraft:birch_slab"
}

View File

@ -1,12 +0,0 @@
package block
type BirchStairs struct {
Facing string
Half string
Shape string
Waterlogged string
}
func (BirchStairs) ID() string {
return "minecraft:birch_stairs"
}

View File

@ -1,13 +0,0 @@
package block
type BirchTrapdoor struct {
Facing string
Half string
Open string
Powered string
Waterlogged string
}
func (BirchTrapdoor) ID() string {
return "minecraft:birch_trapdoor"
}

View File

@ -1,10 +0,0 @@
package block
type BirchWallSign struct {
Facing string
Waterlogged string
}
func (BirchWallSign) ID() string {
return "minecraft:birch_wall_sign"
}

View File

@ -1,9 +0,0 @@
package block
type BirchWood struct {
Axis string
}
func (BirchWood) ID() string {
return "minecraft:birch_wood"
}

View File

@ -1,9 +0,0 @@
package block
type BlackBanner struct {
Rotation string
}
func (BlackBanner) ID() string {
return "minecraft:black_banner"
}

View File

@ -1,11 +0,0 @@
package block
type BlackBed struct {
Facing string
Occupied string
Part string
}
func (BlackBed) ID() string {
return "minecraft:black_bed"
}

View File

@ -1,11 +0,0 @@
package block
type BlackCandle struct {
Candles string
Lit string
Waterlogged string
}
func (BlackCandle) ID() string {
return "minecraft:black_candle"
}

View File

@ -1,9 +0,0 @@
package block
type BlackCandleCake struct {
Lit string
}
func (BlackCandleCake) ID() string {
return "minecraft:black_candle_cake"
}

View File

@ -1,8 +0,0 @@
package block
type BlackCarpet struct {
}
func (BlackCarpet) ID() string {
return "minecraft:black_carpet"
}

View File

@ -1,8 +0,0 @@
package block
type BlackConcrete struct {
}
func (BlackConcrete) ID() string {
return "minecraft:black_concrete"
}

View File

@ -1,8 +0,0 @@
package block
type BlackConcretePowder struct {
}
func (BlackConcretePowder) ID() string {
return "minecraft:black_concrete_powder"
}

View File

@ -1,9 +0,0 @@
package block
type BlackGlazedTerracotta struct {
Facing string
}
func (BlackGlazedTerracotta) ID() string {
return "minecraft:black_glazed_terracotta"
}

View File

@ -1,9 +0,0 @@
package block
type BlackShulkerBox struct {
Facing string
}
func (BlackShulkerBox) ID() string {
return "minecraft:black_shulker_box"
}

View File

@ -1,8 +0,0 @@
package block
type BlackStainedGlass struct {
}
func (BlackStainedGlass) ID() string {
return "minecraft:black_stained_glass"
}

View File

@ -1,13 +0,0 @@
package block
type BlackStainedGlassPane struct {
East string
North string
South string
Waterlogged string
West string
}
func (BlackStainedGlassPane) ID() string {
return "minecraft:black_stained_glass_pane"
}

View File

@ -1,8 +0,0 @@
package block
type BlackTerracotta struct {
}
func (BlackTerracotta) ID() string {
return "minecraft:black_terracotta"
}

View File

@ -1,9 +0,0 @@
package block
type BlackWallBanner struct {
Facing string
}
func (BlackWallBanner) ID() string {
return "minecraft:black_wall_banner"
}

View File

@ -1,8 +0,0 @@
package block
type BlackWool struct {
}
func (BlackWool) ID() string {
return "minecraft:black_wool"
}

View File

@ -1,8 +0,0 @@
package block
type Blackstone struct {
}
func (Blackstone) ID() string {
return "minecraft:blackstone"
}

View File

@ -1,10 +0,0 @@
package block
type BlackstoneSlab struct {
Type string
Waterlogged string
}
func (BlackstoneSlab) ID() string {
return "minecraft:blackstone_slab"
}

View File

@ -1,12 +0,0 @@
package block
type BlackstoneStairs struct {
Facing string
Half string
Shape string
Waterlogged string
}
func (BlackstoneStairs) ID() string {
return "minecraft:blackstone_stairs"
}

View File

@ -1,14 +0,0 @@
package block
type BlackstoneWall struct {
East string
North string
South string
Up string
Waterlogged string
West string
}
func (BlackstoneWall) ID() string {
return "minecraft:blackstone_wall"
}

View File

@ -1,10 +0,0 @@
package block
type BlastFurnace struct {
Facing string
Lit string
}
func (BlastFurnace) ID() string {
return "minecraft:blast_furnace"
}

View File

@ -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]
}

Binary file not shown.

View File

@ -1,40 +0,0 @@
package block
import (
"os"
"testing"
"github.com/Tnze/go-mc/nbt"
)
func TestNewFromStateID(t *testing.T) {
f, err := os.Open("testdata/block_states.nbt")
if err != nil {
t.Fatal(err)
}
defer f.Close()
var states []struct {
Name string
Properties nbt.RawMessage
}
_, err = nbt.NewDecoder(f).Decode(&states)
if err != nil {
t.Fatal(err)
}
for i, v := range states {
state1 := NewFromStateID(i)
if id := "minecraft:" + state1.ID(); id != v.Name {
t.Errorf("StateID [%d] Name not match: %v != %v", i, id, v.Name)
}
state2 := state1
if err := v.Properties.Unmarshal(&state2); err != nil {
t.Errorf("Decode error: %v", err)
}
if state1 != state2 {
t.Errorf("StateID [%d] Properties not match: %v != %v", i, state1, state2)
}
}
}

6358
level/block/blocks.go Normal file

File diff suppressed because it is too large Load Diff

View File

@ -1,9 +0,0 @@
package block
type BlueBanner struct {
Rotation string
}
func (BlueBanner) ID() string {
return "minecraft:blue_banner"
}

View File

@ -1,11 +0,0 @@
package block
type BlueBed struct {
Facing string
Occupied string
Part string
}
func (BlueBed) ID() string {
return "minecraft:blue_bed"
}

View File

@ -1,11 +0,0 @@
package block
type BlueCandle struct {
Candles string
Lit string
Waterlogged string
}
func (BlueCandle) ID() string {
return "minecraft:blue_candle"
}

View File

@ -1,9 +0,0 @@
package block
type BlueCandleCake struct {
Lit string
}
func (BlueCandleCake) ID() string {
return "minecraft:blue_candle_cake"
}

View File

@ -1,8 +0,0 @@
package block
type BlueCarpet struct {
}
func (BlueCarpet) ID() string {
return "minecraft:blue_carpet"
}

View File

@ -1,8 +0,0 @@
package block
type BlueConcrete struct {
}
func (BlueConcrete) ID() string {
return "minecraft:blue_concrete"
}

View File

@ -1,8 +0,0 @@
package block
type BlueConcretePowder struct {
}
func (BlueConcretePowder) ID() string {
return "minecraft:blue_concrete_powder"
}

View File

@ -1,9 +0,0 @@
package block
type BlueGlazedTerracotta struct {
Facing string
}
func (BlueGlazedTerracotta) ID() string {
return "minecraft:blue_glazed_terracotta"
}

View File

@ -1,8 +0,0 @@
package block
type BlueIce struct {
}
func (BlueIce) ID() string {
return "minecraft:blue_ice"
}

View File

@ -1,8 +0,0 @@
package block
type BlueOrchid struct {
}
func (BlueOrchid) ID() string {
return "minecraft:blue_orchid"
}

View File

@ -1,9 +0,0 @@
package block
type BlueShulkerBox struct {
Facing string
}
func (BlueShulkerBox) ID() string {
return "minecraft:blue_shulker_box"
}

View File

@ -1,8 +0,0 @@
package block
type BlueStainedGlass struct {
}
func (BlueStainedGlass) ID() string {
return "minecraft:blue_stained_glass"
}

View File

@ -1,13 +0,0 @@
package block
type BlueStainedGlassPane struct {
East string
North string
South string
Waterlogged string
West string
}
func (BlueStainedGlassPane) ID() string {
return "minecraft:blue_stained_glass_pane"
}

View File

@ -1,8 +0,0 @@
package block
type BlueTerracotta struct {
}
func (BlueTerracotta) ID() string {
return "minecraft:blue_terracotta"
}

View File

@ -1,9 +0,0 @@
package block
type BlueWallBanner struct {
Facing string
}
func (BlueWallBanner) ID() string {
return "minecraft:blue_wall_banner"
}

View File

@ -1,8 +0,0 @@
package block
type BlueWool struct {
}
func (BlueWool) ID() string {
return "minecraft:blue_wool"
}

View File

@ -1,9 +0,0 @@
package block
type BoneBlock struct {
Axis string
}
func (BoneBlock) ID() string {
return "minecraft:bone_block"
}

Some files were not shown because too many files have changed in this diff Show More